def start_poller(proc_id, carbon_queue, job_queue): proc_title = setproctitle.getproctitle() setproctitle.setproctitle("%s - poller#%s" % (proc_title, proc_id)) logger.debug("start start_poller()") while True: lauch_time, job = job_queue.get() launch_timedelta = lauch_time - int(time()) if launch_timedelta > 0: logger.debug("sleep %s", launch_timedelta) sleep(launch_timedelta) else: logger.warning("lateness %s's", launch_timedelta) poll_start = int(time()) logger.warning("--polling--") config = job.config hosts = job.hosts # get indexes in first poll index_oids = config['indexes'].keys() if index_oids: index_oids_group = [(oid,) for oid in list(index_oids)] snmp_data = snmp_poller.poller(hosts, index_oids_group, COMMUNITY) index_table = defaultdict_rec() for snmp_res in snmp_data: host, base_oid, index_part, value = snmp_res index_name = config['indexes'][base_oid] index_table[host][index_name][index_part] = normalize_ifname(value) target_oid_indexes = {} target_oid_metric_pfx = {} for target_oid in config['target_oids']: if 'index_name' in target_oid: target_oid_indexes[target_oid['oid']] = target_oid['index_name'] target_oid_metric_pfx[target_oid['oid']] = target_oid['metric_prefix'] # get other in second poll oids_group = [(oid['oid'],) for oid in config['target_oids']] snmp_data = snmp_poller.poller(hosts, oids_group, COMMUNITY) request_time = int(time()) for snmp_res in snmp_data: host, base_oid, index_part, value = snmp_res if index_table[host][target_oid_indexes[base_oid]][index_part]: oid_index_name = index_table[host][target_oid_indexes[base_oid]][index_part] else: oid_index_name = '%s' % index_part metric_pfx = target_oid_metric_pfx[base_oid] short_hostname = normalize_hostname(host) if "{index}" in metric_pfx: metric = ("%s.%s" % (short_hostname, metric_pfx.format(index=oid_index_name))) else: metric = ("%s.%s.%s" % (short_hostname, metric_pfx, oid_index_name)) # print (metric, value, request_time) msg = "%s %s %s\n" % (metric, value, request_time) carbon_queue.put(msg) logger.debug("polling executed in %s's", int(time()) - poll_start)
def walk(self, oid, vlan=None): ret = {} nextoid = oid offset = 0 if sys.version_info[0] == 3: from fastsnmp import snmp_poller snmp_data =[x for x in snmp_poller.poller((self.ip,), ([oid[1:]],), self.community) ] for data in snmp_data: snmp_host, snmp_oid, snmp_idx, snmp_value, snmp_type = data ret[ ".%s.%s" %(snmp_oid, snmp_idx) ] = ResultTuple(snmp_value, snmp_type) return ret sess, netsnmp = self._snmp_session(vlan) # Abort the walk when it exits the OID tree we are interested in while nextoid.startswith(oid): if offset == 0: var_list = netsnmp.VarList(netsnmp.Varbind(nextoid)) else: var_list = netsnmp.VarList(netsnmp.Varbind(nextoid, offset)) sess.getbulk(nonrepeaters=0, maxrepetitions=self._max_size, varlist=var_list) # WORKAROUND FOR NEXUS BUG (2014-11-24) # Indy told blueCmd that Nexus silently drops the SNMP response # if the packet is fragmented. Try with large size first, but drop down # to smaller one. if sess.ErrorStr == 'Timeout': if self._max_size == 1: raise TimeoutError( 'Timeout getting %s from %s' % (nextoid, self.host)) self._max_size = int(self._max_size / 16) continue if sess.ErrorStr != '': raise SnmpError('SNMP error while walking host %s: %s' % ( self.host, sess.ErrorStr)) for result in var_list: currentoid = '%s.%s' % (result.tag, int(result.iid)) # We don't want to save extra oids that the bulk walk might have # contained. if not currentoid.startswith(oid): break ret[currentoid] = ResultTuple(result.val, result.type) # Continue bulk walk offset = int(var_list[-1].iid) if offset == 0: break nextoid = var_list[-1].tag return ret
def walk_fastsnmp(self, oids, vlan=None): ret = {} from fastsnmp import snmp_poller new_oids = {} for oid in oids: m_oid = oid.rstrip('1234567890') if new_oids.get( m_oid, None ) == None: new_oids[m_oid] = ({}) new_oids[m_oid][oid] = 0 for oid_base in new_oids: t_oids = ([]) for oid in new_oids[oid_base]: t_oids.append(oid) snmp_data = snmp_poller.poller((self.ip,), tuple([t_oids]), self.community) for data in snmp_data: snmp_host, snmp_oid, snmp_idx, snmp_value, snmp_type = data ret[ ".%s.%s" %(snmp_oid, snmp_idx) ] = ResultTuple(snmp_value, snmp_type) return ret
def get(self, oid): if sys.version_info[0] == 3: from fastsnmp import snmp_poller snmp_data =[x for x in snmp_poller.poller((self.ip,), ([oid[1:]],), self.community) ] if len(snmp_data) == 0: return { oid: ResultTuple("", "OCTETSTR") } return { oid: ResultTuple(snmp_data[0][3], snmp_data[0][4]) } sess, netsnmp = self._snmp_session(timeout=5000000, retries=2) var = netsnmp.Varbind(oid) var_list = netsnmp.VarList(var) sess.get(var_list) if sess.ErrorStr != '': if sess.ErrorStr == 'Timeout': raise TimeoutError('Timeout getting %s from %s' % (oid, self.host)) raise SnmpError('SNMP error while talking to host %s: %s' % ( self.host, sess.ErrorStr)) return {var.tag: ResultTuple(var.val, var.type)}
import sys host = sys.argv[1] port = int(sys.argv[2]) # default port is 1161 and cannot be be changed oid_batch_size = int(sys.argv[3]) import time oids = [] for i in range(1, oid_batch_size + 1): oids.append('1.3.6.1.2.1.25.6.3.1.1.{}'.format(i)) start = time.time() from fastsnmp import snmp_poller hosts = (host, ) community = "public" snmp_data = snmp_poller.poller(hosts, [list(oids)], community, msg_type='Get') snmp_data = list(snmp_data) end = time.time() for d in snmp_data: print("host=%s oid=%s value=%s" % (d[0], d[1], d[3])) print("fastsnmp duration: {:.2f} ms".format((end - start) * 1000))
def start_poller(proc_id, carbon_queue, job_queue): proc_title = setproctitle.getproctitle() setproctitle.setproctitle("%s - poller#%s" % (proc_title, proc_id)) logger.debug("start start_poller()") while True: lauch_time, job = job_queue.get() launch_timedelta = lauch_time - int(time()) if launch_timedelta > 0: logger.debug("sleep %s", launch_timedelta) sleep(launch_timedelta) else: logger.warning("lateness %s's", launch_timedelta) poll_start = int(time()) logger.warning("--polling--") config = job.config hosts = job.hosts # get indexes in first poll index_oids = config['indexes'].keys() if index_oids: index_oids_group = [(oid, ) for oid in list(index_oids)] snmp_data = snmp_poller.poller(hosts, index_oids_group, COMMUNITY) index_table = defaultdict_rec() for snmp_res in snmp_data: host, base_oid, index_part, value = snmp_res index_name = config['indexes'][base_oid] index_table[host][index_name][index_part] = normalize_ifname( value) target_oid_indexes = {} target_oid_metric_pfx = {} for target_oid in config['target_oids']: if 'index_name' in target_oid: target_oid_indexes[ target_oid['oid']] = target_oid['index_name'] target_oid_metric_pfx[ target_oid['oid']] = target_oid['metric_prefix'] # get other in second poll oids_group = [(oid['oid'], ) for oid in config['target_oids']] snmp_data = snmp_poller.poller(hosts, oids_group, COMMUNITY) request_time = int(time()) for snmp_res in snmp_data: host, base_oid, index_part, value = snmp_res if index_table[host][target_oid_indexes[base_oid]][index_part]: oid_index_name = index_table[host][ target_oid_indexes[base_oid]][index_part] else: oid_index_name = '%s' % index_part metric_pfx = target_oid_metric_pfx[base_oid] short_hostname = normalize_hostname(host) if "{index}" in metric_pfx: metric = ( "%s.%s" % (short_hostname, metric_pfx.format(index=oid_index_name))) else: metric = ("%s.%s.%s" % (short_hostname, metric_pfx, oid_index_name)) # print (metric, value, request_time) msg = "%s %s %s\n" % (metric, value, request_time) carbon_queue.put(msg) logger.debug("polling executed in %s's", int(time()) - poll_start)