/* * ILD.C: IsdnLog Daemon for Traverse Technologies Netjet ISDN card * * (C) 1999 Marc Durdin * * Version 0.1.2: No changes to ILD; see IL.C * * Version 0.1.1: No changes to ILD; see IL.C * * Version 0.1: Initial release * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include #include #include #include #include #include #define ISDNCTRL0 "/dev/isdnctrl0" #define OUTLOGPATH "/var/log/isdn/ild/" #define UPTIME "/proc/uptime" #define DAYLENGTH 87600 #define JIFFIES_DAYLENGTH DAYLENGTH * HZ int startoflog, startofday, nodaemon, breakloop; char isdnctrl0[255], outlogpath[255]; int jiffies(void) { char str[128]; FILE *fp; if((fp = fopen(UPTIME, "r")) == NULL) exit(1); fgets(str, 128, fp); fclose(fp); return atoi(str) * 100; } char *options = "i:o:"; int parse_params(int argc, char *argv[]) { char *s; int c; strcpy(isdnctrl0, ISDNCTRL0); strcpy(outlogpath, OUTLOGPATH); nodaemon = 0; while((c = getopt(argc, argv, options)) != EOF) switch(c) { case 'i': strcpy(isdnctrl0, optarg); break; case 'o': strcpy(outlogpath, optarg); break; case 'n': nodaemon = 1; break; default: return 0; } return 1; } void signal_catch(int signal) { if(signal == SIGHUP || signal == SIGINT || signal == SIGQUIT) { fprintf(stderr, "ILD: SIGHUP received, clean exit!\n"); breakloop = 1; } else exit(1); } void main(int argc, char *argv[]) { glob_t globbuf; int i, amount_send, amount_recv, logtime, curmin; char str[256], *s, *s2, *s3; time_t t; struct tm *p, tm; FILE *fp, *fplog; if(!parse_params(argc, argv)) { printf("ILD: IsdnLog Daemon for Traverse Technologies Netjet ISDN card\n\n"); printf("Usage: ILD [options]\n"); printf(" -ipath Path of the isdnctrl0 logfile, default /dev/isdnctrl0\n"); printf(" -opath Directory for output files, default /var/log/isdn/ild/\n\n"); printf("ILD will create a new logfile each day in the output directory, for\n"); printf("use with the companion program IL.\n"); exit(0); } if(!nodaemon && fork()) exit(0); breakloop = 0; signal(SIGHUP, signal_catch); signal(SIGINT, signal_catch); signal(SIGQUIT, signal_catch); /* Offset jiffies to real date */ time(&t); startoflog = t - jiffies()/100; /* Get the current local time */ if((fp = fopen(isdnctrl0, "r")) == NULL) { fprintf(stderr, "Failed to open %s [isdnctrl file].\n", isdnctrl0); exit(1); } amount_send = 0; amount_recv = 0; curmin = 0; while(!breakloop && !feof(fp)) { time(&t); p = localtime(&t); /* Open the next log file */ sprintf(str, "%s%4.4d%2.2d%2.2d.log", outlogpath, p->tm_year, p->tm_mon, p->tm_mday); if((fplog = fopen(str, "a")) == NULL) { fprintf(stderr, "Failed to create output file.\n"); exit(1); } /* Find the 0:00 time in seconds for the next day */ tm = *p; tm.tm_sec = 0; tm.tm_min = 0; tm.tm_hour = 0; startofday = mktime(&tm) + DAYLENGTH; // midnight tomorrow while(!breakloop && fgets(str, 256, fp)) { s = strtok(str, " "); s = strtok(NULL, " "); if(!s || strcasecmp(s, "Card")) continue; s = strtok(NULL, " "); if(!s || strcasecmp(s, "1")) continue; s = strtok(NULL, " "); if(!s || strcasecmp(s, "tiger")) continue; s = strtok(NULL, " "); if(!s || strcasecmp(s, "stats")) continue; s = strtok(NULL, " "); if(!s) continue; s2 = strtok(NULL, " "); if(!s2) continue; s3 = strtok(NULL, " "); if(!s3) continue; logtime = atoi(s)/100 + startoflog; if(curmin < logtime/60) { if(amount_send > 0 || amount_recv > 0) fprintf(fplog, "%d %d %d\n", curmin, amount_recv, amount_send); amount_send = 0; amount_recv = 0; curmin = logtime/60; } if(strcasecmp(s2, "snd") == 0) amount_send += atoi(s3); else amount_recv += atoi(s3); if(logtime < startofday) break; } fclose(fplog); } fclose(fp); }