Skip to content

Commit

Permalink
nagios: some refactoring (#5239)
Browse files Browse the repository at this point in the history
* nagios: some refactoring

* rollback one change

* add changelog fragment

* Update changelogs/fragments/5239-nagios-refactor.yaml

Co-authored-by: Felix Fontein <[email protected]>

* Update plugins/modules/monitoring/nagios.py

Co-authored-by: Felix Fontein <[email protected]>

* Update plugins/modules/monitoring/nagios.py

Co-authored-by: Felix Fontein <[email protected]>

Co-authored-by: Felix Fontein <[email protected]>
(cherry picked from commit 570445a)
  • Loading branch information
russoz authored and patchback[bot] committed Sep 6, 2022
1 parent 3427e02 commit f65d1a1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 86 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/5239-nagios-refactor.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- nagios - minor refactoring on parameter validation for different actions (https://github.com//pull/5239).
114 changes: 28 additions & 86 deletions plugins/modules/monitoring/nagios.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,6 @@
from ansible.module_utils.basic import AnsibleModule


######################################################################

def which_cmdfile():
locations = [
# rhel
Expand Down Expand Up @@ -286,8 +284,6 @@ def which_cmdfile():

return None

######################################################################


def main():
ACTION_CHOICES = [
Expand All @@ -308,95 +304,42 @@ def main():

module = AnsibleModule(
argument_spec=dict(
action=dict(required=True, choices=ACTION_CHOICES),
author=dict(default='Ansible'),
comment=dict(default='Scheduling downtime'),
host=dict(required=False, default=None),
servicegroup=dict(required=False, default=None),
start=dict(required=False, default=None),
minutes=dict(default=30, type='int'),
cmdfile=dict(default=which_cmdfile()),
services=dict(default=None, aliases=['service']),
command=dict(required=False, default=None),
)
action=dict(type='str', required=True, choices=ACTION_CHOICES),
author=dict(type='str', default='Ansible'),
comment=dict(type='str', default='Scheduling downtime'),
host=dict(type='str'),
servicegroup=dict(type='str'),
start=dict(type='str'),
minutes=dict(type='int', default=30),
cmdfile=dict(type='str', default=which_cmdfile()),
services=dict(type='str', aliases=['service']),
command=dict(type='str'),
),
required_if=[
('action', 'downtime', ['host', 'services']),
('action', 'delete_downtime', ['host', 'services']),
('action', 'silence', ['host']),
('action', 'unsilence', ['host']),
('action', 'enable_alerts', ['host', 'services']),
('action', 'disable_alerts', ['host', 'services']),
('action', 'command', ['command']),
('action', 'servicegroup_host_downtime', ['host', 'servicegroup']),
('action', 'servicegroup_service_downtime', ['host', 'servicegroup']),
('action', 'acknowledge', ['host', 'services']),
('action', 'forced_check', ['host', 'services']),
],
)

action = module.params['action']
host = module.params['host']
servicegroup = module.params['servicegroup']
start = module.params['start']
services = module.params['services']
cmdfile = module.params['cmdfile']
command = module.params['command']

##################################################################
# Required args per action:
# downtime = (minutes, service, host)
# acknowledge = (service, host)
# (un)silence = (host)
# (enable/disable)_alerts = (service, host)
# command = command
#
# AnsibleModule will verify most stuff, we need to verify
# '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':
# Make sure there's an actual service selected
if not services:
module.fail_json(msg='no service selected to set downtime for')

######################################################################
if action == 'delete_downtime':
# Make sure there's an actual service selected
if not services:
module.fail_json(msg='no service selected to set downtime for')

######################################################################

if action in ['servicegroup_service_downtime', 'servicegroup_host_downtime']:
# Make sure there's an actual servicegroup selected
if not servicegroup:
module.fail_json(msg='no servicegroup selected to set downtime for')

##################################################################
if action in ['enable_alerts', 'disable_alerts']:
if not services:
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 action == 'acknowledge':
# Make sure there's an actual service selected
if not services:
module.fail_json(msg='no service selected to acknowledge')

##################################################################
if action == 'forced_check':
# Make sure there's an actual service selected
if not services:
module.fail_json(msg='no service selected to check')

##################################################################
if not cmdfile:
if not module.params['cmdfile']:
module.fail_json(msg='unable to locate nagios.cfg')

##################################################################
ansible_nagios = Nagios(module, **module.params)
if module.check_mode:
module.exit_json(changed=True)
else:
ansible_nagios.act()
##################################################################


######################################################################
class Nagios(object):
"""
Perform common tasks in Nagios related to downtime and
Expand Down Expand Up @@ -453,10 +396,9 @@ def _write_command(self, cmd):
self.module.fail_json(msg='nagios command file is not a fifo file',
cmdfile=self.cmdfile)
try:
fp = open(self.cmdfile, 'w')
fp.write(cmd)
fp.flush()
fp.close()
with open(self.cmdfile, 'w') as fp:
fp.write(cmd)
fp.flush()
self.command_results.append(cmd.strip())
except IOError:
self.module.fail_json(msg='unable to write to nagios command file',
Expand Down

0 comments on commit f65d1a1

Please sign in to comment.