]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/coremods/core_stats.cpp
Rename `<bind:ssl>` to `<bind:sslprofile>`.
[user/henk/code/inspircd.git] / src / coremods / core_stats.cpp
index 69f1f3cf89e384d16568cf608e1231781cf30911..19e429a95e92089924cc590e47ea39f69b791116 100644 (file)
@@ -1,9 +1,16 @@
 /*
  * InspIRCd -- Internet Relay Chat Daemon
  *
+ *   Copyright (C) 2018 Puck Meerburg <puck@puckipedia.com>
+ *   Copyright (C) 2018 Dylan Frank <b00mx0r@aureus.pw>
+ *   Copyright (C) 2016-2020 Sadie Powell <sadie@witchery.services>
+ *   Copyright (C) 2012-2016 Attila Molnar <attilamolnar@hush.com>
+ *   Copyright (C) 2012, 2019 Robby <robby@chatbelgie.be>
+ *   Copyright (C) 2012 ChrisTX <xpipe@hotmail.de>
+ *   Copyright (C) 2012 Adam <Adam@anope.org>
  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
- *   Copyright (C) 2007-2008 Craig Edwards <craigedwards@brainbox.cc>
- *   Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
+ *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
+ *   Copyright (C) 2006, 2008, 2010 Craig Edwards <brain@inspircd.org>
  *
  * 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
@@ -21,6 +28,7 @@
 
 #include "inspircd.h"
 #include "xline.h"
+#include "modules/stats.h"
 
 #ifdef _WIN32
 #include <psapi.h>
  */
 class CommandStats : public Command
 {
+       Events::ModuleEventProvider statsevprov;
        void DoStats(Stats::Context& stats);
+
  public:
-       /** Constructor for stats.
-        */
-       CommandStats ( Module* parent) : Command(parent,"STATS",1,2) { syntax = "<stats-symbol> [<servername>]"; }
+       /** STATS characters which non-opers can request. */
+       std::string userstats;
+
+       CommandStats(Module* Creator)
+               : Command(Creator, "STATS", 1, 2)
+               , statsevprov(Creator, "event/stats")
+       {
+               allow_empty_last_param = false;
+               syntax = "<symbol> [<servername>]";
+       }
+
        /** Handle command.
         * @param parameters The parameters to the command
         * @param user The user issuing the command
         * @return A value from CmdResult to indicate command success or failure.
         */
-       CmdResult Handle(const std::vector<std::string>& parameters, User *user);
-       RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
+       CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE;
+       RouteDescriptor GetRouting(User* user, const Params& parameters) CXX11_OVERRIDE
        {
-               if (parameters.size() > 1)
+               if ((parameters.size() > 1) && (parameters[1].find('.') != std::string::npos))
                        return ROUTE_UNICAST(parameters[1]);
                return ROUTE_LOCALONLY;
        }
@@ -58,7 +76,7 @@ static void GenerateStatsLl(Stats::Context& stats)
        for (UserManager::LocalList::const_iterator i = list.begin(); i != list.end(); ++i)
        {
                LocalUser* u = *i;
-               stats.AddRow(211, u->nick+"["+u->ident+"@"+(stats.GetSymbol() == 'l' ? u->dhost : u->GetIPString())+"] "+ConvToStr(u->eh.getSendQSize())+" "+ConvToStr(u->cmds_out)+" "+ConvToStr(u->bytes_out)+" "+ConvToStr(u->cmds_in)+" "+ConvToStr(u->bytes_in)+" "+ConvToStr(ServerInstance->Time() - u->signon));
+               stats.AddRow(211, u->nick+"["+u->ident+"@"+(stats.GetSymbol() == 'l' ? u->GetDisplayedHost() : u->GetIPString())+"] "+ConvToStr(u->eh.getSendQSize())+" "+ConvToStr(u->cmds_out)+" "+ConvToStr(u->bytes_out)+" "+ConvToStr(u->cmds_in)+" "+ConvToStr(u->bytes_in)+" "+ConvToStr(ServerInstance->Time() - u->signon));
        }
 }
 
@@ -67,7 +85,7 @@ void CommandStats::DoStats(Stats::Context& stats)
        User* const user = stats.GetSource();
        const char statschar = stats.GetSymbol();
 
-       bool isPublic = ServerInstance->Config->UserStats.find(statschar) != std::string::npos;
+       bool isPublic = userstats.find(statschar) != std::string::npos;
        bool isRemoteOper = IS_REMOTE(user) && (user->IsOper());
        bool isLocalOperWithPrivs = IS_LOCAL(user) && user->HasPrivPermission("servers/auspex");
 
