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