Skip to content

Commit fa333b6

Browse files
author
zhanghe5
committed
first commit
0 parents  commit fa333b6

11 files changed

+914
-0
lines changed

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
pyzabbix
2+
========
3+
4+
**pyzabbix** is a python module for working with the [Zabbix API](https://www.zabbix.com/documentation/2.2/manual/api).
5+
6+
The Zabbix API is a web based API and is shipped as part of the web frontend. It uses the JSON-RPC 2.0 protocol which means two things:
7+
8+
* The API consists of a set of separate methods,like "user.login","item.create",etc.
9+
* Requests and responses between the clients and the API are encoded using the JSON format.
10+
11+
There are some examples using this method:
12+
```
13+
[root@test Zabbix-PyZabbix]# python zabbix_host_delete.py -h
14+
Usage: zabbix_host_delete.py [options]
15+
16+
Options:
17+
-h, --help show this help message and exit
18+
-s SERVER, --server=SERVER
19+
(REQUIRED)Zabbix Server URL.
20+
-u USERNAME, --username=USERNAME
21+
(REQUIRED)Username (Will prompt if not given).
22+
-p PASSWORD, --password=PASSWORD
23+
(REQUIRED)Password (Will prompt if not given).
24+
-H HOSTNAME, --hostname=HOSTNAME
25+
(REQUIRED)hostname for hosts.
26+
-f FILE, --file=FILE Load values from input file. Specify - for standard
27+
input Each line of file contains whitespace delimited:
28+
<hostname>
29+
```

core/__init__.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/bin/bin/env python
2+
#coding:utf-8
3+
4+
import json
5+
import urllib2
6+
7+
class ZabbixAPI(object):
8+
9+
def __init__(self,url,user,password,headers = {"Content-Type":"application/json"}):
10+
self.request_data = {
11+
"jsonrpc":"2.0",
12+
"method":"user.login",
13+
"params":"null",
14+
"id": 1,
15+
"auth":""
16+
}
17+
self.url = url + "/api_jsonrpc.php"
18+
self.headers = headers
19+
self.login(user,password)
20+
21+
def login(self,user,password):
22+
method = "user.login"
23+
params = {"user":user,"password":password}
24+
auth = self.deal_request(method=method,params=params)
25+
self.request_data["auth"] = auth
26+
27+
def deal_request(self,method,params):
28+
self.request_data["method"] = method
29+
self.request_data["params"] = params
30+
request = urllib2.Request(url=self.url,data=json.dumps(self.request_data),headers=self.headers)
31+
try:
32+
response = urllib2.urlopen(request)
33+
#return json.loads(response.read())["result"]
34+
s = json.loads(response.read())
35+
return s["result"]
36+
except Exception as e:
37+
print "Error: ",s
38+
39+
def __getattr__(self,name):
40+
return ZabbixObj(name,self)
41+
42+
class ZabbixObj(object):
43+
44+
def __init__(self,method_fomer,ZabbixAPI):
45+
self.method_fomer = method_fomer
46+
self.ZabbixAPI = ZabbixAPI
47+
48+
def __getattr__(self, name):
49+
def func(params):
50+
method = self.method_fomer+"."+name
51+
params = params
52+
return self.ZabbixAPI.deal_request(method=method,params=params)
53+
return func

zabbix_graph_add.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#!/usr/bin/env python
2+
#coding:utf8
3+
4+
'''
5+
Created on 03.06.2015
6+
'''
7+
8+
import optparse
9+
import sys
10+
import traceback
11+
from getpass import getpass
12+
from core import ZabbixAPI
13+
14+
def get_options():
15+
usage = "usage: %prog [options]"
16+
OptionParser = optparse.OptionParser
17+
parser = OptionParser(usage)
18+
19+
parser.add_option("-s","--server",action="store",type="string",\
20+
dest="server",help="(REQUIRED)Zabbix Server URL.")
21+
parser.add_option("-u", "--username", action="store", type="string",\
22+
dest="username",help="(REQUIRED)Username (Will prompt if not given).")
23+
parser.add_option("-p", "--password", action="store", type="string",\
24+
dest="password",help="(REQUIRED)Password (Will prompt if not given).")
25+
parser.add_option("-H","--hostname",action="store",type="string",\
26+
dest="hostname",help="(REQUIRED)hostname for hosts.")
27+
parser.add_option("-n","--name",action="store",type="string",\
28+
dest="name",help="(REQUIRED)Name of the interface.")
29+
parser.add_option("-i","--interface",action="store",type="string",\
30+
dest="interface",default="",help="(REQUIRED,Default:"")key of the interface.")
31+
parser.add_option("--pool",action="store",type="string",\
32+
dest="pool",default="core-pool",help="(REQUIRED,Default:"")the interface pool.")
33+
34+
options,args = parser.parse_args()
35+
36+
if not options.server:
37+
options.server = raw_input('server http:')
38+
39+
if not options.username:
40+
options.username = raw_input('Username:')
41+
42+
if not options.password:
43+
options.password = getpass()
44+
45+
return options, args
46+
47+
def errmsg(msg):
48+
sys.stderr.write(msg + "\n")
49+
sys.exit(-1)
50+
51+
if __name__ == "__main__":
52+
options, args = get_options()
53+
54+
zapi = ZabbixAPI(options.server,options.username, options.password)
55+
56+
hostname = options.hostname
57+
name = options.name
58+
i = options.interface
59+
pool = options.pool
60+
61+
idc=hostname.rstrip('-summary')
62+
63+
key_responseTime = i+"-responseTime"
64+
key_0_200 = i+"-0-200"
65+
key_200_500 = i+"-200-500"
66+
key_500_1000 = i+"-500-1000"
67+
key_1000_2000 = i+"-1000-2000"
68+
key_2000_999999 = i+"-2000-999999"
69+
key_Total_Requests = i+"-Total-Requests"
70+
key_retMsg_FAIL = i+"-retMsg-FAIL"
71+
key_retMsg_SUCC = i+"-retMsg-SUCC"
72+
73+
print key_responseTime,key_0_200,key_Total_Requests
74+
75+
hostid = zapi.host.get({"filter":{"host":hostname}})[0]["hostid"]
76+
i_responseTime = zapi.item.get({"output": "extend","hostids": hostid,"search":{"key_": key_responseTime}})[0]["itemid"]
77+
i_0_200 = zapi.item.get({"output": "extend","hostids": hostid,"search":{"key_": key_0_200}})[0]["itemid"]
78+
i_200_500 = zapi.item.get({"output": "extend","hostids": hostid,"search":{"key_": key_200_500}})[0]["itemid"]
79+
i_500_1000 = zapi.item.get({"output": "extend","hostids": hostid,"search":{"key_": key_500_1000}})[0]["itemid"]
80+
i_1000_2000 = zapi.item.get({"output": "extend","hostids": hostid,"search":{"key_": key_1000_2000}})[0]["itemid"]
81+
i_2000_999999 = zapi.item.get({"output": "extend","hostids": hostid,"search":{"key_": key_2000_999999}})[0]["itemid"]
82+
i_Total_Requests = zapi.item.get({"output": "extend","hostids": hostid,"search":{"key_": key_Total_Requests}})[0]["itemid"]
83+
i_retMsg_FAIL = zapi.item.get({"output": "extend","hostids": hostid,"search":{"key_": key_retMsg_FAIL}})[0]["itemid"]
84+
i_retMsg_SUCC = zapi.item.get({"output": "extend","hostids": hostid,"search":{"key_": key_retMsg_SUCC}})[0]["itemid"]
85+
86+
api = "API ("+pool+") -- "+name
87+
api_response_time = "API ("+pool+") -- "+name+" response time"
88+
api_response_time_pie = "API ("+pool+") -- "+name+" response time (pie)"
89+
api_succ_rate_pie = "API ("+pool+") -- "+name+" succ rate (pie)"
90+
91+
print i_responseTime,i_0_200,i_200_500,i_500_1000,i_1000_2000,i_2000_999999,i_Total_Requests,i_retMsg_SUCC,i_retMsg_FAIL
92+
print api,api_response_time,api_response_time_pie,api_succ_rate_pie
93+
94+
zapi.graph.create({"name":api,"width":900,"height": 200,"gitems": [{"itemid": i_retMsg_FAIL,"color": "CC0000","drawtype":1,"yaxisside":0},{"itemid": i_retMsg_SUCC,"color": "00EE00","drawtype":1,"yaxisside":0},{"itemid": i_Total_Requests,"color": "C800C8","drawtype":0,"yaxisside":0},{"itemid": i_responseTime,"color": "0000BB","drawtype":0,"yaxisside":1}]})
95+
zapi.graph.create({"name":api_response_time,"width":900,"height": 200,"gitems": [{"itemid": i_0_200,"color": "33FF33","drawtype":1,"yaxisside":0},{"itemid": i_200_500,"color": "008800","drawtype":1,"yaxisside":0},{"itemid": i_500_1000,"color": "CCCC00","drawtype":1,"yaxisside":0},{"itemid": i_1000_2000,"color": "FF3333","drawtype":1,"yaxisside":0},{"itemid": i_2000_999999,"color": "880000","drawtype":1,"yaxisside":0},{"itemid": i_Total_Requests,"color": "CC00CC","drawtype":0,"yaxisside":0},{"itemid": i_responseTime,"color": "0000BB","drawtype":0,"yaxisside":1}]})
96+
zapi.graph.create({"name":api_response_time_pie,"width":900,"height": 300,"graphtype":2,"gitems": [{"itemid": i_0_200,"color": "33FF33"},{"itemid": i_200_500,"color": "008800"},{"itemid": i_500_1000,"color": "CCCC00"},{"itemid": i_1000_2000,"color": "FF3333"},{"itemid": i_2000_999999,"color": "880000"}]})
97+
zapi.graph.create({"name":api_succ_rate_pie,"width":900,"height": 300,"graphtype":2,"gitems": [{"itemid": i_retMsg_FAIL,"color": "C80000"},{"itemid": i_retMsg_SUCC,"color": "00EE00"}]})

zabbix_graph_get.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#!/usr/bin/env python
2+
#coding:utf8
3+
4+
'''
5+
Created on 03.06.2015
6+
'''
7+
8+
import optparse
9+
import sys
10+
import traceback
11+
import simplejson
12+
from getpass import getpass
13+
from core import ZabbixAPI
14+
15+
def get_options():
16+
usage = "usage: %prog [options]"
17+
OptionParser = optparse.OptionParser
18+
parser = OptionParser(usage)
19+
20+
parser.add_option("-s","--server",action="store",type="string",\
21+
dest="server",help="(REQUIRED)Zabbix Server URL.")
22+
parser.add_option("-u", "--username", action="store", type="string",\
23+
dest="username",help="(REQUIRED)Username (Will prompt if not given).")
24+
parser.add_option("-p", "--password", action="store", type="string",\
25+
dest="password",help="(REQUIRED)Password (Will prompt if not given).")
26+
parser.add_option("-H","--hostname",action="store",type="string",\
27+
dest="hostname",help="(REQUIRED)hostname for hosts.")
28+
parser.add_option("-k","--key",action="store",type="string",\
29+
dest="key",help="(REQUIRED)Item key.")
30+
parser.add_option("-n","--name",action="store",type="string",\
31+
dest="name",help="(REQUIRED)Name of the graph.")
32+
33+
options,args = parser.parse_args()
34+
35+
if not options.server:
36+
options.server = raw_input('server http:')
37+
38+
if not options.username:
39+
options.username = raw_input('Username:')
40+
41+
if not options.password:
42+
options.password = getpass()
43+
44+
if not options.hostname:
45+
options.hostname = raw_input('hostname:')
46+
47+
return options, args
48+
49+
def errmsg(msg):
50+
sys.stderr.write(msg + "\n")
51+
sys.exit(-1)
52+
53+
if __name__ == "__main__":
54+
options, args = get_options()
55+
56+
zapi = ZabbixAPI(options.server,options.username, options.password)
57+
58+
hostname = options.hostname
59+
key = options.key
60+
name = options.name
61+
62+
hostid=zapi.host.get({"filter":{"host":hostname}})[0]["hostid"]
63+
graph=zapi.graph.get({"output": "extend","hostids": hostid,"search":{"name":name}})
64+
for i in graph:
65+
print i,"\n"

zabbix_graph_mapi_add.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#!/usr/bin/env python
2+
#coding:utf8
3+
4+
'''
5+
Created on 03.06.2015
6+
'''
7+
8+
import optparse
9+
import sys
10+
import traceback
11+
from getpass import getpass
12+
from core import ZabbixAPI
13+
14+
def get_options():
15+
usage = "usage: %prog [options]"
16+
OptionParser = optparse.OptionParser
17+
parser = OptionParser(usage)
18+
19+
parser.add_option("-s","--server",action="store",type="string",\
20+
dest="server",help="(REQUIRED)Zabbix Server URL.")
21+
parser.add_option("-u", "--username", action="store", type="string",\
22+
dest="username",help="(REQUIRED)Username (Will prompt if not given).")
23+
parser.add_option("-p", "--password", action="store", type="string",\
24+
dest="password",help="(REQUIRED)Password (Will prompt if not given).")
25+
parser.add_option("-H","--hostname",action="store",type="string",\
26+
dest="hostname",help="(REQUIRED)hostname for hosts.")
27+
parser.add_option("-n","--name",action="store",type="string",\
28+
dest="name",help="(REQUIRED)Name of the interface.")
29+
parser.add_option("-i","--interface",action="store",type="string",\
30+
dest="interface",default="",help="(REQUIRED,Default:"")key of the interface.")
31+
32+
options,args = parser.parse_args()
33+
34+
if not options.server:
35+
options.server = raw_input('server http:')
36+
37+
if not options.username:
38+
options.username = raw_input('Username:')
39+
40+
if not options.password:
41+
options.password = getpass()
42+
43+
return options, args
44+
45+
def errmsg(msg):
46+
sys.stderr.write(msg + "\n")
47+
sys.exit(-1)
48+
49+
if __name__ == "__main__":
50+
options, args = get_options()
51+
52+
zapi = ZabbixAPI(options.server,options.username, options.password)
53+
54+
hostname = options.hostname
55+
name = options.name
56+
i = options.interface
57+
58+
#idc=hostname.rstrip('-summary')
59+
60+
key_responseTime = i+"-responseTime"
61+
key_0_200 = i+"-0-200"
62+
key_200_500 = i+"-200-500"
63+
key_500_1000 = i+"-500-1000"
64+
key_1000_2000 = i+"-1000-2000"
65+
key_2000_999999 = i+"-2000-999999"
66+
key_httpCode_200 = i+"-HttpCode-200"
67+
key_httpCode_4xx = i+"-HttpCode-400"
68+
key_httpCode_5xx = i+"-HttpCode-500"
69+
70+
print key_responseTime,key_0_200,key_httpCode_200,key_httpCode_4xx,key_httpCode_5xx
71+
72+
#hostid = zapi.host.get({"filter":{"host":hostname}})[0]["hostid"]
73+
hostid = zapi.template.get({"filter":{"host":hostname}})[0]["templateid"]
74+
i_responseTime = zapi.item.get({"output": "extend","hostids": hostid,"search":{"key_": key_responseTime}})[0]["itemid"]
75+
#i_0_200 = zapi.item.get({"output": "extend","hostids": hostid,"search":{"key_": key_0_200}})[0]["itemid"]
76+
#i_200_500 = zapi.item.get({"output": "extend","hostids": hostid,"search":{"key_": key_200_500}})[0]["itemid"]
77+
#i_500_1000 = zapi.item.get({"output": "extend","hostids": hostid,"search":{"key_": key_500_1000}})[0]["itemid"]
78+
#i_1000_2000 = zapi.item.get({"output": "extend","hostids": hostid,"search":{"key_": key_1000_2000}})[0]["itemid"]
79+
#i_2000_999999 = zapi.item.get({"output": "extend","hostids": hostid,"search":{"key_": key_2000_999999}})[0]["itemid"]
80+
i_httpCode_200 = zapi.item.get({"output": "extend","hostids": hostid,"search":{"key_": key_httpCode_200}})[0]["itemid"]
81+
i_httpCode_4xx = zapi.item.get({"output": "extend","hostids": hostid,"search":{"key_": key_httpCode_4xx}})[0]["itemid"]
82+
i_httpCode_5xx = zapi.item.get({"output": "extend","hostids": hostid,"search":{"key_": key_httpCode_5xx}})[0]["itemid"]
83+
84+
api = "MAPI - "+name
85+
api_response_time = "MAPI - "+name+" response time"
86+
87+
print i_responseTime,i_httpCode_200,i_httpCode_4xx,i_httpCode_5xx
88+
print api,api_response_time
89+
90+
zapi.graph.create({"name":api,"width":900,"height": 200,"gitems": [{"itemid": i_httpCode_200,"color": "00DD00","drawtype":1,"yaxisside":0,"sortorder":0},{"itemid": i_httpCode_4xx,"color": "DD00DD","drawtype":1,"yaxisside":0,"sortorder":1},{"itemid": i_httpCode_5xx,"color": "DD0000","drawtype":1,"yaxisside":0,"sortorder":2},{"itemid": i_responseTime,"color": "0000DD","drawtype":0,"yaxisside":1,"sortorder":3}]})
91+
#zapi.graph.create({"name":api_response_time,"width":900,"height": 200,"gitems": [{"itemid": i_0_200,"color": "33FF33","drawtype":1,"yaxisside":0},{"itemid": i_200_500,"color": "008800","drawtype":1,"yaxisside":0},{"itemid": i_500_1000,"color": "CCCC00","drawtype":1,"yaxisside":0},{"itemid": i_1000_2000,"color": "FF3333","drawtype":1,"yaxisside":0},{"itemid": i_2000_999999,"color": "880000","drawtype":1,"yaxisside":0},{"itemid": i_Total_Requests,"color": "CC00CC","drawtype":0,"yaxisside":0},{"itemid": i_responseTime,"color": "0000BB","drawtype":0,"yaxisside":1}]})

0 commit comments

Comments
 (0)