@@ -76,18 +94,18 @@ void CommandStats::DoStats(Stats::Context& stats)
                ServerInstance->SNO->WriteToSnoMask('t',
                                "%s '%c' denied for %s (%s@%s)",
                                (IS_LOCAL(user) ? "Stats" : "Remote stats"),
-                               statschar, user->nick.c_str(), user->ident.c_str(), user->host.c_str());
+                               statschar, user->nick.c_str(), user->ident.c_str(), user->GetRealHost().c_str());
                stats.AddRow(481, (std::string("Permission Denied - STATS ") + statschar + " requires the servers/auspex priv."));
                return;
        }
 
        ModResult MOD_RESULT;
-       FIRST_MOD_RESULT(OnStats, MOD_RESULT, (stats));
+       FIRST_MOD_RESULT_CUSTOM(statsevprov, Stats::EventListener, OnStats, MOD_RESULT, (stats));
        if (MOD_RESULT == MOD_RES_DENY)
        {
                stats.AddRow(219, statschar, "End of /STATS report");
                ServerInstance->SNO->WriteToSnoMask('t',"%s '%c' requested by %s (%s@%s)",
-                       (IS_LOCAL(user) ? "Stats" : "Remote stats"), statschar, user->nick.c_str(), user->ident.c_str(), user->host.c_str());
+                       (IS_LOCAL(user) ? "Stats" : "Remote stats"), statschar, user->nick.c_str(), user->ident.c_str(), user->GetRealHost().c_str());
                return;
        }
 
@@ -99,18 +117,21 @@ void CommandStats::DoStats(Stats::Context& stats)
                        for (std::vector<ListenSocket*>::const_iterator i = ServerInstance->ports.begin(); i != ServerInstance->ports.end(); ++i)
                        {
                                ListenSocket* ls = *i;
-                               std::string ip = ls->bind_addr;
-                               if (ip.empty())
-                                       ip.assign("*");
-                               else if (ip.find_first_of(':') != std::string::npos)
-                               {
-                                       ip.insert(ip.begin(), '[');
-                                       ip.insert(ip.end(),  ']');
-                               }
-                               std::string type = ls->bind_tag->getString("type", "clients");
-                               std::string hook = ls->bind_tag->getString("ssl", "plaintext");
+                               std::stringstream portentry;
 
-                               stats.AddRow(249, ip + ":"+ConvToStr(ls->bind_port) + " (" + type + ", " + hook + ")");
+                               const std::string type = ls->bind_tag->getString("type", "clients", 1);
+                               portentry << ls->bind_sa.str() << " (type: " << type;
+
+                               const std::string hook = ls->bind_tag->getString("hook");
+                               if (!hook.empty())
+                                       portentry << ", hook: " << hook;
+
+                               const std::string sslprofile = ls->bind_tag->getString("sslprofile", ls->bind_tag->getString("ssl"));
+                               if (!sslprofile.empty())
+                                       portentry << ", ssl profile: " << sslprofile;
+
+                               portentry << ')';
+                               stats.AddRow(249, portentry.str());
                        }
                }
                break;
@@ -139,7 +160,7 @@ void CommandStats::DoStats(Stats::Context& stats)
                                else
                                        param.append(c->host);
 
-                               row.push(param).push(c->config->getString("port", "*"));
+                               row.push(param).push(c->config->getString("port", "*", 1));
                                row.push(ConvToStr(c->GetRecvqMax())).push(ConvToStr(c->GetSendqSoftMax())).push(ConvToStr(c->GetSendqHardMax())).push(ConvToStr(c->GetCommandRate()));
 
                                param = ConvToStr(c->GetPenaltyThreshold());
