~canonical-ci-engineering/ubuntu-ci-services-itself/ansible

« back to all changes in this revision

Viewing changes to library/nagios

  • Committer: Package Import Robot
  • Author(s): Janos Guljas
  • Date: 2013-04-06 23:27:08 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20130406232708-6rn7w7mprfbjpmmk
Tags: 1.1+dfsg-1
* New upstream release.
* Update patches disable-google-analytics.patch and 
  remove-external-image.patch to apply cleanly.
* Add remove-external-footer-image.patch to remove link on external resource.
* Add remove-external-training-references.patch:
  Training advertise contains links to external resources that may not be
  available or may be used for tracking users activity without their
  knowledge by the third-party.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
DOCUMENTATION = '''
19
19
---
20
20
module: nagios
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.
22
22
description:
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.
28
 
version_added: 0.7
 
28
version_added: "0.7"
29
29
options:
30
30
  action:
31
31
    description:
32
32
      - Action to take.
33
33
    required: true
34
34
    default: null
35
 
    choices: [ "downtime", "enable_alerts", "disable_alerts", "silence", "unsilence" ]
 
35
    choices: [ "downtime", "enable_alerts", "disable_alerts", "silence", "unsilence",
 
36
               "silence_nagios", "unsilence_nagios", "command" ]
36
37
  host:
37
38
    description:
38
39
      - Host to operate on in Nagios.
39
 
    required: true
 
40
    required: false
40
41
    default: null
41
42
  cmdfile:
42
43
    description:
64
65
    aliases: [ "service" ]
65
66
    required: true
66
67
    default: null
 
68
  command:
 
69
    description:
 
70
      - raw command to send to nagios
 
71
      - should not include the submitted time header or the line-feed
 
72
      - B(Required) option when using the C(command) action
 
73
    required: true
 
74
    default: null
 
75
 
67
76
author: Tim Bielawa
68
77
requirements: [ "Nagios" ]
69
78
examples:
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'"
86
103
'''
87
104
 
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',
115
136
        ]
116
137
 
117
138
    for path in locations:
131
152
        'silence',
132
153
        'unsilence',
133
154
        'enable_alerts',
134
 
        'disable_alerts'
 
155
        'disable_alerts',
 
156
        'silence_nagios',
 
157
        'unsilence_nagios',
 
158
        'command',
135
159
        ]
136
160
 
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),
145
170
            )
146
171
        )
147
172
 
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']
152
 
 
 
178
    command = module.params['command']
 
179
    
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)
 
185
    # command = command
158
186
    #
159
187
    # AnsibleModule will verify most stuff, we need to verify
160
188
    # 'minutes' and 'service' manually.
161
189
 
162
190
    ##################################################################
 
191
    if action not in ['command', 'silence_nagios', 'unsilence_nagios']:
 
192
        if not host:
 
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
165
197
        if not services:
177
209
        if not services:
178
210
            module.fail_json(msg='a service is required when setting alerts')
179
211
 
 
212
    if action in ['command']:
 
213
        if not command:
 
214
            module.fail_json(msg='no command passed for command action')
180
215
    ##################################################################
181
216
    if not cmdfile:
182
217
        module.fail_json('unable to locate nagios.cfg')
183
218
 
184
219
    ##################################################################
185
220
    ansible_nagios = Nagios(module, **module.params)
186
 
    ansible_nagios.act()
 
221
    if module.check_mode:
 
222
        module.exit_json(changed=True)
 
223
    else:
 
224
        ansible_nagios.act()
187
225
    ##################################################################
188
226
 
189
227
 
210
248
        self.host = kwargs['host']
211
249
        self.minutes = int(kwargs['minutes'])
212
250
        self.cmdfile = kwargs['cmdfile']
 
251
        self.command = kwargs['command']
213
252
 
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']
216
255
        else:
217
256
            self.services = kwargs['services'].split(',')
287
326
 
288
327
        return dt_str
289
328
 
290
 
    def _fmt_notif_str(self, cmd, host, svc=None):
 
329
    def _fmt_notif_str(self, cmd, host=None, svc=None):
291
330
        """
292
331
        Format an external-command notification string.
293
332
 
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
 
335
          for global downtime
296
336
        svc - Service to schedule downtime for. A value is not required
297
337
          for host downtime.
298
338
 
300
340
        """
301
341
 
302
342
        entry_time = self._now()
303
 
        if svc is not None:
304
 
            notif_str = "[%s] %s;%s;%s\n" % (entry_time, cmd, host, svc)
305
 
        else:
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)
 
344
        if host is not None:
 
345
            notif_str += ";%s" % host
 
346
            
 
347
            if svc is not None:
 
348
                notif_str += ";%s" %  svc
 
349
 
 
350
        notif_str += "\n"
308
351
 
309
352
        return notif_str
310
353
 
342
385
        dt_cmd_str = self._fmt_dt_str(cmd, host, minutes)
343
386
        self._write_command(dt_cmd_str)
344
387
 
 
388
    def schedule_host_svc_downtime(self, host, minutes=30):
 
389
        """
 
390
        This command is used to schedule downtime for
 
391
        all services associated with a particular host.
 
392
 
 
393
        During the specified downtime, Nagios will not send
 
394
        notifications out about the host.
 
395
 
 
396
        SCHEDULE_HOST_SVC_DOWNTIME;<host_name>;<start_time>;<end_time>;
 
397
        <fixed>;<trigger_id>;<duration>;<author>;<comment>
 
398
        """
 
399
 
 
400
        cmd = "SCHEDULE_HOST_SVC_DOWNTIME"
 
401
        dt_cmd_str = self._fmt_dt_str(cmd, host, minutes)
 
402
        self._write_command(dt_cmd_str)
 
403
 
345
404
    def schedule_hostgroup_host_downtime(self, hostgroup, minutes=30):
346
405
        """
347
406
        This command is used to schedule downtime for all hosts in a
712
771
            return return_str_list
713
772
        else:
714
773
            return "Fail: could not write to the command file"
715
 
 
 
774
    
 
775
    def silence_nagios(self):
 
776
        """
 
777
        This command is used to disable notifications for all hosts and services
 
778
        in nagios.
 
779
        
 
780
        This is a 'SHUT UP, NAGIOS' command
 
781
        """
 
782
        cmd = 'DISABLE_NOTIFICATIONS'
 
783
        self._write_command(self._fmt_notif_str(cmd))
 
784
    
 
785
    def unsilence_nagios(self):
 
786
        """
 
787
        This command is used to enable notifications for all hosts and services
 
788
        in nagios.
 
789
        
 
790
        This is a 'OK, NAGIOS, GO'' command
 
791
        """
 
792
        cmd = 'ENABLE_NOTIFICATIONS'
 
793
        self._write_command(self._fmt_notif_str(cmd))
 
794
        
 
795
    def nagios_cmd(self, cmd):
 
796
        """
 
797
        This sends an arbitrary command to nagios
 
798
        
 
799
        It prepends the submitted time and appends a \n
 
800
        
 
801
        You just have to provide the properly formatted command
 
802
        """
 
803
        
 
804
        pre = '[%s]' % int(time.time())
 
805
        
 
806
        post = '\n'
 
807
        cmdstr = '%s %s %s' % (pre, cmd, post)
 
808
        self._write_command(cmdstr)
 
809
        
716
810
    def act(self):
717
811
        """
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)
725
821
            else:
726
822
                self.schedule_svc_downtime(self.host,
727
823
                                           services=self.services,
748
844
            else:
749
845
                self.disable_svc_notifications(self.host,
750
846
                                               services=self.services)
 
847
        elif self.action == 'silence_nagios':
 
848
            self.silence_nagios()
 
849
            
 
850
        elif self.action == 'unsilence_nagios':
 
851
            self.unsilence_nagios()
 
852
            
 
853
        elif self.action == 'command':
 
854
            self.nagios_cmd(self.command)
 
855
            
751
856
        # wtf?
752
857
        else:
753
858
            self.module.fail_json(msg="unknown action specified: '%s'" % \