def collectData(self): """Collect network interface data. """ self.data.datahash = {} # hash of same objects keyed on interface name self.data.numinterfaces = 0 # get the interface packet stats rawList = utils.safe_popen('netstat -ind', 'r') for line in rawList.readlines(): f = string.split(line) if len(f) != 10: continue # should be 10 fields per line # only want lines where Address is a valid IP address int_re = "[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+" sre = re.compile(int_re) inx = sre.search( f[3] ) if inx != None: t = interface(f) # new interface instance self.data.datahash[t.name] = t self.data.numinterfaces = self.data.numinterfaces + 1 utils.safe_pclose( rawList ) # get the interface bytes stats rawList = utils.safe_popen('netstat -inb', 'r') for line in rawList.readlines(): f = string.split(line) if len(f) != 6: continue # should be 6 fields per line # only want lines where Address is a valid IP address int_re = "[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+" sre = re.compile(int_re) inx = sre.search( f[3] ) if inx != None: # only want interfaces that were found above if f[0] in self.data.datahash.keys(): self.data.datahash[f[0]].interface_bytes(f) # update interface data else: log.log( "<netstat>IntTable.collectData(): interface mismatch in netstat -inb, %s" %(f[0]), 5 ) utils.safe_pclose( rawList ) log.log( "<netstat>IntTable.collectData(): Collected data for %d interfaces" %(self.data.numinterfaces), 6 )
def collectData(self): """Collect disk usage data. """ dfre = "^([/0-9a-zA-Z]+)\s*\(([/0-9a-zA-Z])\s*\)\s*:\s*([0-9]+)\s*total allocated Kb\s*([0-9]+)\s*free allocated Kb\s*([0-9]+)\s*used allocated Kb\s*([0-9]+)\s*% allocation used" rawList = utils.safe_popen('df -ktffs', 'r') rawList.readline() # skip header self.data.datahash = {} # dict of filesystems keyed by device self.data.mounthash = {} # dict of filesystems keyed by mount point prevline = None for line in rawList.readlines(): if prevline: line = prevline + " " + line # join any previous line to current prevline = None fields = string.split(line) if len(fields) == 1: # if 1 field, assume rest on next line prevline = line continue p = df(fields) self.data.datahash[fields[0]] = p # dict of filesystem devices self.data.mounthash[fields[5]] = p # dict of mount points prevline = None utils.safe_pclose(rawList) log.log( "<df>dfList.collectData(): collected data for %d filesystems" % (len(self.data.datahash.keys())), 6)
def collectData(self): """Collect disk usage data. """ # List all local filesystems # Note: we don't bother with NFS filesystems at this point. # TODO: allow user-specified filesystem types rawList = utils.safe_popen('/bin/df -l -k', 'r') self.data.datahash = {} self.data.mounthash = {} # skip header line rawList.readline() for line in rawList.readlines(): fields = string.split(line) p = df(fields) self.data.datahash[ fields[0]] = p # dictionary of filesystem devices self.data.mounthash[fields[5]] = p # dictionary of mount points utils.safe_pclose(rawList) log.log( "<df>dfList.collectData(): collected data for %d filesystems" % (len(self.data.datahash.keys())), 6)
def collectData(self): """Collect network interface data. """ self.data.datahash = {} # hash of same objects keyed on interface name self.data.numinterfaces = 0 # get the interface stats rawList = utils.safe_popen('/usr/bin/netstat -in', 'r') # skip header line rawList.readline() for line in rawList.readlines(): f = string.split(line) if len(f) != 9: continue # should be 9 fields per line if string.find(f[2], "Link") == -1: continue # only want real interfaces t = interface(f) # new interface instance self.data.datahash[t.name] = t self.data.numinterfaces = self.data.numinterfaces + 1 utils.safe_pclose(rawList) log.log( "<netstat>IntTable.collectData(): Collected data for %d interfaces" % (self.data.numinterfaces), 6)
def collectData(self): self.data.datalist = [] # list of TCP objects self.data.datahash = {} # hash of same objects keyed on '<ip>:<port>' self.data.numconnections = 0 # get list of current TCP connections rawList = utils.safe_popen('netstat -an -t', 'r') # skip header line rawList.readline() for line in rawList.readlines(): f = string.split(line) if len(f) != 6: continue # should be 7 fields per line t = tcp(f) # new TCP instance self.data.datalist.append(t) self.data.datahash['%s:%s' % (t.local_addr_ip,t.local_addr_port)] = t self.data.numconnections = self.data.numconnections + 1 # count number of TCP connections utils.safe_pclose( rawList ) log.log( "<netstat>TCPtable.collectData(): Collected %d TCP connections" %(self.data.numconnections), 6 )
def collectData(self): """ Collect network statistics. """ self.data.datahash = {} # hash of stats # get the network stats rawList = utils.safe_popen('netstat -s', 'r') # regexp for pulling out stats udpre = "\s*(\w+)\s*=\s*([-0-9]+)(.*)" sre = re.compile(udpre) line = rawList.readline() while 1: inx = sre.search(line) if inx == None: #log.log("<netstat>stats_ctrs: getting udp stats, no re match for line '%s'" % (line), 9) line = rawList.readline() if len(line) == 0: break else: self.data.datahash[inx.group(1)] = long(inx.group(2)) line = inx.group(3) utils.safe_pclose(rawList) log.log( "<netstat>stats_ctrs.collectData(): Collected %d network counters" % (len(self.data.datahash)), 6)
def collectData(self): self.data.datalist = [] # list of UDP objects self.data.datahash = {} # hash of same objects keyed on '<ip>:<port>' self.data.numconnections = 0 # get the UDP stats rawList = utils.safe_popen('netstat -anA inet -u', 'r') # skip header lines (2) rawList.readline() rawList.readline() for line in rawList.readlines(): f = string.split(line) if len(f) < 5 or len(f) > 6: continue # should be 5 or 6 fields per line t = udp(f) # new udp instance self.data.datalist.append(t) self.data.datahash['%s:%s' % (t.local_addr_ip,t.local_addr_port)] = t self.data.numconnections = self.data.numconnections + 1 # count number of UDP connections utils.safe_pclose( rawList ) log.log( "<netstat>UDPtable.collectData(): Collected %d UDP connections" %(self.data.numconnections), 6 )
def collectData(self): self.data.datalist = [] # list of tcp objects self.data.datahash = {} # hash of same objects keyed on '<ip>:<port>' self.data.numconnections = 0 # get the tcp stats rawList = utils.safe_popen('/usr/bin/netstat -anf inet -p tcp', 'r') # skip header lines rawList.readline() rawList.readline() for line in rawList.readlines(): f = string.split(line) if len(f) != 6: continue # should be 6 fields per line t = tcp(f) # new tcp instance self.data.datalist.append(t) self.data.datahash['%s:%s' % (t.local_addr_ip, t.local_addr_port)] = t self.data.numconnections = self.data.numconnections + 1 utils.safe_pclose(rawList) log.log( "<netstat>TCPtable.collectData(): Collected %d TCP connections" % (self.data.numconnections), 6)
def collectData(self): """ Collect process list. """ self.data.datahash = {} # dict of processes keyed by pid self.data.proclist = [] # list of processes self.data.nameHash = {} # dict of processes keyed by process name # TODO: read process info from /proc instead of parsing 'ps' output... #rawList = utils.safe_popen('ps -e -o "s user ruser group rgroup uid ruid gid rgid pid ppid pgid sid pri opri pcpu pmem vsz rss osz time etime stime f c tty addr nice class wchan fname comm args"', 'r') # remove wchan field - it sometimes causes kernel warnings rawList = utils.safe_popen('ps -e -o s,user,ruser,group,rgroup,uid,ruid,gid,rgid,pid,ppid,pgid,sid,pri,opri,pcpu,pmem,vsz,rss,osz,time,etime,stime,f,c,tty,addr,nice,class,fname,comm,args', 'r') rawList.readline() for line in rawList.readlines(): p = proc(line) self.data.proclist.append(p) self.data.datahash[p.pid] = p #self.data.nameHash[string.split(p.comm, '/')[-1]] = p self.data.nameHash[string.split(p.procname, '/')[-1]] = p utils.safe_pclose( rawList ) log.log( "<proc>procList.collectData(): new process list created", 7 )
def collectData(self): """Collect network statistics. """ self.data.datahash = {} # hash of stats # get the network stats rawList = utils.safe_popen('/usr/bin/netstat -s', 'r') # regexp for pulling out stats statsre = "\s*([0-9]+)\s*(.*)" sre = re.compile(statsre) line = rawList.readline() while 1: if len(line) == 0: break inx = sre.search(line) if inx != None: self.data.datahash[inx.group(2)] = long(inx.group(1)) line = rawList.readline() utils.safe_pclose(rawList) log.log( "<netstat>stats_ctrs.collectData(): Collected %d network counters" % (len(self.data.datahash)), 6)
def collectData(self): self.data.datalist = [] # list of tcp objects self.data.datahash = {} # hash of same objects keyed on '<ip>:<port>' self.data.numconnections = 0 # get the udp stats rawList = utils.safe_popen('netstat -anf inet | grep ^udp', 'r') for line in rawList.readlines(): f = string.split(line) if len(f) != 5: continue # should be 2 fields per line t = udp(f) # new udp instance self.data.datalist.append(t) self.data.datahash['%s:%s' % (t.local_addr_ip, t.local_addr_port)] = t self.data.numconnections = self.data.numconnections + 1 utils.safe_pclose(rawList) log.log( "<netstat>UDPtable.collectData(): Collected %d UDP connections" % (self.data.numconnections), 6)
def collectData(self): """Collect process list. """ self.data.datahash = {} # dict of processes keyed by pid self.data.proclist = [] # list of processes self.data.nameHash = {} # dict of processes keyed by process name #rawList = utils.safe_popen('/usr/bin/ps -e -o "s user ruser group rgroup uid ruid gid rgid pid ppid pgid sid pri opri pcpu pmem vsz rss osz time etime stime f c tty addr nice class wchan fname comm args"', 'r') rawList = utils.safe_popen( '/bin/ps -axwww -o "state user ruser uid ruid rgid pid ppid pgid pri pcpu pmem vsz rss time f tty nice wchan ucomm command"', 'r') rawList.readline() # skip header for line in rawList.readlines(): try: p = proc(line) except: e = sys.exc_info() log.log( "<proc>procList.collectData(): exception parsing proc: %s, %s; line: %s" % (e[0], e[1], line), 5) else: self.data.proclist.append(p) self.data.datahash[p.pid] = p self.data.nameHash[p.procname] = p utils.safe_pclose(rawList) log.log("<proc>procList.collectData(): new proc list created", 7)
def collect_stats(self): """Collect all the statistics from a call to 'netstat -k <intname>'. """ if int(platform.release().split('.')[1]) <= 9: # Sol <= 9 # interfaces stats rawList = utils.safe_popen('/usr/bin/netstat -k %s' % (self.name), 'r') # skip header line - but bail if there is no output at all if not rawList.readline(): utils.safe_pclose(rawList) #raise InterfaceError, "No stats for interface '%s'" %(name) return # collect all non-logical interface names for line in rawList.readlines(): f = string.split(line) try: d = dict([(f[x], int(f[x + 1])) for x in range(0, len(f), 2)]) self.stats.update(d) except: log.log( "<netstat>Interface.collect_stats(): Could not parse line (skipped), %s: %s" % (sys.exc_info()[0], line), 5) else: # Sol 10+ # interfaces stats rawList = utils.safe_popen( '/usr/bin/kstat -p -c net -n %s' % (self.name), 'r') # collect all non-logical interface names for line in rawList.readlines(): try: k, v = string.split(line) interface, num, instance, key = string.split(k, ':') self.stats[key] = v except ValueError: pass except: log.log( "<netstat>Interface.collect_stats(): Could not parse line (skipped), %s: %s" % (sys.exc_info()[0], line), 5) utils.safe_pclose(rawList)
def collectData(self): #self.data.datalist = [] # list of disk objects self.data.datahash = { } # hash of same objects keyed on device name (eg: sd100, md10) self.data.numdisks = 0 if not os.path.isfile(KSTAT_CMD): #log.log( "<diskdevice>DiskStatistics.collectData(): No kstat command '%s'" %(KSTAT_CMD), 4 ) raise datacollect.DataFailure, "No kstat command '%s'" % ( KSTAT_CMD) # get the tcp stats cmd = "%s %s %s" % (KSTAT_CMD, KSTAT_ARG, self.kstat_class) rawList = utils.safe_popen(cmd, 'r') for line in rawList.readlines(): try: (keys, value) = string.split(line) except ValueError: # should be 2 white-space separated fields per line log.log( "<diskdevice>DiskStatistics.collectData(): cannot parse kstat line '%s'" % (lines), 5) continue try: (type, index, name, key) = string.split(keys, ':') except ValueError: # should be 4 colon separated fields for keys log.log( "<diskdevice>DiskStatistics.collectData(): cannot parse kstat keys '%s'" % (keys), 5) continue try: # fetch already existing Disk object disk = self.data.datahash[name] except KeyError: # create new Disk object if needed disk = Disk(type, index, name) self.data.datahash[name] = disk #self.data.datalist.append( disk ) self.data.numdisks = self.data.numdisks + 1 disk.setStat(key, value) utils.safe_pclose(rawList) log.log( "<diskdevice>DiskStatistics.collectData(): Collected stats for %d disks" % (self.data.numdisks), 6)
def _getProcList(self): self.hash = {} # dict of processes keyed by pid self.list = [] # list of processes self.nameHash = {} # dict of processes keyed by process name rawList = utils.safe_popen('ps -elf', 'r') rawList.readline() for line in rawList.readlines(): p = proc(line) self.list.append(p) self.hash[p.pid] = p #self.nameHash[string.split(p.comm, '/')[-1]] = p self.nameHash[p.procname] = p utils.safe_pclose(rawList) log.log("<proc>procList._procList(): new proc list created", 7)
def collectData(self): """Collect process list. """ self.data.datahash = {} # dict of processes keyed by pid self.data.proclist = [] # list of processes self.data.nameHash = {} # dict of processes keyed by process name # gather data from "ps" rawList = utils.safe_popen('ps -o ppid,nice,ruid,rgid,gid -auxww', 'r') rawList.readline() # skip header for line in rawList.readlines(): p = proc(line) self.data.proclist.append(p) self.data.datahash[p.pid] = p self.data.nameHash[p.procname] = p utils.safe_pclose(rawList) log.log("<proc>procList.collectData(): new proc list created", 7)
def collectData(self): """Collect network interface data. """ self.data.phys_interfaces = [ ] # list of physical (non-logical) interface names self.data.datahash = {} # hash of same objects keyed on interface name self.data.numinterfaces = 0 interfacelines = {} # get list of network interfaces rawList = utils.safe_popen('netstat -in', 'r') # skip header line rawList.readline() # collect all non-logical interface names for line in rawList.readlines(): f = string.split(line) if len(f) > 0 and ':' not in f[0]: # not a logical interface so remember it self.data.phys_interfaces.append(f[0]) interfacelines[ f[0]] = line # store the line to parse a little later utils.safe_pclose(rawList) # now collect stats for all interfaces found above for intname in self.data.phys_interfaces: try: newint = Interface(intname) # new Interface instance newint.parse_netstat_in( interfacelines[intname]) # parse 'netstat -in' line newint.collect_stats() # get stats from 'netstat -k' self.data.datahash[intname] = newint self.data.numinterfaces = self.data.numinterfaces + 1 except InterfaceError, msg: log.log( "<netstat>IntTable.collectData(): Could not create interface for '%s', %s" % (intname, msg), 5)
def collectData(self): """Collect process list. """ self.data.datahash = {} # dict of processes keyed by pid self.data.proclist = [] # list of processes self.data.nameHash = {} # dict of processes keyed by process name rawList = utils.safe_popen( '/usr/bin/ps -e -o "s user ruser group rgroup uid ruid gid rgid pid ppid pgid sid pri opri pcpu pmem vsz rss osz time etime stime f c tty addr nice class wchan fname comm args"', 'r') rawList.readline() # skip header for line in rawList.readlines(): p = proc(line) self.data.proclist.append(p) self.data.datahash[p.pid] = p self.data.nameHash[p.procname] = p utils.safe_pclose(rawList) log.log("<proc>procList.collectData(): new proc list created", 7)
def collectData(self): self.data.datalist = [] # list of tcp objects self.data.datahash = {} # hash of same objects keyed on '<ip>:<port>' self.data.numconnections = 0 # get the tcp stats rawList = utils.safe_popen('netstat -anf inet -P tcp', 'r') # skip to start of TCP (solves bug in Solaris 2.5.1) line = rawList.readline() while line[:3] != 'TCP' and len(line) > 0: line = rawList.readline() # skip header lines rawList.readline() rawList.readline() for line in rawList.readlines(): f = string.split(line) if len(f) != 7: continue # should be 7 fields per line t = tcp(f) # new tcp instance self.data.datalist.append(t) self.data.datahash['%s:%s' % (t.local_addr_ip, t.local_addr_port)] = t self.data.numconnections = self.data.numconnections + 1 utils.safe_pclose(rawList) log.log( "<netstat>TCPtable.collectData(): Collected %d TCP connections" % (self.data.numconnections), 6)
def collectData(self): """ Collect disk usage data. """ # Get information about all local filesystems from 'df'. rawList = utils.safe_popen('df -l', 'r') rawList.readline() # skip header self.data.datahash = {} self.data.mounthash = {} lines = rawList.read() lines = re.sub( r'\n ', '', lines) lines = string.split(lines, '\n') for line in lines: fields = string.split(line) if len(fields) == 6: p = df(fields) self.data.datahash[fields[0]] = p # dictionary of filesystem devices self.data.mounthash[fields[5]] = p # dictionary of mount points utils.safe_pclose( rawList ) log.log( "<df>dfList.collectData(): filesystem data collected", 7 )
def collectData(self): """Collect full disk usage data """ # df -g -l : get detailed information for all local filesystems rawList = utils.safe_popen('/usr/bin/df -g -l', 'r') self.data.datahash = {} self.data.mounthash = {} data = {} m1re = '(?P<mountpt>\S+)\s*\((?P<fsname>[^\)]+)\S*\):\s+(?P<blocksize>\d+) block size\s+(?P<fragsize>\d+) frag size' m2re = '\s*(?P<totalblocks>\d+) total blocks\s+(?P<freeblocks>\d+) free blocks\s+(?P<availblocks>\d+) available\s+(?P<totalfiles>\-?\d+) total files' m3re = '\s*(?P<freefiles>\d+) free files\s+(?P<filesysid>\d+) filesys id.*' m4re = '\s*(?P<fstype>\S+) fstype \s+(?P<flag>\S+) flag\s+(?P<filelen>\d+) filename length' for line in rawList.readlines(): m1 = re.match(m1re, line) if m1: data['mountpt'] = m1.group('mountpt').strip() data['fsname'] = m1.group('fsname').strip() data['blocksize'] = int( m1.group('blocksize')) # filesystem (logical) block size data['fragsize'] = int(m1.group('fragsize')) continue m2 = re.match(m2re, line) if m2: data['totalblocks'] = int(m2.group( 'totalblocks')) # lots of physical block size (512B) data['size'] = data['totalblocks'] / 2 # kBytes data['freeblocks'] = int(m2.group('freeblocks')) data['usedblocks'] = data['totalblocks'] - data['freeblocks'] data['used'] = data['usedblocks'] / 2 # kBytes data['availblocks'] = int(m2.group('availblocks')) data['avail'] = data['availblocks'] / 2 # kBytes data['totalinodes'] = int(m2.group('totalfiles')) # Some pseudo filesystems have no size (eg /proc) try: data['pctused'] = 100.0 * data['usedblocks'] / data[ 'totalblocks'] except ZeroDivisionError: data['pctused'] = 0.0 continue m3 = re.match(m3re, line) if m3: data['availinodes'] = int(m3.group('freefiles')) data['usedinodes'] = data['totalinodes'] - data['availinodes'] try: data['pctinodes'] = 100.0 * data['usedinodes'] / data[ 'totalinodes'] except ZeroDivisionError: data['pctinodes'] = 0.0 data['filesysid'] = int(m3.group('filesysid')) continue m4 = re.match(m4re, line) if m4: data['fstype'] = m4.group('fstype') data['flag'] = m4.group('flag') data['filelen'] = int(m4.group('filelen')) continue if line == '\n': # empty line mount = df(data) # so create df object self.data.mounthash[data['mountpt']] = mount self.data.datahash[data['fsname']] = mount data = {} utils.safe_pclose(rawList) log.log( "<df>dfList.collectData(): collected data for %d filesystems" % (len(self.data.datahash.keys())), 6)