1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2007 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
17 #include "configreader.h"
19 #include "inspsocket.h"
22 /* $ModDesc: Provides statistics over HTTP via m_httpd.so */
24 typedef std::map<irc::string,int> StatsHash;
25 typedef StatsHash::iterator StatsIter;
27 typedef std::vector<std::pair<int,irc::string> > SortedList;
28 typedef SortedList::iterator SortedIter;
30 static StatsHash* sh = new StatsHash();
31 static SortedList* so = new SortedList();
33 static StatsHash* Servers = new StatsHash();
35 class ModuleHttpStats : public Module
38 std::string stylesheet;
45 ConfigReader c(ServerInstance);
46 this->stylesheet = c.ReadValue("httpstats", "stylesheet", 0);
49 ModuleHttpStats(InspIRCd* Me) : Module(Me)
55 void InsertOrder(irc::string channel, int count)
57 /* This function figures out where in the sorted list to put an item from the hash */
59 for (a = so->begin(); a != so->end(); a++)
61 /* Found an item equal to or less than, we insert our item before it */
62 if (a->first <= count)
64 so->insert(a,std::pair<int,irc::string>(count,channel));
68 /* There are no items in the list yet, insert something at the beginning */
69 so->insert(so->begin(), std::pair<int,irc::string>(count,channel));
74 /* Sorts the hash into the sorted list using an insertion sort */
76 for (StatsIter a = sh->begin(); a != sh->end(); a++)
77 InsertOrder(a->first, a->second);
78 for (user_hash::iterator u = ServerInstance->clientlist->begin(); u != ServerInstance->clientlist->end(); u++)
80 StatsHash::iterator n = Servers->find(u->second->server);
81 if (n != Servers->end())
87 Servers->insert(std::make_pair<irc::string,int>(u->second->server,1));
90 this->changed = false;
93 void OnEvent(Event* event)
95 std::stringstream data("");
97 if (event->GetEventID() == "httpd_url")
99 HTTPRequest* http = (HTTPRequest*)event->GetData();
101 if ((http->GetURI() == "/stats") || (http->GetURI() == "/stats/"))
103 data << "<inspircdstats>";
105 data << "<server><name>" << ServerInstance->Config->ServerName << "</name><gecos>" << ServerInstance->Config->ServerDesc << "</gecos></server>";
108 data << "<usercount>" << ServerInstance->clientlist->size() << "</usercount>";
109 data << "<channelcount>" << ServerInstance->chanlist->size() << "</channelcount>";
110 data << "<opercount>" << ServerInstance->all_opers.size() << "</opercount>";
111 data << "<socketcount>" << (ServerInstance->SE->GetMaxFds() - ServerInstance->SE->GetRemainingFds()) << "</socketcount><socketmax>" << ServerInstance->SE->GetMaxFds() <<
112 "</socketmax><socketengine>" << ServerInstance->SE->GetName() << "</socketengine>";
114 time_t current_time = 0;
115 current_time = ServerInstance->Time();
116 time_t server_uptime = current_time - ServerInstance->startup_time;
118 stime = gmtime(&server_uptime);
119 data << "<uptime><days>" << stime->tm_yday << "</days><hours>" << stime->tm_hour << "</hours><mins>" << stime->tm_min << "</mins><secs>" << stime->tm_sec << "</secs></uptime>";
122 data << "</general>";
123 data << "<modulelist>";
124 for (int i = 0; i <= ServerInstance->GetModuleCount(); i++)
126 if (!ServerInstance->Config->module_names[i].empty())
128 Version v = ServerInstance->modules[i]->GetVersion();
129 data << "<module><name>" << ServerInstance->Config->module_names[i] << "</name><version>" <<
130 v.Major << "." << v.Minor << "." << v.Revision << "." << v.Build << "</version></module>";
133 data << "</modulelist>";
135 data << "<channellist>";
136 /* If the list has changed since last time it was displayed, re-sort it
137 * this time only (not every time, as this would be moronic)
142 for (SortedIter a = so->begin(); a != so->end(); a++)
144 chanrec* c = ServerInstance->FindChan(a->second.c_str());
145 if (c && !c->IsModeSet('s') && !c->IsModeSet('p'))
148 data << "<usercount>" << c->GetUsers()->size() << "</usercount><channelname>" << c->name << "</channelname>";
149 data << "<channelops>" << c->GetOppedUsers()->size() << "</channelops>";
150 data << "<channelhalfops>" << c->GetHalfoppedUsers()->size() << "</channelhalfops>";
151 data << "<channelvoices>" << c->GetVoicedUsers()->size() << "</channelvoices>";
152 data << "<channeltopic>" << c->topic << "</channeltopic>";
153 data << "<channelmodes>" << c->ChanModes(false) << "</channelmodes>";
154 data << "</channel>";
158 data << "</channellist>";
160 data << "<serverlist>";
162 for (StatsHash::iterator b = Servers->begin(); b != Servers->end(); b++)
165 data << "<servername>" << b->first << "</servername>";
166 data << "<usercount>" << b->second << "</usercount>";
169 data << "</serverlist>";
171 data << "</inspircdstats>";
173 /* Send the document back to m_httpd */
174 HTTPDocument response(http->sock, &data, 200, "X-Powered-By: m_http_stats.so\r\nContent-Type: text/xml\r\n");
175 Request req((char*)&response, (Module*)this, event->GetSource());
181 void OnChannelDelete(chanrec* chan)
183 StatsIter a = sh->find(chan->name);
188 this->changed = true;
191 void OnUserJoin(userrec* user, chanrec* channel, bool &silent)
193 StatsIter a = sh->find(channel->name);
200 irc::string name = channel->name;
201 sh->insert(std::pair<irc::string,int>(name,1));
203 this->changed = true;
206 void OnUserPart(userrec* user, chanrec* channel, const std::string &partmessage, bool &silent)
208 StatsIter a = sh->find(channel->name);
213 this->changed = true;
216 void OnUserQuit(userrec* user, const std::string &message, const std::string &oper_message)
218 for (UCListIter v = user->chans.begin(); v != user->chans.end(); v++)
220 chanrec* c = v->first;
221 StatsIter a = sh->find(c->name);
227 this->changed = true;
230 char* OnRequest(Request* request)
235 void Implements(char* List)
237 List[I_OnEvent] = List[I_OnRequest] = List[I_OnChannelDelete] = List[I_OnUserJoin] = List[I_OnUserPart] = List[I_OnUserQuit] = 1;
240 virtual ~ModuleHttpStats()
246 virtual Version GetVersion()
248 return Version(1, 1, 0, 0, VF_VENDOR, API_VERSION);
252 MODULE_INIT(ModuleHttpStats)