-
Notifications
You must be signed in to change notification settings - Fork 1
/
zabbix.py
172 lines (152 loc) · 4.61 KB
/
zabbix.py
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from ansible.module_utils.basic import *
try:
from zabbix.api import ZabbixAPI
HAS_ZABBIX_API = True
except ImportError:
HAS_ZABBIX_API = False
DOCUMENTATION = '''
---
module: zabbix
short_description: ansible wrapper for py-zabbix
description:
- This module uses the Zabbix API to make requests to zabbix server.
version_added: "1.9"
requirements: [ 'py-zabbix' ]
options:
object:
description:
- The object to which the action applies. E.g item, host etc.
required: false
required: true
default: null
action:
description:
- Api methods: get, create, update, delete.
required: true
default: null
params:
description:
- Request parameters.
required: true
default: null
server:
description:
- Url of Zabbix server, with protocol (http or https) e.g.
https://monitoring.example.com/zabbix.
required: true
default: null
login:
description:
- Zabbix user name.
required: true
default: null
password:
description:
- Zabbix user password.
required: true
notes:
- The module has been tested with Zabbix Server 2.4.
- all requests set ansible fact called 'response' which contains zabbix api answer
- Zabbix 2.4 Api reference: https://www.zabbix.com/documentation/2.4/manual/api
author: Flashick <flashick@gmail.com>
'''
EXAMPLES = '''
# get groupid by name
- name: hostgroup name to groupid
zabbix:
server: 'https://zabbix-server.example.com'
login: 'api_user'
password: 'api_user_password'
action: 'get'
object: 'hostgroup'
params:
filter:
name:
- "My favorite host group"
- set_fact:
groupid: "{{ response.result[0].groupid }}"
# create new host in group 'My favorite host group'
- name: add new hosts
zabbix:
server: 'https://zabbix-server.example.com'
login: 'api_user'
password: 'api_user_password'
action: 'create'
object: 'host'
params:
host: 'Sample host'
groups:
- groupid: "{{ groupid }}"
templates:
- templateid: 5
interfaces:
- type: 1
main: 1
useip: 1
ip: "127.0.0.1"
port: 10050
dns: ""
# get all hosts in group 'My favorite host group'
- name: get existing hosts
zabbix:
server: 'https://zabbix-server.example.com'
login: 'api_user'
password: 'api_user_password'
action: 'get'
object: 'host'
params:
groupids: "{{ groupid }}"
- set_fact:
host_list: "{{ response.result }}"
# delete all hosts in group 'My favorite host group'
- name: delete hosts
zabbix:
server: 'https://zabbix-server.example.com'
login: 'api_user'
password: 'api_user_password'
action: 'delete'
object: 'host'
params:
- "{{ item.hostid }}"
with_items: "{{ host_list }}"
'''
def main():
module = AnsibleModule(
argument_spec=dict(
server=dict(default=None),
login=dict(default=None),
password=dict(default=None),
action=dict(default=None, required=True),
object=dict(default=None, required=True),
params=dict(default=None),
)
)
if not HAS_ZABBIX_API:
module.fail_json(msg='Missing requried py-zabbix module (check docs or install with: pip install py-zabbix)')
try:
action = module.params['action']
except KeyError, e:
module.fail_json(msg='Missing actions data: %s is not set.' % e.message)
try:
login = module.params['login']
password = module.params['password']
server = module.params['server']
except KeyError, e:
module.fail_json(msg='Missing login data: %s is not set.' % e.message)
try:
zabbix_object = module.params['object']
params = module.params['params']
except KeyError, e:
module.fail_json(msg='Missing actions data: %s is not set.' % e.message)
try:
zapi = ZabbixAPI(url=server, user=login, password=password)
except BaseException as e:
module.fail_json(msg='Authentication error: %s' % e)
try:
request = zapi.do_request(zabbix_object + '.' + action, params)
except BaseException as e:
module.fail_json(msg='Request error: %s ' % e)
module.exit_json(changed=1, ansible_facts={'response': request})
main()