18
18
DOCUMENTATION = '''
21
short_description: Perform common tasks in Nagios related to downtime and notifications.
21
short_description: Perform common tasks in Nagios related to downtime and notifications.
23
23
- "The M(nagios) module has two basic functions: scheduling downtime and toggling alerts for services or hosts."
24
24
- All actions require the I(host) parameter to be given explicitly. In playbooks you can use the C($inventory_hostname) variable to refer to the host the playbook is currently running on.
25
25
- You can specify multiple services at once by separating them with commas, .e.g., C(services=httpd,nfs,puppet).
26
- When specifying what service to handle there is a special service value, I(host), which will handle alerts/downtime for the I(host itself), e.g., C(service=host). This keyword may not be given with other services at the same time. I(Setting alerts/downtime for a host does not affect alerts/downtime for any of the services running on it.)
26
- When specifying what service to handle there is a special service value, I(host), which will handle alerts/downtime for the I(host itself), e.g., C(service=host). This keyword may not be given with other services at the same time. I(Setting alerts/downtime for a host does not affect alerts/downtime for any of the services running on it.) To schedule downtime for all services on particular host use keyword "all", e.g., C(service=all).
27
27
- When using the M(nagios) module you will need to specify your Nagios server using the C(delegate_to) parameter.
35
choices: [ "downtime", "enable_alerts", "disable_alerts", "silence", "unsilence" ]
35
choices: [ "downtime", "enable_alerts", "disable_alerts", "silence", "unsilence",
36
"silence_nagios", "unsilence_nagios", "command" ]
38
39
- Host to operate on in Nagios.
71
80
code: "nagios: action=downtime minutes=30 service=httpd host=$inventory_hostname"
72
81
- description: schedule an hour of HOST downtime
73
82
code: "nagios: action=downtime minutes=60 service=host host=$inventory_hostname"
83
- description: schedule downtime for ALL services on HOST
84
code: "nagios: action=downtime minutes=45 service=all host=$inventory_hostname"
74
85
- description: schedule downtime for a few services
75
86
code: "nagios: action=downtime services=frob,foobar,qeuz host=$inventory_hostname"
76
87
- description: enable SMART disk alerts
83
94
code: "nagios: action=silence host=$inventory_hostname"
84
95
- description: unsilence all alerts
85
96
code: "nagios: action=unsilence host=$inventory_hostname"
97
- description: SHUT UP NAGIOS
98
code: "nagios: action=silence_nagios"
99
- description: ANNOY ME NAGIOS
100
code: "nagios: action=unsilence_nagios"
101
- description: command something
102
code: "nagios: action=command command='DISABLE_FAILURE_PREDICTION'"
88
105
import ConfigParser
111
128
'/usr/local/nagios/etc/nagios.cfg',
112
129
'/usr/local/nagios/nagios.cfg',
113
130
'/opt/nagios/etc/nagios.cfg',
114
'/opt/nagios/nagios.cfg'
131
'/opt/nagios/nagios.cfg',
132
# icinga on debian/ubuntu
133
'/etc/icinga/icinga.cfg',
134
# icinga installed from source (default location)
135
'/usr/local/icinga/etc/icinga.cfg',
117
138
for path in locations:
137
161
module = AnsibleModule(
138
162
argument_spec=dict(
139
163
action=dict(required=True, default=None, choices=ACTION_CHOICES),
140
164
author=dict(default='Ansible'),
141
host=dict(required=True, default=None),
165
host=dict(required=False, default=None),
142
166
minutes=dict(default=30),
143
167
cmdfile=dict(default=which_cmdfile()),
144
168
services=dict(default=None, aliases=['service']),
169
command=dict(required=False, default=None),
148
173
action = module.params['action']
174
host = module.params['host']
149
175
minutes = module.params['minutes']
150
176
services = module.params['services']
151
177
cmdfile = module.params['cmdfile']
178
command = module.params['command']
153
180
##################################################################
154
181
# Required args per action:
155
182
# downtime = (minutes, service, host)
156
183
# (un)silence = (host)
157
184
# (enable/disable)_alerts = (service, host)
159
187
# AnsibleModule will verify most stuff, we need to verify
160
188
# 'minutes' and 'service' manually.
162
190
##################################################################
191
if action not in ['command', 'silence_nagios', 'unsilence_nagios']:
193
module.fail_json(msg='no host specified for action requiring one')
194
######################################################################
163
195
if action == 'downtime':
164
196
# Make sure there's an actual service selected
178
210
module.fail_json(msg='a service is required when setting alerts')
212
if action in ['command']:
214
module.fail_json(msg='no command passed for command action')
180
215
##################################################################
182
217
module.fail_json('unable to locate nagios.cfg')
184
219
##################################################################
185
220
ansible_nagios = Nagios(module, **module.params)
221
if module.check_mode:
222
module.exit_json(changed=True)
187
225
##################################################################
210
248
self.host = kwargs['host']
211
249
self.minutes = int(kwargs['minutes'])
212
250
self.cmdfile = kwargs['cmdfile']
251
self.command = kwargs['command']
214
if (kwargs['services'] is None) or (kwargs['services'] == 'host'):
253
if (kwargs['services'] is None) or (kwargs['services'] == 'host') or (kwargs['services'] == 'all'):
215
254
self.services = kwargs['services']
217
256
self.services = kwargs['services'].split(',')
290
def _fmt_notif_str(self, cmd, host, svc=None):
329
def _fmt_notif_str(self, cmd, host=None, svc=None):
292
331
Format an external-command notification string.
294
333
cmd - Nagios command ID.
295
host - Host to en/disable notifications on..
334
host - Host to en/disable notifications on.. A value is not required
296
336
svc - Service to schedule downtime for. A value is not required
297
337
for host downtime.
302
342
entry_time = self._now()
304
notif_str = "[%s] %s;%s;%s\n" % (entry_time, cmd, host, svc)
306
# Downtime for a host if no svc specified
307
notif_str = "[%s] %s;%s\n" % (entry_time, cmd, host)
343
notif_str = "[%s] %s" % (entry_time, cmd)
345
notif_str += ";%s" % host
348
notif_str += ";%s" % svc
342
385
dt_cmd_str = self._fmt_dt_str(cmd, host, minutes)
343
386
self._write_command(dt_cmd_str)
388
def schedule_host_svc_downtime(self, host, minutes=30):
390
This command is used to schedule downtime for
391
all services associated with a particular host.
393
During the specified downtime, Nagios will not send
394
notifications out about the host.
396
SCHEDULE_HOST_SVC_DOWNTIME;<host_name>;<start_time>;<end_time>;
397
<fixed>;<trigger_id>;<duration>;<author>;<comment>
400
cmd = "SCHEDULE_HOST_SVC_DOWNTIME"
401
dt_cmd_str = self._fmt_dt_str(cmd, host, minutes)
402
self._write_command(dt_cmd_str)
345
404
def schedule_hostgroup_host_downtime(self, hostgroup, minutes=30):
347
406
This command is used to schedule downtime for all hosts in a
712
771
return return_str_list
714
773
return "Fail: could not write to the command file"
775
def silence_nagios(self):
777
This command is used to disable notifications for all hosts and services
780
This is a 'SHUT UP, NAGIOS' command
782
cmd = 'DISABLE_NOTIFICATIONS'
783
self._write_command(self._fmt_notif_str(cmd))
785
def unsilence_nagios(self):
787
This command is used to enable notifications for all hosts and services
790
This is a 'OK, NAGIOS, GO'' command
792
cmd = 'ENABLE_NOTIFICATIONS'
793
self._write_command(self._fmt_notif_str(cmd))
795
def nagios_cmd(self, cmd):
797
This sends an arbitrary command to nagios
799
It prepends the submitted time and appends a \n
801
You just have to provide the properly formatted command
804
pre = '[%s]' % int(time.time())
807
cmdstr = '%s %s %s' % (pre, cmd, post)
808
self._write_command(cmdstr)
718
812
Figure out what you want to do from ansible, and then do the
722
816
if self.action == 'downtime':
723
817
if self.services == 'host':
724
818
self.schedule_host_downtime(self.host, self.minutes)
819
elif self.services == 'all':
820
self.schedule_host_svc_downtime(self.host, self.minutes)
726
822
self.schedule_svc_downtime(self.host,
727
823
services=self.services,
749
845
self.disable_svc_notifications(self.host,
750
846
services=self.services)
847
elif self.action == 'silence_nagios':
848
self.silence_nagios()
850
elif self.action == 'unsilence_nagios':
851
self.unsilence_nagios()
853
elif self.action == 'command':
854
self.nagios_cmd(self.command)
753
858
self.module.fail_json(msg="unknown action specified: '%s'" % \