Advertisement
skrinetzki

Untitled

Apr 4th, 2023
479
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.86 KB | None | 0 0
  1. import argparse
  2. import json
  3. import sys
  4.  
  5. import requests
  6.  
  7.  
  8. class IcingaAPIWrapper:
  9.     def __init__(self, base_url, username, password):
  10.         self.base_url = base_url.rstrip('/')
  11.         self.username = username
  12.         self.password = password
  13.  
  14.     def _get_headers(self):
  15.         return {
  16.             'Accept': 'application/json',
  17.             'Content-Type': 'application/json',
  18.         }
  19.  
  20.     def _get_auth(self):
  21.         return (self.username, self.password)
  22.  
  23.     def _make_request(self, method, url, params=None, data=None):
  24.         full_url = f"{self.base_url}/v1/{url}"
  25.         response = requests.request(method, full_url, headers=self._get_headers(), params=params, auth=self._get_auth(), json=data)
  26.         response.raise_for_status()
  27.         return response.json()
  28.  
  29.     def get_checks_by_status(self, status, name=None):
  30.         filter_param = f"name=={name};" if name else ""
  31.         return self._make_request('GET', f"objects/services?service_state={status}&filter={filter_param}")
  32.  
  33.     def schedule_downtime(self, object_type, object_name, start_time, end_time, author, comment):
  34.         data = {
  35.             "type": object_type,
  36.             "filter": f"name=={object_name}",
  37.             "start_time": start_time,
  38.             "end_time": end_time,
  39.             "author": author,
  40.             "comment": comment
  41.         }
  42.         return self._make_request('POST', 'actions/schedule-downtime', data=data)
  43.  
  44.     def cancel_downtime(self, object_type, object_name):
  45.         data = {
  46.             "type": object_type,
  47.             "filter": f"name=={object_name}"
  48.         }
  49.         return self._make_request('POST', 'actions/remove-downtime', data=data)
  50.  
  51.     def rerun_check(self, object_type, object_name):
  52.         data = {
  53.             "type": object_type,
  54.             "filter": f"name=={object_name}"
  55.         }
  56.         return self._make_request('POST', 'actions/reschedule-check', data=data)
  57.  
  58.  
  59. def parse_args(args):
  60.     parser = argparse.ArgumentParser(description='Icinga 2 API Wrapper')
  61.     parser.add_argument('-d', '--downtime', action='store_true', help='Activate downtime')
  62.     parser.add_argument('-e', '--cancel-downtime', action='store_true', help='Cancel downtime')
  63.     parser.add_argument('-H', '--host', help='Host object to disable checks for')
  64.     return parser.parse_args(args)
  65.  
  66.  
  67. def read_credentials():
  68.     with open('icinga_credentials.json') as f:
  69.         data = json.load(f)
  70.         return data['url'], data['username'], data['password']
  71.  
  72.  
  73. def main():
  74.     url, username, password = read_credentials()
  75.     icinga = IcingaAPIWrapper(url, username, password)
  76.  
  77.     args = parse_args(sys.argv[1:])
  78.  
  79.     if args.host:
  80.         host_name = args.host
  81.     else:
  82.         host_name = input('Enter host FQDN: ')
  83.  
  84.     if args.downtime:
  85.         start_time = input('Enter start time (YYYY-MM-DD HH:MM:SS): ')
  86.         end_time = input
  87.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement