python & snmp
用python獲取snmp信息有多個現成的庫可以使用,其中比較常用的是netsnmp
和pysnmp
兩個庫。網上有較多的關于兩個庫的例子。
本文重點在于如何并發的獲取snmp的數據,即同時獲取多臺機器的snmp信息。
netsnmp
先說netsnmp。python的netsnmp,其實是來自于net-snmp包。
python通過一個c文件調用net-snmp的接口獲取數據。
因此,在并發獲取多臺機器的時候,不能夠使用協程獲取。因為使用協程,在get數據的時候,協程會一直等待net-snmp接口返回數據,而不會像socket使用時那樣在等待數據時把CPU切換給其他協程使用。從這點上來說,使用協程和串行獲取沒有區別。
那么如何解決并發獲取的問題呢?可以使用線程,多線程獲取(當然也可以使用多進程)。多個線程同時調用net-snmp的接口獲取數據,然后cpu在多個線程之間不停切換。當一個線程獲取一個結果后,可以繼續調用接口獲取下一個snmp數據。
這里我寫了一個樣例程序。首先把所有的host和oid做成任務放到隊列里,然后啟動多個線程,去執行獲取任務。程序樣例如下:
import threadingimport timeimport netsnmpimport Queuestart_time = time.time()hosts = ["192.20.150.109", "192.20.150.110", "192.20.150.111", "192.20.150.112", "192.20.150.113", "192.20.150.114", "192.20.150.115", "192.20.150.116", "192.20.150.117", "192.20.150.118", "192.20.150.119", "192.20.150.120", "192.20.150.121", "192.20.80.148", "192.20.80.149", "192.20.96.59", "192.20.82.14", "192.20.82.15", "192.20.82.17", "192.20.82.19", "192.20.82.12", "192.20.80.139", "192.20.80.137", "192.20.80.136", "192.20.80.134", "192.20.80.133", "192.20.80.131", "192.20.80.130", "192.20.81.141", "192.20.81.140", "192.20.82.26", "192.20.82.28", "192.20.82.23", "192.20.82.21", "192.20.80.128", "192.20.80.127", "192.20.80.122", "192.20.81.159", "192.20.80.121", "192.20.80.124", "192.20.81.151", "192.20.80.118", "192.20.80.119", "192.20.80.113", "192.20.80.112", "192.20.80.116", "192.20.80.115", "192.20.78.62", "192.20.81.124", "192.20.81.125", "192.20.81.122", "192.20.81.121", "192.20.82.33", "192.20.82.31", "192.20.82.32", "192.20.82.30", "192.20.81.128", "192.20.82.39", "192.20.82.37", "192.20.82.35", "192.20.81.130", "192.20.80.200", "192.20.81.136", "192.20.81.137", "192.20.81.131", "192.20.81.133", "192.20.81.134", "192.20.82.43", "192.20.82.45", "192.20.82.41", "192.20.79.152", "192.20.79.155", "192.20.79.154", "192.25.76.235", "192.25.76.234", "192.25.76.233", "192.25.76.232", "192.25.76.231", "192.25.76.228", "192.25.20.96", "192.25.20.95", "192.25.20.94", "192.25.20.93", "192.24.163.14", "192.24.163.21", "192.24.163.29", "192.24.163.6", "192.18.136.22", "192.18.136.23", "192.24.193.2", "192.24.193.19", "192.24.193.18", "192.24.193.11", "192.20.157.132", "192.20.157.133", "192.24.212.232", "192.24.212.231", "192.24.212.230"]oids = [".1.3.6.1.4.1.2021.11.9.0",".1.3.6.1.4.1.2021.11.10.0",".1.3.6.1.4.1.2021.11.11.0",".1.3.6.1.4.1.2021.10.1.3.1", ".1.3.6.1.4.1.2021.10.1.3.2",".1.3.6.1.4.1.2021.10.1.3.3",".1.3.6.1.4.1.2021.4.6.0",".1.3.6.1.4.1.2021.4.14.0", ".1.3.6.1.4.1.2021.4.15.0"]myq = Queue.Queue()rq = Queue.Queue()#把host和oid組成任務for host in hosts: for oid in oids: myq.put((host,oid))def poll_one_host(): while True: try: #死循環從隊列中獲取任務,直到隊列任務為空 host, oid = myq.get(block=False) session = netsnmp.Session(Version=2, DestHost=host, Community="cluster",Timeout=3000000,Retries=0) var_list = netsnmp.VarList() var_list.append(netsnmp.Varbind(oid)) ret = session.get(var_list) rq.put((host, oid, ret, (time.time() - start_time))) except Queue.Empty: breakthread_arr = []#開啟多線程num_thread = 50for 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
新聞熱點
疑難解答