summaryrefslogtreecommitdiff
path: root/modules/core/os_stats.cpp
blob: d7680018bb1a034b21fb0f3552e6d12104c89456 (plain)
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/* OperServ core functions
 *
 * (C) 2003-2011 Anope Team
 * Contact us at team@anope.org
 *
 * Please read COPYING and README for further details.
 *
 * Based on the original code of Epona by Lara.
 * Based on the original code of Services by Andy Church.
 */

/*************************************************************************/

#include "module.h"

void get_operserv_stats(long *nrec, long *memuse);

/**
 * Count servers connected to server s
 * @param s The server to start counting from
 * @return Amount of servers connected to server s
 **/
static int stats_count_servers(Server *s)
{
	if (!s)
		return 0;

	int count = 1;

	if (!s->GetLinks().empty())
		for (unsigned i = 0, j = s->GetLinks().size(); i < j; ++i)
			count += stats_count_servers(s->GetLinks()[i]);

	return count;
}

class CommandOSStats : public Command
{
 private:
	CommandReturn DoStatsAkill(CommandSource &source)
	{
		int timeout;
		/* AKILLs */
		source.Reply(_("Current number of AKILLs: \002%d\002"), SGLine->GetCount());
		timeout = Config->AutokillExpiry + 59;
		if (timeout >= 172800)
			source.Reply(_("Default AKILL expiry time: \002%d days\002"), timeout / 86400);
		else if (timeout >= 86400)
			source.Reply(_("Default AKILL expiry time: \0021 day\002"));
		else if (timeout >= 7200)
			source.Reply(_("Default AKILL expiry time: \002%d hours\002"), timeout / 3600);
		else if (timeout >= 3600)
			source.Reply(_("Default AKILL expiry time: \0021 hour\002"));
		else if (timeout >= 120)
			source.Reply(_("Default AKILL expiry time: \002%d minutes\002"), timeout / 60);
		else if (timeout >= 60)
			source.Reply(_("Default AKILL expiry time: \0021 minute\002"));
		else
			source.Reply(_("Default AKILL expiry time: \002No expiration\002"));
		if (ircd->snline)
		{
			/* SNLINEs */
			source.Reply(_("Current number of SNLINEs: \002%d\002"), SNLine->GetCount());
			timeout = Config->SNLineExpiry + 59;
			if (timeout >= 172800)
				source.Reply(_("Default SNLINE expiry time: \002%d days\002"), timeout / 86400);
			else if (timeout >= 86400)
				source.Reply(_("Default SNLINE expiry time: \0021 day\002"));
			else if (timeout >= 7200)
				source.Reply(_("Default SNLINE expiry time: \002%d hours\002"), timeout / 3600);
			else if (timeout >= 3600)
				source.Reply(_("Default SNLINE expiry time: \0021 hour\002"));
			else if (timeout >= 120)
				source.Reply(_("Default SNLINE expiry time: \002%d minutes\002"), timeout / 60);
			else if (timeout >= 60)
				source.Reply(_("Default SNLINE expiry time: \0021 minute\002"));
			else
				source.Reply(_("Default SNLINE expiry time: \002No expiration\002"));
		}
		if (ircd->sqline)
		{
			/* SQLINEs */
			source.Reply(_("Current number of SQLINEs: \002%d\002"), SQLine->GetCount());
			timeout = Config->SQLineExpiry + 59;
			if (timeout >= 172800)
				source.Reply(_("Default SQLINE expiry time: \002%d days\002"), timeout / 86400);
			else if (timeout >= 86400)
				source.Reply(_("Default SQLINE expiry time: \0021 day\002"));
			else if (timeout >= 7200)
				source.Reply(_("Default SQLINE expiry time: \002%d hours\002"), timeout / 3600);
			else if (timeout >= 3600)
				source.Reply(_("Default SQLINE expiry time: \0021 hour\002"));
			else if (timeout >= 120)
				source.Reply(_("Default SQLINE expiry time: \002%d minutes\002"), timeout / 60);
			else if (timeout >= 60)
				source.Reply(_("Default SQLINE expiry time: \0021 minute\002"));
			else
				source.Reply(_("Default SQLINE expiry time: \002No expiration\002"));
		}
		if (ircd->szline)
		{
			/* SZLINEs */
			source.Reply(_("Current number of SZLINEs: \002%d\002"), SZLine->GetCount());
			timeout = Config->SZLineExpiry + 59;
			if (timeout >= 172800)
				source.Reply(_("Default SZLINE expiry time: \002%d days\002"), timeout / 86400);
			else if (timeout >= 86400)
				source.Reply(_("Default SZLINE expiry time: \0021 day\002"));
			else if (timeout >= 7200)
				source.Reply(_("Default SZLINE expiry time: \002%d hours\002"), timeout / 3600);
			else if (timeout >= 3600)
				source.Reply(_("Default SZLINE expiry time: \0021 hour\002"));
			else if (timeout >= 120)
				source.Reply(_("Default SZLINE expiry time: \002%d minutes\002"), timeout / 60);
			else if (timeout >= 60)
				source.Reply(_("Default SZLINE expiry time: \0021 minute\002"));
			else
				source.Reply(_("Default SZLINE expiry time: \002No expiration\002"));
		}
		return MOD_CONT;
	}

