alertmanager¶
Alertmanager module.
- exception spicerack.alertmanager.AlertmanagerError(message: str, response: Response | None = None)[source]¶
Bases:
SpicerackError
Custom exception class for errors of this module.
Initializes an AlertmanagerError instance with the API response instance.
- Parameters:
message (str) -- the actual exception message.
response (requests.Response, optional) -- the requests response object, if present.
- class spicerack.alertmanager.Alertmanager(*, dry_run: bool = True)[source]¶
Bases:
object
Operate on Alertmanager via its APIs.
Initialize the instance.
When using Alertmanager in high availability (cluster) make sure to pass all hosts in your cluster as alertmanager_urls.
- Parameters:
dry_run (bool, optional) -- set to False to cause writes to Alertmanager occur.
- downtime(reason: Reason, *, matchers: Sequence[Dict[str, str | int | float | bool]], duration: timedelta = datetime.timedelta(seconds=14400)) str [source]¶
Issue a new downtime.
- Parameters:
reason (Reason) -- the downtime reason.
matchers (list) -- the list of matchers to be applied to the downtime. The downtime will match alerts that match all the matchers provided, as they are ANDed by AlertManager.
duration (datetime.timedelta, optional) -- the length of the downtime period.
- Returns:
the downtime ID.
- Return type:
- Raises:
spicerack.alertmanager.AlertmanagerError -- if none of the alertmanager_urls API returned a success or the
parameters are invalid. --
- downtimed(reason: Reason, *, matchers: Sequence[Dict[str, str | int | float | bool]], duration: timedelta = datetime.timedelta(seconds=14400), remove_on_error: bool = False) Iterator[None] [source]¶
Context manager to perform actions while the matching alerts are downtimed on Alertmanager.
- Parameters:
reason (spicerack.administrative.Reason) -- the reason to set for the downtime on Alertmanager.
matchers (list) -- the list of matchers to be applied to the downtime. The downtime will match alerts that match all the matchers provided, as they are ANDed by AlertManager.
duration (datetime.timedelta, optional) -- the length of the downtime period.
remove_on_error -- should the downtime be removed even if an exception was raised.
- Yields:
None -- it just yields control to the caller once Alertmanager has received the downtime and deletes the downtime once getting back the control.
- class spicerack.alertmanager.AlertmanagerHosts(target_hosts: TypeHosts, *, verbatim_hosts: bool = False, dry_run: bool = True)[source]¶
Bases:
Alertmanager
Operate on Alertmanager for a list of hosts via its APIs.
Initialize the instance.
- Parameters:
target_hosts (spicerack.typing.TypeHosts) -- the target hosts either as a NodeSet instance or a sequence of strings.
verbatim_hosts (bool, optional) -- if
True
use the hosts passed verbatim as is, if insteadFalse
, the default, consider the given target hosts as FQDNs and extract their hostnames to be used in Alertmanager.dry_run (bool, optional) -- set to False to cause writes to Alertmanager occur.
- Raises:
spicerack.alertmanager.AlertmanagerError -- if no target hosts are provided.
- downtime(reason: Reason, *, matchers: Sequence[Dict[str, str | int | float | bool]] = (), duration: timedelta = datetime.timedelta(seconds=14400)) str [source]¶
Issue a new downtime for the given hosts.
- Parameters:
reason (Reason) -- the downtime reason.
matchers (list, optional) -- an optional list of matchers to be applied to the downtime. They will be added to the matcher automatically generated to match the current instance
target_hosts
hosts. For this reason the provided matchers cannot be for theinstance
property. The downtime will match alerts that match all the matchers provided, as they are ANDed by AlertManager.duration (datetime.timedelta, optional) -- the length of the downtime period.
- Returns:
the downtime ID.
- Return type:
- Raises:
spicerack.alertmanager.AlertmanagerError -- if none of the alertmanager_urls API returned a success or the
parameters are invalid. --
- downtimed(reason: Reason, *, matchers: Sequence[Dict[str, str | int | float | bool]] = (), duration: timedelta = datetime.timedelta(seconds=14400), remove_on_error: bool = False) Iterator[None] [source]¶
Context manager to perform actions while the hosts are downtimed on Alertmanager.
- Parameters:
reason (spicerack.administrative.Reason) -- the reason to set for the downtime on Alertmanager.
matchers (list, optional) -- an optional list of matchers to be applied to the downtime. They will be added to the matcher automatically generated to match the current instance
target_hosts
hosts. For this reason the provided matchers cannot be for theinstance
property. The downtime will match alerts that match all the matchers provided, as they are ANDed by AlertManager.duration (datetime.timedelta, optional) -- the length of the downtime period.
remove_on_error -- should the downtime be removed even if an exception was raised.
- Yields:
None -- it just yields control to the caller once Alertmanager has received the downtime and deletes the downtime once getting back the control.