码迷,mamicode.com
首页 > 编程语言 > 详细

Python3调用zabbixAPI,snmp协议监控路由器简单模板

时间:2020-07-01 15:54:46      阅读:69      评论:0      收藏:0      [点我收藏+]

标签:int   comm   sub   登录   xxxx   bsp   RoCE   imp   系统   

python version: python3.6

system version: Centos 8.2.2004

zabbix version: 5.0

 

 

import requests
import json
import uuid
import sys
import subprocess


auth = {
    "jsonrpc": "2.0",
    "method": "user.login",
    "params": {
        "user": "user_name",
        "password":"user_password"
        },
        "id": 1,
        "auth":None,
    }

auth_id = requests.post("http://zabbix_url/api_jsonrpc.php", data=json.dumps(auth), headers={Content-Type: application/json-rpc})
auth_id = json.loads(auth_id.text)[result]

def post_action(data):
    req = requests.post("http://zabbix_url/api_jsonrpc.php", data=json.dumps(data), headers={Content-Type: application/json-rpc})
    dict_data = json.loads(req.text)
    # print(dict_data)
    return dict_data[result]

def zabbix_action(method, params):
    data = {}
    jsonrpc_ver = "2.0"
# 登录系统操作的key,就是上面的auth_id,最好是获取一次,删掉上面的登录信息填入这里。 auth ="XXXXXXX" data[method] = method data[params] = params data[auth] = auth data[jsonrpc] = jsonrpc_ver data[id] = str(uuid.uuid1()) return post_action(data) # 获取 template_id method1 = "template.get" params1 = { "output": "templateid", "filter": { "host": [ "Template Net Huawei VRP SNMPv2", #模板名称,用于获取模板ID ] } } template_id = zabbix_action(method1, params1) # 获取 groupid method2 = "hostgroup.get" params2 = { "output": "groupid", "filter": { "name": [ "Network_Devices" #组名,用于获取组ID ] } } groups_id = zabbix_action(method2,params2) # 根据主机IP获取主机ID def host_get_id(ip): method_host_get = "host.get" params_host_get = { "output" : ["hostid"], # 输出结果筛选,值显示hostid "filter" : {       # 条件搜索 "ip" : ip } } host_id = zabbix_action(method_host_get,params_host_get) return host_id[0]["hostid"] def host_create_snmp(ip, groups_id, template_id): snmp_port = 161 method_host_create = "host.create" params_host_create = { "host": ip, "interfaces": [ { "type": 2, "main": 1, "useip": 1, "ip": ip, "dns": "", "port": snmp_port, "details": { "version": 2, "community": "{$SNMP_COMMUNITY}", "bulk": 1 } } ], "proxy_hostid": 10322, "groups": groups_id, "templates": template_id, } result = zabbix_action(method_host_create, params_host_create) for i in range(1,255): host_ip = "172.16.1.{}".format(i) a = subprocess.run("snmpwalk -v2c -cknyy_rd {0} 1.3.6.1.2.1.1.5.0".format(host_ip),shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE) status = a.returncode if(status == 0): print("host_ip: {0},status:{1}".format(host_ip,status)) host_create_snmp(host_ip,groups_id,template_id)

 

Python3调用zabbixAPI,snmp协议监控路由器简单模板

标签:int   comm   sub   登录   xxxx   bsp   RoCE   imp   系统   

原文地址:https://www.cnblogs.com/kemengkai/p/13219486.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!