Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import argparse
- import json
- import sys
- import requests
- class IcingaAPIWrapper:
- def __init__(self, base_url, username, password):
- self.base_url = base_url.rstrip('/')
- self.username = username
- self.password = password
- def _get_headers(self):
- return {
- 'Accept': 'application/json',
- 'Content-Type': 'application/json',
- }
- def _get_auth(self):
- return (self.username, self.password)
- def _make_request(self, method, url, params=None, data=None):
- full_url = f"{self.base_url}/v1/{url}"
- response = requests.request(method, full_url, headers=self._get_headers(), params=params, auth=self._get_auth(), json=data)
- response.raise_for_status()
- return response.json()
- def get_checks_by_status(self, status, name=None):
- filter_param = f"name=={name};" if name else ""
- return self._make_request('GET', f"objects/services?service_state={status}&filter={filter_param}")
- def schedule_downtime(self, object_type, object_name, start_time, end_time, author, comment):
- data = {
- "type": object_type,
- "filter": f"name=={object_name}",
- "start_time": start_time,
- "end_time": end_time,
- "author": author,
- "comment": comment
- }
- return self._make_request('POST', 'actions/schedule-downtime', data=data)
- def cancel_downtime(self, object_type, object_name):
- data = {
- "type": object_type,
- "filter": f"name=={object_name}"
- }
- return self._make_request('POST', 'actions/remove-downtime', data=data)
- def rerun_check(self, object_type, object_name):
- data = {
- "type": object_type,
- "filter": f"name=={object_name}"
- }
- return self._make_request('POST', 'actions/reschedule-check', data=data)
- def parse_args(args):
- parser = argparse.ArgumentParser(description='Icinga 2 API Wrapper')
- parser.add_argument('-d', '--downtime', action='store_true', help='Activate downtime')
- parser.add_argument('-e', '--cancel-downtime', action='store_true', help='Cancel downtime')
- parser.add_argument('-H', '--host', help='Host object to disable checks for')
- return parser.parse_args(args)
- def read_credentials():
- with open('icinga_credentials.json') as f:
- data = json.load(f)
- return data['url'], data['username'], data['password']
- def main():
- url, username, password = read_credentials()
- icinga = IcingaAPIWrapper(url, username, password)
- args = parse_args(sys.argv[1:])
- if args.host:
- host_name = args.host
- else:
- host_name = input('Enter host FQDN: ')
- if args.downtime:
- start_time = input('Enter start time (YYYY-MM-DD HH:MM:SS): ')
- end_time = input
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement