def check_iftraf(self): """ Gets interface traffic """ def ifck(iface): return "%s-%s" % (datetime.datetime.now().day, iface) # Clean keys to prevent memory leaks day = datetime.datetime.now().day keySet = self.iftraf.keys() for key in keySet: d, iface = key.split('-') if int(d) != day: del self.iftraf[key] for iface, vals in Utils.getIFStat().items(): ifchk = ifck(iface) if ifchk not in self.iftraf: try: datee = datetime.datetime.now() l = open('/usr/local/tcs/tums/rrd/iface_%s_%s-%s-%s-total.nid' % (iface, datee.day, datee.month, datee.year)) vi, vo = l.read().strip('\n').split(':') self.iftraf[ifchk] = [float(vi), float(vo)] except Exception, e: print e, "no NID" self.iftraf[ifchk] = [0,0] tin, tout = vals if self.ifdisp.get(iface): oin, oout = self.ifdisp[iface] if (tin+tout) >= (oin+oout): nin = tin - oin nout = tout - oout else: # Counter reset nin = tin nout = tout if (nin+nout) > 0: self.iftraf[ifchk][0] += nin self.iftraf[ifchk][1] += nout datee = datetime.datetime.now() l = open('/usr/local/tcs/tums/rrd/iface_%s_%s-%s-%s-total.nid' % (iface, datee.day, datee.month, datee.year), 'wt') l.write('%s:%s' % (self.iftraf[ifchk][0], self.iftraf[ifchk][1])) l.close() self.ifdisp[iface] = (tin, tout)
def continueParse(vmstat): # Read off /proc/net/dev it = Utils.getIFStat() str = ','.join(["%s:%s:%s" % (i[0], i[1][0], i[1][1]) for i in it.items()]) self.sendPerfdata('ifaces', str) # Read off uptime and IO l = open('/proc/loadavg').read() loads = l.split() # Proccess our vmstat output vms = vmstat.split('\n')[2].split() str = "%s:%s:%s:%s:%s" % (loads[0], loads[1], loads[2], vms[8], vms[9]) self.sendPerfdata('ioload', str)
def check_iftraf(self): """ Gets interface traffic """ for iface, vals in Utils.getIFStat().items(): if iface not in self.iftraf: try: datee = datetime.datetime.now() l = open( '/usr/local/tcs/tums/rrd/iface_%s_%s-%s-%s-total.nid' % (iface, datee.day, datee.month, datee.year)) vi, vo = l.read().strip('\n').split(':') self.iftraf[iface] = [float(vi), float(vo)] except Exception, e: print e, "no NID" self.iftraf[iface] = [0, 0] tin, tout = vals if self.ifdisp.get(iface): oin, oout = self.ifdisp[iface] if (tin + tout) >= (oin + oout): nin = tin - oin nout = tout - oout else: # Counter reset nin = tin nout = tout if (nin + nout) > 0: self.iftraf[iface][0] += nin self.iftraf[iface][1] += nout datee = datetime.datetime.now() l = open( '/usr/local/tcs/tums/rrd/iface_%s_%s-%s-%s-total.nid' % (iface, datee.day, datee.month, datee.year), 'wt') l.write('%s:%s' % (self.iftraf[iface][0], self.iftraf[iface][1])) l.close() self.ifdisp[iface] = (tin, tout)
def sumRRA(rra, timeframe): fetch = "/usr/bin/rrdtool fetch /usr/local/tcs/tums/rrd/%s AVERAGE -r 300 -s -%s" l = os.popen(fetch % (rra, timeframe)) total = 0 timediv = 1 for i in l: if i.replace('\n', '').strip() and ':' in i: this = i.split(':')[-1].strip() if 'nan' in this: value = 0 else: value = float(this) if total > 0: # After the first zero value only apply the average of bps over 5 minutes # to estimate the real consumption for that 5 minutes timediv = 300 total += value * timediv return total for iface, vals in Utils.getIFStat().items(): tin, tout = vals createRRD('iface_%s_in' % iface) createRRD('iface_%s_out' % iface) updateRRA('iface_%s_in' % iface, now, int(tin)) updateRRA('iface_%s_out' % iface, now, int(tout))