ec2493d767b0c60ffe6c9b6037df8d89b1499fa6
[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 "serv_extensions.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                 if (cptr->internal_pgm == 0) {
88                         ++connected_users;
89
90                         if ( (time(NULL) - (cptr->lastidle)) < 900L) {
91                                 ++active_users;
92                         }
93                 }
94
95         }
96
97         mrtg_output(connected_users, active_users);
98 }
99
100
101 /*
102  * Volume of messages submitted
103  */
104 void mrtg_messages(void) {
105         mrtg_output(CitControl.MMhighest, 0L);
106 }
107
108
109 /*
110  * Fetch data for MRTG
111  */
112 void cmd_mrtg(char *argbuf) {
113         char which[32];
114
115         extract_token(which, argbuf, 0, '|', sizeof which);
116
117         if (!strcasecmp(which, "users")) {
118                 mrtg_users();
119         }
120         else if (!strcasecmp(which, "messages")) {
121                 mrtg_messages();
122         }
123         else {
124                 cprintf("%d Unrecognized keyword '%s'\n",
125                         ERROR + ILLEGAL_VALUE, which);
126         }
127 }
128
129
130 char *serv_mrtg_init(void)
131 {
132         CtdlRegisterProtoHook(cmd_mrtg, "MRTG", "Supply stats to MRTG");
133
134         /* return our Subversion id for the Log */
135         return "$Id$";
136 }