]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/stats.cpp
cd132afed9c95f064c075a9efbb3a89e5ae9ac6f
[user/henk/code/inspircd.git] / src / stats.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2010 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *          the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15 #include "xline.h"
16 #include "commands/cmd_whowas.h"
17
18 void InspIRCd::DoStats(char statschar, User* user, string_list &results)
19 {
20         std::string sn(this->Config->ServerName);
21
22         bool isPublic = Config->UserStats.find(statschar) != std::string::npos;
23         bool isRemoteOper = IS_REMOTE(user) && IS_OPER(user);
24         bool isLocalOperWithPrivs = IS_LOCAL(user) && user->HasPrivPermission("servers/auspex");
25
26         if (!isPublic && !isRemoteOper && !isLocalOperWithPrivs)
27         {
28                 this->SNO->WriteToSnoMask('t',
29                                 "%s '%c' denied for %s (%s@%s)",
30                                 (IS_LOCAL(user) ? "Stats" : "Remote stats"),
31                                 statschar, user->nick.c_str(), user->ident.c_str(), user->host.c_str());
32                 results.push_back(sn + " 481 " + user->nick + " :Permission denied - STATS " + statschar + " requires the servers/auspex priv.");
33                 return;
34         }
35
36         ModResult MOD_RESULT;
37         FIRST_MOD_RESULT(OnStats, MOD_RESULT, (statschar, user, results));
38         if (MOD_RESULT == MOD_RES_DENY)
39         {
40                 results.push_back(sn+" 219 "+user->nick+" "+statschar+" :End of /STATS report");
41                 this->SNO->WriteToSnoMask('t',"%s '%c' requested by %s (%s@%s)",
42                         (IS_LOCAL(user) ? "Stats" : "Remote stats"), statschar, user->nick.c_str(), user->ident.c_str(), user->host.c_str());
43                 return;
44         }
45
46         switch (statschar)
47         {
48                 /* stats p (show listening ports) */
49                 case 'p':
50                 {
51                         for (size_t i = 0; i < this->ports.size(); i++)
52                         {
53                                 std::string ip = this->ports[i]->bind_addr;
54                                 if (ip.empty())
55                                         ip.assign("*");
56                                 std::string type = ports[i]->bind_tag->getString("type", "clients");
57                                 std::string hook = ports[i]->bind_tag->getString("ssl", "plaintext");
58
59                                 results.push_back(sn+" 249 "+user->nick+" :"+ ip + ":"+ConvToStr(ports[i]->bind_port)+
60                                         " (" + type + ", " + hook + ")");
61                         }
62                 }
63                 break;
64
65                 /* These stats symbols must be handled by a linking module */
66                 case 'n':
67                 case 'c':
68                 break;
69
70                 case 'i':
71                 {
72                         for (ClassVector::iterator i = this->Config->Classes.begin(); i != this->Config->Classes.end(); i++)
73                         {
74                                 ConnectClass* c = *i;
75                                 std::stringstream res;
76                                 res << sn << " 215 " << user->nick << " I " << c->name << ' ';
77                                 if (c->type == CC_ALLOW)
78                                         res << '+';
79                                 if (c->type == CC_DENY)
80                                         res << '-';
81
82                                 if (c->type == CC_NAMED)
83                                         res << '*';
84                                 else
85                                         res << c->host;
86
87                                 res << ' ' << c->config->getString("port", "*") << ' ';
88
89                                 res << c->GetRecvqMax() << ' ' << c->GetSendqSoftMax() << ' ' << c->GetSendqHardMax()
90                                         << ' ' << c->GetCommandRate() << ' ' << c->GetPenaltyThreshold();
91                                 if (c->fakelag)
92                                         res << '*';
93                                 results.push_back(res.str());
94                         }
95                 }
96                 break;
97
98                 case 'Y':
99                 {
100                         int idx = 0;
101                         for (ClassVector::iterator i = this->Config->Classes.begin(); i != this->Config->Classes.end(); i++)
102                         {
103                                 ConnectClass* c = *i;
104                                 results.push_back(sn+" 215 "+user->nick+" i NOMATCH * "+c->GetHost()+" "+ConvToStr(c->limit ? c->limit : this->SE->GetMaxFds())+" "+ConvToStr(idx)+" "+this->Config->ServerName+" *");
105                                 results.push_back(sn+" 218 "+user->nick+" Y "+ConvToStr(idx)+" "+ConvToStr(c->GetPingTime())+" 0 "+ConvToStr(c->GetSendqHardMax())+" :"+
106                                                 ConvToStr(c->GetRecvqMax())+" "+ConvToStr(c->GetRegTimeout()));
107                                 idx++;
108                         }
109                 }
110                 break;
111
112                 case 'U':
113                 {
114                         for(std::map<irc::string, bool>::iterator i = Config->ulines.begin(); i != Config->ulines.end(); ++i)
115                         {
116                                 results.push_back(sn+" 248 "+user->nick+" U "+std::string(i->first.c_str()));
117                         }
118                 }
119                 break;
120
121                 case 'P':
122                 {
123                         int idx = 0;
124                         for (user_hash::iterator i = this->Users->clientlist->begin(); i != this->Users->clientlist->end(); i++)
125                         {
126                                 if (IS_OPER(i->second) && !this->ULine(i->second->server))
127                                 {
128                                         results.push_back(sn+" 249 "+user->nick+" :"+i->second->nick+" ("+i->second->ident+"@"+i->second->dhost+") Idle: "+
129                                                         (IS_LOCAL(i->second) ? ConvToStr(this->Time() - i->second->idle_lastmsg) + " secs" : "unavailable"));
130                                         idx++;
131                                 }
132                         }
133                         results.push_back(sn+" 249 "+user->nick+" :"+ConvToStr(idx)+" OPER(s)");
134                 }
135                 break;
136
137                 case 'k':
138                         this->XLines->InvokeStats("K",216,user,results);
139                 break;
140                 case 'g':
141                         this->XLines->InvokeStats("G",223,user,results);
142                 break;
143                 case 'q':
144                         this->XLines->InvokeStats("Q",217,user,results);
145                 break;
146                 case 'Z':
147                         this->XLines->InvokeStats("Z",223,user,results);
148                 break;
149                 case 'e':
150                         this->XLines->InvokeStats("E",223,user,results);
151                 break;
152                 case 'E':
153                         results.push_back(sn+" 249 "+user->nick+" :Total events: "+ConvToStr(this->SE->TotalEvents));
154                         results.push_back(sn+" 249 "+user->nick+" :Read events:  "+ConvToStr(this->SE->ReadEvents));
155                         results.push_back(sn+" 249 "+user->nick+" :Write events: "+ConvToStr(this->SE->WriteEvents));
156                         results.push_back(sn+" 249 "+user->nick+" :Error events: "+ConvToStr(this->SE->ErrorEvents));
157                 break;
158
159                 /* stats m (list number of times each command has been used, plus bytecount) */
160                 case 'm':
161                         for (Commandtable::iterator i = this->Parser->cmdlist.begin(); i != this->Parser->cmdlist.end(); i++)
162                         {
163                                 if (i->second->use_count)
164                                 {
165                                         /* RPL_STATSCOMMANDS */
166                                         results.push_back(sn+" 212 "+user->nick+" "+i->second->name+" "+ConvToStr(i->second->use_count)+" "+ConvToStr(i->second->total_bytes));
167                                 }
168                         }
169                 break;
170
171                 /* stats z (debug and memory info) */
172                 case 'z':
173                 {
174                         results.push_back(sn+" 249 "+user->nick+" :Users: "+ConvToStr(this->Users->clientlist->size()));
175                         results.push_back(sn+" 249 "+user->nick+" :Channels: "+ConvToStr(this->chanlist->size()));
176                         results.push_back(sn+" 249 "+user->nick+" :Commands: "+ConvToStr(this->Parser->cmdlist.size()));
177
178                         if (!this->Config->WhoWasGroupSize == 0 && !this->Config->WhoWasMaxGroups == 0)
179                         {
180                                 Module* whowas = Modules->Find("cmd_whowas.so");
181                                 if (whowas)
182                                 {
183                                         WhowasRequest req(NULL, whowas, WhowasRequest::WHOWAS_STATS);
184                                         req.user = user;
185                                         req.Send();
186                                         results.push_back(sn+" 249 "+user->nick+" :"+req.value);
187                                 }
188                         }
189
190                         float kbitpersec_in, kbitpersec_out, kbitpersec_total;
191                         char kbitpersec_in_s[30], kbitpersec_out_s[30], kbitpersec_total_s[30];
192
193                         this->SE->GetStats(kbitpersec_in, kbitpersec_out, kbitpersec_total);
194
195                         snprintf(kbitpersec_total_s, 30, "%03.5f", kbitpersec_total);
196                         snprintf(kbitpersec_out_s, 30, "%03.5f", kbitpersec_out);
197                         snprintf(kbitpersec_in_s, 30, "%03.5f", kbitpersec_in);
198
199                         results.push_back(sn+" 249 "+user->nick+" :Bandwidth total:  "+ConvToStr(kbitpersec_total_s)+" kilobits/sec");
200                         results.push_back(sn+" 249 "+user->nick+" :Bandwidth out:    "+ConvToStr(kbitpersec_out_s)+" kilobits/sec");
201                         results.push_back(sn+" 249 "+user->nick+" :Bandwidth in:     "+ConvToStr(kbitpersec_in_s)+" kilobits/sec");
202
203 #ifndef WIN32
204                         /* Moved this down here so all the not-windows stuff (look w00tie, I didn't say win32!) is in one ifndef.
205                          * Also cuts out some identical code in both branches of the ifndef. -- Om
206                          */
207                         rusage R;
208
209                         /* Not sure why we were doing '0' with a RUSAGE_SELF comment rather than just using RUSAGE_SELF -- Om */
210                         if (!getrusage(RUSAGE_SELF,&R)) /* RUSAGE_SELF */
211                         {
212                                 results.push_back(sn+" 249 "+user->nick+" :Total allocation: "+ConvToStr(R.ru_maxrss)+"K");
213                                 results.push_back(sn+" 249 "+user->nick+" :Signals:          "+ConvToStr(R.ru_nsignals));
214                                 results.push_back(sn+" 249 "+user->nick+" :Page faults:      "+ConvToStr(R.ru_majflt));
215                                 results.push_back(sn+" 249 "+user->nick+" :Swaps:            "+ConvToStr(R.ru_nswap));
216                                 results.push_back(sn+" 249 "+user->nick+" :Context Switches: Voluntary; "+ConvToStr(R.ru_nvcsw)+" Involuntary; "+ConvToStr(R.ru_nivcsw));
217
218                                 char percent[30];
219
220                                 float n_elapsed = (ServerInstance->Time() - this->stats->LastSampled.tv_sec) * 1000000
221                                         + (ServerInstance->Time_ns() - this->stats->LastSampled.tv_nsec) / 1000;
222                                 float n_eaten = ((R.ru_utime.tv_sec - this->stats->LastCPU.tv_sec) * 1000000 + R.ru_utime.tv_usec - this->stats->LastCPU.tv_usec);
223                                 float per = (n_eaten / n_elapsed) * 100;
224
225                                 snprintf(percent, 30, "%03.5f%%", per);
226                                 results.push_back(sn+" 249 "+user->nick+" :CPU Use (now):    "+percent);
227
228                                 n_elapsed = ServerInstance->Time() - ServerInstance->startup_time;
229                                 n_eaten = (float)R.ru_utime.tv_sec + R.ru_utime.tv_usec / 100000.0;
230                                 per = (n_eaten / n_elapsed) * 100;
231                                 snprintf(percent, 30, "%03.5f%%", per);
232                                 results.push_back(sn+" 249 "+user->nick+" :CPU Use (total):  "+percent);
233                         }
234 #else
235                         PROCESS_MEMORY_COUNTERS MemCounters;
236                         if (GetProcessMemoryInfo(GetCurrentProcess(), &MemCounters, sizeof(MemCounters)))
237                         {
238                                 results.push_back(sn+" 249 "+user->nick+" :Total allocation: "+ConvToStr((MemCounters.WorkingSetSize + MemCounters.PagefileUsage) / 1024)+"K");
239                                 results.push_back(sn+" 249 "+user->nick+" :Pagefile usage:   "+ConvToStr(MemCounters.PagefileUsage / 1024)+"K");
240                                 results.push_back(sn+" 249 "+user->nick+" :Page faults:      "+ConvToStr(MemCounters.PageFaultCount));
241                                 results.push_back(sn+" 249 "+user->nick+" :CPU Usage: " + ConvToStr(getcpu()) + "%");
242                         }
243 #endif
244                 }
245                 break;
246
247                 case 'T':
248                 {
249                         char buffer[MAXBUF];
250                         results.push_back(sn+" 249 "+user->nick+" :accepts "+ConvToStr(this->stats->statsAccept)+" refused "+ConvToStr(this->stats->statsRefused));
251                         results.push_back(sn+" 249 "+user->nick+" :unknown commands "+ConvToStr(this->stats->statsUnknown));
252                         results.push_back(sn+" 249 "+user->nick+" :nick collisions "+ConvToStr(this->stats->statsCollisions));
253                         results.push_back(sn+" 249 "+user->nick+" :dns requests "+ConvToStr(this->stats->statsDnsGood+this->stats->statsDnsBad)+" succeeded "+ConvToStr(this->stats->statsDnsGood)+" failed "+ConvToStr(this->stats->statsDnsBad));
254                         results.push_back(sn+" 249 "+user->nick+" :connection count "+ConvToStr(this->stats->statsConnects));
255                         snprintf(buffer,MAXBUF," 249 %s :bytes sent %5.2fK recv %5.2fK",
256                                 user->nick.c_str(),this->stats->statsSent / 1024.0,this->stats->statsRecv / 1024.0);
257                         results.push_back(sn+buffer);
258                 }
259                 break;
260
261                 /* stats o */
262                 case 'o':
263                 {
264                         ConfigTagList tags = ServerInstance->Config->ConfTags("oper");
265                         for(ConfigIter i = tags.first; i != tags.second; ++i)
266                         {
267                                 ConfigTag* tag = i->second;
268                                 results.push_back(sn+" 243 "+user->nick+" O "+tag->getString("host")+" * "+
269                                         tag->getString("name") + " " + tag->getString("type")+" 0");
270                         }
271                 }
272                 break;
273                 case 'O':
274                 {
275                         for(OperIndex::iterator i = ServerInstance->Config->oper_blocks.begin(); i != ServerInstance->Config->oper_blocks.end(); i++)
276                         {
277                                 // just the types, not the actual oper blocks...
278                                 if (i->first[0] != ' ')
279                                         continue;
280                                 OperInfo* tag = i->second;
281                                 tag->init();
282                                 std::string umodes;
283                                 std::string cmodes;
284                                 for(char c='A'; c < 'z'; c++)
285                                 {
286                                         ModeHandler* mh = ServerInstance->Modes->FindMode(c, MODETYPE_USER);
287                                         if (mh && mh->NeedsOper() && tag->AllowedUserModes[c - 'A'])
288                                                 umodes.push_back(c);
289                                         mh = ServerInstance->Modes->FindMode(c, MODETYPE_CHANNEL);
290                                         if (mh && mh->NeedsOper() && tag->AllowedChanModes[c - 'A'])
291                                                 cmodes.push_back(c);
292                                 }
293                                 results.push_back(sn+" 243 "+user->nick+" O "+tag->NameStr() + " " + umodes + " " + cmodes);
294                         }
295                 }
296                 break;
297
298                 /* stats l (show user I/O stats) */
299                 case 'l':
300                         results.push_back(sn+" 211 "+user->nick+" :nick[ident@host] sendq cmds_out bytes_out cmds_in bytes_in time_open");
301                         for (std::vector<LocalUser*>::iterator n = this->Users->local_users.begin(); n != this->Users->local_users.end(); n++)
302                         {
303                                 LocalUser* i = *n;
304                                 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(this->Time() - i->age));
305                         }
306                 break;
307
308                 /* stats L (show user I/O stats with IP addresses) */
309                 case 'L':
310                         results.push_back(sn+" 211 "+user->nick+" :nick[ident@ip] sendq cmds_out bytes_out cmds_in bytes_in time_open");
311                         for (std::vector<LocalUser*>::iterator n = this->Users->local_users.begin(); n != this->Users->local_users.end(); n++)
312                         {
313                                 LocalUser* i = *n;
314                                 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(this->Time() - i->age));
315                         }
316                 break;
317
318                 /* stats u (show server uptime) */
319                 case 'u':
320                 {
321                         time_t current_time = 0;
322                         current_time = this->Time();
323                         time_t server_uptime = current_time - this->startup_time;
324                         struct tm* stime;
325                         stime = gmtime(&server_uptime);
326                         /* i dont know who the hell would have an ircd running for over a year nonstop, but
327                          * Craig suggested this, and it seemed a good idea so in it went */
328                         if (stime->tm_year > 70)
329                         {
330                                 char buffer[MAXBUF];
331                                 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);
332                                 results.push_back(sn+buffer);
333                         }
334                         else
335                         {
336                                 char buffer[MAXBUF];
337                                 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);
338                                 results.push_back(sn+buffer);
339                         }
340                 }
341                 break;
342
343                 default:
344                 break;
345         }
346
347         results.push_back(sn+" 219 "+user->nick+" "+statschar+" :End of /STATS report");
348         this->SNO->WriteToSnoMask('t',"%s '%c' requested by %s (%s@%s)",
349                 (IS_LOCAL(user) ? "Stats" : "Remote stats"), statschar, user->nick.c_str(), user->ident.c_str(), user->host.c_str());
350         return;
351 }