根据SNMP对应OID批量修改Zabbix2.4自动探索后设备的名称 v2 20151202更新

新:

#!/usr/local/python-3.5/bin/python3.5
# coding: utf-8

import pymysql
from pysnmp.entity.rfc3413.oneliner import cmdgen

def sqlconnect():
    communitys = ['jurassic','3AeG8knL','areyouready']
    cur_host = conn.cursor()
    host_str = "select hostid,ip from interface where ip!='127.0.0.1'"
    host_sql = cur_host.execute(host_str)
    for row_host_sql in cur_host.fetchall():
        for community in communitys:
            host_id = str(row_host_sql[0])
            host_ip = row_host_sql[1].decode()
            host_name = getname(host_ip, community)
            if host_name == "IndexError":
                print("主机(" + host_ip + ")团体名(" + community + ")错误或者网络不通,跳过该主机!")
            elif host_name == "UnknowError":
                print("读取主机(" + host_ip + ")主机名时遇到未知错误!跳过该主机!")
            elif host_name == "":
                print("读取主机(" + host_ip + ")主机名失败!跳过该主机!")
                break
            else:
                host_str_count= "select count(*) as count from hosts group by name=\"" + host_name + "\" having count=1"
                host_count = cur_host.execute(host_str_count)
                if host_count==0:
                    print(host_id + "   " + host_name)
                    host_host_update = "update hosts set host=\"" + host_name + "\",name=\"" + host_name + "\" where hostid=" + host_id
                    cur_host.execute(host_host_update)
                    print("主机(" + host_ip + ")更新名称为:" + host_name + " 。")
                    conn.commit()
                break

def getname(hosts_ip, hosts_community):
    try:
        cmdGen = cmdgen.CommandGenerator()
        errorIndication, errorStatus, errorIndex, varBinds = cmdGen.getCmd(
            cmdgen.CommunityData(hosts_community),
            cmdgen.UdpTransportTarget((hosts_ip, 161)),
            '1.3.6.1.2.1.1.5.0',
            lookupNames=True, lookupValues=True
            )
        name, value = varBinds[0]
        return str(value)
    except IndexError:
        return "IndexError"
    except:
        return "UnknowError"

conn = pymysql.connect(host='127.0.0.1', user='root', passwd='gpA9c7kmysql', db='zabbix', charset='utf8')
sqlconnect()





旧:

#!/usr/local/python-3.4/bin/python3.4
# coding: utf-8

import pymysql
from pysnmp.entity.rfc3413.oneliner import cmdgen

def sqlconnect():
    communitys = ['jurassic','3AeG8knL','areyouready']
    cur_host = conn.cursor()
    host_all_str = "select hostid from hosts"
    host_all_id = cur_host.execute(host_all_str)
    for row_host_id in cur_host.fetchall():
        host_id = str(row_host_id[0])
        host_ip_str = "select ip from interface where hostid=" + host_id
        host_ip = cur_host.execute(host_ip_str)
        for row_host_ip in cur_host.fetchall():
            for community in communitys:
                print_host_ip = row_host_ip[0].decode()
                host_name = getname(print_host_ip, community)
                if host_name == "IndexError":
                    print("主机(" + print_host_ip + ")团体名(" + community + ")错误或者网络不通,跳过该主机!")
                elif host_name == "UnknowError":
                    print("读取主机(" + print_host_ip + ")主机名时遇到未知错误!跳过该主机!")
                else:
                    host_host_update = "update hosts set host=\"" + host_name + "\" where hostid=" + host_id
                    cur_host.execute(host_host_update)
                    host_name_update = "update hosts set name=\"" + host_name + "\" where hostid=" + host_id
                    cur_host.execute(host_name_update)
                    print("主机(" + print_host_ip + ")更新名称为:" + host_name + " 。")
                    conn.commit()
                    break

def getname(hosts_ip, hosts_community):
    try:
        cmdGen = cmdgen.CommandGenerator()
        errorIndication, errorStatus, errorIndex, varBinds = cmdGen.getCmd(
            cmdgen.CommunityData(hosts_community),
            cmdgen.UdpTransportTarget((hosts_ip, 161)),
            '1.3.6.1.2.1.1.5.0',
            lookupNames=True, lookupValues=True
            )
        name, value = varBinds[0]
        return str(value)
    except IndexError:
        return "IndexError"
    except:
        return "UnknowError"

conn = pymysql.connect(host='127.0.0.1', user='root', passwd='数据库密码', db='zabbix', charset='utf8')
sqlconnect()


转载请注明原文链接:http://my.oschina.net/caiyuanbo/blog/501434

暂时没时间添加注解及详细解释说明,代码比较简单,可能有些纰漏,如果有问题可以跟我一起研究。

QQ:384152164

E-mail:[email protected]


你可能感兴趣的:(python,pymysql,模块,zabbix,批量修改,pysnmp,自动探索,设备名称)