* Fixed problems related to saving vCards in config rooms. For starters, we were...
[citadel.git] / citadel / modules / mrtg / serv_mrtg.c
blob8eb9c53fdfe39faa1569da14dc1cf05fd80c4191
1 /*
2 * $Id$
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.
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>
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
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"
50 #include "ctdl_module.h"
54 * Other functions call this one to output data in MRTG format
56 void mrtg_output(long value1, long value2) {
57 time_t uptime_t;
58 int uptime_days, uptime_hours, uptime_minutes;
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);
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");
78 * Tell us how many users are online
80 void mrtg_users(void) {
81 long connected_users = 0;
82 long active_users = 0;
84 struct CitContext *cptr;
86 begin_critical_section(S_SESSION_TABLE);
87 for (cptr = ContextList; cptr != NULL; cptr = cptr->next) {
89 if (cptr->internal_pgm == 0) {
90 ++connected_users;
92 if ( (time(NULL) - (cptr->lastidle)) < 900L) {
93 ++active_users;
98 end_critical_section(S_SESSION_TABLE);
100 mrtg_output(connected_users, active_users);
105 * Volume of messages submitted
107 void mrtg_messages(void) {
108 mrtg_output(CitControl.MMhighest, 0L);
112 struct num_accounts {
113 long total;
114 long active;
118 * Helper function for mrtg_accounts()
120 void tally_account(struct ctdluser *EachUser, void *userdata)
122 struct num_accounts *n = (struct num_accounts *) userdata;
124 ++n->total;
125 if ( (time(NULL) - EachUser->lastcall) <= 2592000 ) ++n->active;
130 * Number of accounts and active accounts
132 void mrtg_accounts(void) {
133 struct num_accounts n = {
138 ForEachUser(tally_account, (void *)&n );
139 mrtg_output(n.total, n.active);
144 * Fetch data for MRTG
146 void cmd_mrtg(char *argbuf) {
147 char which[32];
149 extract_token(which, argbuf, 0, '|', sizeof which);
151 if (!strcasecmp(which, "users")) {
152 mrtg_users();
154 else if (!strcasecmp(which, "messages")) {
155 mrtg_messages();
157 else if (!strcasecmp(which, "accounts")) {
158 mrtg_accounts();
160 else {
161 cprintf("%d Unrecognized keyword '%s'\n", ERROR + ILLEGAL_VALUE, which);
166 CTDL_MODULE_INIT(mrtg)
168 if (!threading)
170 CtdlRegisterProtoHook(cmd_mrtg, "MRTG", "Supply stats to MRTG");
173 /* return our Subversion id for the Log */
174 return "$Id$";