add function for servicegrup downtimes

reviewable/pr18780/r1
Michael Gröning 10 years ago
parent 5a514ccdda
commit 91d0b2c00f

@ -169,6 +169,7 @@ def main():
'silence_nagios', 'silence_nagios',
'unsilence_nagios', 'unsilence_nagios',
'command', 'command',
'servicegroup_downtime'
] ]
module = AnsibleModule( module = AnsibleModule(
@ -176,6 +177,7 @@ def main():
action=dict(required=True, default=None, choices=ACTION_CHOICES), action=dict(required=True, default=None, choices=ACTION_CHOICES),
author=dict(default='Ansible'), author=dict(default='Ansible'),
host=dict(required=False, default=None), host=dict(required=False, default=None),
servicegroup=dict(required=False, default=None),
minutes=dict(default=30), minutes=dict(default=30),
cmdfile=dict(default=which_cmdfile()), cmdfile=dict(default=which_cmdfile()),
services=dict(default=None, aliases=['service']), services=dict(default=None, aliases=['service']),
@ -185,11 +187,12 @@ def main():
action = module.params['action'] action = module.params['action']
host = module.params['host'] host = module.params['host']
servicegroup = module.params['servicegroup']
minutes = module.params['minutes'] minutes = module.params['minutes']
services = module.params['services'] services = module.params['services']
cmdfile = module.params['cmdfile'] cmdfile = module.params['cmdfile']
command = module.params['command'] command = module.params['command']
################################################################## ##################################################################
# Required args per action: # Required args per action:
# downtime = (minutes, service, host) # downtime = (minutes, service, host)
@ -201,7 +204,7 @@ def main():
# 'minutes' and 'service' manually. # 'minutes' and 'service' manually.
################################################################## ##################################################################
if action not in ['command', 'silence_nagios', 'unsilence_nagios']: if action not in ['command', 'silence_nagios', 'unsilence_nagios', 'servicegroup_downtime']:
if not host: if not host:
module.fail_json(msg='no host specified for action requiring one') module.fail_json(msg='no host specified for action requiring one')
###################################################################### ######################################################################
@ -217,6 +220,20 @@ def main():
except Exception: except Exception:
module.fail_json(msg='invalid entry for minutes') module.fail_json(msg='invalid entry for minutes')
######################################################################
if action == 'servicegroup_downtime':
# Make sure there's an actual service selected
if not servicegroup:
module.fail_json(msg='no servicegroup selected to set downtime for')
# Make sure minutes is a number
try:
m = int(minutes)
if not isinstance(m, types.IntType):
module.fail_json(msg='minutes must be a number')
except Exception:
module.fail_json(msg='invalid entry for minutes')
################################################################## ##################################################################
if action in ['enable_alerts', 'disable_alerts']: if action in ['enable_alerts', 'disable_alerts']:
if not services: if not services:
@ -259,6 +276,7 @@ class Nagios(object):
self.action = kwargs['action'] self.action = kwargs['action']
self.author = kwargs['author'] self.author = kwargs['author']
self.host = kwargs['host'] self.host = kwargs['host']
self.service_group = kwargs['servicegroup']
self.minutes = int(kwargs['minutes']) self.minutes = int(kwargs['minutes'])
self.cmdfile = kwargs['cmdfile'] self.cmdfile = kwargs['cmdfile']
self.command = kwargs['command'] self.command = kwargs['command']
@ -356,7 +374,7 @@ class Nagios(object):
notif_str = "[%s] %s" % (entry_time, cmd) notif_str = "[%s] %s" % (entry_time, cmd)
if host is not None: if host is not None:
notif_str += ";%s" % host notif_str += ";%s" % host
if svc is not None: if svc is not None:
notif_str += ";%s" % svc notif_str += ";%s" % svc
@ -784,42 +802,42 @@ class Nagios(object):
return return_str_list return return_str_list
else: else:
return "Fail: could not write to the command file" return "Fail: could not write to the command file"
def silence_nagios(self): def silence_nagios(self):
""" """
This command is used to disable notifications for all hosts and services This command is used to disable notifications for all hosts and services
in nagios. in nagios.
This is a 'SHUT UP, NAGIOS' command This is a 'SHUT UP, NAGIOS' command
""" """
cmd = 'DISABLE_NOTIFICATIONS' cmd = 'DISABLE_NOTIFICATIONS'
self._write_command(self._fmt_notif_str(cmd)) self._write_command(self._fmt_notif_str(cmd))
def unsilence_nagios(self): def unsilence_nagios(self):
""" """
This command is used to enable notifications for all hosts and services This command is used to enable notifications for all hosts and services
in nagios. in nagios.
This is a 'OK, NAGIOS, GO'' command This is a 'OK, NAGIOS, GO'' command
""" """
cmd = 'ENABLE_NOTIFICATIONS' cmd = 'ENABLE_NOTIFICATIONS'
self._write_command(self._fmt_notif_str(cmd)) self._write_command(self._fmt_notif_str(cmd))
def nagios_cmd(self, cmd): def nagios_cmd(self, cmd):
""" """
This sends an arbitrary command to nagios This sends an arbitrary command to nagios
It prepends the submitted time and appends a \n It prepends the submitted time and appends a \n
You just have to provide the properly formatted command You just have to provide the properly formatted command
""" """
pre = '[%s]' % int(time.time()) pre = '[%s]' % int(time.time())
post = '\n' post = '\n'
cmdstr = '%s %s %s' % (pre, cmd, post) cmdstr = '%s %s %s' % (pre, cmd, post)
self._write_command(cmdstr) self._write_command(cmdstr)
def act(self): def act(self):
""" """
Figure out what you want to do from ansible, and then do the Figure out what you want to do from ansible, and then do the
@ -835,6 +853,9 @@ class Nagios(object):
self.schedule_svc_downtime(self.host, self.schedule_svc_downtime(self.host,
services=self.services, services=self.services,
minutes=self.minutes) minutes=self.minutes)
if self.action == "servicegroup_downtime":
if self.services == 'servicegroup':
self.schedule_servicegroup_host_downtime(self, self.servicegroup, minutes=30)
# toggle the host AND service alerts # toggle the host AND service alerts
elif self.action == 'silence': elif self.action == 'silence':
@ -859,13 +880,13 @@ class Nagios(object):
services=self.services) services=self.services)
elif self.action == 'silence_nagios': elif self.action == 'silence_nagios':
self.silence_nagios() self.silence_nagios()
elif self.action == 'unsilence_nagios': elif self.action == 'unsilence_nagios':
self.unsilence_nagios() self.unsilence_nagios()
elif self.action == 'command': elif self.action == 'command':
self.nagios_cmd(self.command) self.nagios_cmd(self.command)
# wtf? # wtf?
else: else:
self.module.fail_json(msg="unknown action specified: '%s'" % \ self.module.fail_json(msg="unknown action specified: '%s'" % \

Loading…
Cancel
Save