Skip to content
Snippets Groups Projects
Commit fc7a6f7d authored by Timur Perst's avatar Timur Perst
Browse files

cip-monitoring-powerdown-downtime: enable scheduling service downtimes

parent edce9ed6
No related branches found
No related tags found
No related merge requests found
......@@ -32,24 +32,32 @@ class PowerdownDowtime:
self._read_config()
def run(self):
action = self.args.action[0]
if action == 'schedule':
self.schedule()
elif action == 'remove':
self.remove()
else:
raise ValueError('invalid action')
self.args.func()
def _parse_args(self):
curtime = int(time.time())
parser = argparse.ArgumentParser(description='Schedule or remove powerdown downtimes via Icinga2 API')
parser = argparse.ArgumentParser(description='Schedule or remove downtimes via Icinga2 API (check out helpsection of subcommands for more options)')
parser.add_argument('--config', default='/etc/cip-monitoring-tools/icinga2-api.conf', help='path to the configuration file storing the Icinga2 API credentials.')
parser.add_argument('--host', default=socket.gethostname(), help='use this hostname to override local hostname.')
parser.add_argument('--downtime-start', type=int, default=curtime, help='start of the scheduled downtime as UNIX timestamp. (default: now)')
parser.add_argument('--downtime-end', type=int, default=curtime+7200, help='end of the scheduled downtime as UNIX timestamp. (default: in 2 hours)')
parser.add_argument('--downtime-author', default='powerdown', help='author of the scheduled downtime.')
parser.add_argument('--downtime-comment', default='Downtime scheduled by {}'.format(sys.argv[0]), help='comment for the scheduled downtime.')
parser.add_argument('action', nargs=1, choices=['schedule', 'remove'])
subparsers = parser.add_subparsers(title='Subcommands', required=True)
parser_schedule = subparsers.add_parser('schedule', help='Schedules a downtime for the specified host including all of its services')
parser_remove = subparsers.add_parser('remove', help='Removes downtimes for specified host')
parser_service = subparsers.add_parser('services', help='Schedules downtime for a set of services (e.g. salt-highstate)')
parser_schedule.add_argument('--downtime-start', type=int, default=curtime, help='start of the scheduled downtime as UNIX timestamp. (default: now)')
parser_schedule.add_argument('--downtime-end', type=int, default=curtime + 7200, help='end of the scheduled downtime as UNIX timestamp. (default: in 2 hours)')
parser_schedule.set_defaults(func=self.schedule)
parser_service.add_argument('--downtime-start', type=int, default=curtime, help='start of the scheduled downtime as UNIX timestamp. (default: now)')
parser_service.add_argument('--downtime-end', type=int, default=curtime + 7200, help='end of the scheduled downtime as UNIX timestamp. (default: in 2 hours)')
parser_service.add_argument('--services', nargs='+', help='list of services to schedule a downtime for')
parser_service.set_defaults(func=self.schedule_services)
parser_remove.set_defaults(func=self.remove)
self.args = parser.parse_args()
def _read_config(self):
......@@ -111,6 +119,38 @@ class PowerdownDowtime:
print(x, file=sys.stderr)
print('Scheduled {num} {type} downtime(s).'.format(num=num_scheduled, type=t))
def schedule_services(self):
url = self.api_url('v1/actions/schedule-downtime')
payload = {
"start_time": self.args.downtime_start,
"end_time": self.args.downtime_end,
"duration": 0,
"author": self.args.downtime_author,
"comment": self.args.downtime_comment,
}
num_scheduled = 0
for service in self.args.services:
params = {
'type': 'Service',
'filter': 'host.name=="{}"&&service.name=="{}"'.format(self.args.host, service),
}
r = requests.post(url,
headers=self.default_headers,
params=params,
auth=self.api_creds,
data=json.dumps(payload),
verify=self.api_tls_verify)
for x in r.json().get('results', []):
if api_ret_ok(x.get('code', 0)):
num_scheduled += 1
else:
print(x, file=sys.stderr)
print('Scheduled {num} service downtime(s).'.format(num=num_scheduled))
def remove(self):
url = self.api_url('v1/actions/remove-downtime')
......
cip-monitoring-tools (132) unstable; urgency=medium
* cip-monitoring-powerdown-downtime: enable scheduling service downtimes
-- Timur Perst <sy51goqy-adm@cip.cs.fau.de> Wed, 29 Jan 2025 13:58:21 +0100
cip-monitoring-tools (131+nmu1) UNRELEASED; urgency=medium
* Non-maintainer upload.
......
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