]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/m_httpd_stats.cpp
Add call to protocol interface to get useful info on the server map. Return a std...
[user/henk/code/inspircd.git] / src / modules / m_httpd_stats.cpp
index d8d71f78dcf7609fbaf9fbf46fe7a5b3b8e30539..aa64dc3eabc63783a30a82f9cf38f91632bacae7 100644 (file)
@@ -2,39 +2,21 @@
  *       | Inspire Internet Relay Chat Daemon |
  *       +------------------------------------+
  *
- *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
- *                    E-mail:
- *             <brain@chatspike.net>
- *               <Craig@chatspike.net>
- *     
- * Written by Craig Edwards, Craig McLure, and others.
+ *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
+ * See: http://www.inspircd.org/wiki/index.php/Credits
+ *
  * This program is free but copyrighted software; see
  *         the file COPYING for details.
  *
  * ---------------------------------------------------
  */
 
-using namespace std;
-
-#include <stdio.h>
-#include "users.h"
-#include "channels.h"
-#include "configreader.h"
-#include "modules.h"
-#include "inspsocket.h"
-#include "helperfuncs.h"
-#include "httpd.h"
 #include "inspircd.h"
+#include "httpd.h"
+#include "protocol.h"
 
 /* $ModDesc: Provides statistics over HTTP via m_httpd.so */
-
-extern user_hash clientlist;
-extern chan_hash chanlist;
-extern std::vector<userrec*> all_opers;
-extern InspIRCd* ServerInstance;
-extern ServerConfig* Config;
-
-extern int MODCOUNT;
+/* $ModDep: httpd.h */
 
 typedef std::map<irc::string,int> StatsHash;
 typedef StatsHash::iterator StatsIter;
@@ -47,7 +29,7 @@ static SortedList* so = new SortedList();
 
 class ModuleHttpStats : public Module
 {
-       Server* Srv;
+       
        std::string stylesheet;
        bool changed;
 
@@ -55,15 +37,16 @@ class ModuleHttpStats : public Module
 
        void ReadConfig()
        {
-               ConfigReader c;
+               ConfigReader c(ServerInstance);
                this->stylesheet = c.ReadValue("httpstats", "stylesheet", 0);
        }
 
-       ModuleHttpStats(Server* Me) : Module::Module(Me)
+       ModuleHttpStats(InspIRCd* Me) : Module(Me)
        {
-               Srv = Me;
                ReadConfig();
-               this->changed = false;
+               this->changed = true;
+               Implementation eventlist[] = { I_OnEvent, I_OnRequest, I_OnChannelDelete, I_OnUserJoin, I_OnUserPart, I_OnUserQuit };
+               ServerInstance->Modules->Attach(eventlist, this, 6);
        }
 
        void InsertOrder(irc::string channel, int count)
@@ -88,10 +71,7 @@ class ModuleHttpStats : public Module
                /* Sorts the hash into the sorted list using an insertion sort */
                so->clear();
                for (StatsIter a = sh->begin(); a != sh->end(); a++)
-               {
-                       log(DEBUG, "InsertOrder on %d %s",a->second,a->first.c_str());
                        InsertOrder(a->first, a->second);
-               }
                this->changed = false;
        }
 
