2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5 * Copyright (C) 2007-2008 Craig Edwards <craigedwards@brainbox.cc>
6 * Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
8 * This file is part of InspIRCd. InspIRCd is free software: you can
9 * redistribute it and/or modify it under the terms of the GNU General Public
10 * License as published by the Free Software Foundation, version 2.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "commands/cmd_whowas.h"
27 # pragma comment(lib, "psapi.lib") // For GetProcessMemoryInfo()
30 /** Handle /STATS. These command handlers can be reloaded by the core,
31 * and handle basic RFC1459 commands. Commands within modules work
32 * the same way, however, they can be fully unloaded, where these
35 class CommandStats : public Command
37 void DoStats(char statschar, User* user, string_list &results);
39 /** Constructor for stats.
41 CommandStats ( Module* parent) : Command(parent,"STATS",1,2) { syntax = "<stats-symbol> [<servername>]"; }
43 * @param parameters The parameters to the comamnd
44 * @param pcnt The number of parameters passed to teh command
45 * @param user The user issuing the command
46 * @return A value from CmdResult to indicate command success or failure.
48 CmdResult Handle(const std::vector<std::string>& parameters, User *user);
49 RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
51 if (parameters.size() > 1)
52 return ROUTE_UNICAST(parameters[1]);
53 return ROUTE_LOCALONLY;
57 void CommandStats::DoStats(char statschar, User* user, string_list &results)
59 std::string sn(ServerInstance->Config->ServerName);
61 bool isPublic = ServerInstance->Config->UserStats.find(statschar) != std::string::npos;
62 bool isRemoteOper = IS_REMOTE(user) && IS_OPER(user);
63 bool isLocalOperWithPrivs = IS_LOCAL(user) && user->HasPrivPermission("servers/auspex");
65 if (!isPublic && !isRemoteOper && !isLocalOperWithPrivs)
67 ServerInstance->SNO->WriteToSnoMask('t',
68 "%s '%c' denied for %s (%s@%s)",
69 (IS_LOCAL(user) ? "Stats" : "Remote stats"),
70 statschar, user->nick.c_str(), user->ident.c_str(), user->host.c_str());
71 results.push_back(sn + " 481 " + user->nick + " :Permission denied - STATS " + statschar + " requires the servers/auspex priv.");
76 FIRST_MOD_RESULT(OnStats, MOD_RESULT, (statschar, user, results));
77 if (MOD_RESULT == MOD_RES_DENY)
79 results.push_back(sn+" 219 "+user->nick+" "+statschar+" :End of /STATS report");
80 ServerInstance->SNO->WriteToSnoMask('t',"%s '%c' requested by %s (%s@%s)",
81 (IS_LOCAL(user) ? "Stats" : "Remote stats"), statschar, user->nick.c_str(), user->ident.c_str(), user->host.c_str());
87 /* stats p (show listening ports) */
90 for (std::vector<ListenSocket*>::const_iterator i = ServerInstance->ports.begin(); i != ServerInstance->ports.end(); ++i)
92 ListenSocket* ls = *i;
93 std::string ip = ls->bind_addr;
96 std::string type = ls->bind_tag->getString("type", "clients");
97 std::string hook = ls->bind_tag->getString("ssl", "plaintext");
99 results.push_back(sn+" 249 "+user->nick+" :"+ ip + ":"+ConvToStr(ls->bind_port)+
100 " (" + type + ", " + hook + ")");
105 /* These stats symbols must be handled by a linking module */
112 for (ClassVector::iterator i = ServerInstance->Config->Classes.begin(); i != ServerInstance->Config->Classes.end(); i++)
114 ConnectClass* c = *i;
115 std::stringstream res;
116 res << sn << " 215 " << user->nick << " I " << c->name << ' ';
117 if (c->type == CC_ALLOW)
119 if (c->type == CC_DENY)
122 if (c->type == CC_NAMED)
127 res << ' ' << c->config->getString("port", "*") << ' ';
129 res << c->GetRecvqMax() << ' ' << c->GetSendqSoftMax() << ' ' << c->GetSendqHardMax()
130 << ' ' << c->GetCommandRate() << ' ' << c->GetPenaltyThreshold();
133 results.push_back(res.str());
141 for (ClassVector::iterator i = ServerInstance->Config->Classes.begin(); i != ServerInstance->Config->Classes.end(); i++)
143 ConnectClass* c = *i;
144 results.push_back(sn+" 215 "+user->nick+" i NOMATCH * "+c->GetHost()+" "+ConvToStr(c->limit ? c->limit : ServerInstance->SE->GetMaxFds())+" "+ConvToStr(idx)+" "+ServerInstance->Config->ServerName+" *");
145 results.push_back(sn+" 218 "+user->nick+" Y "+ConvToStr(idx)+" "+ConvToStr(c->GetPingTime())+" 0 "+ConvToStr(c->GetSendqHardMax())+" :"+
146 ConvToStr(c->GetRecvqMax())+" "+ConvToStr(c->GetRegTimeout()));
154 for(std::map<irc::string, bool>::iterator i = ServerInstance->Config->ulines.begin(); i != ServerInstance->Config->ulines.end(); ++i)
156 results.push_back(sn+" 248 "+user->nick+" U "+std::string(i->first.c_str()));
164 for (user_hash::iterator i = ServerInstance->Users->clientlist->begin(); i != ServerInstance->Users->clientlist->end(); i++)
166 if (IS_OPER(i->second) && !ServerInstance->ULine(i->second->server))
168 results.push_back(sn+" 249 "+user->nick+" :"+i->second->nick+" ("+i->second->ident+"@"+i->second->dhost+") Idle: "+
169 (IS_LOCAL(i->second) ? ConvToStr(ServerInstance->Time() - i->second->idle_lastmsg) + " secs" : "unavailable"));
173 results.push_back(sn+" 249 "+user->nick+" :"+ConvToStr(idx)+" OPER(s)");
178 ServerInstance->XLines->InvokeStats("K",216,user,results);
181 ServerInstance->XLines->InvokeStats("G",223,user,results);
184 ServerInstance->XLines->InvokeStats("Q",217,user,results);
187 ServerInstance->XLines->InvokeStats("Z",223,user,results);
190 ServerInstance->XLines->InvokeStats("E",223,user,results);
193 results.push_back(sn+" 249 "+user->nick+" :Total events: "+ConvToStr(ServerInstance->SE->TotalEvents));
194 results.push_back(sn+" 249 "+user->nick+" :Read events: "+ConvToStr(ServerInstance->SE->ReadEvents));
195 results.push_back(sn+" 249 "+user->nick+" :Write events: "+ConvToStr(ServerInstance->SE->WriteEvents));
196 results.push_back(sn+" 249 "+user->nick+" :Error events: "+ConvToStr(ServerInstance->SE->ErrorEvents));
199 /* stats m (list number of times each command has been used, plus bytecount) */
201 for (Commandtable::iterator i = ServerInstance->Parser->cmdlist.begin(); i != ServerInstance->Parser->cmdlist.end(); i++)
203 if (i->second->use_count)
205 /* RPL_STATSCOMMANDS */
206 results.push_back(sn+" 212 "+user->nick+" "+i->second->name+" "+ConvToStr(i->second->use_count)+" "+ConvToStr(i->second->total_bytes));
211 /* stats z (debug and memory info) */
214 results.push_back(sn+" 249 "+user->nick+" :Users: "+ConvToStr(ServerInstance->Users->clientlist->size()));
215 results.push_back(sn+" 249 "+user->nick+" :Channels: "+ConvToStr(ServerInstance->chanlist->size()));
216 results.push_back(sn+" 249 "+user->nick+" :Commands: "+ConvToStr(ServerInstance->Parser->cmdlist.size()));
218 if (!ServerInstance->Config->WhoWasGroupSize == 0 && !ServerInstance->Config->WhoWasMaxGroups == 0)
220 Module* whowas = ServerInstance->Modules->Find("cmd_whowas.so");
223 WhowasRequest req(NULL, whowas, WhowasRequest::WHOWAS_STATS);
226 results.push_back(sn+" 249 "+user->nick+" :"+req.value);
230 float kbitpersec_in, kbitpersec_out, kbitpersec_total;
231 char kbitpersec_in_s[30], kbitpersec_out_s[30], kbitpersec_total_s[30];
233 ServerInstance->SE->GetStats(kbitpersec_in, kbitpersec_out, kbitpersec_total);
235 snprintf(kbitpersec_total_s, 30, "%03.5f", kbitpersec_total);
236 snprintf(kbitpersec_out_s, 30, "%03.5f", kbitpersec_out);
237 snprintf(kbitpersec_in_s, 30, "%03.5f", kbitpersec_in);
239 results.push_back(sn+" 249 "+user->nick+" :Bandwidth total: "+ConvToStr(kbitpersec_total_s)+" kilobits/sec");
240 results.push_back(sn+" 249 "+user->nick+" :Bandwidth out: "+ConvToStr(kbitpersec_out_s)+" kilobits/sec");
241 results.push_back(sn+" 249 "+user->nick+" :Bandwidth in: "+ConvToStr(kbitpersec_in_s)+" kilobits/sec");
244 /* Moved this down here so all the not-windows stuff (look w00tie, I didn't say win32!) is in one ifndef.
245 * Also cuts out some identical code in both branches of the ifndef. -- Om
249 /* Not sure why we were doing '0' with a RUSAGE_SELF comment rather than just using RUSAGE_SELF -- Om */
250 if (!getrusage(RUSAGE_SELF,&R)) /* RUSAGE_SELF */
252 results.push_back(sn+" 249 "+user->nick+" :Total allocation: "+ConvToStr(R.ru_maxrss)+"K");
253 results.push_back(sn+" 249 "+user->nick+" :Signals: "+ConvToStr(R.ru_nsignals));
254 results.push_back(sn+" 249 "+user->nick+" :Page faults: "+ConvToStr(R.ru_majflt));
255 results.push_back(sn+" 249 "+user->nick+" :Swaps: "+ConvToStr(R.ru_nswap));
256 results.push_back(sn+" 249 "+user->nick+" :Context Switches: Voluntary; "+ConvToStr(R.ru_nvcsw)+" Involuntary; "+ConvToStr(R.ru_nivcsw));
260 float n_elapsed = (ServerInstance->Time() - ServerInstance->stats->LastSampled.tv_sec) * 1000000
261 + (ServerInstance->Time_ns() - ServerInstance->stats->LastSampled.tv_nsec) / 1000;
262 float n_eaten = ((R.ru_utime.tv_sec - ServerInstance->stats->LastCPU.tv_sec) * 1000000 + R.ru_utime.tv_usec - ServerInstance->stats->LastCPU.tv_usec);
263 float per = (n_eaten / n_elapsed) * 100;
265 snprintf(percent, 30, "%03.5f%%", per);
266 results.push_back(sn+" 249 "+user->nick+" :CPU Use (now): "+percent);
268 n_elapsed = ServerInstance->Time() - ServerInstance->startup_time;
269 n_eaten = (float)R.ru_utime.tv_sec + R.ru_utime.tv_usec / 100000.0;
270 per = (n_eaten / n_elapsed) * 100;
271 snprintf(percent, 30, "%03.5f%%", per);
272 results.push_back(sn+" 249 "+user->nick+" :CPU Use (total): "+percent);
275 PROCESS_MEMORY_COUNTERS MemCounters;
276 if (GetProcessMemoryInfo(GetCurrentProcess(), &MemCounters, sizeof(MemCounters)))
278 results.push_back(sn+" 249 "+user->nick+" :Total allocation: "+ConvToStr((MemCounters.WorkingSetSize + MemCounters.PagefileUsage) / 1024)+"K");
279 results.push_back(sn+" 249 "+user->nick+" :Pagefile usage: "+ConvToStr(MemCounters.PagefileUsage / 1024)+"K");
280 results.push_back(sn+" 249 "+user->nick+" :Page faults: "+ConvToStr(MemCounters.PageFaultCount));
281 results.push_back(sn+" 249 "+user->nick+" :CPU Usage: " + ConvToStr(getcpu()) + "%");
290 results.push_back(sn+" 249 "+user->nick+" :accepts "+ConvToStr(ServerInstance->stats->statsAccept)+" refused "+ConvToStr(ServerInstance->stats->statsRefused));
291 results.push_back(sn+" 249 "+user->nick+" :unknown commands "+ConvToStr(ServerInstance->stats->statsUnknown));
292 results.push_back(sn+" 249 "+user->nick+" :nick collisions "+ConvToStr(ServerInstance->stats->statsCollisions));
293 results.push_back(sn+" 249 "+user->nick+" :dns requests "+ConvToStr(ServerInstance->stats->statsDnsGood+ServerInstance->stats->statsDnsBad)+" succeeded "+ConvToStr(ServerInstance->stats->statsDnsGood)+" failed "+ConvToStr(ServerInstance->stats->statsDnsBad));
294 results.push_back(sn+" 249 "+user->nick+" :connection count "+ConvToStr(ServerInstance->stats->statsConnects));
295 snprintf(buffer,MAXBUF," 249 %s :bytes sent %5.2fK recv %5.2fK",
296 user->nick.c_str(),ServerInstance->stats->statsSent / 1024.0,ServerInstance->stats->statsRecv / 1024.0);
297 results.push_back(sn+buffer);
304 ConfigTagList tags = ServerInstance->Config->ConfTags("oper");
305 for(ConfigIter i = tags.first; i != tags.second; ++i)
307 ConfigTag* tag = i->second;
308 results.push_back(sn+" 243 "+user->nick+" O "+tag->getString("host")+" * "+
309 tag->getString("name") + " " + tag->getString("type")+" 0");
315 for(OperIndex::iterator i = ServerInstance->Config->oper_blocks.begin(); i != ServerInstance->Config->oper_blocks.end(); i++)
317 // just the types, not the actual oper blocks...
318 if (i->first[0] != ' ')
320 OperInfo* tag = i->second;
324 for(char c='A'; c < 'z'; c++)
326 ModeHandler* mh = ServerInstance->Modes->FindMode(c, MODETYPE_USER);
327 if (mh && mh->NeedsOper() && tag->AllowedUserModes[c - 'A'])
329 mh = ServerInstance->Modes->FindMode(c, MODETYPE_CHANNEL);
330 if (mh && mh->NeedsOper() && tag->AllowedChanModes[c - 'A'])
333 results.push_back(sn+" 243 "+user->nick+" O "+tag->NameStr() + " " + umodes + " " + cmodes);
338 /* stats l (show user I/O stats) */
340 results.push_back(sn+" 211 "+user->nick+" :nick[ident@host] sendq cmds_out bytes_out cmds_in bytes_in time_open");
341 for (std::vector<LocalUser*>::iterator n = ServerInstance->Users->local_users.begin(); n != ServerInstance->Users->local_users.end(); n++)
344 results.push_back(sn+" 211 "+user->nick+" "+i->nick+"["+i->ident+"@"+i->dhost+"] "+ConvToStr(i->eh.getSendQSize())+" "+ConvToStr(i->cmds_out)+" "+ConvToStr(i->bytes_out)+" "+ConvToStr(i->cmds_in)+" "+ConvToStr(i->bytes_in)+" "+ConvToStr(ServerInstance->Time() - i->age));
348 /* stats L (show user I/O stats with IP addresses) */
350 results.push_back(sn+" 211 "+user->nick+" :nick[ident@ip] sendq cmds_out bytes_out cmds_in bytes_in time_open");
351 for (std::vector<LocalUser*>::iterator n = ServerInstance->Users->local_users.begin(); n != ServerInstance->Users->local_users.end(); n++)
354 results.push_back(sn+" 211 "+user->nick+" "+i->nick+"["+i->ident+"@"+i->GetIPString()+"] "+ConvToStr(i->eh.getSendQSize())+" "+ConvToStr(i->cmds_out)+" "+ConvToStr(i->bytes_out)+" "+ConvToStr(i->cmds_in)+" "+ConvToStr(i->bytes_in)+" "+ConvToStr(ServerInstance->Time() - i->age));
358 /* stats u (show server uptime) */
361 time_t current_time = 0;
362 current_time = ServerInstance->Time();
363 time_t server_uptime = current_time - ServerInstance->startup_time;
365 stime = gmtime(&server_uptime);
366 /* i dont know who the hell would have an ircd running for over a year nonstop, but
367 * Craig suggested this, and it seemed a good idea so in it went */
368 if (stime->tm_year > 70)
371 snprintf(buffer,MAXBUF," 242 %s :Server up %d years, %d days, %.2d:%.2d:%.2d",user->nick.c_str(),(stime->tm_year-70),stime->tm_yday,stime->tm_hour,stime->tm_min,stime->tm_sec);
372 results.push_back(sn+buffer);
377 snprintf(buffer,MAXBUF," 242 %s :Server up %d days, %.2d:%.2d:%.2d",user->nick.c_str(),stime->tm_yday,stime->tm_hour,stime->tm_min,stime->tm_sec);
378 results.push_back(sn+buffer);
387 results.push_back(sn+" 219 "+user->nick+" "+statschar+" :End of /STATS report");
388 ServerInstance->SNO->WriteToSnoMask('t',"%s '%c' requested by %s (%s@%s)",
389 (IS_LOCAL(user) ? "Stats" : "Remote stats"), statschar, user->nick.c_str(), user->ident.c_str(), user->host.c_str());
393 CmdResult CommandStats::Handle (const std::vector<std::string>& parameters, User *user)
395 if (parameters.size() > 1 && parameters[1] != ServerInstance->Config->ServerName)
398 char search = parameters[0][0];
399 DoStats(search, user, values);
400 for (size_t i = 0; i < values.size(); i++)
401 user->SendText(":%s", values[i].c_str());
406 COMMAND_INIT(CommandStats)