这是我从网上找的一个python post访问zabbix接口的demo
import json
import urllib2
zabbix_url="http://192.168.0.244/zabbix/api_jsonrpc.php"
zabbix_header={"Content-Type":"application/json"}
auth_data=json.dumps({"jsonrpc":"2.0","method":"user.login","params":{"user":"Admin","password":"zabbix"},"id":0})
auth_code=""
request=urllib2.Request(zabbix_url,auth_data)
for key in zabbix_header:
request.add_header(key,zabbix_header[key])
try:
result=urllib2.urlopen(request)
except urllib2.URLError as err:
print 'We failed,but I don\'t know the reaseon'
else:
response=json.loads(result.read())
result.close()
if 'result' in response:
auth_code=response['result']
print 'We succeed,and the auth_id is ',auth_code
else:
print response['error']['data']
hosts_data=json.dumps({"jsonrpc":"2.0","method":"host.get","params":{"output":["hostid","host"],"selectInterfaces":["interfaceid","ip"]},"id":1,"auth":auth_code})
request=urllib2.Request(zabbix_url,hosts_data)
for key in zabbix_header:
request.add_header(key,zabbix_header[key])
try:
result=urllib2.urlopen(request)
except urllib2.URLError as e:
print 'fail'
else:
response=json.loads(result.read())
result.close()
for host in response['result']:
print "Host ID:",host['hostid'],",Host Name:",host['host'],",Interface ID:",host['interfaces'][0]['interfaceid'],",ip:",host['interfaces'][0]['ip']
print "Number of hosts:",len(response['result'])
在linux下的执行结果:
[[email protected] PyDir]# python zabbix_api_demo.py
We succeed,and the auth_id is d9367fded69993832fd64f60d8375a03
Host ID: 10084 ,Host Name: Zabbix server ,Interface ID: 1 ,ip: 127.0.0.1
Host ID: 10132 ,Host Name: jmxDemo ,Interface ID: 29 ,ip: 127.0.0.1
Host ID: 10133 ,Host Name: SnmpDemo ,Interface ID: 30 ,ip: 127.0.0.1
Host ID: 10136 ,Host Name: ligang ,Interface ID: 33 ,ip: 192.168.0.28
Host ID: 10137 ,Host Name: yegucheng ,Interface ID: 34 ,ip: 192.168.0.129
Host ID: 10155 ,Host Name: 192.168.0.233 ,Interface ID: 53 ,ip: 192.168.0.233
Host ID: 10174 ,Host Name: delavior ,Interface ID: 68 ,ip: 192.168.0.147
Host ID: 10175 ,Host Name: UbuntuDemo ,Interface ID: 69 ,ip: 192.168.0.20
Host ID: 10176 ,Host Name: DemoHost ,Interface ID: 72 ,ip: 192.168.0.20
Host ID: 10177 ,Host Name: cxj demo ,Interface ID: 73 ,ip: 192.168.0.139
Number of hosts: 10
在windows下的执行结果,开始是这个:
G:\Desktop>py -2 zabbix_api_demo.py
Traceback (most recent call last):
File "zabbix_api_demo.py", line 14, in <module>
except URLError as err:
NameError: name 'URLError' is not defined
然后我把URLError改成了urllib2.URLError,然后结果变成了这个:
G:\Desktop>py -2 zabbix_api_demo.py
We failed,but I don't know the reaseon
fail
不懂python,不太明白为啥同样的文件在linux下和windows下执行结果不一样,麻烦给指点一下
ps:linux下的python版本是2.7.9,windows下装了两个,一个是2.7.9,一个是3.4.3
1
ivanlw 2015-05-13 16:12:25 +08:00
断点逐句调试
|
2
linkiosk 2015-05-13 18:08:16 +08:00
在编辑器中转一下换行方式,转为windows的
|
3
laoyur 2015-05-13 18:28:18 +08:00
喜闻乐见py格式被毁,别人想帮你测试都没门了
|
5
realityone 2015-05-13 20:51:47 +08:00
except urllib2.URLError as err:
print 'We failed,but I don\'t know the reason' 我不太建议写成这样。。好歹输出点有用的信息让人知道什么异常 |
6
saber000 2015-05-13 22:08:42 +08:00
Python版本不一样吧?
刚好我有个ZabbixApi的实现:https://github.com/MrLYC/ZabbixPythonApi 使用pip install zabbixapi就能安装上. 使用方式: from zabbixapi.zabbixapi import ZabbixAPI zapi = ZabbixAPI("http://192.168.0.244/zabbix/api_jsonrpc.php") zapi.login("Admin", "zabbix") result = zapi.host.get({"output": ["hostid", "host"], "selectInterfaces": ["interfaceid", "ip"]}) print result 这样就相当于你上面的代码了 |
7
binux 2015-05-13 22:25:26 +08:00
You failed,but we don't know the reaseon
|
8
jugelizi 2015-05-13 22:35:34 +08:00
大快人心
曾经也是win下跑不出结果 换到linux啥问题也没有 |
9
ericls 2015-05-13 22:57:18 +08:00 via Android
是同一个 interpreter 么?
|
10
Septembers 2015-05-13 23:16:32 +08:00
@Livid 建议做一个主题发布/回复指南统一说明V2EX支持那些语法,进而提升主题/回复质量
|
11
wind3110991 2015-05-14 02:12:01 +08:00 via iPhone
能用下markdown吗。。。py首行缩进没了感觉在这个社区还是不太合适吧。。毕竟一群pythoner
|
12
delavior OP @realityone 嗯嗯,加了code和msg,输出是502
|
16
WKPlus 2015-05-14 10:46:20 +08:00
@Septembers @Livid 正在摸索发帖时支持哪些格式,非常赞同这个提议:建议做一个主题发布/回复指南统一说明V2EX支持那些语法,进而提升主题/回复质量
|
17
endrollex 2015-05-14 10:50:10 +08:00
@Septembers 同感,找不到帮助,Markdown用的少背不出
|
18
WKPlus 2015-05-14 10:50:10 +08:00
可以先在windows机器上用浏览器插件来调用zabbix api,看看是否OK,这样可以用来排查是网络问题还是python(解析器版本或者其他什么)的问题
|
19
CRVV 2015-05-14 14:05:26 +08:00
URLError没改成urllib2.URLError的时候在Linux下能执行成功?
我对这个表示怀疑 另外像楼上说的,应该先排除网络的原因 |
20
xzem 2015-05-14 14:56:38 +08:00 via Android
版本问题?
|