2 * This module supplies statistics about the activity levels of your Citadel
3 * system. We didn't bother writing a reporting module, because there is
4 * already an excellent tool called MRTG (Multi Router Traffic Grapher) which
5 * is available at http://www.mrtg.org that can fetch data using external
6 * scripts. This module supplies data in the format expected by MRTG.
8 * Copyright (c) 1987-2009 by the citadel.org team
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 3 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33 #include <sys/types.h>
35 #if TIME_WITH_SYS_TIME
36 # include <sys/time.h>
40 # include <sys/time.h>
49 #include <libcitadel.h>
52 #include "citserver.h"
61 #include "ctdl_module.h"
65 * Other functions call this one to output data in MRTG format
67 void mrtg_output(long value1, long value2) {
69 int uptime_days, uptime_hours, uptime_minutes;
71 uptime_t = time(NULL) - server_startup_time;
72 uptime_days = (int) (uptime_t / 86400L);
73 uptime_hours = (int) ((uptime_t % 86400L) / 3600L);
74 uptime_minutes = (int) ((uptime_t % 3600L) / 60L);
76 cprintf("%d ok\n", LISTING_FOLLOWS);
77 cprintf("%ld\n", value1);
78 cprintf("%ld\n", value2);
79 cprintf("%d days, %d hours, %d minutes\n",
80 uptime_days, uptime_hours, uptime_minutes);
81 cprintf("%s\n", config.c_humannode);
89 * Tell us how many users are online
91 void mrtg_users(void) {
92 long connected_users = 0;
93 long active_users = 0;
95 struct CitContext *cptr;
97 begin_critical_section(S_SESSION_TABLE);
98 for (cptr = ContextList; cptr != NULL; cptr = cptr->next) {
100 if (cptr->internal_pgm == 0) {
103 if ( (time(NULL) - (cptr->lastidle)) < 900L) {
109 end_critical_section(S_SESSION_TABLE);
111 mrtg_output(connected_users, active_users);
116 * Volume of messages submitted
118 void mrtg_messages(void) {
119 mrtg_output(CitControl.MMhighest, 0L);
123 struct num_accounts {
129 * Helper function for mrtg_accounts()
131 void tally_account(struct ctdluser *EachUser, void *userdata)
133 struct num_accounts *n = (struct num_accounts *) userdata;
136 if ( (time(NULL) - EachUser->lastcall) <= 2592000 ) ++n->active;
141 * Number of accounts and active accounts
143 void mrtg_accounts(void) {
144 struct num_accounts n = {
149 ForEachUser(tally_account, (void *)&n );
150 mrtg_output(n.total, n.active);
155 * Fetch data for MRTG
157 void cmd_mrtg(char *argbuf) {
160 extract_token(which, argbuf, 0, '|', sizeof which);
162 if (!strcasecmp(which, "users")) {
165 else if (!strcasecmp(which, "messages")) {
168 else if (!strcasecmp(which, "accounts")) {
172 cprintf("%d Unrecognized keyword '%s'\n", ERROR + ILLEGAL_VALUE, which);
177 CTDL_MODULE_INIT(mrtg)
181 CtdlRegisterProtoHook(cmd_mrtg, "MRTG", "Supply stats to MRTG");
184 /* return our Subversion id for the Log */