]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/stats.cpp
Replace copyright headers with headers granting specific authors copyright
[user/henk/code/inspircd.git] / src / stats.cpp
index 02cfeaa7c8e87bbbaae776f90043d6fba0ba3efa..e5ea94b9cc229a3fcfa2dd8abfd3b0471f3fa707 100644 (file)
@@ -1,16 +1,22 @@
-/*       +------------------------------------+
- *       | Inspire Internet Relay Chat Daemon |
- *       +------------------------------------+
+/*
+ * InspIRCd -- Internet Relay Chat Daemon
  *
- *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
- * See: http://wiki.inspircd.org/Credits
+ *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
  *
- * This program is free but copyrighted software; see
- *         the file COPYING for details.
+ * This file is part of InspIRCd.  InspIRCd 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 <http://www.gnu.org/licenses/>.
  */
 
+
 #include "inspircd.h"
 #include "xline.h"
 #include "commands/cmd_whowas.h"
@@ -19,7 +25,11 @@ void InspIRCd::DoStats(char statschar, User* user, string_list &results)
 {
        std::string sn(this->Config->ServerName);
 
-       if (!user->HasPrivPermission("servers/auspex") && !strchr(this->Config->UserStats, statschar))
+       bool isPublic = Config->UserStats.find(statschar) != std::string::npos;
+       bool isRemoteOper = IS_REMOTE(user) && IS_OPER(user);
+       bool isLocalOperWithPrivs = IS_LOCAL(user) && user->HasPrivPermission("servers/auspex");
+
+       if (!isPublic && !isRemoteOper && !isLocalOperWithPrivs)
        {
                this->SNO->WriteToSnoMask('t',
                                "%s '%c' denied for %s (%s@%s)",
@@ -30,7 +40,7 @@ void InspIRCd::DoStats(char statschar, User* user, string_list &results)
        }
 
        ModResult MOD_RESULT;
-       FIRST_MOD_RESULT(this, OnStats, MOD_RESULT, (statschar, user, results));
+       FIRST_MOD_RESULT(OnStats, MOD_RESULT, (statschar, user, results));
        if (MOD_RESULT == MOD_RES_DENY)
        {
                results.push_back(sn+" 219 "+user->nick+" "+statschar+" :End of /STATS report");
@@ -41,17 +51,19 @@ void InspIRCd::DoStats(char statschar, User* user, string_list &results)
 
        switch (statschar)
        {
-               /* stats p (show listening ports and registered clients on each) */
+               /* stats p (show listening ports) */
                case 'p':
                {
                        for (size_t i = 0; i < this->ports.size(); i++)
                        {
-                               std::string ip = this->ports[i]->GetIP();
+                               std::string ip = this->ports[i]->bind_addr;
                                if (ip.empty())
                                        ip.assign("*");
+                               std::string type = ports[i]->bind_tag->getString("type", "clients");
+                               std::string hook = ports[i]->bind_tag->getString("ssl", "plaintext");
 
-                               results.push_back(sn+" 249 "+user->nick+" :"+ ip + ":"+ConvToStr(this->ports[i]->GetPort())+" (client, " +
-                                               this->ports[i]->GetDescription() + ")");
+                               results.push_back(sn+" 249 "+user->nick+" :"+ ip + ":"+ConvToStr(ports[i]->bind_port)+
+                                       " (" + type + ", " + hook + ")");
                        }
                }
                break;
@@ -63,12 +75,28 @@ void InspIRCd::DoStats(char statschar, User* user, string_list &results)
 
                case 'i':
                {
-                       int idx = 0;
                        for (ClassVector::iterator i = this->Config->Classes.begin(); i != this->Config->Classes.end(); i++)
                        {
                                ConnectClass* c = *i;
-                               results.push_back(sn+" 215 "+user->nick+" i NOMATCH * "+c->GetHost()+" "+ConvToStr(c->limit ? c->limit : this->SE->GetMaxFds())+" "+ConvToStr(idx)+" "+this->Config->ServerName+" *");
-                               idx++;
+                               std::stringstream res;
+                               res << sn << " 215 " << user->nick << " I " << c->name << ' ';
+                               if (c->type == CC_ALLOW)
+                                       res << '+';
+                               if (c->type == CC_DENY)
+                                       res << '-';
+
+                               if (c->type == CC_NAMED)
+                                       res << '*';
+                               else
+                                       res << c->host;
+
+                               res << ' ' << c->config->getString("port", "*") << ' ';
+
+                               res << c->GetRecvqMax() << ' ' << c->GetSendqSoftMax() << ' ' << c->GetSendqHardMax()
+                                       << ' ' << c->GetCommandRate() << ' ' << c->GetPenaltyThreshold();
+                               if (c->fakelag)
+                                       res << '*';
+                               results.push_back(res.str());
                        }
                }
                break;
@@ -79,7 +107,8 @@ void InspIRCd::DoStats(char statschar, User* user, string_list &results)
                        for (ClassVector::iterator i = this->Config->Classes.begin(); i != this->Config->Classes.end(); i++)
                        {
                                ConnectClass* c = *i;
-                               results.push_back(sn+" 218 "+user->nick+" Y "+ConvToStr(idx)+" "+ConvToStr(c->GetPingTime())+" 0 "+ConvToStr(c->GetSendqMax())+" :"+
+                               results.push_back(sn+" 215 "+user->nick+" i NOMATCH * "+c->GetHost()+" "+ConvToStr(c->limit ? c->limit : this->SE->GetMaxFds())+" "+ConvToStr(idx)+" "+this->Config->ServerName+" *");
+                               results.push_back(sn+" 218 "+user->nick+" Y "+ConvToStr(idx)+" "+ConvToStr(c->GetPingTime())+" 0 "+ConvToStr(c->GetSendqHardMax())+" :"+
                                                ConvToStr(c->GetRecvqMax())+" "+ConvToStr(c->GetRegTimeout()));
                                idx++;
                        }
@@ -88,11 +117,9 @@ void InspIRCd::DoStats(char statschar, User* user, string_list &results)
 
                case 'U':
                {
-                       char ulined[MAXBUF];
-                       for (int i = 0; i < this->Config->ConfValueEnum("uline"); i++)
+                       for(std::map<irc::string, bool>::iterator i = Config->ulines.begin(); i != Config->ulines.end(); ++i)
                        {
-                               this->Config->ConfValue("uline","server", i, ulined, MAXBUF);
-                                       results.push_back(sn+" 248 "+user->nick+" U "+std::string(ulined));
+                               results.push_back(sn+" 248 "+user->nick+" U "+std::string(i->first.c_str()));
                        }
                }
                break;
@@ -100,7 +127,7 @@ void InspIRCd::DoStats(char statschar, User* user, string_list &results)
                case 'P':
                {
                        int idx = 0;
-                       for (user_hash::iterator i = this->Users->clientlist->begin(); i != this->Users->clientlist->end(); i++)
+                       for (user_hash::iterator i = this->Users->clientlist->begin(); i != this->Users->clientlist->end(); i++)
                        {
                                if (IS_OPER(i->second) && !this->ULine(i->second->server))
                                {
@@ -142,7 +169,7 @@ void InspIRCd::DoStats(char statschar, User* user, string_list &results)
                                if (i->second->use_count)
                                {
                                        /* RPL_STATSCOMMANDS */
-                                       results.push_back(sn+" 212 "+user->nick+" "+i->second->command+" "+ConvToStr(i->second->use_count)+" "+ConvToStr(i->second->total_bytes));
+                                       results.push_back(sn+" 212 "+user->nick+" "+i->second->name+" "+ConvToStr(i->second->use_count)+" "+ConvToStr(i->second->total_bytes));
                                }
                        }
                break;
@@ -150,7 +177,6 @@ void InspIRCd::DoStats(char statschar, User* user, string_list &results)
                /* stats z (debug and memory info) */
                case 'z':
                {
-                       results.push_back(sn+" 240 "+user->nick+" :InspIRCd: "+ConvToStr(sizeof(InspIRCd))+" bytes");
                        results.push_back(sn+" 249 "+user->nick+" :Users: "+ConvToStr(this->Users->clientlist->size()));
                        results.push_back(sn+" 249 "+user->nick+" :Channels: "+ConvToStr(this->chanlist->size()));
                        results.push_back(sn+" 249 "+user->nick+" :Commands: "+ConvToStr(this->Parser->cmdlist.size()));
@@ -167,8 +193,6 @@ void InspIRCd::DoStats(char statschar, User* user, string_list &results)
                                }
                        }
 
-                       results.push_back(sn+" 249 "+user->nick+" :MOTD "+ConvToStr(this->Config->MOTD.size())+", RULES "+ConvToStr(this->Config->RULES.size()));
-
                        float kbitpersec_in, kbitpersec_out, kbitpersec_total;
                        char kbitpersec_in_s[30], kbitpersec_out_s[30], kbitpersec_total_s[30];
 
@@ -197,16 +221,21 @@ void InspIRCd::DoStats(char statschar, User* user, string_list &results)
                                results.push_back(sn+" 249 "+user->nick+" :Swaps:            "+ConvToStr(R.ru_nswap));
                                results.push_back(sn+" 249 "+user->nick+" :Context Switches: Voluntary; "+ConvToStr(R.ru_nvcsw)+" Involuntary; "+ConvToStr(R.ru_nivcsw));
 
-                               timeval tv;
                                char percent[30];
-                               gettimeofday(&tv, NULL);
 
-                               float n_elapsed = ((tv.tv_sec - this->stats->LastSampled.tv_sec) * 1000000 + tv.tv_usec - this->stats->LastSampled.tv_usec);
+                               float n_elapsed = (ServerInstance->Time() - this->stats->LastSampled.tv_sec) * 1000000
+                                       + (ServerInstance->Time_ns() - this->stats->LastSampled.tv_nsec) / 1000;
                                float n_eaten = ((R.ru_utime.tv_sec - this->stats->LastCPU.tv_sec) * 1000000 + R.ru_utime.tv_usec - this->stats->LastCPU.tv_usec);
                                float per = (n_eaten / n_elapsed) * 100;
 
                                snprintf(percent, 30, "%03.5f%%", per);
-                               results.push_back(sn+" 249 "+user->nick+" :CPU Usage: "+percent);
+                               results.push_back(sn+" 249 "+user->nick+" :CPU Use (now):    "+percent);
+
+                               n_elapsed = ServerInstance->Time() - ServerInstance->startup_time;
+                               n_eaten = (float)R.ru_utime.tv_sec + R.ru_utime.tv_usec / 100000.0;
+                               per = (n_eaten / n_elapsed) * 100;
+                               snprintf(percent, 30, "%03.5f%%", per);
+                               results.push_back(sn+" 249 "+user->nick+" :CPU Use (total):  "+percent);
                        }
 #else
                        PROCESS_MEMORY_COUNTERS MemCounters;
@@ -229,42 +258,66 @@ void InspIRCd::DoStats(char statschar, User* user, string_list &results)
                        results.push_back(sn+" 249 "+user->nick+" :nick collisions "+ConvToStr(this->stats->statsCollisions));
                        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));
                        results.push_back(sn+" 249 "+user->nick+" :connection count "+ConvToStr(this->stats->statsConnects));
-                       snprintf(buffer,MAXBUF," 249 %s :bytes sent %5.2fK recv %5.2fK",user->nick.c_str(),this->stats->statsSent / 1024,this->stats->statsRecv / 1024);
+                       snprintf(buffer,MAXBUF," 249 %s :bytes sent %5.2fK recv %5.2fK",
+                               user->nick.c_str(),this->stats->statsSent / 1024.0,this->stats->statsRecv / 1024.0);
                        results.push_back(sn+buffer);
                }
                break;
 
                /* stats o */
                case 'o':
-                       for (int i = 0; i < this->Config->ConfValueEnum("oper"); i++)
+               {
+                       ConfigTagList tags = ServerInstance->Config->ConfTags("oper");
+                       for(ConfigIter i = tags.first; i != tags.second; ++i)
                        {
-                               char LoginName[MAXBUF];
-                               char HostName[MAXBUF];
-                               char OperType[MAXBUF];
-                               this->Config->ConfValue("oper","name", i, LoginName, MAXBUF);
-                               this->Config->ConfValue("oper","host", i, HostName, MAXBUF);
-                               this->Config->ConfValue("oper","type", i, OperType, MAXBUF);
-                               results.push_back(sn+" 243 "+user->nick+" O "+HostName+" * "+LoginName+" "+OperType+" 0");
+                               ConfigTag* tag = i->second;
+                               results.push_back(sn+" 243 "+user->nick+" O "+tag->getString("host")+" * "+
+                                       tag->getString("name") + " " + tag->getString("type")+" 0");
                        }
+               }
+               break;
+               case 'O':
+               {
+                       for(OperIndex::iterator i = ServerInstance->Config->oper_blocks.begin(); i != ServerInstance->Config->oper_blocks.end(); i++)
+                       {
+                               // just the types, not the actual oper blocks...
+                               if (i->first[0] != ' ')
+                                       continue;
+                               OperInfo* tag = i->second;
+                               tag->init();
+                               std::string umodes;
+                               std::string cmodes;
+                               for(char c='A'; c < 'z'; c++)
+                               {
+                                       ModeHandler* mh = ServerInstance->Modes->FindMode(c, MODETYPE_USER);
+                                       if (mh && mh->NeedsOper() && tag->AllowedUserModes[c - 'A'])
+                                               umodes.push_back(c);
+                                       mh = ServerInstance->Modes->FindMode(c, MODETYPE_CHANNEL);
+                                       if (mh && mh->NeedsOper() && tag->AllowedChanModes[c - 'A'])
+                                               cmodes.push_back(c);
+                               }
+                               results.push_back(sn+" 243 "+user->nick+" O "+tag->NameStr() + " " + umodes + " " + cmodes);
+                       }
+               }
                break;
 
                /* stats l (show user I/O stats) */
                case 'l':
                        results.push_back(sn+" 211 "+user->nick+" :nick[ident@host] sendq cmds_out bytes_out cmds_in bytes_in time_open");
-                       for (std::vector<User*>::iterator n = this->Users->local_users.begin(); n != this->Users->local_users.end(); n++)
+                       for (std::vector<LocalUser*>::iterator n = this->Users->local_users.begin(); n != this->Users->local_users.end(); n++)
                        {
-                               User* i = *n;
-                               results.push_back(sn+" 211 "+user->nick+" "+i->nick+"["+i->ident+"@"+i->dhost+"] "+ConvToStr(i->getSendQSize())+" "+ConvToStr(i->cmds_out)+" "+ConvToStr(i->bytes_out)+" "+ConvToStr(i->cmds_in)+" "+ConvToStr(i->bytes_in)+" "+ConvToStr(this->Time() - i->age));
+                               LocalUser* i = *n;
+                               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));
                        }
                break;
 
-       /* stats L (show user I/O stats with IP addresses) */
+               /* stats L (show user I/O stats with IP addresses) */
                case 'L':
                        results.push_back(sn+" 211 "+user->nick+" :nick[ident@ip] sendq cmds_out bytes_out cmds_in bytes_in time_open");
-                       for (std::vector<User*>::iterator n = this->Users->local_users.begin(); n != this->Users->local_users.end(); n++)
+                       for (std::vector<LocalUser*>::iterator n = this->Users->local_users.begin(); n != this->Users->local_users.end(); n++)
                        {
-                               User* i = *n;
-                               results.push_back(sn+" 211 "+user->nick+" "+i->nick+"["+i->ident+"@"+i->GetIPString()+"] "+ConvToStr(i->getSendQSize())+" "+ConvToStr(i->cmds_out)+" "+ConvToStr(i->bytes_out)+" "+ConvToStr(i->cmds_in)+" "+ConvToStr(i->bytes_in)+" "+ConvToStr(this->Time() - i->age));
+                               LocalUser* i = *n;
+                               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));
                        }
                break;