	CommandReturn DoStatsReset(CommandSource &source)
	{
		maxusercnt = usercnt;
		source.Reply(_("Statistics reset."));
		return MOD_CONT;
	}

	CommandReturn DoStatsUptime(CommandSource &source)
	{
		time_t uptime = Anope::CurTime - start_time;
		source.Reply(_("Current users: \002%d\002 (\002%d\002 ops)"), usercnt, opcnt);
		source.Reply(_("Maximum users: \002%d\002 (%s)"), maxusercnt, do_strftime(maxusertime).c_str());
		source.Reply(_("Services up %s"), duration(uptime).c_str());

		return MOD_CONT;
	}

	CommandReturn DoStatsUplink(CommandSource &source)
	{
		Anope::string buf;

		for (unsigned j = 0; !Capab_Info[j].Token.empty(); ++j)
			if (Capab.HasFlag(Capab_Info[j].Flag))
				buf += " " + Capab_Info[j].Token;

		if (!buf.empty())
			buf.erase(buf.begin());

		source.Reply(_("Uplink server: %s"), Me->GetLinks().front()->GetName().c_str());
		source.Reply(_("Uplink capab: %s"), buf.c_str());
		source.Reply(_("Servers found: %d"), stats_count_servers(Me->GetLinks().front()));
		return MOD_CONT;
	}

	CommandReturn DoStatsMemory(CommandSource &source)
	{
		long count, mem;

		source.Reply(_("Bytes read    : %5d kB"), TotalRead / 1024);
		source.Reply(_("Bytes written : %5d kB"), TotalWritten / 1024);

		get_user_stats(count, mem);
		source.Reply(_("User          : \002%6d\002 records, \002%5d\002 kB"), count, (mem + 512) / 1024);
		get_channel_stats(&count, &mem);
		source.Reply(_("Channel       : \002%6d\002 records, \002%5d\002 kB"), count, (mem + 512) / 1024);
		get_core_stats(count, mem);
		source.Reply(_("NS Groups     : \002%6d\002 records, \002%5d\002 kB"), count, (mem + 512) / 1024);
		get_aliases_stats(count, mem);
		source.Reply(_("NS Aliases    : \002%6d\002 records, \002%5d\002 kB"), count, (mem + 512) / 1024);
		get_chanserv_stats(&count, &mem);
		source.Reply(_("ChanServ      : \002%6d\002 records, \002%5d\002 kB"), count, (mem + 512) / 1024);
		if (!Config->s_BotServ.empty())
		{
			get_botserv_stats(&count, &mem);
			source.Reply(_("BotServ       : \002%6d\002 records, \002%5d\002 kB"), count, (mem + 512) / 1024);
		}
		if (!Config->s_HostServ.empty())
		{
			get_hostserv_stats(&count, &mem);
			source.Reply(_("HostServ      : \002%6d\002 records, \002%5d\002 kB"), count, (mem + 512) / 1024);
		}
		get_operserv_stats(&count, &mem);
		source.Reply(_("OperServ      : \002%6d\002 records, \002%5d\002 kB"), count, (mem + 512) / 1024);
		get_session_stats(count, mem);
		source.Reply(_("Sessions      : \002%6d\002 records, \002%5d\002 kB"), count, (mem + 512) / 1024);

		return MOD_CONT;
	}
 public:
	CommandOSStats() : Command("STATS", 0, 1, "operserv/stats")
	{
		this->SetDesc(_("Show status of Services and network"));
	}

	CommandReturn Execute(CommandSource &source, const std::vector<Anope::string> &params)
	{
		const Anope::string &extra = !params.empty() ? params[0] : "";

		if (!extra.empty() && !extra.equals_ci("ALL"))
		{
			if (extra.equals_ci("AKILL"))
				return this->DoStatsAkill(source);
			else if (extra.equals_ci("RESET"))
				return this->DoStatsReset(source);
			else if (!extra.equals_ci("MEMORY") && !extra.equals_ci("UPLINK"))
				source.Reply(_("Unknown STATS option \002%s\002."), extra.c_str());
		}

		if (extra.empty() || (!extra.equals_ci("MEMORY") && !extra.equals_ci("UPLINK")))
			this->DoStatsUptime(source);

		if (!extra.empty() && (extra.equals_ci("ALL") || extra.equals_ci("UPLINK")))
			this->DoStatsUplink(source);

		if (!extra.empty() && (extra.equals_ci("ALL") || extra.equals_ci("MEMORY")))
			this->DoStatsMemory(source);

		return MOD_CONT;
	}

	bool OnHelp(CommandSource &source, const Anope::string &subcommand)
	{
		source.Reply(_("Syntax: \002STATS [AKILL | ALL | RESET | MEMORY | UPLINK]\002\n"
				" \n"
				"Without any option, shows the current number of users and\n"
				"IRCops online (excluding Services), the highest number of\n"
				"users online since Services was started, and the length of\n"
				"time Services has been running.\n"
				" \n"
				"With the \002AKILL\002 option, displays the current size of the\n"
				"AKILL list and the current default expiry time.\n"
				" \n"
				"The \002RESET\002 option currently resets the maximum user count\n"
				"to the number of users currently present on the network.\n"
				" \n"
				"The \002MEMORY\002 option displays information on the memory\n"
				"usage of Services. Using this option can freeze Services for\n"
				"a short period of time on large networks; don't overuse it!\n"
				" \n"
				"The \002UPLINK\002 option displays information about the current\n"
				"server Anope uses as an uplink to the network.\n"
				" \n"
				"The \002ALL\002 displays the user and uptime statistics, and\n"
				"everything you'd see with \002MEMORY\002 and \002UPLINK\002 options."));
		return true;
	}
};

class OSStats : public Module
{
	CommandOSStats commandosstats;

 public:
	OSStats(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator)
	{
		this->SetAuthor("Anope");
		this->SetType(CORE);

		this->AddCommand(OperServ, &commandosstats);
	}
};

void get_operserv_stats(long *nrec, long *memuse)
{
	unsigned i, end;
	long mem = 0, count = 0, mem2 = 0, count2 = 0;
	XLine *x;

	end = SGLine->GetCount();
	count += end;
	mem += end * sizeof(XLine);

	for (i = 0; i < end; ++i)
	{
		x = SGLine->GetEntry(i);

		mem += x->GetNick().length() + 1;
		mem += x->GetUser().length() + 1;
		mem += x->GetHost().length() + 1;
		mem += x->Mask.length() + 1;
		mem += x->By.length() + 1;
		mem += x->Reason.length() + 1;
	}

	if (ircd->snline)
	{
		end = SNLine->GetCount();
		count += end;
		mem += end * sizeof(XLine);

		for (i = 0; i < end; ++i)
		{
			x = SNLine->GetEntry(i);

			mem += x->GetNick().length() + 1;
			mem += x->GetUser().length() + 1;
			mem += x->GetHost().length() + 1;
			mem += x->Mask.length() + 1;
			mem += x->By.length() + 1;
			mem += x->Reason.length() + 1;
		}
	}
	if (ircd->sqline)
	{
		end = SQLine->GetCount();
		count += end;
		mem += end * sizeof(XLine);

		for (i = 0; i < end; ++i)
		{
			x = SQLine->GetEntry(i);

			mem += x->GetNick().length() + 1;
			mem += x->GetUser().length() + 1;
			mem += x->GetHost().length() + 1;
			mem += x->Mask.length() + 1;
			mem += x->By.length() + 1;
			mem += x->Reason.length() + 1;
		}
	}
	if (ircd->szline)
	{
		end = SZLine->GetCount();
		count += end;
		mem += end * sizeof(XLine);

		for (i = 0; i < end; ++i)
		{
			x = SZLine->GetEntry(i);

			mem += x->GetNick().length() + 1;
			mem += x->GetUser().length() + 1;
			mem += x->GetHost().length() + 1;
			mem += x->Mask.length() + 1;
			mem += x->By.length() + 1;
			mem += x->Reason.length() + 1;
		}
	}

	get_exception_stats(count2, mem2);
	count += count2;
	mem += mem2;

	*nrec = count;
	*memuse = mem;
}

MODULE_INIT(OSStats)