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 */
23 /* $ModDep: httpd.h */
25 typedef std::map<irc::string,int> StatsHash;
26 typedef StatsHash::iterator StatsIter;
28 typedef std::vector<std::pair<int,irc::string> > SortedList;
29 typedef SortedList::iterator SortedIter;
31 static StatsHash* sh = new StatsHash();
32 static SortedList* so = new SortedList();
34 static StatsHash* Servers = new StatsHash();
36 class ModuleHttpStats : public Module
39 std::string stylesheet;
46 ConfigReader c(ServerInstance);
47 this->stylesheet = c.ReadValue("httpstats", "stylesheet", 0);
50 ModuleHttpStats(InspIRCd* Me) : Module(Me)
56 void InsertOrder(irc::string channel, int count)
58 /* This function figures out where in the sorted list to put an item from the hash */
60 for (a = so->begin(); a != so->end(); a++)
62 /* Found an item equal to or less than, we insert our item before it */
63 if (a->first <= count)
65 so->insert(a,std::pair<int,irc::string>(count,channel));
69 /* There are no items in the list yet, insert something at the beginning */
70 so->insert(so->begin(), std::pair<int,irc::string>(count,channel));
75 /* Sorts the hash into the sorted list using an insertion sort */
77 for (StatsIter a = sh->begin(); a != sh->end(); a++)
78 InsertOrder(a->first, a->second);
79 for (user_hash::iterator u = ServerInstance->clientlist->begin(); u != ServerInstance->clientlist->end(); u++)
81 StatsHash::iterator n = Servers->find(u->second->server);
82 if (n != Servers->end())
88 Servers->insert(std::make_pair<irc::string,int>(u->second->server,1));
91 this->changed = false;
94 void OnEvent(Event* event)
96 std::stringstream data("");
98 if (event->GetEventID() == "httpd_url")
100 HTTPRequest* http = (HTTPRequest*)event->GetData();
102 if ((http->GetURI() == "/stats") || (http->GetURI() == "/stats/"))
104 data << "<inspircdstats>";
106 data << "<server><name>" << ServerInstance->Config->ServerName << "</name><gecos>" << ServerInstance->Config->ServerDesc << "</gecos></server>";
109 data << "<usercount>" << ServerInstance->clientlist->size() << "</usercount>";
110 data << "<channelcount>" << ServerInstance->chanlist->size() << "</channelcount>";
111 data << "<opercount>" << ServerInstance->all_opers.size() << "</opercount>";
112 data << "<socketcount>" << (ServerInstance->SE->GetMaxFds() - ServerInstance->SE->GetRemainingFds()) << "</socketcount><socketmax>" << ServerInstance->SE->GetMaxFds() <<
113 "</socketmax><socketengine>" << ServerInstance->SE->GetName() << "</socketengine>";
115 time_t current_time = 0;
116 current_time = ServerInstance->Time();
117 time_t server_uptime = current_time - ServerInstance->startup_time;
119 stime = gmtime(&server_uptime);
120 data << "<uptime><days>" << stime->tm_yday << "</days><hours>" << stime->tm_hour << "</hours><mins>" << stime->tm_min << "</mins><secs>" << stime->tm_sec << "</secs></uptime>";
123 data << "</general>";
124 data << "<modulelist>";
125 for (int i = 0; i <= ServerInstance->GetModuleCount(); i++)
127 if (!ServerInstance->Config->module_names[i].empty())
129 Version v = ServerInstance->modules[i]->GetVersion();
130 data << "<module><name>" << ServerInstance->Config->module_names[i] << "</name><version>" <<
131 v.Major << "." << v.Minor << "." << v.Revision << "." << v.Build << "</version></module>";
134 data << "</modulelist>";
136 data << "<channellist>";
137 /* If the list has changed since last time it was displayed, re-sort it
138 * this time only (not every time, as this would be moronic)
143 for (SortedIter a = so->begin(); a != so->end(); a++)
145 chanrec* c = ServerInstance->FindChan(a->second.c_str());
146 if (c && !c->IsModeSet('s') && !c->IsModeSet('p'))
149 data << "<usercount>" << c->GetUsers()->size() << "</usercount><channelname>" << c->name << "</channelname>";
150 data << "<channelops>" << c->GetOppedUsers()->size() << "</channelops>";
151 data << "<channelhalfops>" << c->GetHalfoppedUsers()->size() << "</channelhalfops>";
152 data << "<channelvoices>" << c->GetVoicedUsers()->size() << "</channelvoices>";
153 data << "<channeltopic>" << c->topic << "</channeltopic>";
154 data << "<channelmodes>" << c->ChanModes(false) << "</channelmodes>";
155 data << "</channel>";
159 data << "</channellist>";
161 data << "<serverlist>";
163 for (StatsHash::iterator b = Servers->begin(); b != Servers->end(); b++)
166 data << "<servername>" << b->first << "</servername>";
167 data << "<usercount>" << b->second << "</usercount>";
170 data << "</serverlist>";
172 data << "</inspircdstats>";
174 /* Send the document back to m_httpd */
175 HTTPDocument response(http->sock, &data, 200);
176 response.headers.SetHeader("X-Powered-By", "m_httpd_stats.so");
177 response.headers.SetHeader("Content-Type", "text/xml");
178 Request req((char*)&response, (Module*)this, event->GetSource());
184 void OnChannelDelete(chanrec* chan)
186 StatsIter a = sh->find(chan->name);
191 this->changed = true;
194 void OnUserJoin(userrec* user, chanrec* channel, bool &silent)
196 StatsIter a = sh->find(channel->name);
203 irc::string name = channel->name;
204 sh->insert(std::pair<irc::string,int>(name,1));
206 this->changed = true;
209 void OnUserPart(userrec* user, chanrec* channel, const std::string &partmessage, bool &silent)
211 StatsIter a = sh->find(channel->name);
216 this->changed = true;
219 void OnUserQuit(userrec* user, const std::string &message, const std::string &oper_message)
221 for (UCListIter v = user->chans.begin(); v != user->chans.end(); v++)
223 chanrec* c = v->first;
224 StatsIter a = sh->find(c->name);
230 this->changed = true;
233 char* OnRequest(Request* request)
238 void Implements(char* List)
240 List[I_OnEvent] = List[I_OnRequest] = List[I_OnChannelDelete] = List[I_OnUserJoin] = List[I_OnUserPart] = List[I_OnUserQuit] = 1;
243 virtual ~ModuleHttpStats()
249 virtual Version GetVersion()
251 return Version(1, 1, 0, 0, VF_VENDOR, API_VERSION);
255 MODULE_INIT(ModuleHttpStats)