8eb9c53fdfe39faa1569da14dc1cf05fd80c4191
[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         begin_critical_section(S_SESSION_TABLE);
87         for (cptr = ContextList; cptr != NULL; cptr = cptr->next) {
88
89                 if (cptr->internal_pgm == 0) {
90                         ++connected_users;
91
92                         if ( (time(NULL) - (cptr->lastidle)) < 900L) {
93                                 ++active_users;
94                         }
95                 }
96
97         }
98         end_critical_section(S_SESSION_TABLE);
99         
100         mrtg_output(connected_users, active_users);
101 }
102
103
104 /*
105  * Volume of messages submitted
106  */
107 void mrtg_messages(void) {
108         mrtg_output(CitControl.MMhighest, 0L);
109 }
110
111
112 struct num_accounts {
113         long total;
114         long active;
115 };
116
117 /*
118  * Helper function for mrtg_accounts()
119  */
120 void tally_account(struct ctdluser *EachUser, void *userdata)
121 {
122         struct num_accounts *n = (struct num_accounts *) userdata;
123
124         ++n->total;
125         if ( (time(NULL) - EachUser->lastcall) <= 2592000 ) ++n->active;
126 }
127
128
129 /*
130  * Number of accounts and active accounts
131  */
132 void mrtg_accounts(void) {
133         struct num_accounts n = {
134                 0,
135                 0
136         };
137
138         ForEachUser(tally_account, (void *)&n );
139         mrtg_output(n.total, n.active);
140 }
141
142
143 /*
144  * Fetch data for MRTG
145  */
146 void cmd_mrtg(char *argbuf) {
147         char which[32];
148
149         extract_token(which, argbuf, 0, '|', sizeof which);
150
151         if (!strcasecmp(which, "users")) {
152                 mrtg_users();
153         }
154         else if (!strcasecmp(which, "messages")) {
155                 mrtg_messages();
156         }
157         else if (!strcasecmp(which, "accounts")) {
158                 mrtg_accounts();
159         }
160         else {
161                 cprintf("%d Unrecognized keyword '%s'\n", ERROR + ILLEGAL_VALUE, which);
162         }
163 }
164
165
166 CTDL_MODULE_INIT(mrtg)
167 {
168         if (!threading)
169         {
170                 CtdlRegisterProtoHook(cmd_mrtg, "MRTG", "Supply stats to MRTG");
171         }
172         
173         /* return our Subversion id for the Log */
174         return "$Id$";
175 }