Skip to content
Snippets Groups Projects
Unverified Commit 9153db39 authored by Felix's avatar Felix Committed by GitHub
Browse files

Merge pull request #6 from idealo/pausealerts

Pausealerts
parents 601a8a3c b32bf2ed
No related branches found
No related tags found
No related merge requests found
...@@ -89,7 +89,6 @@ def search_annotations(grafana_url, ts_from, ts_to, http_get_headers, verify_ssl ...@@ -89,7 +89,6 @@ def search_annotations(grafana_url, ts_from, ts_to, http_get_headers, verify_ssl
# annotation: are user created, custom ones and can be managed via the api # annotation: are user created, custom ones and can be managed via the api
# alert: are created by Grafana itself, can NOT be managed by the api # alert: are created by Grafana itself, can NOT be managed by the api
url = '{0}/api/annotations?type=annotation&limit=5000&from={1}&to={2}'.format(grafana_url, ts_from, ts_to) url = '{0}/api/annotations?type=annotation&limit=5000&from={1}&to={2}'.format(grafana_url, ts_from, ts_to)
print("query annotation uri: {0}".format(url))
(status_code, content) = send_grafana_get(url, http_get_headers, verify_ssl, client_cert, debug) (status_code, content) = send_grafana_get(url, http_get_headers, verify_ssl, client_cert, debug)
return (status_code, content) return (status_code, content)
...@@ -125,6 +124,26 @@ def delete_alert_channel_by_id(id_, grafana_url, http_post_headers): ...@@ -125,6 +124,26 @@ def delete_alert_channel_by_id(id_, grafana_url, http_post_headers):
return int(r.status_code) return int(r.status_code)
def search_alerts(grafana_url, http_get_headers, verify_ssl, client_cert, debug):
url = '{0}/api/alerts'.format(grafana_url)
(status_code, content) = send_grafana_get(url, http_get_headers, verify_ssl, client_cert, debug)
return (status_code, content)
def pause_alert(id_, grafana_url, http_post_headers, verify_ssl, client_cert, debug):
url = '{0}/api/alerts/{1}/pause'.format(grafana_url, id_)
payload = '{ "paused": true }'
(status_code, content) = send_grafana_post(url, payload, http_post_headers, verify_ssl, client_cert, debug)
return (status_code, content)
def unpause_alert(id_, grafana_url, http_post_headers, verify_ssl, client_cert, debug):
url = '{0}/api/alerts/{1}/pause'.format(grafana_url, id_)
payload = '{ "paused": false }'
(status_code, content) = send_grafana_post(url, payload, http_post_headers, verify_ssl, client_cert, debug)
return (status_code, content)
def delete_folder(uid, grafana_url, http_post_headers): def delete_folder(uid, grafana_url, http_post_headers):
r = requests.delete('{0}/api/folders/{1}'.format(grafana_url, uid), headers=http_post_headers) r = requests.delete('{0}/api/folders/{1}'.format(grafana_url, uid), headers=http_post_headers)
return int(r.status_code) return int(r.status_code)
......
import os
import sys
from docopt import docopt
from grafana_backup.constants import (PKG_NAME, PKG_VERSION, JSON_CONFIG_PATH)
from grafana_backup.grafanaSettings import main as conf
from grafana_backup.pausealerts import main as pause
from grafana_backup.unpausealerts import main as unpause
docstring = """
{0} {1}
Usage:
grafana-backup pausealerts [--config=<filename>]
grafana-backup unpausealerts <alerts_filename> [--config=<filename>]
grafana-backup -h | --help
grafana-backup --version
Options:
-h --help Show this help message and exit
--version Get version information and exit
--config=<filename> Override default configuration path
""".format(PKG_NAME, PKG_VERSION)
args = docopt(docstring, version='{0} {1}'.format(PKG_NAME, PKG_VERSION))
def main():
arg_config = args.get('--config', False)
default_config = '{0}/conf/grafanaSettings.json'.format(os.path.dirname(__file__))
if arg_config:
settings = conf(arg_config)
elif os.path.isfile(JSON_CONFIG_PATH):
settings = conf(JSON_CONFIG_PATH)
elif os.path.isfile(default_config):
settings = conf(default_config)
if args.get('pausealerts', None):
pause(args, settings)
sys.exit()
elif args.get('unpausealerts', None):
unpause(args, settings)
sys.exit()
else:
print(docstring)
sys.exit()
if __name__ == '__main__':
main()
import os
import sys
from grafana_backup.commons import save_json
from grafana_backup.api_checks import main as api_checks
from grafana_backup.dashboardApi import search_alerts, pause_alert
def main(args, settings):
(status, json_resp, uid_support, paging_support) = api_checks(settings)
# Do not continue if API is unavailable or token is not valid
if not status == 200:
print("server status is not ok: {0}".format(json_resp))
sys.exit(1)
settings.update({'UID_SUPPORT': uid_support})
settings.update({'PAGING_SUPPORT': paging_support})
debug = settings.get('DEBUG')
timestamp = settings.get('TIMESTAMP')
verify_ssl = settings.get('VERIFY_SSL')
client_cert = settings.get('CLIENT_CERT')
grafana_url = settings.get('GRAFANA_URL')
pretty_print = settings.get('PRETTY_PRINT')
http_get_headers = settings.get('HTTP_POST_HEADERS')
folder_path = 'alert_status/{0}'.format(timestamp)
if not os.path.exists(folder_path):
os.makedirs(folder_path)
alerts = get_all_alerts(grafana_url, http_get_headers, verify_ssl, client_cert, debug)
file_path = save_json("alerts.json", alerts, folder_path, 'alerts', pretty_print)
print("alerts have been saved to {0}".format(file_path))
for alert in alerts:
(status, content) = pause_alert(alert['id'], grafana_url, http_get_headers, verify_ssl, client_cert, debug)
if status != 200:
print("pausing of alert {0} failed with {1}".format(alert['name'], status))
def get_all_alerts(grafana_url, http_get_headers, verify_ssl, client_cert, debug):
(status_code, content) = search_alerts(grafana_url, http_get_headers, verify_ssl, client_cert, debug)
if status_code == 200:
return content
else:
print("got status {0} when trying to get alerts".format(status_code))
exit(1)
import sys
import json
from grafana_backup.api_checks import main as api_checks
from grafana_backup.dashboardApi import unpause_alert
def main(args, settings):
alerts_file = args.get('<alerts_filename>', None)
print(f"got alerts_file {alerts_file}")
(status, json_resp, uid_support, paging_support) = api_checks(settings)
# Do not continue if API is unavailable or token is not valid
if not status == 200:
sys.exit(1)
debug = settings.get('DEBUG')
verify_ssl = settings.get('VERIFY_SSL')
client_cert = settings.get('CLIENT_CERT')
grafana_url = settings.get('GRAFANA_URL')
http_post_headers = settings.get('HTTP_POST_HEADERS')
with open(alerts_file, 'r') as f:
data = f.read()
alerts = json.loads(data)
print(alerts)
for alert in alerts:
if alert['state'] != 'paused':
result = unpause_alert(alert['id'], grafana_url, http_post_headers, verify_ssl, client_cert, debug)
if result[0] != 200:
print(f"failed to unpause alert: {alert['id']} - {alert['name']} with {result[0]}")
print(f"unpausing alert {alert['id']} - {alert['name']} with previous state: {alert['state']}")
else:
print(f"keeping alert {alert['id']} - {alert['name']} paused")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment