summaryrefslogtreecommitdiff
path: root/modules/operserv/stats.cpp
blob: e3a655ffa8e08af6e71186a214a152b3c3f9f058 (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
/*
 * Anope IRC Services
 *
 * Copyright (C) 2003-2017 Anope Team <team@anope.org>
 *
 * This file is part of Anope. Anope is free software; you can
 * redistribute it and/or modify it under the terms of the GNU
 * General Public License as published by the Free Software
 * Foundation, version 2.
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see see <http://www.gnu.org/licenses/>.
 */

#include "module.h"
#include "modules/operserv/session.h"
#include "modules/operserv/stats.h"

class StatsImpl : public Stats
{
	friend class StatsType;

	Serialize::Storage<unsigned int> maxusercnt;
	Serialize::Storage<time_t> maxusertime;

 public:
	using Stats::Stats;

	unsigned int GetMaxUserCount() override;
	void SetMaxUserCount(unsigned int i) override;

	time_t GetMaxUserTime() override;
	void SetMaxUserTime(time_t t) override;
};

class StatsType : public Serialize::Type<StatsImpl>
{
 public:
	Serialize::Field<StatsImpl, unsigned int> maxusercount;
	Serialize::Field<StatsImpl, time_t> maxusertime;

	StatsType(Module *me) : Serialize::Type<StatsImpl>(me)
		, maxusercount(this, "maxusercount", &StatsImpl::maxusercnt)
		, maxusertime(this, "maxusertime", &StatsImpl::maxusertime)
	{
	}
};

unsigned int StatsImpl::GetMaxUserCount()
{
	return Get(&StatsType::maxusercount);
}

void StatsImpl::SetMaxUserCount(unsigned int i)
{
	Set(&StatsType::maxusercount, i);
}

time_t StatsImpl::GetMaxUserTime()
{
	return Get(&StatsType::maxusertime);
}

void StatsImpl::SetMaxUserTime(time_t t)
{
	Set(&StatsType::maxusertime, t);
}

/**
 * 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
{
	ServiceReference<XLineManager> akills, snlines, sqlines;
	ServiceReference<SessionService> session_service;

 private:
	void DoStatsAkill(CommandSource &source)
	{
		int timeout;
		if (akills)
		{
			/* AKILLs */
			source.Reply(_("Current number of AKILLs: \002{0}\002"), akills->GetCount());
			timeout = Config->GetModule("operserv/main")->Get<time_t>("autokillexpiry", "30d") + 59;
			if (timeout >= 172800)
				source.Reply(_("Default AKILL expiry time: \002{0} 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{0} 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{0} 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 (snlines)
		{
			/* SNLINEs */
			source.Reply(_("Current number of SNLINEs: \002{0}\002"), snlines->GetCount());
			timeout = Config->GetModule("operserv/main")->Get<time_t>("snlineexpiry", "30d") + 59;
			if (timeout >= 172800)
				source.Reply(_("Default SNLINE expiry time: \002{0} 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{0} 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{0} 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 (sqlines)
		{
			/* SQLINEs */
			source.Reply(_("Current number of SQLINEs: \002{0}\002"), sqlines->GetCount());
			timeout = Config->GetModule("operserv/main")->Get<time_t>("sglineexpiry", "30d") + 59;
			if (timeout >= 172800)
				source.Reply(_("Default SQLINE expiry time: \002{0} 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{0} 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{0} 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"));
		}
	}

	void DoStatsReset(CommandSource &source)
	{
		Stats *stats = Serialize::GetObject<Stats *>();
		stats->SetMaxUserCount(UserListByNick.size());
		source.Reply(_("Statistics reset."));
		return;
	}

	void DoStatsUptime(CommandSource &source)
	{
		Stats *stats = Serialize::GetObject<Stats *>();
		time_t uptime = Anope::CurTime - Anope::StartTime;
		
		source.Reply(_("Current users: \002{0}\002 (\002{1}\002 ops)"), UserListByNick.size(), OperCount);
		source.Reply(_("Maximum users: \002{0}\002 ({1})"), stats->GetMaxUserCount(), Anope::strftime(stats->GetMaxUserTime(), source.GetAccount()));
		source.Reply(_("Services up \002{0}\002."), Anope::Duration(uptime, source.GetAccount()));
	}

	void DoStatsUplink(CommandSource &source)
	{
		Anope::string buf;
		for (std::set<Anope::string>::iterator it = Servers::Capab.begin(); it != Servers::Capab.end(); ++it)
			buf += " " + *it;
		if (!buf.empty())
			buf.erase(buf.begin());

		source.Reply(_("Uplink server: \002{0}\002"), Me->GetLinks().front()->GetName());
		source.Reply(_("Uplink capab: {0}"), buf);
		source.Reply(_("Servers found: {0}"), stats_count_servers(Me->GetLinks().front()));
	}

	template<typename T> void GetHashStats(const T& map, size_t& entries, size_t& buckets, size_t& max_chain)
	{
		entries = map.size(), buckets = map.bucket_count(), max_chain = 0;
		for (size_t i = 0; i < buckets; ++i)
			if (map.bucket_size(i) > max_chain)
				max_chain = map.bucket_size(i);
	}

	void DoStatsHash(CommandSource &source)
	{
		size_t entries, buckets, max_chain;

		GetHashStats(UserListByNick, entries, buckets, max_chain);
		source.Reply(_("Users (nick): {0} entries, {1} buckets, longest chain is {2}"), entries, buckets, max_chain);

		if (!UserListByUID.empty())
		{
			GetHashStats(UserListByUID, entries, buckets, max_chain);
			source.Reply(_("Users (uid): {0} entries, {1} buckets, longest chain is {2}"), entries, buckets, max_chain);
		}

		GetHashStats(ChannelList, entries, buckets, max_chain);
		source.Reply(_("Channels: {0} entries, {1} buckets, longest chain is {2}"), entries, buckets, max_chain);

		GetHashStats(ChanServ::service->GetChannels(), entries, buckets, max_chain);
		source.Reply(_("Registered channels: {0} entries, {1} buckets, longest chain is {2}"), entries, buckets, max_chain);

		GetHashStats(NickServ::service->GetNickMap(), entries, buckets, max_chain);
		source.Reply(_("Registered nicknames: {0} entries, {1} buckets, longest chain is {2}"), entries, buckets, max_chain);

		GetHashStats(NickServ::service->GetAccountMap(), entries, buckets, max_chain);
		source.Reply(_("Registered nick groups: {0} entries, {1} buckets, longest chain is {2}"), entries, buckets, max_chain);

		if (session_service)
		{
			GetHashStats(session_service->GetSessions(), entries, buckets, max_chain);
			source.Reply(_("Sessions: {0} entries, {1} buckets, longest chain is {2}"), entries, buckets, max_chain);
		}
	}

 public:
	CommandOSStats(Module *creator) : Command(creator, "operserv/stats", 0, 1)
		, akills("sgline")
		, snlines("snline")
		, sqlines("sqline")
	{
		this->SetDesc(_("Show status of Services and network"));
		this->SetSyntax("[AKILL | HASH | UPLINK | UPTIME | ALL | RESET]");
	}

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

		logger.Admin(source, _("{source} used {command} {0}"), extra);

		if (extra.equals_ci("RESET"))
			return this->DoStatsReset(source);

		if (extra.equals_ci("ALL") || extra.equals_ci("AKILL"))
			this->DoStatsAkill(source);

		if (extra.equals_ci("ALL") || extra.equals_ci("HASH"))
			this->DoStatsHash(source);

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

		if (extra.empty() || extra.equals_ci("ALL") || extra.equals_ci("UPTIME"))
			this->DoStatsUptime(source);

		if (!extra.empty() && !extra.equals_ci("ALL") && !extra.equals_ci("AKILL") && !extra.equals_ci("HASH") && !extra.equals_ci("UPLINK") && !extra.equals_ci("UPTIME"))
			source.Reply(_("Unknown STATS option: \002{0}\002"), extra);
	}

	bool OnHelp(CommandSource &source, const Anope::string &subcommand) override
	{
		source.Reply(_("Without any option, shows the current number of users online, and the highest number of users online since Services was started, and the length of time Services has been running.\n"
		               "\n"
		               "With the \002AKILL\002 option, displays the current size of the AKILL list and the current default expiry time.\n"
		               "\n"
		               "The \002RESET\002 option currently resets the maximum user count to the number of users currently present on the network.\n"
		               "\n"
		               "The \002UPLINK\002 option displays information about the current server Anope uses as an uplink to the network.\n"
		               "\n"
		               "The \002HASH\002 option displays information about the hash maps.\n"
		               "\n"
		               "The \002ALL\002 option displays all of the above statistics."));
		return true;
	}
};

class OSStats : public Module
	, public EventHook<Event::UserConnect>
{
	CommandOSStats commandosstats;
	StatsType stats_type;

	Stats *GetStats()
	{
		Stats *stats = Serialize::GetObject<Stats *>();
		if (stats)
			return stats;
		else
			return Serialize::New<Stats *>();
	}

 public:
	OSStats(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, VENDOR)
		, EventHook<Event::UserConnect>(this)
		, commandosstats(this)
		, stats_type(this)
	{
	}

	void OnUserConnect(User *u, bool &exempt) override
	{
		Stats *stats = GetStats();

		if (stats && UserListByNick.size() > stats->GetMaxUserCount())
		{
			stats->SetMaxUserCount(UserListByNick.size());
			stats->SetMaxUserTime(Anope::CurTime);

			Server *sserver = u->server;
			if (sserver && sserver->IsSynced())
				u->logger.Category("maxusers").Log(_("{0} connected - new maximum user count: {1}"), u->GetMask(), UserListByNick.size());
		}
	}
};

MODULE_INIT(OSStats)