cleaned a few things up in messages.c ... geez this is such old code
[citadel.git] / citadel / modules / mrtg / serv_mrtg.c
1 /*
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.
7  *
8  * Copyright (c) 1987-2015 by the citadel.org team
9  *
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.
12  *
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.
17  */
18
19 #include "sysdep.h"
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <stdio.h>
23 #include <fcntl.h>
24 #include <signal.h>
25 #include <pwd.h>
26 #include <errno.h>
27 #include <sys/types.h>
28
29 #if TIME_WITH_SYS_TIME
30 # include <sys/time.h>
31 # include <time.h>
32 #else
33 # if HAVE_SYS_TIME_H
34 #  include <sys/time.h>
35 # else
36 #  include <time.h>
37 # endif
38 #endif
39
40 #include <sys/wait.h>
41 #include <string.h>
42 #include <limits.h>
43 #include <libcitadel.h>
44 #include "citadel.h"
45 #include "server.h"
46 #include "citserver.h"
47 #include "support.h"
48 #include "config.h"
49 #include "control.h"
50 #include "user_ops.h"
51 #include "database.h"
52 #include "msgbase.h"
53
54
55 #include "ctdl_module.h"
56
57
58 /*
59  * Other functions call this one to output data in MRTG format
60  */
61 void mrtg_output(long value1, long value2) {
62         time_t uptime_t;
63         int uptime_days, uptime_hours, uptime_minutes;
64         
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);
69
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"));
76         cprintf("000\n");
77 }
78
79
80
81
82 /*
83  * Tell us how many users are online
84  */
85 void mrtg_users(void) {
86         long connected_users = 0;
87         long active_users = 0;
88         
89         struct CitContext *cptr;
90
91         begin_critical_section(S_SESSION_TABLE);
92         for (cptr = ContextList; cptr != NULL; cptr = cptr->next) {
93
94                 if (cptr->internal_pgm == 0) {
95                         ++connected_users;
96
97                         if ( (time(NULL) - (cptr->lastidle)) < 900L) {
98                                 ++active_users;
99                         }
100                 }
101
102         }
103         end_critical_section(S_SESSION_TABLE);
104         
105         mrtg_output(connected_users, active_users);
106 }
107
108
109 /*
110  * Volume of messages submitted
111  */
112 void mrtg_messages(void) {
113         mrtg_output(CtdlGetConfigLong("MMhighest"), 0);
114 }
115
116
117 struct num_accounts {
118         long total;
119         long active;
120 };
121
122 /*
123  * Helper function for mrtg_accounts()
124  */
125 void tally_account(struct ctdluser *EachUser, void *userdata)
126 {
127         struct num_accounts *n = (struct num_accounts *) userdata;
128
129         ++n->total;
130         if ( (time(NULL) - EachUser->lastcall) <= 2592000 ) ++n->active;
131 }
132
133
134 /*
135  * Number of accounts and active accounts
136  */
137 void mrtg_accounts(void) {
138         struct num_accounts n = {
139                 0,
140                 0
141         };
142
143         ForEachUser(tally_account, (void *)&n );
144         mrtg_output(n.total, n.active);
145 }
146
147
148 /*
149  * Fetch data for MRTG
150  */
151 void cmd_mrtg(char *argbuf) {
152         char which[32];
153
154         extract_token(which, argbuf, 0, '|', sizeof which);
155
156         if (!strcasecmp(which, "users")) {
157                 mrtg_users();
158         }
159         else if (!strcasecmp(which, "messages")) {
160                 mrtg_messages();
161         }
162         else if (!strcasecmp(which, "accounts")) {
163                 mrtg_accounts();
164         }
165         else {
166                 cprintf("%d Unrecognized keyword '%s'\n", ERROR + ILLEGAL_VALUE, which);
167         }
168 }
169
170
171 CTDL_MODULE_INIT(mrtg)
172 {
173         if (!threading)
174         {
175                 CtdlRegisterProtoHook(cmd_mrtg, "MRTG", "Supply stats to MRTG");
176         }
177         
178         /* return our module name for the log */
179         return "mrtg";
180 }