if not targetdev: print '\nTarget device not specified.\n\n%s' % usage print '\n Run\n\t%s --device HELP\n to get possible devices names.\n' % sys.argv[0] sys.exit(1) # # Working: # cw = configWorker.configWorker('config.ini') alldevs = [ commonFuncs.cleanHostdirName ( h.hostname ) for h in cw.hosts ] if targetdev not in alldevs: # print them sorted: alldevs.sort() if not targetdev in ('HELP', 'ALL'): # nor HELP, neither ALL: print 'No such device: %s.' % targetdev elif targetdev == 'HELP': # HELP: print 'Possible devices are:\n%s\n' % ', '.join ( alldevs ) else: # ALL:
if not os.access(dev_ifacesfile, os.F_OK): continue ifacesConfig = ConfigParser.ConfigParser() ifacesConfig.read ( dev_ifacesfile ) # dev = device(ifacesConfig.get('global', 'name')) dev.ip = ifacesConfig.get('global', 'ip') if ifacesConfig.has_option('global', 'vendor'): dev.vendor = ifacesConfig.get('global', 'vendor') else: dev.vendor = None dev.hostname = commonFuncs.cleanHostdirName(dev.name) dev.ifacesfile = dev_ifacesfile dev.ifaces = ifacesConfig.items('ifnames') dev.ifacesDict = dict(dev.ifaces) dev.ifaliases = ifacesConfig.items('ifaliases') dev.ifaliasesDict = dict(dev.ifaliases) # dev.picturepath = picturepath dev.perfbase = '%s/%s/%s' % ( rrddir, dev.hostname, perfbase ) devices.append(dev) devices.sort ()
def configRead(self): # main config: self.config = ConfigParser.ConfigParser() # self.config.read(self.configfile) fp = open(self.configfile) self.config.readfp(fp) fp.close() # if self.config.has_option("global", "maxthreads"): self.maxthreads = int(self.config.get("global", "maxthreads")) # # default for ports: if self.config.has_option("hosts", "ports"): ports = self.config.get("hosts", "ports") if ports == "any": ports = None else: ports = None # # hostdir (where rrd bases should be): self.rrdhostdir = self.config.get("rrd", "dir") self.perfbase = self.config.get("rrd", "perfbase") self.ifacesfile = self.config.get("rrd", "ifaces") # # common snmp parameters: snmp_timeout = 1 snmp_retries = 3 snmp_bulksize = 15 if self.config.has_option("hosts", "version"): snmp_version = self.config.get("hosts", "version") else: snmp_version = "2c" # if self.config.has_section("snmp"): if self.config.has_option("snmp", "timeout"): snmp_timeout = int(self.config.get("snmp", "timeout")) if self.config.has_option("snmp", "retries"): snmp_retries = int(self.config.get("snmp", "retries")) if self.config.has_option("snmp", "bulksize"): snmp_bulksize = int(self.config.get("snmp", "bulksize")) # # config for hosts (in a separate file): self.hostsfile = self.config.get("hosts", "hostsfile") self.hostsconfig = ConfigParser.ConfigParser() self.hostsconfig.read(self.hostsfile) hostnames = self.hostsconfig.sections() # for host in hostnames: # ip = self.hostsconfig.get(host, "ip") community = self.hostsconfig.get(host, "community") # h = snmpAgent.snmpDevice(host, ip, community) # # tmout and retries: snmp_to = snmp_timeout snmp_rt = snmp_retries snmp_ve = snmp_version snmp_bs = snmp_bulksize # # timeout: if self.hostsconfig.has_option(host, "timeout"): snmp_to = int(self.hostsconfig.get(host, "timeout")) # set to host: if snmp_to: h.timeout = snmp_to # # retries: if self.hostsconfig.has_option(host, "retries"): snmp_rt = int(self.hostsconfig.get(host, "retries")) # set to host: if snmp_rt: h.retries = snmp_rt # # bulksize: if self.hostsconfig.has_option(host, "bulksize"): snmp_bs = int(self.hostsconfig.get(host, "bulksize")) # set to host: if snmp_bs: h.bulksize = snmp_bs # # version: if self.hostsconfig.has_option(host, "version"): snmp_ve = self.hostsconfig.get(host, "version") # set to host: if snmp_ve: h.version = snmp_ve # # ports -- if any: if self.hostsconfig.has_option(host, "ports"): p = self.hostsconfig.get(host, "ports") else: p = ports h.ports = self.parseNumbers(p) # # ifnameoid: if self.hostsconfig.has_option(host, "ifnameoid"): ifnameoidname = self.hostsconfig.get(host, "ifnameoid") h.ifnameoid = ifnameoids[ifnameoidname] # # ifaliasoid: if self.hostsconfig.has_option(host, "ifaliasoid"): ifaliasoidname = self.hostsconfig.get(host, "ifaliasoid") h.ifaliasoid = snmpAgent.oid("IfAlias", tuple([int(s) for s in ifaliasoidname.split(".")]), "alias") else: h.ifaliasoid = None # # rrd hostdir: h.hostdir = commonFuncs.cleanHostdirName("%s/%s" % (self.rrdhostdir, h.hostname)) # # ifaces (index -> ifname) file: h.ifacesfile = self.ifacesfile # # open ini with ifnames: ifnamesConfig = ConfigParser.ConfigParser() ifnamesConfig.read("%s/%s" % (h.hostdir, h.ifacesfile)) # # fill ifnamesDict: if ifnamesConfig.has_section("ifnames"): h.ifnamesDict = dict([(int(x), y) for (x, y) in ifnamesConfig.items("ifnames")]) else: print "Error: %s/%s has no section `ifnames'" % (h.hostdir, h.ifacesfile) # # -------------------------------------------- # # oids to get -- high res. (HC) by default: if self.hostsconfig.has_option(host, "hc"): if self.hostsconfig.get(host, "hc") in ("off", "0"): h.hc = False h.oids = oids else: h.oids = HCoids h.hc = True else: h.oids = HCoids h.hc = True h.results = [] for o in h.oids: h.results.append(dict()) h.perfbase = self.perfbase # -------------------------------------------- # create cmdgen.AsynCommandGenerator() for this host: h.asynCommandGenerator() # ## append to a list of hosts: # -------------------------------------------- self.hosts.append(h)