HUGE PATCH. This moves all of mime_parser.c and all
[citadel.git] / citadel / modules / mrtg / serv_mrtg.c
1 /*
2  * $Id$
3  *
4  * This module supplies statistics about the activity levels of your Citadel
5  * system.  We didn't bother writing a reporting module, because there is
6  * already an excellent tool called MRTG (Multi Router Traffic Grapher) which
7  * is available at http://www.mrtg.org that can fetch data using external
8  * scripts.  This module supplies data in the format expected by MRTG.
9  *
10  */
11
12 #include "sysdep.h"
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <stdio.h>
16 #include <fcntl.h>
17 #include <signal.h>
18 #include <pwd.h>
19 #include <errno.h>
20 #include <sys/types.h>
21
22 #if TIME_WITH_SYS_TIME
23 # include <sys/time.h>
24 # include <time.h>
25 #else
26 # if HAVE_SYS_TIME_H
27 #  include <sys/time.h>
28 # else
29 #  include <time.h>
30 # endif
31 #endif
32
33 #include <sys/wait.h>
34 #include <string.h>
35 #include <limits.h>
36 #include <libcitadel.h>
37 #include "citadel.h"
38 #include "server.h"
39 #include "citserver.h"
40 #include "support.h"
41 #include "config.h"
42 #include "control.h"
43 #include "room_ops.h"
44 #include "user_ops.h"
45 #include "policy.h"
46 #include "database.h"
47 #include "msgbase.h"
48
49
50 #include "ctdl_module.h"
51
52
53 /*
54  * Other functions call this one to output data in MRTG format
55  */
56 void mrtg_output(long value1, long value2) {
57         time_t uptime_t;
58         int uptime_days, uptime_hours, uptime_minutes;
59         
60         uptime_t = time(NULL) - server_startup_time;
61         uptime_days = (int) (uptime_t / 86400L);
62         uptime_hours = (int) ((uptime_t % 86400L) / 3600L);
63         uptime_minutes = (int) ((uptime_t % 3600L) / 60L);
64
65         cprintf("%d ok\n", LISTING_FOLLOWS);
66         cprintf("%ld\n", value1);
67         cprintf("%ld\n", value2);
68         cprintf("%d days, %d hours, %d minutes\n",
69                 uptime_days, uptime_hours, uptime_minutes);
70         cprintf("%s\n", config.c_humannode);
71         cprintf("000\n");
72 }
73
74
75
76
77 /*
78  * Tell us how many users are online
79  */
80 void mrtg_users(void) {
81         long connected_users = 0;
82         long active_users = 0;
83         
84         struct CitContext *cptr;
85
86         for (cptr = ContextList; cptr != NULL; cptr = cptr->next) {
87
88                 if (cptr->internal_pgm == 0) {
89                         ++connected_users;
90
91                         if ( (time(NULL) - (cptr->lastidle)) < 900L) {
92                                 ++active_users;
93                         }
94                 }
95
96         }
97
98         mrtg_output(connected_users, active_users);
99 }
100
101
102 /*
103  * Volume of messages submitted
104  */
105 void mrtg_messages(void) {
106         mrtg_output(CitControl.MMhighest, 0L);
107 }
108
109
110 /*
111  * Fetch data for MRTG
112  */
113 void cmd_mrtg(char *argbuf) {
114         char which[32];
115
116         extract_token(which, argbuf, 0, '|', sizeof which);
117
118         if (!strcasecmp(which, "users")) {
119                 mrtg_users();
120         }
121         else if (!strcasecmp(which, "messages")) {
122                 mrtg_messages();
123         }
124         else {
125                 cprintf("%d Unrecognized keyword '%s'\n",
126                         ERROR + ILLEGAL_VALUE, which);
127         }
128 }
129
130
131 CTDL_MODULE_INIT(mrtg)
132 {
133         CtdlRegisterProtoHook(cmd_mrtg, "MRTG", "Supply stats to MRTG");
134
135         /* return our Subversion id for the Log */
136         return "$Id$";
137 }