def in_rates(self): this_collect_time = time.time() in_octers = snmp_walk(self.target, PORT_OCTERS_IN) if not in_octers: return None for in_octer in in_octers: oid = str(in_octer[0][0]).split('.')[-1] octer = int(in_octer[0][1]) self.port_in_octers[oid]["pre"] = self.port_in_octers[oid]["now"] self.port_in_octers[oid]["now"] = octer period = this_collect_time - self.in_rates_collect_time if self.port_in_octers[oid]["now"] < self.port_in_octers[oid][ "pre"]: in_rate = \ round( (self.port_in_octers[oid]["now"] * 8) / (1000 * period), 2) else: in_rate = \ round( ((self.port_in_octers[oid]["now"] - self.port_in_octers[oid]["pre"])*8)/(1000*period), 2 ) self.ports_in_rate[self.oid_names[oid]] = in_rate # 第一次采集的端口速率有误差 不上报 返回None if not self.in_rates_collect_time: self.in_rates_collect_time = this_collect_time self.ports_in_rate = {} return None return self.ports_in_rate
def status(self): statuses = snmp_walk(self.target, PORT_STATUSES) for status in statuses: oid = str(status[0][0]).split('.')[-1] state = int(status[0][1]) self.port_statuses[self.oid_names[oid]] = state return self.port_statuses
def get_min_mac_md5(self, target): ip = target.ip mac_lst = [] mac_lst_number = [] # 尝试walk '1.3.6.1.2.1.17.1.1'可直接获得最小mac地址 min_mac_lst = snmp_walk(target, oid='1.3.6.1.2.1.17.1.1') if min_mac_lst: min_mac = min_mac_lst[0][0][1].__repr__().split("'")[1] return self.get_md5(ip, min_mac) # 如果直接获得最小mac地址失败,尝试walk'1.3.6.1.2.1.2.2.1.6' macs = snmp_walk(target, oid='1.3.6.1.2.1.2.2.1.6') if not macs: return '' for mac in macs: mac_addr = mac[0][-1] mac_addr = mac_addr.__repr__().split("'") mac_addr = mac_addr[1] if mac_addr and mac_addr not in ["000000000000", "FFFFFFFFFFFF"]: mac_lst.append(mac_addr) mac_lst_number.append(int('0x' + mac_addr, 16)) min_mac = mac_lst[mac_lst_number.index(min(mac_lst_number))] return self.get_md5(ip, min_mac)
def _effective_mem_oids(self): count = 5 while count >= 0: for value in self.producer.mem.values(): temp = self._oid_expression_parser(value) oid = temp[0] mem_usage_lst = snmp_walk(self.target, oid) if mem_usage_lst: for i in mem_usage_lst: mem_oid = str(i[0][0]) mem_usage = i[0][-1] if mem_usage: suffix = self._sub_strings(mem_oid, oid) return value, temp, [i + suffix for i in temp] time.sleep(1) count -= 1 return None
def __collect_ports_bandwidths(self): port_bandwidths = snmp_walk(self.target, PORT_BANDWIDTHS) for port_bandwidth in port_bandwidths: oid = str(port_bandwidth[0][0]).split('.')[-1] bandwidth = int(port_bandwidth[0][1]) self.oid_bandwidths[oid] = bandwidth
def __collect_ports_names(self): port_names = snmp_walk(self.target, PORT_NAMES) for port_name in port_names: oid = str(port_name[0][0]).split('.')[-1] name = str(port_name[0][1]) self.oid_names[oid] = name