zabbix之所以能成为比较优秀的监控工具,个人觉得是因为两个特点:一个是数据的存放方式,方便做数据处理,容量规划,归档等,这个之前说过。另一个是其强大的api,利用其api可以方便的和其他的组件进行整合,比如cmdb可以通过zabbix api来实现监控的添加,更新和禁用等,zabbix的官方文档提供了比较详细的api列表。
因为最近添加screen的需求比较多,就简单写了一个python脚本用来自动化添加screen,有兴趣的可以借鉴下。
使用方式如下:
python ./screen_host.py -H 主机列表 -n screen名称 -G graph名称
具体的脚本:
#!/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:
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相关教程集合: