-
Notifications
You must be signed in to change notification settings - Fork 0
/
nagios_post.yml
109 lines (89 loc) · 4.61 KB
/
nagios_post.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/python
# Copyright: (c) 2018, Philipp Rintz <[email protected]>
# GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt)
# Ansible Module for Nagios Integration via HTTP Post requests
DOCUMENTATION = '''
---
module: nagios_post
author: "Philipp Rintz (https://github.com/p-rintz)"
short_description: Ansible Module for Nagios integration via Post requests.
description:
- Ansible Module for Nagios integration via Post requests. Features implemented: silence/unsilence
version_added: "2.6"
options:
action:
description:
- What action the module should undertake
choice: ["silence", "unsilence"]
nagios_host:
description:
- Nagios Host as full http string, up to cmd.cgi e.g. "https://nagioshost.com/nagios/cgi-bin/cmd.cgi"
required: true
default: status
'''
EXAMPLES = '''
- name: Silence host
nagios_post:
nagios_host=https://localhost.testing/nagios/cgi-bin/cmd.cgi
target_host=sometargethostname
action=silence
- name: Silence nagios
delegate_to: localhost
nagios_post:
nagios_host=http://nagios.via.de/nagios/cgi-bin/cmd.cgi
auth=true
target_host=productionhost
action=unsilence
user=nagiosuser
password=somerandompassword
run_once: true
'''
#####################################################################################################
from ansible.module_utils.basic import *
import requests
def post(url, payload, payloadsvc):
if module.params['auth'] == 'true':
stdout = requests.post(url, data=payload, auth=requests.auth.HTTPBasicAuth('%s' % module.params['user'], '%s' % module.params['password']))
stdoutsvc = requests.post(url, data=payloadsvc, auth=requests.auth.HTTPBasicAuth('%s' % module.params['user'], '%s' % module.params['password']))
else:
stdout = requests.post(url, data=payload)
stdoutsvc = requests.post(url, data=payloadsvc)
if stdout.status_code == 200:
module.exit_json(changed=False, responce="Success")
else:
msg = 'Status code was not 200 :: Responded Status code %s' % (stdout.status_code,)
module.fail_json(msg=msg)
def main():
global module
module = AnsibleModule(
argument_spec = dict(
nagios_host = dict(required=True, type='str'),
target_host = dict(required=True, type='str'),
user = dict(required=False, type='str'),
password = dict(required=False, type='str', no_log=True),
auth = dict(required=False, default='false', type='str'),
action = dict(required=True, type='str', choice=["silence", "unsilence"])
)
)
if module.params['action'] == "silence" and module.params['auth'] == 'true':
url = module.params['nagios_host']
payload = {'cmd_typ': '25', 'cmd_mod': '2', 'host': '%s' % module.params['target_host'], 'btnSubmit': 'Commit'}
payloadsvc = {'cmd_typ': '29', 'cmd_mod': '2', 'host': '%s' % module.params['target_host'], 'btnSubmit': 'Commit'}
post(url, payload, payloadsvc)
if module.params['action'] == "silence" and module.params['auth'] == 'false':
url = module.params['nagios_host']
payload = {'cmd_typ': '25', 'cmd_mod': '2', 'host': '%s' % module.params['target_host'], 'btnSubmit': 'Commit'}
payloadsvc = {'cmd_typ': '29', 'cmd_mod': '2', 'host': '%s' % module.params['target_host'], 'btnSubmit': 'Commit'}
post(url, payload, payloadsvc)
if module.params['action'] == "unsilence" and module.params['auth'] == 'true':
url = module.params['nagios_host']
payload = {'cmd_typ': '24', 'cmd_mod': '2', 'host': '%s' % module.params['target_host'], 'btnSubmit': 'Commit'}
payloadsvc = {'cmd_typ': '28', 'cmd_mod': '2', 'host': '%s' % module.params['target_host'], 'btnSubmit': 'Commit'}
post(url, payload, payloadsvc)
if module.params['action'] == "unsilence" and module.params['auth'] == 'false':
url = module.params['nagios_host']
payload = {'cmd_typ': '24', 'cmd_mod': '2', 'host': '%s' % module.params['target_host'], 'btnSubmit': 'Commit'}
payloadsvc = {'cmd_typ': '28', 'cmd_mod': '2', 'host': '%s' % module.params['target_host'], 'btnSubmit': 'Commit'}
post(url, payload, payloadsvc)
if __name__ == '__main__':
main()