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-2015 by the citadel.org team
10 * This program is open source software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 3.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
27 #include <sys/types.h>
29 #if TIME_WITH_SYS_TIME
30 # include <sys/time.h>
34 # include <sys/time.h>
43 #include <libcitadel.h>
46 #include "citserver.h"
55 #include "ctdl_module.h"
59 * Other functions call this one to output data in MRTG format
61 void mrtg_output(long value1, long value2) {
63 int uptime_days, uptime_hours, uptime_minutes;
65 uptime_t = time(NULL) - server_startup_time;
66 uptime_days = (int) (uptime_t / 86400L);
67 uptime_hours = (int) ((uptime_t % 86400L) / 3600L);
68 uptime_minutes = (int) ((uptime_t % 3600L) / 60L);
70 cprintf("%d ok\n", LISTING_FOLLOWS);
71 cprintf("%ld\n", value1);
72 cprintf("%ld\n", value2);
73 cprintf("%d days, %d hours, %d minutes\n",
74 uptime_days, uptime_hours, uptime_minutes);
75 cprintf("%s\n", CtdlGetConfigStr("c_humannode"));
83 * Tell us how many users are online
85 void mrtg_users(void) {
86 long connected_users = 0;
87 long active_users = 0;
89 struct CitContext *cptr;
91 begin_critical_section(S_SESSION_TABLE);
92 for (cptr = ContextList; cptr != NULL; cptr = cptr->next) {
94 if (cptr->internal_pgm == 0) {
97 if ( (time(NULL) - (cptr->lastidle)) < 900L) {
103 end_critical_section(S_SESSION_TABLE);
105 mrtg_output(connected_users, active_users);
110 * Volume of messages submitted
112 void mrtg_messages(void) {
113 mrtg_output(CtdlGetConfigLong("MMhighest"), 0);
117 struct num_accounts {
123 * Helper function for mrtg_accounts()
125 void tally_account(struct ctdluser *EachUser, void *userdata)
127 struct num_accounts *n = (struct num_accounts *) userdata;
130 if ( (time(NULL) - EachUser->lastcall) <= 2592000 ) ++n->active;
135 * Number of accounts and active accounts
137 void mrtg_accounts(void) {
138 struct num_accounts n = {
143 ForEachUser(tally_account, (void *)&n );
144 mrtg_output(n.total, n.active);
149 * Fetch data for MRTG
151 void cmd_mrtg(char *argbuf) {
154 extract_token(which, argbuf, 0, '|', sizeof which);
156 if (!strcasecmp(which, "users")) {
159 else if (!strcasecmp(which, "messages")) {
162 else if (!strcasecmp(which, "accounts")) {
166 cprintf("%d Unrecognized keyword '%s'\n", ERROR + ILLEGAL_VALUE, which);
171 CTDL_MODULE_INIT(mrtg)
175 CtdlRegisterProtoHook(cmd_mrtg, "MRTG", "Supply stats to MRTG");
178 /* return our module name for the log */