* move policy.c into modules/expire/expire_policy.c, since it just controls this.
[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 "database.h"
60 #include "msgbase.h"
61
62
63 #include "ctdl_module.h"
64
65
66 /*
67  * Other functions call this one to output data in MRTG format
68  */
69 void mrtg_output(long value1, long value2) {
70         time_t uptime_t;
71         int uptime_days, uptime_hours, uptime_minutes;
72         
73         uptime_t = time(NULL) - server_startup_time;
74         uptime_days = (int) (uptime_t / 86400L);
75         uptime_hours = (int) ((uptime_t % 86400L) / 3600L);
76         uptime_minutes = (int) ((uptime_t % 3600L) / 60L);
77
78         cprintf("%d ok\n", LISTING_FOLLOWS);
79         cprintf("%ld\n", value1);
80         cprintf("%ld\n", value2);
81         cprintf("%d days, %d hours, %d minutes\n",
82                 uptime_days, uptime_hours, uptime_minutes);
83         cprintf("%s\n", config.c_humannode);
84         cprintf("000\n");
85 }
86
87
88
89
90 /*
91  * Tell us how many users are online
92  */
93 void mrtg_users(void) {
94         long connected_users = 0;
95         long active_users = 0;
96         
97         struct CitContext *cptr;
98
99         begin_critical_section(S_SESSION_TABLE);
100         for (cptr = ContextList; cptr != NULL; cptr = cptr->next) {
101
102                 if (cptr->internal_pgm == 0) {
103                         ++connected_users;
104
105                         if ( (time(NULL) - (cptr->lastidle)) < 900L) {
106                                 ++active_users;
107                         }
108                 }
109
110         }
111         end_critical_section(S_SESSION_TABLE);
112         
113         mrtg_output(connected_users, active_users);
114 }
115
116
117 /*
118  * Volume of messages submitted
119  */
120 void mrtg_messages(void) {
121         mrtg_output(CitControl.MMhighest, 0L);
122 }
123
124
125 struct num_accounts {
126         long total;
127         long active;
128 };
129
130 /*
131  * Helper function for mrtg_accounts()
132  */
133 void tally_account(struct ctdluser *EachUser, void *userdata)
134 {
135         struct num_accounts *n = (struct num_accounts *) userdata;
136
137         ++n->total;
138         if ( (time(NULL) - EachUser->lastcall) <= 2592000 ) ++n->active;
139 }
140
141
142 /*
143  * Number of accounts and active accounts
144  */
145 void mrtg_accounts(void) {
146         struct num_accounts n = {
147                 0,
148                 0
149         };
150
151         ForEachUser(tally_account, (void *)&n );
152         mrtg_output(n.total, n.active);
153 }
154
155
156 /*
157  * Fetch data for MRTG
158  */
159 void cmd_mrtg(char *argbuf) {
160         char which[32];
161
162         extract_token(which, argbuf, 0, '|', sizeof which);
163
164         if (!strcasecmp(which, "users")) {
165                 mrtg_users();
166         }
167         else if (!strcasecmp(which, "messages")) {
168                 mrtg_messages();
169         }
170         else if (!strcasecmp(which, "accounts")) {
171                 mrtg_accounts();
172         }
173         else {
174                 cprintf("%d Unrecognized keyword '%s'\n", ERROR + ILLEGAL_VALUE, which);
175         }
176 }
177
178
179 CTDL_MODULE_INIT(mrtg)
180 {
181         if (!threading)
182         {
183                 CtdlRegisterProtoHook(cmd_mrtg, "MRTG", "Supply stats to MRTG");
184         }
185         
186         /* return our Subversion id for the Log */
187         return "$Id$";
188 }