File: serv_mrtg.c

package info (click to toggle)
citadel 8.14-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 4,096 kB
  • sloc: ansic: 61,289; sh: 4,353; yacc: 660; makefile: 448; xml: 40
file content (186 lines) | stat: -rw-r--r-- 3,835 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
 * This module supplies statistics about the activity levels of your Citadel
 * system.  We didn't bother writing a reporting module, because there is
 * already an excellent tool called MRTG (Multi Router Traffic Grapher) which
 * is available at http://www.mrtg.org that can fetch data using external
 * scripts.  This module supplies data in the format expected by MRTG.
 *
 * Copyright (c) 1987-2012 by the citadel.org team
 *
 *  This program is open source software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 3.
 *  
 *  
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  
 *  
 *  
 */

#include "sysdep.h"
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <signal.h>
#include <pwd.h>
#include <errno.h>
#include <sys/types.h>

#if TIME_WITH_SYS_TIME
# include <sys/time.h>
# include <time.h>
#else
# if HAVE_SYS_TIME_H
#  include <sys/time.h>
# else
#  include <time.h>
# endif
#endif

#include <sys/wait.h>
#include <string.h>
#include <limits.h>
#include <libcitadel.h>
#include "citadel.h"
#include "server.h"
#include "citserver.h"
#include "support.h"
#include "config.h"
#include "control.h"
#include "user_ops.h"
#include "database.h"
#include "msgbase.h"


#include "ctdl_module.h"


/*
 * Other functions call this one to output data in MRTG format
 */
void mrtg_output(long value1, long value2) {
	time_t uptime_t;
	int uptime_days, uptime_hours, uptime_minutes;
	
	uptime_t = time(NULL) - server_startup_time;
	uptime_days = (int) (uptime_t / 86400L);
	uptime_hours = (int) ((uptime_t % 86400L) / 3600L);
	uptime_minutes = (int) ((uptime_t % 3600L) / 60L);

	cprintf("%d ok\n", LISTING_FOLLOWS);
	cprintf("%ld\n", value1);
	cprintf("%ld\n", value2);
	cprintf("%d days, %d hours, %d minutes\n",
		uptime_days, uptime_hours, uptime_minutes);
	cprintf("%s\n", config.c_humannode);
	cprintf("000\n");
}




/*
 * Tell us how many users are online
 */
void mrtg_users(void) {
	long connected_users = 0;
	long active_users = 0;
	
	struct CitContext *cptr;

	begin_critical_section(S_SESSION_TABLE);
        for (cptr = ContextList; cptr != NULL; cptr = cptr->next) {

		if (cptr->internal_pgm == 0) {
			++connected_users;

			if ( (time(NULL) - (cptr->lastidle)) < 900L) {
				++active_users;
			}
		}

	}
	end_critical_section(S_SESSION_TABLE);
	
	mrtg_output(connected_users, active_users);
}


/*
 * Volume of messages submitted
 */
void mrtg_messages(void) {
	mrtg_output(CitControl.MMhighest, 0L);
}


struct num_accounts {
	long total;
	long active;
};

/*
 * Helper function for mrtg_accounts()
 */
void tally_account(struct ctdluser *EachUser, void *userdata)
{
	struct num_accounts *n = (struct num_accounts *) userdata;

	++n->total;
	if ( (time(NULL) - EachUser->lastcall) <= 2592000 ) ++n->active;
}


/*
 * Number of accounts and active accounts
 */
void mrtg_accounts(void) {
	struct num_accounts n = {
		0,
		0
	};

	ForEachUser(tally_account, (void *)&n );
	mrtg_output(n.total, n.active);
}


/*
 * Fetch data for MRTG
 */
void cmd_mrtg(char *argbuf) {
	char which[32];

	extract_token(which, argbuf, 0, '|', sizeof which);

	if (!strcasecmp(which, "users")) {
		mrtg_users();
	}
	else if (!strcasecmp(which, "messages")) {
		mrtg_messages();
	}
	else if (!strcasecmp(which, "accounts")) {
		mrtg_accounts();
	}
	else {
		cprintf("%d Unrecognized keyword '%s'\n", ERROR + ILLEGAL_VALUE, which);
	}
}


CTDL_MODULE_INIT(mrtg)
{
	if (!threading)
	{
	        CtdlRegisterProtoHook(cmd_mrtg, "MRTG", "Supply stats to MRTG");
	}
	
	/* return our module name for the log */
        return "mrtg";
}