@@ -175,7 +196,7 @@ void CommandStats::DoStats(Stats::Context& stats)
                                if (!oper->server->IsULine())
                                {
                                        LocalUser* lu = IS_LOCAL(oper);
-                                       stats.AddRow(249, oper->nick + " (" + oper->ident + "@" + oper->dhost + ") Idle: " +
+                                       stats.AddRow(249, oper->nick + " (" + oper->ident + "@" + oper->GetDisplayedHost() + ") Idle: " +
                                                        (lu ? ConvToStr(ServerInstance->Time() - lu->idle_lastmsg) + " secs" : "unavailable"));
                                        idx++;
                                }
@@ -185,19 +206,19 @@ void CommandStats::DoStats(Stats::Context& stats)
                break;
 
                case 'k':
-                       ServerInstance->XLines->InvokeStats("K",216,stats);
+                       ServerInstance->XLines->InvokeStats("K", stats);
                break;
                case 'g':
-                       ServerInstance->XLines->InvokeStats("G",223,stats);
+                       ServerInstance->XLines->InvokeStats("G", stats);
                break;
                case 'q':
-                       ServerInstance->XLines->InvokeStats("Q",217,stats);
+                       ServerInstance->XLines->InvokeStats("Q", stats);
                break;
                case 'Z':
-                       ServerInstance->XLines->InvokeStats("Z",223,stats);
+                       ServerInstance->XLines->InvokeStats("Z", stats);
                break;
                case 'e':
-                       ServerInstance->XLines->InvokeStats("E",223,stats);
+                       ServerInstance->XLines->InvokeStats("E", stats);
                break;
                case 'E':
                {
@@ -232,17 +253,11 @@ void CommandStats::DoStats(Stats::Context& stats)
                        stats.AddRow(249, "Commands: "+ConvToStr(ServerInstance->Parser.GetCommands().size()));
 
                        float kbitpersec_in, kbitpersec_out, kbitpersec_total;
-                       char kbitpersec_in_s[30], kbitpersec_out_s[30], kbitpersec_total_s[30];
-
                        SocketEngine::GetStats().GetBandwidth(kbitpersec_in, kbitpersec_out, kbitpersec_total);
 
-                       snprintf(kbitpersec_total_s, 30, "%03.5f", kbitpersec_total);
-                       snprintf(kbitpersec_out_s, 30, "%03.5f", kbitpersec_out);
-                       snprintf(kbitpersec_in_s, 30, "%03.5f", kbitpersec_in);
-
-                       stats.AddRow(249, "Bandwidth total:  "+ConvToStr(kbitpersec_total_s)+" kilobits/sec");
-                       stats.AddRow(249, "Bandwidth out:    "+ConvToStr(kbitpersec_out_s)+" kilobits/sec");
-                       stats.AddRow(249, "Bandwidth in:     "+ConvToStr(kbitpersec_in_s)+" kilobits/sec");
+                       stats.AddRow(249, InspIRCd::Format("Bandwidth total:  %03.5f kilobits/sec", kbitpersec_total));
+                       stats.AddRow(249, InspIRCd::Format("Bandwidth out:    %03.5f kilobits/sec", kbitpersec_out));
+                       stats.AddRow(249, InspIRCd::Format("Bandwidth in:     %03.5f kilobits/sec", kbitpersec_in));
 
 #ifndef _WIN32
                        /* Moved this down here so all the not-windows stuff (look w00tie, I didn't say win32!) is in one ifndef.
@@ -253,27 +268,25 @@ void CommandStats::DoStats(Stats::Context& stats)
                        /* Not sure why we were doing '0' with a RUSAGE_SELF comment rather than just using RUSAGE_SELF -- Om */
                        if (!getrusage(RUSAGE_SELF,&R)) /* RUSAGE_SELF */
                        {
+#ifndef __HAIKU__
                                stats.AddRow(249, "Total allocation: "+ConvToStr(R.ru_maxrss)+"K");
                                stats.AddRow(249, "Signals:          "+ConvToStr(R.ru_nsignals));
                                stats.AddRow(249, "Page faults:      "+ConvToStr(R.ru_majflt));
                                stats.AddRow(249, "Swaps:            "+ConvToStr(R.ru_nswap));
                                stats.AddRow(249, "Context Switches: Voluntary; "+ConvToStr(R.ru_nvcsw)+" Involuntary; "+ConvToStr(R.ru_nivcsw));
-
-                               char percent[30];
-
+#endif
                                float n_elapsed = (ServerInstance->Time() - ServerInstance->stats.LastSampled.tv_sec) * 1000000
                                        + (ServerInstance->Time_ns() - ServerInstance->stats.LastSampled.tv_nsec) / 1000;
                                float n_eaten = ((R.ru_utime.tv_sec - ServerInstance->stats.LastCPU.tv_sec) * 1000000 + R.ru_utime.tv_usec - ServerInstance->stats.LastCPU.tv_usec);
                                float per = (n_eaten / n_elapsed) * 100;
 
-                               snprintf(percent, 30, "%03.5f%%", per);
-                               stats.AddRow(249, std::string("CPU Use (now):    ")+percent);
+                               stats.AddRow(249, InspIRCd::Format("CPU Use (now):    %03.5f%%", per));
 
                                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);
-                               stats.AddRow(249, std::string("CPU Use (total):  ")+percent);
+
+                               stats.AddRow(249, InspIRCd::Format("CPU Use (total):  %03.5f%%", per));
                        }
 #else
                        PROCESS_MEMORY_COUNTERS MemCounters;
@@ -298,16 +311,13 @@ void CommandStats::DoStats(Stats::Context& stats)
                                double n_elapsed = (double)(ThisSample.QuadPart - ServerInstance->stats.LastSampled.QuadPart) / ServerInstance->stats.QPFrequency.QuadPart;
                                double per = (n_eaten/n_elapsed);
 
-                               char percent[30];
-
-                               snprintf(percent, 30, "%03.5f%%", per);
-                               stats.AddRow(249, std::string("CPU Use (now):    ")+percent);
+                               stats.AddRow(249, InspIRCd::Format("CPU Use (now):    %03.5f%%", per));
 
                                n_elapsed = ServerInstance->Time() - ServerInstance->startup_time;
                                n_eaten = (double)(( (uint64_t)(KernelTime.dwHighDateTime) << 32 ) + (uint64_t)(KernelTime.dwLowDateTime))/100000;
                                per = (n_eaten / n_elapsed);
-                               snprintf(percent, 30, "%03.5f%%", per);
-                               stats.AddRow(249, std::string("CPU Use (total):  ")+percent));
+
+                               stats.AddRow(249, InspIRCd::Format("CPU Use (total):  %03.5f%%", per));
                        }
 #endif
                }
