def is_this_vendor(self, host, credential): """ Determine if the hostname is accociated witH this vendor. This example will use snmp sysDescr OID ,regex to extract the vendor's name ,and then compare with self.name variable. :param host: swtich's IP address :param credential: credential to access switch """ if not utils.valid_ip_format(host): #invalid ip address return False if "Version" not in credential or "Community" not in credential: # The format of credential is incompatible with this vendor error_msg = "[huawei]Missing 'Version' or 'Community' in %r" logging.error(error_msg, credential) return False sys_info = utils.snmp_get(host, credential, "sysDescr.0") if not sys_info: return False if re.search(r"\b" + re.escape(self.__name) + r"\b", sys_info.lower()): return True return False
def get_vendor(self, host, credential): """Check and get vendor of the switch. :param host: switch ip: :param credential: credential to access switch :return a tuple (vendor, switch_state, error) """ # Returen appliance plugin once we see 127.0.0.1 ass switch ip if host == '127.0.0.1': return ("appliance", "Found", "") # TODO(grace): Why do we need to have valid IP? # a hostname should also work. if not utils.valid_ip_format(host): logging.error("host '%s' is not valid IP address!", host) return (None, ERROR, "Invalid IP address %s!" % host) if not utils.is_valid_snmp_v2_credential(credential): logging.debug("******The credential %s of host %s cannot " "be used for either SNMP v2 or SSH*****", credential, host) return (None, ERROR, "Invalid credential") sys_info, err = self.get_sys_info(host, credential) if not sys_info: return (None, UNREACHABLE, err) # List all vendors in vendors directory -- a directory but hidden # under ../vendors all_vendors = [o for o in os.listdir(self.vendors_dir) if os.path.isdir(os.path.join(self.vendors_dir, o)) and re.match(r'^[^\.]', o)] logging.debug("[get_vendor][available vendors]: %s ", all_vendors) logging.debug("[get_vendor] System Information is [%s]", sys_info) # TODO(grace): should not conver to lower. The vendor impl can choose # to do case-insensitive match # sys_info = sys_info.lower() vendor = None for vname in all_vendors: vpath = os.path.join(self.vendors_dir, vname) instance = utils.load_module(vname, vpath) if not instance: logging.error('no instance %s load from %s', vname, vpath) continue if instance.is_this_vendor(sys_info): logging.info("[get_vendor]****Found vendor '%s'****", vname) vendor = vname break if not vendor: logging.debug("[get_vendor] No vendor found! <==================") return (None, NOTSUPPORTED, "Not supported switch vendor!") return (vendor, "Found", "")