* Added a module "serv_mrtg" which allows activity reporting to MRTG
[citadel.git] / citadel / 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 "citadel.h"
37 #include "server.h"
38 #include "sysdep_decls.h"
39 #include "citserver.h"
40 #include "support.h"
41 #include "config.h"
42 #include "control.h"
43 #include "dynloader.h"
44 #include "room_ops.h"
45 #include "user_ops.h"
46 #include "policy.h"
47 #include "database.h"
48 #include "msgbase.h"
49 #include "tools.h"
50
51
52 /*
53  * Other functions call this one to output data in MRTG format
54  */
55 void mrtg_output(long value1, long value2) {
56         time_t uptime_t;
57         int uptime_days, uptime_hours, uptime_minutes;
58         
59         uptime_t = time(NULL) - server_startup_time;
60         uptime_days = (int) (uptime_t / 86400L);
61         uptime_hours = (int) ((uptime_t % 86400L) / 3600L);
62         uptime_minutes = (int) ((uptime_t % 3600L) / 60L);
63
64         cprintf("%d ok\n", LISTING_FOLLOWS);
65         cprintf("%ld\n", value1);
66         cprintf("%ld\n", value2);
67         cprintf("%d days, %d hours, %d minutes\n",
68                 uptime_days, uptime_hours, uptime_minutes);
69         cprintf("%s\n", config.c_humannode);
70         cprintf("000\n");
71 }
72
73
74
75
76 /*
77  * Tell us how many users are online
78  */
79 void mrtg_users(void) {
80         long connected_users = 0;
81         long active_users = 0;
82         
83         struct CitContext *cptr;
84
85         for (cptr = ContextList; cptr != NULL; cptr = cptr->next) {
86
87                 ++connected_users;
88
89                 if ( (time(NULL) - (cptr->lastidle)) < 900L) {
90                         ++active_users;
91                 }
92
93         }
94
95         mrtg_output(connected_users, active_users);
96 }
97
98
99 /*
100  * Volume of messages submitted
101  */
102 void mrtg_messages(void) {
103         mrtg_output(CitControl.MMhighest, 0L);
104 }
105
106
107 /*
108  * Fetch data for MRTG
109  */
110 void cmd_mrtg(char *argbuf) {
111         char which[SIZ];
112
113         extract(which, argbuf, 0);
114
115         if (!strcasecmp(which, "users")) {
116                 mrtg_users();
117         }
118         if (!strcasecmp(which, "messages")) {
119                 mrtg_messages();
120         }
121         else {
122                 cprintf("%d Unrecognized keyword '%s'\n",
123                         ERROR+ILLEGAL_VALUE, which);
124         }
125 }
126
127
128 char *Dynamic_Module_Init(void)
129 {
130         CtdlRegisterProtoHook(cmd_mrtg, "MRTG", "Supply stats to MRTG");
131         return "$Id$";
132 }