@@ -328,10 +338,10 @@ void CommandStats::DoStats(Stats::Context& stats)
                /* stats o */
                case 'o':
                {
-                       ConfigTagList tags = ServerInstance->Config->ConfTags("oper");
-                       for(ConfigIter i = tags.first; i != tags.second; ++i)
+                       for (ServerConfig::OperIndex::const_iterator i = ServerInstance->Config->oper_blocks.begin(); i != ServerInstance->Config->oper_blocks.end(); ++i)
                        {
-                               ConfigTag* tag = i->second;
+                               OperInfo* ifo = i->second;
+                               ConfigTag* tag = ifo->oper_block;
                                stats.AddRow(243, 'O', tag->getString("host"), '*', tag->getString("name"), tag->getString("type"), '0');
                        }
                }
@@ -380,13 +390,13 @@ void CommandStats::DoStats(Stats::Context& stats)
 
        stats.AddRow(219, statschar, "End of /STATS report");
        ServerInstance->SNO->WriteToSnoMask('t',"%s '%c' requested by %s (%s@%s)",
-               (IS_LOCAL(user) ? "Stats" : "Remote stats"), statschar, user->nick.c_str(), user->ident.c_str(), user->host.c_str());
+               (IS_LOCAL(user) ? "Stats" : "Remote stats"), statschar, user->nick.c_str(), user->ident.c_str(), user->GetRealHost().c_str());
        return;
 }
 
-CmdResult CommandStats::Handle (const std::vector<std::string>& parameters, User *user)
+CmdResult CommandStats::Handle(User* user, const Params& parameters)
 {
-       if (parameters.size() > 1 && parameters[1] != ServerInstance->Config->ServerName)
+       if (parameters.size() > 1 && !irc::equals(parameters[1], ServerInstance->Config->ServerName))
        {
                // Give extra penalty if a non-oper does /STATS <remoteserver>
                LocalUser* localuser = IS_LOCAL(user);
@@ -406,4 +416,27 @@ CmdResult CommandStats::Handle (const std::vector<std::string>& parameters, User
        return CMD_SUCCESS;
 }
 
-COMMAND_INIT(CommandStats)
+class CoreModStats : public Module
+{
+ private:
+       CommandStats cmd;
+
+ public:
+       CoreModStats()
+               : cmd(this)
+       {
+       }
+
+       void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
+       {
+               ConfigTag* security = ServerInstance->Config->ConfValue("security");
+               cmd.userstats = security->getString("userstats");
+       }
+
+       Version GetVersion() CXX11_OVERRIDE
+       {
+               return Version("Provides the STATS command", VF_CORE | VF_VENDOR);
+       }
+};
+
+MODULE_INIT(CoreModStats)