安装模块
pip3 install urllib_kerberos
pip3 install requests
pip3 install pyyaml
common
目录结构
common.py
#!/usr/bin/env python3
import yaml
import json
import requests
from requests.packages import urllib3
urllib3.disable_warnings()
class Common(object):
def __new__(cls, *args, **kwargs):
if not hasattr(cls, '_instance'):
cls._instance = object.__new__(cls, *args, **kwargs)
return cls._instance
def __init__(self):
with open('config.yml') as conf_file:
zconf = yaml.load(conf_file, Loader=yaml.BaseLoader)
self.url = "%s/zabbix/api_jsonrpc.php" % zconf['server']
self.user = zconf['user']
self.password = zconf['password']
def get_token(self):
"""
:return: token id
"""
request_body = {
"jsonrpc": "2.0",
"method": "user.login",
"params": {
"user": self.user,
"password": self.password
},
"id": 1,
"auth": None
}
token = requests.post(self.url, json=request_body, verify=False).json()['result']
return token
def zabbix_get(self, method, params_json):
"""
:param method: str
:param params_json: json
:return: json
"""
token = self.get_token()
request_body = {
"jsonrpc": "2.0",
"method": method,
"params": params_json,
"id": 1,
"auth": token
}
result = requests.get(self.url, json=request_body, verify=False)
return result
def main():
if __name__ == '__main__':
zbx = Common()
get_temp_body = {
"output": "extend",
"filter": {
"host": ["Ceph PROC"]
}
}
result = zbx.zabbix_get('template.get', get_temp_body)
print(json.dumps(result.json(), indent=4))
main()
config.yml
---
user: Admin
password: xxxxx
server: http://x.x.x.x
pyzabbix
pyzabbix/common.py
#!/usr/bin/env python3
import yaml
import json
import requests
from requests.packages import urllib3
urllib3.disable_warnings()
class Common(object):
def __new__(cls, *args, **kwargs):
if not hasattr(cls, '_instance'):
cls._instance = object.__new__(cls, *args, **kwargs)
return cls._instance
def __init__(self):
with open('config.yml') as conf_file:
zconf = yaml.load(conf_file, Loader=yaml.BaseLoader)
self.url = "%s/zabbix/api_jsonrpc.php" % zconf['server']
self.user = zconf['user']
self.password = zconf['password']
def get_token(self):
"""
:return: token id
"""
request_body = {
"jsonrpc": "2.0",
"method": "user.login",
"params": {
"user": self.user,
"password": self.password
},
"id": 1,
"auth": None
}
token = requests.post(self.url, json=request_body, verify=False).json()['result']
return token
def zabbix_get(self, method, params_json):
"""
:param method: str
:param params_json: json
:return: json
"""
token = self.get_token()
request_body = {
"jsonrpc": "2.0",
"method": method,
"params": params_json,
"id": 1,
"auth": token
}
result = requests.get(self.url, json=request_body, verify=False)
return result
def main():
if __name__ == '__main__':
zbx = Common()
get_temp_body = {
"filter": {
"host": [
"ceph-mgr"
]
}
}
result = zbx.zabbix_get('host.get', get_temp_body)
print(json.dumps(result.json(), indent=4))
main()
host.py
#!/usr/bin/env python3
import sys
import json
import pyzabbix
from pyzabbix import common
class Zabbix(object):
def __init__(self):
pass
def get_host(self):
zbx=common.Common()
get_body= {
"filter": {
"host": [
"ceph-mgr"
]
}
}
result=zbx.zabbix_get('host.get',get_body).json()['result']
return result
def get():
zabbix=Zabbix()
print(zabbix.get_host())
if __name__ == '__main__':
get()
config.yml
---
user: xxxxxx
password: xxxxxx
server: http://xxxxxx