-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.py
65 lines (63 loc) · 2.08 KB
/
api.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
#!/usr/bin/python
#coding:utf8
import json
import urllib2
import ConfigParser
class variables():
def __init__(self):
'''define zabbix need information'''
config = ConfigParser.ConfigParser()
config.read('zabbix.conf')
if config.has_option("zserver","Url"):
_url = config.get("zserver","Url")
if config.has_option("zserver","User"):
_user = config.get("zserver","User")
if config.has_option("zserver","Password"):
_password = config.get("zserver","Password")
self.url = _url
self.user = _user
self.password = _password
self.header = {"Content-Type": "application/json"}
def loginData(self):
data = json.dumps({
"jsonrpc": "2.0",
"method": "user.login",
"params": {
"user": self.user,
"password": self.password
},
"id": 0
})
request = urllib2.Request(self.url,data)
for key in self.header:
request.add_header(key,self.header[key])
try:
result = urllib2.urlopen(request)
except Exception as e:
print "Auth Failed, Please Check Your Name And Password:",e.code
else:
response = json.loads(result.read())
result.close()
authID = response['result']
return authID
def dataGet(self,data,hostip=""):
request = urllib2.Request(self.url,data,self.header)
try:
result = urllib2.urlopen(request)
except Exception as e:
if hasattr(e, "reason"):
print "Failed to reach a server."
print "Reason:", e.reason
elif hasattr(e, "reason"):
print 'The server could not fulfill the request.'
print 'Error code: ', e.code
return 0
else:
response = json.loads(result.read())
result.close()
return response
def main():
API = variables()
return API.loginData()
if __name__ == "__main__":
main()