Zabbix has become an excellent monitoring tool because of two features: data storage, convenient data processing, capacity planning, archiving, etc. Another is its powerful api, which can be easily integrated with other components. For example, cmdb can add, update, and disable monitoring through zabbix api, the official documents of zabbix provide a detailed list of APIs.
Because there are many requirements for screen addition recently, we simply wrote a python script to automatically add screen. If you are interested, you can use it for reference.
The usage is as follows:
Python./screen_host.py-H host list-n screen name-G graph name
Specific script:
#! /Usr/bin/env python
Import urllib2
Import sys
Import json
Import argparse
Def requestJason (url, values ):
Data = json. dumps (values)
Req = urllib2.Request (url, data, {'content-type': 'application/json-rpc '})
Response = urllib2.urlopen (req, data)
Data_get = response. read ()
Output = json. loads (data_get)
Print output
Try:
Message = output ['result']
Except t:
Message = output ['error'] ['data']
Quit ()
Print json. dumps (message)
Return output
Def authenticate (url, username, password ):
Values = {'jsonrpc': '2. 0 ',
'Method': 'user. login ',
'Params ':{
'User': username,
'Password': password
},
'Id': '0'
}
Idvalue = requestJason (url, values)
Return idvalue ['result']
Def getHosts (hostname, url, auth ):
Host_list = []
Values = {'jsonrpc': '2. 0 ',
'Method': 'host. get ',
'Params ':{
'Output': ["hostid", "host"],
'Filter ':{
'Host': hostname
}
},
'Auth': auth,
'Id': '2'
}
Output = requestJason (url, values)
For host in output ['result']:
Host_list.append (host ['hostid'])
Return host_list
Def getGraphs (host_list, name_list, url, auth, columns, graphtype = 0, dynamic = 0 ):
If (graphtype = 0 ):
Selecttype = ['graphid']
Select = 'selectgraphs'
If (graphtype = 1 ):
Selecttype = ['itemid', 'value _ type']
Select = 'selectitems'
Values = ({'jsonrpc': '2. 0 ',
"Method": "graph. get ",
"Params ":{
Select: [selecttype, "name"],
"Output": ["graphid", "name"],
"Hostids": host_list,
"Filter": {'name': name_list },
"Sortfield": "name ",
},
"Auth": auth,
"Id": '3'
})
Print values
Output = requestJason (url, values)
Bb = sorted (output ['result'], key = lambda x: x ['hosts'] [0] ['hostid'])
Output ['result'] = bb
Graphs = []
If (graphtype = 0 ):
For I in output ['result']:
Print I
Graphs. append (I ['graphid'])
If (graphtype = 1 ):
For I in output ['result']:
If int (I ['value _ type']) in (0, 3 ):
Graphs. append (I ['itemid'])
Graph_list = []
X = 0
Y = 0
Print graphs
For graph in graphs:
Print "x is" + str (x)
Print "y is" + str (y)
Graph_list.append ({
"Resourcetype": graphtype,
"Resourceid": graph,
"Width": "500 ",
"Height": "100 ",
"X": str (x ),
"Y": str (y ),
"Colspan": "0 ",
"Rowspan": "0 ",
"Elements": "0 ",
"Valign": "0 ",
"Halign": "0 ",
"Style": "0 ",
"Url ":"",
"Dynamic": str (dynamic)
})
X + = 1
Print type (x)
Print type (columns)
If x = int (columns ):
X = 0
Y + = 1
Print graph_list
Return graph_list
Def screenCreate (url, auth, screen_name, graphids, columns ):
Columns = int (columns)
If len (graphids) % columns = 0:
Vsize = len (graphids)/columns
Else:
Vsize = (len (graphids)/columns) + 1
Values = {"jsonrpc": "2.0 ",
"Method": "screen. create ",
"Params ":[{
"Name": screen_name,
"Hsize": columns,
"Vsize": vsize,
"Screenitems": []
}],
"Auth": auth,
"Id": 2
}
For I in graphids:
Values ['params'] [0] ['screenitems ']. append (I)
Output = requestJason (url, values)
Def main ():
Url = 'HTTP: // xxxx/api_jsonrpc.php'
Username = 'xxxx'
Password = 'xxxx'
Auth = authenticate (url, username, password)
Host_list = getHosts (hostname, url, auth)
Print host_list
Graph_ids = getGraphs (host_list, graphname, url, auth, columns)
ScreenCreate (url, auth, screenname, graph_ids, columns)
If _ name _ = '_ main __':
Parser = argparse. ArgumentParser (description = 'create Zabbix screen from all of a host Items or Graphs .')
Parser. add_argument ('-G', dest = 'graphname', nargs =' + ', metavar = ('grah name '),
Help = 'zabbix Host Graph to create screen from ')
Parser. add_argument ('-H', dest = 'hostname', nargs =' + ', metavar = ('10. 19.111.145 '),
Help = 'zabbix Host to create screen from ')
Parser. add_argument ('-n', dest = 'screenname', type = str,
Help = 'screen name in Zabbix. Put quotes around it if you want spaces in the name .')
Parser. add_argument ('-C', dest = 'columns', type = int,
Help = 'number of columns in the screen ')
Args = parser. parse_args ()
Print args
Hostname = args. hostname
Screenname = args. screenname
Columns = args. columns
Graphname = args. graphname
If columns is None:
Columns = len (graphname)
Print columns
Main ()
ZABBIX details: click here
ZABBIX: click here
Install and deploy the distributed monitoring system Zabbix 2.06
Install and deploy the distributed monitoring system Zabbix 2.06
Install and deploy Zabbix in CentOS 6.3
Zabbix distributed monitoring system practice
Under CentOS 6.3, Zabbix monitors apache server-status
Monitoring MySQL database Parameters Using Zabbix in CentOS 6.3