-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathzabbix_web_scenario.py
executable file
·114 lines (103 loc) · 4.83 KB
/
zabbix_web_scenario.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
#!/usr/bin/env python
# coding: utf-8
# Project Source: https://github.com/renanvicente/zabbix-web-scenario
# Version: 0.0.1
# Author: Renan Vicente
# Mail: [email protected]
# Website: http://www.renanvicente.com
# Github: https://www.github.com/renanvicente
# Linkedin: http://www.linkedin.com/pub/renan-silva/6a/802/59b/en
from pyzabbix import ZabbixAPI
import sys
from re import compile,IGNORECASE
reload(sys)
sys.setdefaultencoding("utf-8")
"""
This is a script to add a web scenario and create a trigger.
"""
# The hostname at which the Zabbix web interface is available
def authentication(server_url,user,password):
if server_url and user and password:
ZABBIX_SERVER = server_url
zapi = ZabbixAPI(ZABBIX_SERVER)
try:
# Login to the Zabbix API
zapi.login(user,password)
return zapi
except Exception, e:
print(e)
sys.exit(1)
else:
print('Zabbix Server url , user and password are required, try use --help')
sys.exit(1)
def create_web_scenario(self,name,url,group,hostid,applicationid, url_name='Homepage',status='200'):
request = ZabbixAPI.do_request(self, 'webcheck.get', params={ "filter": {"name": name}})
if request['result']:
print('Host "%s" already registered' % name)
sys.exit(1)
else:
try:
ZabbixAPI.do_request(self, 'webcheck.create',params={"name": name,"hostid": hostid,"applicationid": applicationid, "delay": '60',"retries": '3', "steps": [ { 'name': url_name, 'url': url,'status_codes': status, 'no': '1'} ] } )
triggers = create_trigger(auth,name,url,group)
except Exception, e:
print(e)
sys.exit(1)
def create_by_file(auth, group, hostid, applicationid, filename):
try:
file_to_parse = open(filename,'r')
try:
for line in file_to_parse:
values = line.split(',')
try:
name = values[0]
url = values[1]
except IndexError, e:
print('Need at minimun 2 params Traceback %s:' % e)
sys.exit(1)
try:
url_name = values[2]
except IndexError:
url_name = None
if url_name:
create_web_scenario(auth,name,url,group,hostid,applicationid, url_name)
else:
create_web_scenario(auth,name,url,group,hostid, applicationid)
finally:
file_to_parse.close()
except IOError:
print('could not open the file %s' % filename)
def create_trigger(auth,name,url,group):
triggers = auth.trigger.create(description=name,comments="The website below does not response the HTTP request ( visit website member ) at least 120 seconds, this warning means that the website is down or unstable.\n%s" % url,expression='{%s:web.test.fail[%s].sum(120)}=1' % (group,name),priority=5)
return triggers
if __name__ == '__main__':
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-z","--zabbix",dest="server_url",help='URL for Zabbix Server',metavar='ZABBIX_SERVER')
parser.add_option('-n','--name',dest='name',help='Name of the Host',metavar='NAME')
parser.add_option('-w','--url-name',dest='url_name',help='URL name',metavar='URL_NAME')
parser.add_option('--url',dest='url',help='URL',metavar='URL')
parser.add_option('-s','--status',dest='status',help='Status Code',metavar='STATUS_CODE')
parser.add_option('-u','--user',dest='user',help='User for authentication',metavar='USER')
parser.add_option('-p','--password',dest='password',help='Password for authentication',metavar='PASSWORD')
parser.add_option('-f','--file',dest='filename',help='File with Name,URL',metavar='FILE')
parser.add_option('-g','--group-name',dest='group',help='Host Group Name',metavar='GROUP')
parser.add_option('-i','--host-id',dest='hostid',help='Host ID',metavar='HOSTID')
parser.add_option('-a','--application-id',dest='applicationid',help='Application ID',metavar='Application ID')
(options, args) = parser.parse_args()
auth = authentication(options.server_url,options.user,options.password)
if options.filename:
create_by_file(auth, options.group, options.hostid, options.applicationid, options.filename)
else:
if not options.group:
print('Group must be required')
sys.exit(1)
if options.status:
if options.url_name:
web_scenario = create_web_scenario(auth, options.name,options.url,options.group, options.hostid, options.applicationid, options.url_name,options.status)
else:
web_scenario = create_web_scenario(auth, options.name,None,options.url, options.group, options.hostid, options.applicationid, options.status)
else:
if options.url_name:
web_scenario = create_web_scenario(auth, options.name,options.url, options.group, options.hostid, options.applicationid, options.url_name)
else:
web_scenario = create_web_scenario(auth, options.name,options.url, options.group, options.hostid, options.applicationid)