def parseVNStat(): process = subprocess.check_output(["vnstat","--dumpdb"]) lines = process.split("\n") hourLines = lines[65:89] dayLines = lines[13:43] monthLines = lines[43:55] trk = int(getVal(lines[10])) / 1024 ttk = int(getVal(lines[11])) / 1024 tr = int(getVal(lines[6])) + trk tt = int(getVal(lines[7])) + ttk createDate = int(getVal(lines[4])) ttl = tr + tt total = {'tr': tr, 'tt': tt, 'trs': scaleBytes(tr, 'MB'), 'total': ttl, 'tts': scaleBytes(tt, 'MB'), 'totals': scaleBytes(ttl, 'MB'), 'create': time.strftime("%Y-%m-%d", time.localtime(createDate))} months = [] for month in monthLines: if (getVal(month, 2) != '0'): # skip empty entries months.append(parseEntry(month, 'm', "MB")) days = [] for day in dayLines: if (getVal(day, 2) != '0'): # skip empty entries days.append(parseEntry(day, 'd', "MB")) hours = [] for hour in hourLines: if (getVal(hour, 2) != '0'): # skip empty entries hours.append(parseEntry(hour, 'h', "kB")) return {'total': total, 'months': months, 'days': days, 'hours': hours}
def index(): stats = os.statvfs('/home') free = stats.f_bavail * stats.f_frsize total = stats.f_blocks * stats.f_frsize # note that f_bfree counts root reserved blocks as free, f_bavail does not used = (stats.f_blocks - stats.f_bavail) * stats.f_frsize pctused = round(used / total * 100, 2) df = {'pctfree': 100 - pctused, 'pctused': pctused} free = scaleBytes(free) total = scaleBytes(total) used = scaleBytes(used) df.update({'total' : total, 'used' : used, 'free' : free}) stats = VNStat.parseVNStat() jsonStats = statToJson(stats) return render_template('index.html', df=df, stats=stats, jsonStats = jsonStats)
def parseEntry(line, expected, scale): if (getVal(line, 0) != expected): raise Exception("Unexpected entry type during vnstat parsing") index = getVal(line) ts = getVal(line, 2) rx = int(getVal(line, 3)) tx = int(getVal(line, 4)) # add the kilobytes part, if it exists if (getVal(line, 5)): rxk = int(getVal(line, 5)) txk = int(getVal(line, 6)) rx = rx + (rxk / 1024) tx = tx + (txk / 1024) total = rx + tx if (expected == 'm'): avg = calcMonthAvg(total, scale, float(ts)) else: avg = calcDayAvg(total, scale) return {'index': index, 'time': ts, 'rx': rx, 'tx': tx, 'avg': avg, 'total': total, 'totals': scaleBytes(total, scale), 'rxs': scaleBytes(rx, scale), 'txs': scaleBytes(tx, scale), 'avgs': scaleBytes(avg, 'kB') + '/s'}