python通过snmp协议运用多线程获取多台主机网卡信息,写入数据库
#-*- coding:utf-8 -*- import netsnmp class SnmpClass(object): """ SNMP """ def __init__(self, oid="sysDescr", version=2, destHost="localhost", community="aaa"): self.oid = oid self.version = version self.destHost = destHost self.community = community @property def query(self): """ snmpwalk """ try: result = netsnmp.snmpwalk(self.oid, Version=self.version, DestHost=self.destHost, Community=self.community) except Exception, err: print err result = None return result def getflow(): x = SnmpClass(oid="ifInOctets", destHost="218.205.104.1") y = SnmpClass(oid="ifDescr", destHost="218.205.104.1") datakey = y.query datavaule = x.query data = {} for z in range(len(datakey)): data[datakey[z]] = datavaule[z] print data getflow()
community:为团体字
运用多线程获取多台主机网卡信息,写入数据库。
多线程(英语:multithreading),是指从软件或者硬件上实现多个线程并发执行的技术。具有多线程能力的计算机因有硬件支持而能够在同一时间执行多于一个线程,进而提升整体处理性能。具有这种能力的系统包括对称多处理机、多核心处理器以及芯片级多处理(Chip-level multithreading)或同时多线程(Simultaneous multithreading)处理器。在一个程序中,这些独立运行的程序片段叫作“线程”(Thread),利用它编程的概念就叫作“多线程处理(Multithreading)”。具有多线程能力的计算机因有硬件支持而能够在同一时间执行多于一个线程(台湾译作“执行绪”),进而提升整体处理性能。
#coding:utf-8 import threading import time import netsnmp import Queue import logging import logging.config import MySQLdb import datetime from flask_sqlalchemy import SQLAlchemy start_time = time.time() #tm = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") #start_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") #start_time = datetime.datetime.now() hosts = ["218.205.104.3", "218.205.104.8"] myq = Queue.Queue() rq = Queue.Queue() conn = MySQLdb.connect(host='218.205.104.27', user='admin',passwd='7758258', port=12306, db='yxjs') cursor = conn.cursor() logging.config.fileConfig("/root/tmp/logger.ini") logger = logging.getLogger("status_domain") # #把host和oid组成任务 def list_sum(x): for i in range(len(x)): try:x[i] = int(x[i]) except:x[i] = 0 return sum(x) def insert_DB_flow_mul( dt,**other): while True: try: MysqlDB = MysqldbClass("localhost", "admin", "7758258", "WCMSS", "utf8" ) MysqlDB.getCurs() MysqlDB.insert(insert_sql_cache_domain, [(dt, domain, sum, hit, miss, size,resptime, s_code_000, s_code_20x, s_code_30x, s_code_40x, s_code_50x, othercode, svrname)]) print 'insert success' logger.debug(((dt, domain, sum, hit, miss, size,resptime, s_code_000, s_code_20x, s_code_30x, s_code_40x, s_code_50x, othercode, svrname), "insert_sql OK.")) MysqlDB.freeCurs() MysqlDB.commit() MysqlDB.close() break except: time.sleep(10) logger.debug(('retry: ', (datetime, svrname), sys.exc_info())) for host in hosts: myq.put(host) def poll_one_host(): while True: try: #死循环从队列中获取任务,直到队列任务为空 host = myq.get(block=False) session = netsnmp.Session(Version=2, DestHost=host, Community="dnion",Timeout=3000000,Retries=0) oid0 = netsnmp.Varbind('.1.3.6.1.2.1.1.5.0') #0 get 主机名 oid1 = netsnmp.Varbind('.1.3.6.1.2.1.2.2.1.2') #1 walk 网卡信息 oid2 = netsnmp.Varbind('.1.3.6.1.2.1.2.2.1.10') #2 walk 网卡接收的字节数 oid3 = netsnmp.Varbind('.1.3.6.1.2.1.2.2.1.16') #3 walk 网卡流出字节数 oid4 = netsnmp.Varbind('.1.3.6.1.4.1.2021.11.11.0') #4 空闲cpu百分比 oid5 = netsnmp.Varbind('.1.3.6.1.4.1.2021.10.1.3.1') #5 5分钟系统负载 oid6 = netsnmp.Varbind('.1.3.6.1.4.1.2021.10.1.3.2') #6 10分钟系统负载 oid7 = netsnmp.Varbind('.1.3.6.1.4.1.2021.10.1.3.3') #7 15分钟系统负载 oid8 = netsnmp.Varbind('.1.3.6.1.4.1.2021.4.5.0') #8 机器内存总数 oid9 = netsnmp.Varbind('.1.3.6.1.4.1.2021.4.6.0') #9 机器已用内存 oid10 = netsnmp.Varbind('.1.3.6.1.4.1.2021.9.1.6') #10 walk 系统盘总容量 oid11 = netsnmp.Varbind('.1.3.6.1.4.1.2021.9.1.8') #11 walk 系统盘已用容量 var_list = netsnmp.VarList(oid0,oid4,oid5,oid6,oid7,oid8,oid9) var_list1 = netsnmp.VarList() var_list1.append(oid1) var_list2 = netsnmp.VarList() var_list2.append(oid2) var_list3 = netsnmp.VarList() var_list3.append(oid3) var_list10 = netsnmp.VarList() var_list10.append(oid10) var_list11 = netsnmp.VarList() var_list11.append(oid11) ret = session.get(var_list) ret1 = session.walk(var_list1) # 网卡信息 ret2 = list(session.walk(var_list2)) # 网卡流入流量 ret3 = list(session.walk(var_list3)) # 网卡流出流量 ret10 = list(session.walk(var_list10))# 系统盘总容量 ret11 = list(session.walk(var_list11))# 系统盘已用容量 print ret1 print ret2 tm = datetime.datetime.now().strftime("%Y%m%d%H%M%S") hostname = str(ret[0]) cpu_ret = str(float(ret[1])/100)+'%' #CPU使用率 ll_1 = float(ret[2]) ll_5 = float(ret[3]) ll_15 = float(ret[4]) ipaddr = str(host) ram_ret = str(round((float(ret[6])/float(ret[5]))*100,2))+'%' #内存使用率 disk_ret = str(round(float(list_sum(ret11))/float(list_sum(ret10))*100,2))+'%' #磁盘使用率 value = (ll_1, ll_5, ll_15, ipaddr, tm) #print value me = Mondata(ll_1, ll_5, ll_15, ipaddr, tm) # db.session.add(me) # db.session.commit() #cursor.execute("insert into mondata (ll_1, ll_5, ll_15, ipaddr, datetime)values (%s, %s, %s, %s, %s)", (ll_1, ll_5, ll_15, ipaddr, tm)) #cursor.close() #conn.commit() #conn.close() rq.put((host, ret, start_time)) except Queue.Empty: break thread_arr = [] #开启多线程 num_thread = 200 for i in range(num_thread): t = threading.Thread(target=poll_one_host, kwargs={}) t.setDaemon(True) t.start() thread_arr.append(t) #等待任务执行完毕 for i in range(num_thread): thread_arr[i].join() while True: try: info = rq.get(block=False) #print info except Queue.Empty: #print time.time() - start_time break