mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
add (un)silence_nagios and command actions to the nagios module
also fix a typo with the check_mode check
This commit is contained in:
parent
6d41983173
commit
677dcc56ae
1 changed files with 91 additions and 14 deletions
105
library/nagios
105
library/nagios
|
@ -32,11 +32,12 @@ options:
|
||||||
- Action to take.
|
- Action to take.
|
||||||
required: true
|
required: true
|
||||||
default: null
|
default: null
|
||||||
choices: [ "downtime", "enable_alerts", "disable_alerts", "silence", "unsilence" ]
|
choices: [ "downtime", "enable_alerts", "disable_alerts", "silence", "unsilence",
|
||||||
|
"silence_nagios", "unsilence_nagios", "command" ]
|
||||||
host:
|
host:
|
||||||
description:
|
description:
|
||||||
- Host to operate on in Nagios.
|
- Host to operate on in Nagios.
|
||||||
required: true
|
required: false
|
||||||
default: null
|
default: null
|
||||||
cmdfile:
|
cmdfile:
|
||||||
description:
|
description:
|
||||||
|
@ -64,6 +65,14 @@ options:
|
||||||
aliases: [ "service" ]
|
aliases: [ "service" ]
|
||||||
required: true
|
required: true
|
||||||
default: null
|
default: null
|
||||||
|
command:
|
||||||
|
description:
|
||||||
|
- raw command to send to nagios
|
||||||
|
- should not include the submitted time header or the line-feed
|
||||||
|
- B(Required) option when using the C(command) action
|
||||||
|
required: true
|
||||||
|
default: null
|
||||||
|
|
||||||
author: Tim Bielawa
|
author: Tim Bielawa
|
||||||
requirements: [ "Nagios" ]
|
requirements: [ "Nagios" ]
|
||||||
examples:
|
examples:
|
||||||
|
@ -85,6 +94,12 @@ examples:
|
||||||
code: "nagios: action=silence host=$inventory_hostname"
|
code: "nagios: action=silence host=$inventory_hostname"
|
||||||
- description: unsilence all alerts
|
- description: unsilence all alerts
|
||||||
code: "nagios: action=unsilence host=$inventory_hostname"
|
code: "nagios: action=unsilence host=$inventory_hostname"
|
||||||
|
- description: SHUT UP NAGIOS
|
||||||
|
code: "nagios: action=silence_nagios"
|
||||||
|
- description: ANNOY ME NAGIOS
|
||||||
|
code: "nagios: action=unsilence_nagios"
|
||||||
|
- description: command something
|
||||||
|
code: "nagios: action=command command='DISABLE_FAILURE_PREDICTION'"
|
||||||
'''
|
'''
|
||||||
|
|
||||||
import ConfigParser
|
import ConfigParser
|
||||||
|
@ -133,17 +148,21 @@ def main():
|
||||||
'silence',
|
'silence',
|
||||||
'unsilence',
|
'unsilence',
|
||||||
'enable_alerts',
|
'enable_alerts',
|
||||||
'disable_alerts'
|
'disable_alerts',
|
||||||
|
'silence_nagios',
|
||||||
|
'unsilence_nagios',
|
||||||
|
'command',
|
||||||
]
|
]
|
||||||
|
|
||||||
module = AnsibleModule(
|
module = AnsibleModule(
|
||||||
argument_spec=dict(
|
argument_spec=dict(
|
||||||
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=True, default=None),
|
host=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']),
|
||||||
|
command=dict(required=False, default=None),
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -151,17 +170,23 @@ def main():
|
||||||
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']
|
||||||
|
|
||||||
##################################################################
|
##################################################################
|
||||||
# Required args per action:
|
# Required args per action:
|
||||||
# downtime = (minutes, service, host)
|
# downtime = (minutes, service, host)
|
||||||
# (un)silence = (host)
|
# (un)silence = (host)
|
||||||
# (enable/disable)_alerts = (service, host)
|
# (enable/disable)_alerts = (service, host)
|
||||||
|
# command = command
|
||||||
#
|
#
|
||||||
# AnsibleModule will verify most stuff, we need to verify
|
# AnsibleModule will verify most stuff, we need to verify
|
||||||
# 'minutes' and 'service' manually.
|
# 'minutes' and 'service' manually.
|
||||||
|
|
||||||
##################################################################
|
##################################################################
|
||||||
|
if action not in ['command', 'silence_nagios', 'unsilence_nagios']:
|
||||||
|
if not host:
|
||||||
|
module.fail_json(msg='no host specified for action requiring one')
|
||||||
|
######################################################################
|
||||||
if action == 'downtime':
|
if action == 'downtime':
|
||||||
# Make sure there's an actual service selected
|
# Make sure there's an actual service selected
|
||||||
if not services:
|
if not services:
|
||||||
|
@ -179,13 +204,16 @@ def main():
|
||||||
if not services:
|
if not services:
|
||||||
module.fail_json(msg='a service is required when setting alerts')
|
module.fail_json(msg='a service is required when setting alerts')
|
||||||
|
|
||||||
|
if action in ['command']:
|
||||||
|
if not command:
|
||||||
|
module.fail_json(msg='no command passed for command action')
|
||||||
##################################################################
|
##################################################################
|
||||||
if not cmdfile:
|
if not cmdfile:
|
||||||
module.fail_json('unable to locate nagios.cfg')
|
module.fail_json('unable to locate nagios.cfg')
|
||||||
|
|
||||||
##################################################################
|
##################################################################
|
||||||
ansible_nagios = Nagios(module, **module.params)
|
ansible_nagios = Nagios(module, **module.params)
|
||||||
if self.check_mode:
|
if module.check_mode:
|
||||||
module.exit_json(changed=True)
|
module.exit_json(changed=True)
|
||||||
else:
|
else:
|
||||||
ansible_nagios.act()
|
ansible_nagios.act()
|
||||||
|
@ -215,6 +243,7 @@ class Nagios(object):
|
||||||
self.host = kwargs['host']
|
self.host = kwargs['host']
|
||||||
self.minutes = int(kwargs['minutes'])
|
self.minutes = int(kwargs['minutes'])
|
||||||
self.cmdfile = kwargs['cmdfile']
|
self.cmdfile = kwargs['cmdfile']
|
||||||
|
self.command = kwargs['command']
|
||||||
|
|
||||||
if (kwargs['services'] is None) or (kwargs['services'] == 'host') or (kwargs['services'] == 'all'):
|
if (kwargs['services'] is None) or (kwargs['services'] == 'host') or (kwargs['services'] == 'all'):
|
||||||
self.services = kwargs['services']
|
self.services = kwargs['services']
|
||||||
|
@ -292,12 +321,13 @@ class Nagios(object):
|
||||||
|
|
||||||
return dt_str
|
return dt_str
|
||||||
|
|
||||||
def _fmt_notif_str(self, cmd, host, svc=None):
|
def _fmt_notif_str(self, cmd, host=None, svc=None):
|
||||||
"""
|
"""
|
||||||
Format an external-command notification string.
|
Format an external-command notification string.
|
||||||
|
|
||||||
cmd - Nagios command ID.
|
cmd - Nagios command ID.
|
||||||
host - Host to en/disable notifications on..
|
host - Host to en/disable notifications on.. A value is not required
|
||||||
|
for global downtime
|
||||||
svc - Service to schedule downtime for. A value is not required
|
svc - Service to schedule downtime for. A value is not required
|
||||||
for host downtime.
|
for host downtime.
|
||||||
|
|
||||||
|
@ -305,11 +335,14 @@ class Nagios(object):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
entry_time = self._now()
|
entry_time = self._now()
|
||||||
if svc is not None:
|
notif_str = "[%s] %s" % (entry_time, cmd)
|
||||||
notif_str = "[%s] %s;%s;%s\n" % (entry_time, cmd, host, svc)
|
if host is not None:
|
||||||
else:
|
notif_str += ";%s" % host
|
||||||
# Downtime for a host if no svc specified
|
|
||||||
notif_str = "[%s] %s;%s\n" % (entry_time, cmd, host)
|
if svc is not None:
|
||||||
|
notif_str += ";%s" % svc
|
||||||
|
|
||||||
|
notif_str += "\n"
|
||||||
|
|
||||||
return notif_str
|
return notif_str
|
||||||
|
|
||||||
|
@ -733,7 +766,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):
|
||||||
|
"""
|
||||||
|
This command is used to disable notifications for all hosts and services
|
||||||
|
in nagios.
|
||||||
|
|
||||||
|
This is a 'SHUT UP, NAGIOS' command
|
||||||
|
"""
|
||||||
|
cmd = 'DISABLE_NOTIFICATIONS'
|
||||||
|
self._write_command(self._fmt_notif_str(cmd))
|
||||||
|
|
||||||
|
def unsilence_nagios(self):
|
||||||
|
"""
|
||||||
|
This command is used to enable notifications for all hosts and services
|
||||||
|
in nagios.
|
||||||
|
|
||||||
|
This is a 'OK, NAGIOS, GO'' command
|
||||||
|
"""
|
||||||
|
cmd = 'ENABLE_NOTIFICATIONS'
|
||||||
|
self._write_command(self._fmt_notif_str(cmd))
|
||||||
|
|
||||||
|
def nagios_cmd(self, cmd):
|
||||||
|
"""
|
||||||
|
This sends an arbitrary command to nagios
|
||||||
|
|
||||||
|
It prepends the submitted time and appends a \n
|
||||||
|
|
||||||
|
You just have to provide the properly formatted command
|
||||||
|
"""
|
||||||
|
|
||||||
|
pre = '[%s]' % int(time.time())
|
||||||
|
|
||||||
|
post = '\n'
|
||||||
|
cmdstr = '%s %s %s' % (pre, cmd, post)
|
||||||
|
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
|
||||||
|
@ -771,6 +839,15 @@ class Nagios(object):
|
||||||
else:
|
else:
|
||||||
self.disable_svc_notifications(self.host,
|
self.disable_svc_notifications(self.host,
|
||||||
services=self.services)
|
services=self.services)
|
||||||
|
elif self.action == 'silence_nagios':
|
||||||
|
self.silence_nagios()
|
||||||
|
|
||||||
|
elif self.action == 'unsilence_nagios':
|
||||||
|
self.unsilence_nagios()
|
||||||
|
|
||||||
|
elif self.action == '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…
Reference in a new issue