https://github.com/aborgt/pylertalertmanager
A python library for prometheus's alertmanager
https://github.com/aborgt/pylertalertmanager
alertmanager kuberenetes-alertmanager prometheus prometheus-alerts python python2 python3
Last synced: about 2 months ago
JSON representation
A python library for prometheus's alertmanager
- Host: GitHub
- URL: https://github.com/aborgt/pylertalertmanager
- Owner: ABORGT
- License: mit
- Created: 2018-04-20T00:16:53.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2024-09-30T22:37:39.000Z (8 months ago)
- Last Synced: 2025-04-05T06:04:48.822Z (about 2 months ago)
- Topics: alertmanager, kuberenetes-alertmanager, prometheus, prometheus-alerts, python, python2, python3
- Language: Python
- Size: 85 KB
- Stars: 34
- Watchers: 6
- Forks: 16
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# PylertAlertManager
PylertAlertManager aims to be an easy-to-use interface for interacting with the Alert Manager API.
### Getting Started
The latest stable release is available from PyPI:
```
pip install pylertalertmanager
```Otherwise you can install from git:
```
pip install git+https://github.com/ABORGT/PylertAlertManager.git
```### Usage
Here we cover some basic usage examples to get folks off and running. We are importing json here just to pretty print our objects. Additionally, we have an Alert Manager instance running in docker to target.
```python
>>> import json
>>> from alertmanager import AlertManager
>>> from alertmanager import Alert
>>>
>>> # Provide some test data to be converted into an Alert object.
>>> test_data = {
... "labels": {
... "alertname": "TestAlert",
... "instance": "TestInstance",
... "severity": "critical"
... },
... "annotations": {
... "description": "This is a test alert",
... "info": "Test Alert",
... "summary": "A simple Test alert"
... }
... }
>>># Run the from_dict method on our test_data.
>>> test_alert = Alert.from_dict(test_data)
>>> type(test_alert)>>>
>>> # Add an annotation with the add_annotation method.
>>> test_alert.add_annotation('test_annotation', 'this is a test annotation')
>>> print(json.dumps(test_alert, indent=4))
{
"labels": {
"alertname": "TestAlert",
"instance": "TestInstance",
"severity": "critical"
},
"annotations": {
"description": "This is a test alert",
"info": "Test Alert",
"summary": "A simple Test alert",
"test_annotation": "this is a test annotation"
}
}
>>> # Add a label with the add_label method.
>>> test_alert.add_label('test_label', 'this is a test label')
>>> print(json.dumps(test_alert, indent=4))
{
"labels": {
"alertname": "TestAlert",
"instance": "TestInstance",
"severity": "critical",
"test_label": "this is a test label"
},
"annotations": {
"description": "This is a test alert",
"info": "Test Alert",
"summary": "A simple Test alert",
"test_annotation": "this is a test annotation"
}
}
>>> # Specify an Alert Manager host to connect to.
>>> host = 'http://127.0.0.1'
>>> a_manager = AlertManager(host=host)
>>>
>>> # Post an alert to our Alert Manager.
>>> a_manager.post_alerts(test_alert)>>> # Return a list of alerts from our Alert Manager.
>>> alerts = a_manager.get_alerts()
>>> print(json.dumps(alerts, indent=4))
[
{
"labels": {
"alertname": "TestAlert",
"instance": "TestInstance",
"severity": "critical",
"test_label": "this is a test label"
},
"annotations": {
"description": "This is a test alert",
"info": "Test Alert",
"summary": "A simple Test alert",
"test_annotation": "this is a test annotation"
},
"startsAt": "2018-11-08T16:25:02.327027475Z",
"endsAt": "2018-11-08T16:30:02.327027475Z",
"generatorURL": "",
"status": {
"state": "unprocessed",
"silencedBy": [],
"inhibitedBy": []
},
"receivers": [
"team-X-mails"
],
"fingerprint": "e6b119b9ce57e0c4"
}
]>>> # Return a list of silences from our Alert Manager
>>> silences = alert_manager.get_silences()
>>> print(json.dumps(silences, indent=4))
[
{
"id": "ed21ca94-383a-4a04-b759-ed817c8a8029",
"matchers": [
{
"name": "foo",
"value": "bar",
"isRegex": false
}
],
"startsAt": "2020-02-18T08:56:33.5429772Z",
"endsAt": "2020-02-18T09:08:01.206791Z",
"updatedAt": "2020-02-18T09:08:01.206791Z",
"createdBy": "",
"status": {
"state": "expired"
}
}
]>>> # Filter our silences by matchers
>>> silences = alert_manager.get_silences(filter={"foo": "bar"})
>>> print(json.dumps(silences, indent=4))
[
{
"id": "ed21ca94-383a-4a04-b759-ed817c8a8029",
"matchers": [
{
"name": "foo",
"value": "bar",
"isRegex": false
}
],
"startsAt": "2020-02-18T08:56:33.5429772Z",
"endsAt": "2020-02-18T09:08:01.206791Z",
"updatedAt": "2020-02-18T09:08:01.206791Z",
"createdBy": "",
"status": {
"state": "expired"
}
}
]```
## Running the testsTODO: Add tests
## Contributing
1. Fork it.
2. Create a branch describing either the issue or feature you're working.
3. Making changes, committing along the way.
4. Follow PEP8, except where ridiculous.
5. Include tests for any functionality changes.
6. Push the changes and create a pull request :D.## Built With
* [Python3](https://www.python.org/downloads/) - Beautiful language.
## Authors
* **Tyler Coil** - [Other Projects](https://github.com/kamori)
* **Justin Palmer** - [Other Projects](https://github.com/jpavlav)## Acknowledgments
* Kenneth Reitz -> [setup](https://github.com/kennethreitz/setup.py) - Thanks!