File: mrtg-ip-acct.c

package info (click to toggle)
mrtgutils 0.5
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, sarge
  • size: 80 kB
  • ctags: 34
  • sloc: ansic: 327; makefile: 73
file content (39 lines) | stat: -rw-r--r-- 765 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include "uptime.h"
#include "util.h"
#include <stdio.h>
#include <string.h>
#include <ctype.h>

void print_ipacct(char *dev)
{
  char buf[1024];
  char *p;
  FILE *f;
  unsigned long long in = 0, out = 0;

  f = fopen("/proc/net/dev", "r");
  if (!f) return;
  while (!feof(f)) {
    fgets(buf, sizeof(buf), f);
    p = buf;
    while (isspace(*p)) p++;
    if (strncmp(dev, p, strlen(dev)) == 0) {
      while (*p != ':') p++; p++;
      sscanf(p, "%Lu %*d %*d %*d %*d %*d %*d %*d %Lu %*d %*d %*d %*d %*d %*d %*d", &in, &out);
    }
  }
  fclose(f);
  
  printf("%Lu\n%Lu\n", in, out);
}
  
int main(int argc, char **argv)
{
  char *device = "eth0";
  if (argc > 1) device = argv[1];
  
  print_ipacct(device);
  print_uptime();
  print_hostname();
  return 0;
}