downtime script
This commit is contained in:
parent
5eb684e7ea
commit
1abdfc4bcd
3 changed files with 103 additions and 12 deletions
73
bundles/monitored/files/downtime
Normal file
73
bundles/monitored/files/downtime
Normal file
|
@ -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')
|
13
bundles/monitored/items.py
Normal file
13
bundles/monitored/items.py
Normal file
|
@ -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'),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
|
@ -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
|
# icingacli setup config webserver nginx --document-root /usr/share/icingaweb2/public --config /etc/icingaweb2 --fpm-uri 127.0.0.1:9000
|
||||||
|
|
||||||
server {
|
server {
|
||||||
listen 443 ssl http2;
|
listen 443 ssl http2;
|
||||||
listen [::]:443 ssl http2;
|
listen [::]:443 ssl http2;
|
||||||
|
|
||||||
server_name ${server_name};
|
server_name ${server_name};
|
||||||
root /usr/share/icingaweb2/public;
|
root /usr/share/icingaweb2/public;
|
||||||
|
|
||||||
ssl_certificate /var/lib/dehydrated/certs/${server_name}/fullchain.pem;
|
ssl_certificate /var/lib/dehydrated/certs/${server_name}/fullchain.pem;
|
||||||
ssl_certificate_key /var/lib/dehydrated/certs/${server_name}/privkey.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;
|
return 302 /icingaweb2/index.php;
|
||||||
}
|
}
|
||||||
|
|
||||||
location ~ ^/icingaweb2/index\.php(.*)$ {
|
location ~ ^/icingaweb2/index\.php(.*)$ {
|
||||||
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
|
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
|
||||||
fastcgi_index index.php;
|
fastcgi_index index.php;
|
||||||
include fastcgi_params;
|
include fastcgi_params;
|
||||||
fastcgi_param SCRIPT_FILENAME /usr/share/icingaweb2/public/index.php;
|
fastcgi_param SCRIPT_FILENAME /usr/share/icingaweb2/public/index.php;
|
||||||
fastcgi_param ICINGAWEB_CONFIGDIR /etc/icingaweb2;
|
fastcgi_param ICINGAWEB_CONFIGDIR /etc/icingaweb2;
|
||||||
fastcgi_param REMOTE_USER $remote_user;
|
fastcgi_param REMOTE_USER $remote_user;
|
||||||
}
|
}
|
||||||
|
|
||||||
location ~ ^/icingaweb2(.+)? {
|
location ~ ^/icingaweb2(.+)? {
|
||||||
alias /usr/share/icingaweb2/public;
|
alias /usr/share/icingaweb2/public;
|
||||||
index index.php;
|
index index.php;
|
||||||
try_files $1 $uri $uri/ /icingaweb2/index.php$is_args$args;
|
try_files $1 $uri $uri/ /icingaweb2/index.php$is_args$args;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue