From 1abdfc4bcd7c768706d86aa563e955f41d64194d Mon Sep 17 00:00:00 2001 From: cronekorkn Date: Fri, 23 Sep 2022 18:25:53 +0200 Subject: [PATCH] downtime script --- bundles/monitored/files/downtime | 73 ++++++++++++++++++++++++++++++++ bundles/monitored/items.py | 13 ++++++ data/icingaweb2/vhost.conf | 29 +++++++------ 3 files changed, 103 insertions(+), 12 deletions(-) create mode 100644 bundles/monitored/files/downtime create mode 100644 bundles/monitored/items.py diff --git a/bundles/monitored/files/downtime b/bundles/monitored/files/downtime new file mode 100644 index 0000000..ed6957d --- /dev/null +++ b/bundles/monitored/files/downtime @@ -0,0 +1,73 @@ +#!/usr/bin/env python3 + +from requests import Session +from sys import argv +from time import time + +if len(argv) > 1 and argv[1] == "remove": + action = "remove" +else: + action = "add" + duration_seconds = int(argv[1]) if len(argv) == 2 else 60 * 60 * 24 + +author = 'downtime-script' +node_name = '${node_name}' +api_url = 'https://${icinga_hostname}/api/v1' +session = Session() +session.auth = ('root', '${icinga_password}') +now = int(time()) + +# look for existing downtimes + +response = session.get( + f'{api_url}/objects/downtimes', + headers={ + 'Content-Type': 'application/json', + 'Accept': 'application/json', + }, + json={ + 'filter': f'match("{node_name}", host.name), match("{author}", downtime.author)', + } +) +response.raise_for_status() + +# remove existing downtimes + +if response.json()['results']: + response = session.post( + f'{api_url}/actions/remove-downtime', + headers={ + 'Content-Type': 'application/json', + 'Accept': 'application/json', + }, + json={ + 'type': 'Downtime', + 'filter': f'match("{node_name}", host.name), match("{author}", downtime.author)', + 'pretty': True, + } + ) + response.raise_for_status() + print('removed downtime') + +# add downtime + +if action == 'add': + response = session.post( + f'{api_url}/actions/schedule-downtime', + headers={ + 'Content-Type': 'application/json', + 'Accept': 'application/json', + }, + json={ + 'author': author, + 'comment': f'downtime by {argv[0]}', + 'start_time': now, + 'end_time': now + duration_seconds, + 'type': 'Host', + 'child_options': 'DowntimeTriggeredChildren', + 'filter': f'match("{node_name}", host.name)', + 'pretty': True, + } + ) + response.raise_for_status() + print('added downtime') diff --git a/bundles/monitored/items.py b/bundles/monitored/items.py new file mode 100644 index 0000000..82885bb --- /dev/null +++ b/bundles/monitored/items.py @@ -0,0 +1,13 @@ +icinga_node = repo.get_node(node.metadata.get('monitoring/icinga2_node')) + +files = { + '/usr/local/bin/downtime': { + 'content_type': 'mako', + 'mode': '0750', + 'context': { + 'node_name': node.name, + 'icinga_hostname': icinga_node.metadata.get('icinga2/hostname'), + 'icinga_password': icinga_node.metadata.get('icinga2/api_users/root/password'), + }, + }, +} diff --git a/data/icingaweb2/vhost.conf b/data/icingaweb2/vhost.conf index 9d5030b..d37ec69 100644 --- a/data/icingaweb2/vhost.conf +++ b/data/icingaweb2/vhost.conf @@ -1,31 +1,36 @@ # icingacli setup config webserver nginx --document-root /usr/share/icingaweb2/public --config /etc/icingaweb2 --fpm-uri 127.0.0.1:9000 server { - listen 443 ssl http2; - listen [::]:443 ssl http2; + listen 443 ssl http2; + listen [::]:443 ssl http2; - server_name ${server_name}; - root /usr/share/icingaweb2/public; + server_name ${server_name}; + root /usr/share/icingaweb2/public; - ssl_certificate /var/lib/dehydrated/certs/${server_name}/fullchain.pem; - ssl_certificate_key /var/lib/dehydrated/certs/${server_name}/privkey.pem; + ssl_certificate /var/lib/dehydrated/certs/${server_name}/fullchain.pem; + ssl_certificate_key /var/lib/dehydrated/certs/${server_name}/privkey.pem; - location / { + location /api/ { + proxy_pass https://127.0.0.1:5665/; + proxy_http_version 1.1; + } + + location / { return 302 /icingaweb2/index.php; - } + } - location ~ ^/icingaweb2/index\.php(.*)$ { + location ~ ^/icingaweb2/index\.php(.*)$ { fastcgi_pass unix:/run/php/php7.4-fpm.sock; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME /usr/share/icingaweb2/public/index.php; fastcgi_param ICINGAWEB_CONFIGDIR /etc/icingaweb2; fastcgi_param REMOTE_USER $remote_user; - } + } - location ~ ^/icingaweb2(.+)? { + location ~ ^/icingaweb2(.+)? { alias /usr/share/icingaweb2/public; index index.php; try_files $1 $uri $uri/ /icingaweb2/index.php$is_args$args; - } + } }