@@ -101,73 +81,96 @@ class ModuleHttpStats : public Module
 
                if (event->GetEventID() == "httpd_url")
                {
+                       ServerInstance->Logs->Log("m_http_stats", DEBUG,"Handling httpd event");
                        HTTPRequest* http = (HTTPRequest*)event->GetData();
 
                        if ((http->GetURI() == "/stats") || (http->GetURI() == "/stats/"))
                        {
-                               log(DEBUG,"HTTP URL!");
-
-                               data << "<HTML><HEAD>";
-                               data << "<TITLE>InspIRCd server statisitics for " << Srv->GetServerName() << " (" << Srv->GetServerDescription() << ")</TITLE>";
-                               data << "</HEAD><BODY>";
-                               data << "<H1>InspIRCd server statisitics for " << Srv->GetServerName() << " (" << Srv->GetServerDescription() << ")</H1>";
-
-                               data << "<DIV ID='TOTALS'>";
-                               data << "<H2>Totals</H2>";
-                               data << "<TABLE>";
-                               data << "<TR><TD>Users</TD><TD>" << clientlist.size() << "</TD></TR>";
-                               data << "<TR><TD>Channels</TD><TD>" << chanlist.size() << "</TD></TR>";
-                               data << "<TR><TD>Opers</TD><TD>" << all_opers.size() << "</TD></TR>";
-                               data << "<TR><TD>Sockets</TD><TD>" << (ServerInstance->SE->GetMaxFds() - ServerInstance->SE->GetRemainingFds()) << " (Max: " << ServerInstance->SE->GetMaxFds() << " via socket engine '" << ServerInstance->SE->GetName() << "')</TD></TR>";
-                               data << "</TABLE>";
-                               data << "</DIV>";
-
-                               data << "<DIV ID='MODULES'>";
-                               data << "<H2>Modules</H2>";
-                               data << "<TABLE>";
-                               for (int i = 0; i <= MODCOUNT; i++)
+                               data << "<inspircdstats>";
+
+                               data << "<server><name>" << ServerInstance->Config->ServerName << "</name><gecos>" << ServerInstance->Config->ServerDesc << "</gecos></server>";
+
+                               data << "<general>";
+                               data << "<usercount>" << ServerInstance->Users->clientlist->size() << "</usercount>";
+                               data << "<channelcount>" << ServerInstance->chanlist->size() << "</channelcount>";
+                               data << "<opercount>" << ServerInstance->Users->all_opers.size() << "</opercount>";
+                               data << "<socketcount>" << (ServerInstance->SE->GetMaxFds() - ServerInstance->SE->GetRemainingFds()) << "</socketcount><socketmax>" << ServerInstance->SE->GetMaxFds() <<
+                                       "</socketmax><socketengine>" << ServerInstance->SE->GetName() << "</socketengine>";
+
+                               time_t current_time = 0;
+                               current_time = ServerInstance->Time();
+                               time_t server_uptime = current_time - ServerInstance->startup_time;
+                               struct tm* stime;
+                               stime = gmtime(&server_uptime);
+                               data << "<uptime><days>" << stime->tm_yday << "</days><hours>" << stime->tm_hour << "</hours><mins>" << stime->tm_min << "</mins><secs>" << stime->tm_sec << "</secs></uptime>";
+
+
+                               data << "</general>";
+                               data << "<modulelist>";
+                               std::vector<std::string> module_names = ServerInstance->Modules->GetAllModuleNames(0);
+                               for (std::vector<std::string>::iterator i = module_names.begin(); i != module_names.end(); ++i)
                                {
-                                       if (Config->module_names[i] != "")
-                                               data << "<TR><TD>" << Config->module_names[i] << "</TD></TR>";
+                                       Module* m = ServerInstance->Modules->Find(i->c_str());
+                                       Version v = m->GetVersion();
+                                       data << "<module><name>" << *i << "</name><version>" << v.Major << "." <<  v.Minor << "." << v.Revision << "." << v.Build << "</version></module>";
                                }
-                               data << "</TABLE>";
-                               data << "</DIV>";
-
-                               data << "<DIV ID='CHANNELS'>";
-                               data << "<H2>Channels</H2>";
-                               data << "<TABLE>";
-                               data << "<TR><TH>Users</TH><TH>Count</TH></TR>";
+                               data << "</modulelist>";
 
+                               data << "<channellist>";
                                /* If the list has changed since last time it was displayed, re-sort it
                                 * this time only (not every time, as this would be moronic)
                                 */
                                if (this->changed)
                                        this->SortList();
 
-                               int n = 0;
-                               for (SortedIter a = so->begin(); ((a != so->end()) && (n < 25)); a++, n++)
+                               for (SortedIter a = so->begin(); a != so->end(); a++)
                                {
-                                       data << "<TR><TD>" << a->first << "</TD><TD>" << a->second << "</TD></TR>";
+                                       Channel* c = ServerInstance->FindChan(a->second.c_str());
+                                       if (c && !c->IsModeSet('s') && !c->IsModeSet('p'))
+                                       {
+                                               data << "<channel>";
+                                               data << "<usercount>" << c->GetUsers()->size() << "</usercount><channelname>" << c->name << "</channelname>";
+                                               data << "<channelops>" << c->GetOppedUsers()->size() << "</channelops>";
+                                               data << "<channelhalfops>" << c->GetHalfoppedUsers()->size() << "</channelhalfops>";
+                                               data << "<channelvoices>" << c->GetVoicedUsers()->size() << "</channelvoices>";
+                                               data << "<channeltopic>" << c->topic << "</channeltopic>";
+                                               data << "<channelmodes>" << c->ChanModes(false) << "</channelmodes>";
+                                               data << "</channel>";
+                                       }
                                }
 
-                               data << "</TABLE>";
-                               data << "</DIV>";
+                               data << "</channellist>";
 
+                               data << "<serverlist>";
                                
-                               data << "</BODY>";
-                               data << "</HTML>";
+                               ProtoServerList sl;
+                               ServerInstance->PI->GetServerList(sl);
+
+                               for (ProtoServerList::iterator b = sl.begin(); b != sl.end(); ++b)
+                               {
+                                       data << "<server>";
+                                       data << "<servername>" << b->servername << "</servername>";
+                                       data << "<parentname>" << b->parentname << "</parentname>";
+                                       data << "<usercount>" << b->usercount << "</usercount>";
+                                       data << "<opercount>" << b->opercount << "</opercount>";
+                                       data << "<lagmillisecs>" << b->latencyms << "</lagmillisecs>";
+                                       data << "</server>";
+                               }
+                               data << "</serverlist>";
+
+                               data << "</inspircdstats>";
 
                                /* Send the document back to m_httpd */
-                               HTTPDocument response(http->sock, &data, 200, "X-Powered-By: m_http_stats.so\r\nContent-Type: text/html\r\n");
+                               HTTPDocument response(http->sock, &data, 200);
+                               response.headers.SetHeader("X-Powered-By", "m_httpd_stats.so");
+                               response.headers.SetHeader("Content-Type", "text/xml");
                                Request req((char*)&response, (Module*)this, event->GetSource());
                                req.Send();
-
-                               log(DEBUG,"Sent");
                        }
                }
        }
 
