/* counter.c */ #include #include #include #include int main() { unsigned dummy, ibytes, obytes; FILE *f; char line[1024]; previbytes = prevobytes = 0; while (1) { time_t t = time(0); struct tm *lt = localtime(&t); if (lt->tm_sec == 0 && lt->tm_min % 10 == 0) { /* every 10 minutes */ system("/sbin/ipchains -L -v -x -Z | /bin/grep eth1 >/tmp/counter.out"); f = fopen("/tmp/counter.out", "r"); if (f == NULL) break; if (fgets(line, sizeof line, f) == NULL) break; sscanf(line, "%u%u", &dummy, &ibytes); if (fgets(line, sizeof line, f) == NULL) break; sscanf(line, "%u%u", &dummy, &obytes); fclose(f); f = fopen("/usr/local/apache/htdocs/traffic.dat", "a"); if (f == NULL) break; fprintf(f, "%04d-%02d-%02d %02d:%02d %10u %10u\n", lt->tm_year + 1900, lt->tm_mon + 1, lt->tm_mday, lt->tm_hour, lt->tm_min, ibytes, obytes); fclose(f); } sleep(1); } return 0; }