1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
22 #include "configreader.h"
24 #include "inspsocket.h"
25 #include "helperfuncs.h"
29 /* $ModDesc: Provides statistics over HTTP via m_httpd.so */
31 extern user_hash clientlist;
32 extern chan_hash chanlist;
33 extern std::vector<userrec*> all_opers;
34 extern InspIRCd* ServerInstance;
35 extern ServerConfig* Config;
39 typedef std::map<irc::string,int> StatsHash;
40 typedef StatsHash::iterator StatsIter;
41 static StatsHash* sh = new StatsHash();
43 class ModuleHttpStats : public Module
46 std::string stylesheet;
53 this->stylesheet = c.ReadValue("httpstats", "stylesheet", 0);
56 ModuleHttpStats(Server* Me) : Module::Module(Me)
62 void OnEvent(Event* event)
64 std::stringstream data("");
66 if (event->GetEventID() == "httpd_url")
68 HTTPRequest* http = (HTTPRequest*)event->GetData();
70 if ((http->GetURI() == "/stats") || (http->GetURI() == "/stats/"))
72 log(DEBUG,"HTTP URL!");
74 data << "<HTML><HEAD>";
75 data << "<TITLE>InspIRCd server statisitics for " << Srv->GetServerName() << " (" << Srv->GetServerDescription() << ")</TITLE>";
76 data << "</HEAD><BODY>";
77 data << "<H1>InspIRCd server statisitics for " << Srv->GetServerName() << " (" << Srv->GetServerDescription() << ")</H1>";
79 data << "<DIV ID='TOTALS'>";
80 data << "<H2>Totals</H2>";
82 data << "<TR><TD>Users</TD><TD>" << clientlist.size() << "</TD></TR>";
83 data << "<TR><TD>Channels</TD><TD>" << chanlist.size() << "</TD></TR>";
84 data << "<TR><TD>Opers</TD><TD>" << all_opers.size() << "</TD></TR>";
85 data << "<TR><TD>Sockets</TD><TD>" << (ServerInstance->SE->GetMaxFds() - ServerInstance->SE->GetRemainingFds()) << " (Max: " << ServerInstance->SE->GetMaxFds() << " via socket engine '" << ServerInstance->SE->GetName() << "')</TD></TR>";
89 data << "<DIV ID='MODULES'>";
90 data << "<H2>Modules</H2>";
92 for (int i = 0; i <= MODCOUNT; i++)
94 if (Config->module_names[i] != "")
95 data << "<TR><TD>" << Config->module_names[i] << "</TD></TR>";
100 data << "<DIV ID='CHANNELS'>";
101 data << "<H2>Channels</H2>";
103 data << "<TR><TH>Users</TH><TH>Count</TH></TR>";
104 for (StatsIter a = sh->begin(); a != sh->end(); a++)
106 data << "<TR><TD>" << a->second << "</TD><TD>" << a->first << "</TD></TR>";
115 /* Send the document back to m_httpd */
116 HTTPDocument response(http->sock, &data, 200, "X-Powered-By: m_http_stats.so\r\nContent-Type: text/html\r\n");
117 Request req((char*)&response, (Module*)this, event->GetSource());
125 void OnChannelDelete(chanrec* chan)
127 StatsIter a = sh->find(chan->name);
134 void OnUserJoin(userrec* user, chanrec* channel)
136 StatsIter a = sh->find(channel->name);
143 a[channel->name] = 1;
147 void OnUserPart(userrec* user, chanrec* channel, const std::string &partmessage)
149 StatsIter a = sh->find(channel->name);
156 void OnUserQuit(userrec* user, const std::string &message)
158 for (std::vector<ucrec*>::const_iterator v = user->chans.begin(); v != user->chans.end(); v++)
160 if (((ucrec*)(*v))->channel)
162 chanrec* c = ((ucrec*)(*v))->channel;
163 StatsIter a = sh->find(c->name);
172 char* OnRequest(Request* request)
177 void Implements(char* List)
179 List[I_OnEvent] = List[I_OnRequest] = List[I_OnChannelDelete] = List[I_OnUserJoin] = List[I_OnUserPart] = List[I_OnUserQuit] = 1;
182 virtual ~ModuleHttpStats()
187 virtual Version GetVersion()
189 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
194 class ModuleHttpStatsFactory : public ModuleFactory
197 ModuleHttpStatsFactory()
201 ~ModuleHttpStatsFactory()
205 virtual Module * CreateModule(Server* Me)
207 return new ModuleHttpStats(Me);
212 extern "C" void * init_module( void )
214 return new ModuleHttpStatsFactory;