-       void OnChannelDelete(chanrec* chan)
+       void OnChannelDelete(Channel* chan)
        {
                StatsIter a = sh->find(chan->name);
                if (a != sh->end())
@@ -177,7 +180,7 @@ class ModuleHttpStats : public Module
                this->changed = true;
        }
 
-       void OnUserJoin(userrec* user, chanrec* channel)
+       void OnUserJoin(User* user, Channel* channel, bool sync, bool &silent)
        {
                StatsIter a = sh->find(channel->name);
                if (a != sh->end())
@@ -192,7 +195,7 @@ class ModuleHttpStats : public Module
                this->changed = true;
        }
 
-       void OnUserPart(userrec* user, chanrec* channel, const std::string &partmessage)
+       void OnUserPart(User* user, Channel* channel, const std::string &partmessage, bool &silent)
        {
                StatsIter a = sh->find(channel->name);
                if (a != sh->end())
@@ -202,64 +205,36 @@ class ModuleHttpStats : public Module
                this->changed = true;
        }
 
-       void OnUserQuit(userrec* user, const std::string &message)
+       void OnUserQuit(User* user, const std::string &message, const std::string &oper_message)
        {
-               for (std::vector<ucrec*>::const_iterator v = user->chans.begin(); v != user->chans.end(); v++)
+               for (UCListIter v = user->chans.begin(); v != user->chans.end(); v++)
                {
-                       if (((ucrec*)(*v))->channel)
+                       Channel* c = v->first;
+                       StatsIter a = sh->find(c->name);
+                       if (a != sh->end())
                        {
-                               chanrec* c = ((ucrec*)(*v))->channel;
-                               StatsIter a = sh->find(c->name);
-                               if (a != sh->end())
-                               {
-                                       a->second--;
-                               }
+                               a->second--;
                        }
                }
                this->changed = true;
        }
 
-       char* OnRequest(Request* request)
+       const char* OnRequest(Request* request)
        {
                return NULL;
        }
 
-       void Implements(char* List)
-       {
-               List[I_OnEvent] = List[I_OnRequest] = List[I_OnChannelDelete] = List[I_OnUserJoin] = List[I_OnUserPart] = List[I_OnUserQuit] = 1;
-       }
 
        virtual ~ModuleHttpStats()
        {
                delete sh;
+               delete so;
        }
 
        virtual Version GetVersion()
        {
-               return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
-       }
-};
-
-
-class ModuleHttpStatsFactory : public ModuleFactory
-{
- public:
-       ModuleHttpStatsFactory()
-       {
-       }
-       
-       ~ModuleHttpStatsFactory()
-       {
-       }
-       
-       virtual Module * CreateModule(Server* Me)
-       {
-               return new ModuleHttpStats(Me);
+               return Version(1, 2, 0, 0, VF_VENDOR, API_VERSION);
        }
 };
 
-
-extern "C" void * init_module( void )
-{
-       return new ModuleHttpStatsFactory;
-}
+MODULE_INIT(ModuleHttpStats)