Citadel API clean up.
[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  * Copyright (c) 1987-2009 by the citadel.org team
11  *
12  *  This program is free software; you can redistribute it and/or modify
13  *  it under the terms of the GNU General Public License as published by
14  *  the Free Software Foundation; either version 3 of the License, or
15  *  (at your option) any later version.
16  *
17  *  This program is distributed in the hope that it will be useful,
18  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  *  GNU General Public License for more details.
21  *
22  *  You should have received a copy of the GNU General Public License
23  *  along with this program; if not, write to the Free Software
24  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25  */
26
27 #include "sysdep.h"
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <stdio.h>
31 #include <fcntl.h>
32 #include <signal.h>
33 #include <pwd.h>
34 #include <errno.h>
35 #include <sys/types.h>
36
37 #if TIME_WITH_SYS_TIME
38 # include <sys/time.h>
39 # include <time.h>
40 #else
41 # if HAVE_SYS_TIME_H
42 #  include <sys/time.h>
43 # else
44 #  include <time.h>
45 # endif
46 #endif
47
48 #include <sys/wait.h>
49 #include <string.h>
50 #include <limits.h>
51 #include <libcitadel.h>
52 #include "citadel.h"
53 #include "server.h"
54 #include "citserver.h"
55 #include "support.h"
56 #include "config.h"
57 #include "control.h"
58 #include "user_ops.h"
59 #include "policy.h"
60 #include "database.h"
61 #include "msgbase.h"
62
63
64 #include "ctdl_module.h"
65
66
67 /*
68  * Other functions call this one to output data in MRTG format
69  */
70 void mrtg_output(long value1, long value2) {
71         time_t uptime_t;
72         int uptime_days, uptime_hours, uptime_minutes;
73         
74         uptime_t = time(NULL) - server_startup_time;
75         uptime_days = (int) (uptime_t / 86400L);
76         uptime_hours = (int) ((uptime_t % 86400L) / 3600L);
77         uptime_minutes = (int) ((uptime_t % 3600L) / 60L);
78
79         cprintf("%d ok\n", LISTING_FOLLOWS);
80         cprintf("%ld\n", value1);
81         cprintf("%ld\n", value2);
82         cprintf("%d days, %d hours, %d minutes\n",
83                 uptime_days, uptime_hours, uptime_minutes);
84         cprintf("%s\n", config.c_humannode);
85         cprintf("000\n");
86 }
87
88
89
90
91 /*
92  * Tell us how many users are online
93  */
94 void mrtg_users(void) {
95         long connected_users = 0;
96         long active_users = 0;
97         
98         struct CitContext *cptr;
99
100         begin_critical_section(S_SESSION_TABLE);
101         for (cptr = ContextList; cptr != NULL; cptr = cptr->next) {
102
103                 if (cptr->internal_pgm == 0) {
104                         ++connected_users;
105
106                         if ( (time(NULL) - (cptr->lastidle)) < 900L) {
107                                 ++active_users;
108                         }
109                 }
110
111         }
112         end_critical_section(S_SESSION_TABLE);
113         
114         mrtg_output(connected_users, active_users);
115 }
116
117
118 /*
119  * Volume of messages submitted
120  */
121 void mrtg_messages(void) {
122         mrtg_output(CitControl.MMhighest, 0L);
123 }
124
125
126 struct num_accounts {
127         long total;
128         long active;
129 };
130
131 /*
132  * Helper function for mrtg_accounts()
133  */
134 void tally_account(struct ctdluser *EachUser, void *userdata)
135 {
136         struct num_accounts *n = (struct num_accounts *) userdata;
137
138         ++n->total;
139         if ( (time(NULL) - EachUser->lastcall) <= 2592000 ) ++n->active;
140 }
141
142
143 /*
144  * Number of accounts and active accounts
145  */
146 void mrtg_accounts(void) {
147         struct num_accounts n = {
148                 0,
149                 0
150         };
151
152         ForEachUser(tally_account, (void *)&n );
153         mrtg_output(n.total, n.active);
154 }
155
156
157 /*
158  * Fetch data for MRTG
159  */
160 void cmd_mrtg(char *argbuf) {
161         char which[32];
162
163         extract_token(which, argbuf, 0, '|', sizeof which);
164
165         if (!strcasecmp(which, "users")) {
166                 mrtg_users();
167         }
168         else if (!strcasecmp(which, "messages")) {
169                 mrtg_messages();
170         }
171         else if (!strcasecmp(which, "accounts")) {
172                 mrtg_accounts();
173         }
174         else {
175                 cprintf("%d Unrecognized keyword '%s'\n", ERROR + ILLEGAL_VALUE, which);
176         }
177 }
178
179
180 CTDL_MODULE_INIT(mrtg)
181 {
182         if (!threading)
183         {
184                 CtdlRegisterProtoHook(cmd_mrtg, "MRTG", "Supply stats to MRTG");
185         }
186         
187         /* return our Subversion id for the Log */
188         return "$Id$";
189 }