Skip to content
Snippets Groups Projects
Unverified Commit a2ceafc3 authored by Varac's avatar Varac
Browse files

Bring back query_alertmanager.py

parent bd2c740c
No related branches found
No related tags found
No related merge requests found
...@@ -9,29 +9,19 @@ based on [libappindicator](https://launchpad.net/libappindicator). I don't use ...@@ -9,29 +9,19 @@ based on [libappindicator](https://launchpad.net/libappindicator). I don't use
it anymore and it doesn't work in the current state, although it might be it anymore and it doesn't work in the current state, although it might be
easy to get it working again. easy to get it working again.
![tray icon example](./tray-example.png)
## Todo ## Todo
* Red icon on `Can't connect` * Reduce duplicator code in `query_alertmanager.py` and `alertmanager_status.py`
* trigger refresh
* notifications
* flash icon / paint red
* Show alert details in menu
* example systemd service
## Install ## Install
Install dependencies (`libffi7` is needed by `pygobject`): Install dependencies:
sudo apt install gir1.2-appindicator3-0.1 libffi7
pip install -r requirements.txt pip install -r requirements.txt
## Configuration ## Configuration
Config file example (`~/.config/indicator_alertmanager/config.yml`): Config file example (`~/.config/alertmanager_status/config.yml`):
```yaml ```yaml
--- ---
...@@ -42,21 +32,32 @@ password: 123 ...@@ -42,21 +32,32 @@ password: 123
``` ```
## Usage ## Standalone usage
Start the indicator: Start the indicator:
```bash ```bash
./indicator_alertmanager.py ./alertmanager_status.py
``` ```
## query_alertmanager.py ## Waybar integration
Use as a library for indicator_alertmanager, but can also get used from command ```json
line, see: "custom/alertmanager": {
"return-type": "json",
```bash "interval": 60,
query_alertmanager.py -h "format": "Alerts: {} {icon}",
//"exec-if": "exit 0", // always run; consider advanced run conditions
"format-icons": {
"firing": "🔥",
"snafu": "🟢",
"error": "🚫"
},
"exec": "/bin/bash -c 'cd /home/varac/projects/monitoring/indicator_alertmanager/ && . ./.direnv/*/bin/activate && ./alertmanager_status.py'",
"signal": 21,
"on-click": "pkill -RTMIN+21 waybar",
"on-click-right": "xdg-open https://alertmanager.oas.varac.net",
}
``` ```
## Similar projects ## Similar projects
......
...@@ -41,7 +41,7 @@ def show_alerts(opts): ...@@ -41,7 +41,7 @@ def show_alerts(opts):
@plac.opt('configpath', "Path to alertmanager config", type=Path) @plac.opt('configpath', "Path to alertmanager config", type=Path)
@plac.opt('mode', "Mode: wayland or indicator (not working)", type=str) @plac.opt('mode', "Mode: wayland or indicator (not working)", type=str)
@plac.flg('debug', "Enable debug mode") @plac.flg('debug', "Enable debug mode")
def main(configpath='~/.config/indicator_alertmanager/config.yml', def main(configpath='~/.config/alertmanager_status/config.yml',
mode="wayland"): mode="wayland"):
"""Main function.""" """Main function."""
......
#!/usr/bin/env python3
"""Queries alertmanager for alerts, outputs json.
Can be also used as cli application.
Usage:
./query_alertmananger.py -h
"""
import json
import plac
import requests
from alertmanager import AlertManager
def get_alerts(url, password, port=443, user='admin'):
req = requests.Session()
req.auth = (user, password)
AM = AlertManager(host=url, port=port, req_obj=req)
count = 0
alerts = []
state = "unknown"
try:
alerts_raw = AM.get_alerts()
for alert in alerts_raw:
name = alert['labels']['alertname']
severity = alert['labels']['severity']
instance = alert['labels']['instance']
alert_state = alert['status']['state']
release_name = alert['labels']['release_name']
if severity != "none" and alert_state != "suppressed":
count += 1
alert_text = f'{name}: {instance}, {release_name} ({severity})'
alerts.append(alert_text)
if count > 2:
state = "critical"
elif count > 0:
state = "warning"
elif count == 0:
state = "ok"
summary = f'{count} alerts'
except:
summary = "Cant connect"
state = "warning"
return_data = {
'summary': summary,
'alerts': alerts,
'state': state,
'count': count}
return return_data
@plac.opt('url', "URL of alertmanager instance", type=str, abbrev='U')
@plac.opt('port', "Port of alertmanager instance", type=int, abbrev='P')
@plac.opt('user', "User name", type=str)
@plac.opt('password', "Password", type=str)
def main(url, password, port=443, user='admin'):
"""Main function."""
alerts = get_alerts(url=url, password=password, port=port, user=user)
print(json.dumps(alerts))
if __name__ == '__main__':
plac.call(main)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment