more sprintf bashing
[citadel.git] / citadel / stats.c
1 /*
2  * $Id$
3  *
4  * Citadel/UX call log stats program
5  *
6  */
7
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <fcntl.h>
11
12 #if TIME_WITH_SYS_TIME
13 # include <sys/time.h>
14 # include <time.h>
15 #else
16 # if HAVE_SYS_TIME_H
17 #  include <sys/time.h>
18 # else
19 #  include <time.h>
20 # endif
21 #endif
22
23 #include <stdio.h>
24 #include <errno.h>
25 #include <string.h>
26 #include <limits.h>
27 #include "citadel.h"
28 #include "config.h"
29 #include "ipc.h"
30 #include "tools.h"
31
32 #define disply(x,y) printf("%20s            %4.1f    %4.1f    %4d\n",x,((float)y)/calls,((float)y)/days,y)
33
34 #define GRANULARITY 100
35
36 int batch_mode = 0;
37
38 struct caller {
39         struct caller *next;
40         char Cname[30];
41         int Ctimescalled;
42 };
43
44 void logoff(int code) {
45         exit(code);
46         }
47
48 void prompt(void)
49 {
50         char buf[16];
51         if (batch_mode == 0) {
52                 printf("Press return to continue...");
53                 fgets(buf, 16, stdin);
54         } else {
55                 printf("\n");
56         }
57 }
58
59 int halfhour(time_t time)
60 {                               /* Returns half-hour time period of time */
61         int a;
62         struct tm *tm;
63         tm = (struct tm *) localtime(&time);
64         a = (tm->tm_hour) * 3;
65         if ((tm->tm_min) > 19)
66                 ++a;
67         if ((tm->tm_min) > 39)
68                 ++a;
69         return (a);
70 }
71
72
73
74 void progress(long int curr, long int max)
75 {
76         static int dots;
77         int pos;
78
79         if (curr == 0L) {
80                 printf("--------------------------------------");
81                 printf("--------------------------------------\r");
82                 fflush(stdout);
83                 dots = 0;
84         }
85         pos = (curr * 72) / max;
86         while (dots < pos) {
87                 putc('*', stdout);
88                 fflush(stdout);
89                 ++dots;
90         }
91
92         if (dots == 72) {
93                 printf("                                      ");
94                 printf("                                      \r");
95                 fflush(stdout);
96         }
97 }
98
99
100
101
102 int main(int argc, char **argv)
103 {
104         char hostbuf[SIZ], portbuf[SIZ];
105         time_t LogTime, now;
106         unsigned int LogType;
107         char LogName[SIZ];
108         int a, b, lowest;
109         float p, q;
110         long timeon[72];
111         long timeup[72];
112         char dname[30];
113         int sess = 0;
114         long cftime, cttime, aa;
115         int calls, logins, newusers;
116         int badpws, terms, drops, sleeps;
117         time_t from, to, tottime;
118         int days, hours, minutes;
119         char aaa[100];
120         struct tm *tm;
121         struct caller *callers = NULL;
122         struct caller *callptr = NULL;
123         FILE *sortpipe;
124         char thegraph[GRANULARITY][73];
125         int pc_only = 0;
126         char buf[SIZ];
127         FILE *logfp;
128         char *fakeargs[4];
129
130         for (a = 0; a < argc; ++a) {
131                 if (!strcmp(argv[a], "-b"))
132                         batch_mode = 1;
133                 if (!strcmp(argv[a], "-p"))
134                         pc_only = 1;
135         }
136
137
138         for (a = 0; a < GRANULARITY; ++a)
139                 strcpy(thegraph[a],
140                        "........................................................................");
141
142         get_config();
143         time(&now);
144
145         if (pc_only)
146                 goto PC_ONLY_HERE;
147
148         from = 0;
149         to = 0;
150         for (a = 0; a < 72; ++a) {
151                 timeon[a] = 0L;
152                 timeup[a] = 0L;
153         }
154         cftime = 0L;
155         cttime = 0L;
156
157         calls = 0;
158         logins = 0;
159         newusers = 0;
160         badpws = 0;
161         terms = 0;
162         drops = 0;
163         sleeps = 0;
164
165
166         logfp = fopen("citadel.log", "r");
167         if (logfp == NULL) {
168                 perror("Could not open citadel.log");
169                 exit(errno);
170         } else {
171                 if (!batch_mode) {
172                         printf("Scanning call log, please wait...\n");
173                 }
174                 while (fgets(buf, sizeof buf, logfp) != NULL) {
175                         buf[strlen(buf) - 1] = 0;
176                         strcat(buf, "| ");
177
178                         LogTime = atol(strtok(buf, "|"));
179                         LogType = atol(strtok(NULL, "|"));
180                         strcpy(LogName, strtok(NULL, "|"));
181
182                         if ( (LogType != 0) && (LogTime > 100L)
183                            && (LogTime <= now) ) {
184                                 if ((LogTime < from) || (from == 0L))
185                                         from = LogTime;
186                                 if ((LogTime > to) || (to == 0L))
187                                         to = LogTime;
188                                 strcpy(aaa, "");
189                                 if (LogType & CL_CONNECT) {
190                                         ++calls;
191                                         ++sess;
192                                         if (sess == 1)
193                                                 cftime = LogTime;
194                                         strcpy(dname, LogName);
195                                 }
196                                 if (LogType & CL_LOGIN) {
197                                         ++logins;
198                                         b = 0;
199                                         for (callptr = callers; callptr != NULL; callptr = callptr->next) {
200                                                 if (!strcmp(callptr->Cname, LogName)) {
201                                                         ++b;
202                                                         ++callptr->Ctimescalled;
203                                                 }
204                                         }
205                                         if (b == 0) {
206                                                 callptr = (struct caller *) malloc(sizeof(struct caller));
207                                                 callptr->next = callers;
208                                                 callers = callptr;
209                                                 strcpy(callers->Cname, LogName);
210                                                 callers->Ctimescalled = 1;
211                                         }
212                                 }
213                                 if (LogType & CL_NEWUSER)
214                                         ++newusers;
215                                 if (LogType & CL_BADPW)
216                                         ++badpws;
217                                 if (LogType & CL_TERMINATE) {
218                                         ++terms;
219                                         --sess;
220                                         if (sess == 0) {
221                                                 cttime = LogTime;
222                                                 for (aa = cftime; aa <= cttime; aa = aa + 300L)
223                                                         timeon[halfhour(aa)] = timeon[halfhour(aa)] + 5L;
224                                                 cftime = 0L;
225                                                 cttime = 0L;
226                                         }
227                                 }
228                                 if (LogType & CL_DROPCARR) {
229                                         ++drops;
230                                         --sess;
231                                         if (sess == 0) {
232                                                 cttime = LogTime;
233                                                 for (aa = cftime; aa <= cttime; aa = aa + 300L)
234                                                         timeon[halfhour(aa)] = timeon[halfhour(aa)] + 5L;
235                                                 cftime = 0L;
236                                                 cttime = 0L;
237                                         }
238                                 }
239                                 if (LogType & CL_SLEEPING) {
240                                         ++sleeps;
241                                         --sess;
242                                         if (sess == 0) {
243                                                 cttime = LogTime;
244                                                 for (aa = cftime; aa <= cttime; aa = aa + 300L)
245                                                         timeon[halfhour(aa)] = timeon[halfhour(aa)] + 5L;
246                                                 cftime = 0L;
247                                                 cttime = 0L;
248                                         }
249                                 }
250                                 if (sess < 0)
251                                         sess = 0;
252
253                         }
254                 }
255                 fclose(logfp);
256                 tottime = to - from;
257                 days = (int) (tottime / 86400L);
258                 hours = (int) ((tottime % 86400L) / 3600L);
259                 minutes = (int) ((tottime % 3600L) / 60L);
260
261                 printf("                              Avg/Call Avg/Day  Total\n");
262                 disply("Calls:", calls);
263                 disply("Logins:", logins);
264                 disply("New users:", newusers);
265                 disply("Bad pw attempts:", badpws);
266                 disply("Proper logoffs:", terms);
267                 disply("Carrier drops:", drops);
268                 disply("Sleeping drops:", sleeps);
269
270                 printf("\n");
271                 tm = (struct tm *) localtime(&from);
272                 printf("From:              %s", (char *) asctime(localtime(&from)));
273                 printf("To:                %s", (char *) asctime(localtime(&to)));
274                 printf("Total report time: ");
275                 printf("%d days, %d hours, %d minutes\n",
276                        days, hours, minutes);
277
278                 for (aa = from; aa <= to; aa = aa + 1200L)
279                         timeup[halfhour(aa)] = timeup[halfhour(aa)] + 20L;
280                 prompt();
281
282                 lowest = GRANULARITY - 1;
283                 for (b = 0; b < 72; ++b) {
284                         for (a = 0; a <= GRANULARITY; ++a) {
285                                 p = ((float) timeon[b]) / ((float) timeup[b]) * GRANULARITY;
286                                 q = (float) a;
287                                 if (p >= q) {
288                                         thegraph[(GRANULARITY - 1) - a][b] = '#';
289                                         if (lowest > (GRANULARITY - 1) - a)
290                                                 lowest = (GRANULARITY - 1) - a;
291                                 }
292                         }
293                 }
294
295                 printf("\n\n\n\n\n\n");
296
297                 b = ((GRANULARITY - lowest) / 18);
298                 if (b < 1)
299                         b = 1;
300                 for (a = lowest; a < GRANULARITY; a = a + b)
301                         printf("%2d%% |%s\n",
302                                100 - (a + 1),
303                                thegraph[a]);
304                 printf("    +------------------------------------------------------------------------\n");
305                 printf("     0  1  2  3  4  5  6  7  8  9  10 11 12 13 14 15 16 17 18 19 20 21 22 23\n");
306                 fflush(stdout);
307                 prompt();
308
309                 printf("\n\n\n\n");
310
311
312                 printf("Top 20 Callers (sorted by total number of logins)\n");
313                 printf("Calls Avg/Day Username\n");
314                 printf("----- ------- ------------------------------\n");
315                 fflush(stdout);
316                 sortpipe = (FILE *) popen("sort |tail -20 |sort -nr", "w");
317                 for (callptr = callers; callptr != NULL; callptr = callptr->next) {
318                         fprintf(sortpipe, "%5d %7.2f %-30s\n",
319                                 callptr->Ctimescalled,
320                                 (((float) callptr->Ctimescalled) / ((float) days)),
321                                 callptr->Cname);
322                 }
323                 pclose(sortpipe);
324                 while (callers != NULL) {
325                         callptr = callers->next;
326                         free(callers);
327                         callers = callptr;
328                 }
329                 prompt();
330
331               PC_ONLY_HERE:;    /* yes this semicolon is necessary; the DEC C compiler
332                                    complains that a label must be followed by an actual
333                                    statement. */
334
335
336                 printf("Top 20 Contributing Users (post to call ratio)\n");
337                 printf("P/C Ratio Username\n");
338                 printf("--------- ------------------------------\n");
339                 fflush(stdout);
340                 sortpipe = (FILE *) popen("sort |tail -20 |sort -nr", "w");
341
342
343
344                 fakeargs[0] = "whobbs";
345                 fakeargs[1] = "localhost";
346                 fakeargs[2] = malloc(64);
347                 snprintf(fakeargs[2], 64, "%d", config.c_port_number);
348                 fakeargs[3] = NULL;
349                 attach_to_server(3, fakeargs, hostbuf, portbuf);
350                 free(fakeargs[2]);
351                 serv_gets(buf);
352                 if ((buf[0]!='2')&&(strncmp(buf,"551",3))) {
353                         fprintf(stderr,"%s: %s\n",argv[0],&buf[4]);
354                 }
355                 else {
356                         serv_puts("LIST");
357                         serv_gets(buf);
358                         if (buf[0]=='1') while (serv_gets(buf), 
359                                                 strcmp(buf, "000")) {
360                                 extract(LogName, buf, 0);
361                                 fprintf(sortpipe, "%9.2f %-30s\n",
362                                         ((float) extract_int(buf,5) / (float) extract_int(buf,4)),
363                                         LogName);
364                         }
365                 }
366                 pclose(sortpipe);
367         }
368         return 0;
369 }