#!/usr/bin/env python # # from pysnmp.entity.rfc3413.oneliner import cmdgen from pysnmp.proto.rfc1902 import OctetString import sys import MySQLdb import time dbuser="root" dbpass="" dbhost="localhost" dbname="stats" nodes="/usr/local/users/nodes-release.txt" db=MySQLdb.connect(host=dbhost, user=dbuser, passwd=dbpass, db=dbname, port=int(3306)) cursor = db.cursor() def snmpwalk(host, oid): try: errorIndication, errorStatus, errorIndex, varBinds = cmdgen.CommandGenerator().bulkCmd( cmdgen.CommunityData('my-agent', 'public', 1), cmdgen.UdpTransportTarget((host, 161)), 15, 0, # nonRepeaters, maxRepetitions oid ) except: return () return varBinds def get_info(host, version): versions = {} versions[0] = '.1.3.6.1.4.1.2021.80.4.1.2' versions[1] = '.1.3.6.1.4.1.2021.81.4.1.2' versions[2] = '.1.3.6.1.4.1.2021.82.4.1.2' versions[3] = '.1.3.6.1.4.1.2021.83.4.1.2' versions[4] = '.1.3.6.1.4.1.2021.84.4.1.2' versions[5] = '.1.3.6.1.4.1.2021.85.4.1.2' clients = [] lines = snmpwalk(host, versions[version]) try: return str(lines[0][0][1]).strip() except: return str() def insertintodb ( cursor, value, host, type ): value = value host = host type = type try: sql="insert into relmgmt (node, type, value) values ('%s', '%s', '%s');" % ( host, type, value) cursor.execute(sql) except: sql="update relmgmt set value = '%s' where node ='%s' and type='%s';" % ( value, host, type) cursor.execute(sql) def gethosts ( file ) : f = open( file, 'r') return f.readlines() hosts = gethosts ( nodes ) for host in hosts: host = host.strip() for info in range(0, 6): value = get_info(host, info) insertintodb ( cursor, value, host, info )