1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 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 /* $ModDesc: Provides statistics over HTTP via m_httpd.so */
18 /* $ModDep: httpd.h */
20 typedef std::map<irc::string,int> StatsHash;
21 typedef StatsHash::iterator StatsIter;
23 typedef std::vector<std::pair<int,irc::string> > SortedList;
24 typedef SortedList::iterator SortedIter;
26 static StatsHash* sh = new StatsHash();
27 static SortedList* so = new SortedList();
29 static StatsHash* Servers = new StatsHash();
31 class ModuleHttpStats : public Module
34 std::string stylesheet;
41 ConfigReader c(ServerInstance);
42 this->stylesheet = c.ReadValue("httpstats", "stylesheet", 0);
45 ModuleHttpStats(InspIRCd* Me) : Module(Me)
49 Implementation eventlist[] = { I_OnEvent, I_OnRequest, I_OnChannelDelete, I_OnUserJoin, I_OnUserPart, I_OnUserQuit };
50 ServerInstance->Modules->Attach(eventlist, this, 6);
53 void InsertOrder(irc::string channel, int count)
55 /* This function figures out where in the sorted list to put an item from the hash */
57 for (a = so->begin(); a != so->end(); a++)
59 /* Found an item equal to or less than, we insert our item before it */
60 if (a->first <= count)
62 so->insert(a,std::pair<int,irc::string>(count,channel));
66 /* There are no items in the list yet, insert something at the beginning */
67 so->insert(so->begin(), std::pair<int,irc::string>(count,channel));
72 /* Sorts the hash into the sorted list using an insertion sort */
74 for (StatsIter a = sh->begin(); a != sh->end(); a++)
75 InsertOrder(a->first, a->second);
76 for (user_hash::iterator u = ServerInstance->Users->clientlist->begin(); u != ServerInstance->Users->clientlist->end(); u++)
78 StatsHash::iterator n = Servers->find(u->second->server);
79 if (n != Servers->end())
85 Servers->insert(std::make_pair<irc::string,int>(u->second->server,1));
88 this->changed = false;
91 void OnEvent(Event* event)
93 std::stringstream data("");
95 if (event->GetEventID() == "httpd_url")
97 ServerInstance->Logs->Log("m_http_stats", DEBUG,"Handling httpd event");
98 HTTPRequest* http = (HTTPRequest*)event->GetData();
100 if ((http->GetURI() == "/stats") || (http->GetURI() == "/stats/"))
102 data << "<inspircdstats>";
104 data << "<server><name>" << ServerInstance->Config->ServerName << "</name><gecos>" << ServerInstance->Config->ServerDesc << "</gecos></server>";
107 data << "<usercount>" << ServerInstance->Users->clientlist->size() << "</usercount>";
108 data << "<channelcount>" << ServerInstance->chanlist->size() << "</channelcount>";
109 data << "<opercount>" << ServerInstance->Users->all_opers.size() << "</opercount>";
110 data << "<socketcount>" << (ServerInstance->SE->GetMaxFds() - ServerInstance->SE->GetRemainingFds()) << "</socketcount><socketmax>" << ServerInstance->SE->GetMaxFds() <<
111 "</socketmax><socketengine>" << ServerInstance->SE->GetName() << "</socketengine>";
113 time_t current_time = 0;
114 current_time = ServerInstance->Time();
115 time_t server_uptime = current_time - ServerInstance->startup_time;
117 stime = gmtime(&server_uptime);
118 data << "<uptime><days>" << stime->tm_yday << "</days><hours>" << stime->tm_hour << "</hours><mins>" << stime->tm_min << "</mins><secs>" << stime->tm_sec << "</secs></uptime>";
121 data << "</general>";
122 data << "<modulelist>";
123 std::vector<std::string> module_names = ServerInstance->Modules->GetAllModuleNames(0);
124 for (std::vector<std::string>::iterator i = module_names.begin(); i != module_names.end(); ++i)
126 Module* m = ServerInstance->Modules->Find(i->c_str());
127 Version v = m->GetVersion();
128 data << "<module><name>" << *i << "</name><version>" << v.Major << "." << v.Minor << "." << v.Revision << "." << v.Build << "</version></module>";
130 data << "</modulelist>";
132 data << "<channellist>";
133 /* If the list has changed since last time it was displayed, re-sort it
134 * this time only (not every time, as this would be moronic)
139 for (SortedIter a = so->begin(); a != so->end(); a++)
141 Channel* c = ServerInstance->FindChan(a->second.c_str());
142 if (c && !c->IsModeSet('s') && !c->IsModeSet('p'))
145 data << "<usercount>" << c->GetUsers()->size() << "</usercount><channelname>" << c->name << "</channelname>";
146 data << "<channelops>" << c->GetOppedUsers()->size() << "</channelops>";
147 data << "<channelhalfops>" << c->GetHalfoppedUsers()->size() << "</channelhalfops>";
148 data << "<channelvoices>" << c->GetVoicedUsers()->size() << "</channelvoices>";
149 data << "<channeltopic>" << c->topic << "</channeltopic>";
150 data << "<channelmodes>" << c->ChanModes(false) << "</channelmodes>";
151 data << "</channel>";
155 data << "</channellist>";
157 data << "<serverlist>";
159 for (StatsHash::iterator b = Servers->begin(); b != Servers->end(); b++)
162 data << "<servername>" << b->first << "</servername>";
163 data << "<usercount>" << b->second << "</usercount>";
166 data << "</serverlist>";
168 data << "</inspircdstats>";
170 /* Send the document back to m_httpd */
171 HTTPDocument response(http->sock, &data, 200);
172 response.headers.SetHeader("X-Powered-By", "m_httpd_stats.so");
173 response.headers.SetHeader("Content-Type", "text/xml");
174 Request req((char*)&response, (Module*)this, event->GetSource());
180 void OnChannelDelete(Channel* chan)
182 StatsIter a = sh->find(chan->name);
187 this->changed = true;
190 void OnUserJoin(User* user, Channel* channel, bool sync, bool &silent)
192 StatsIter a = sh->find(channel->name);
199 irc::string name = channel->name;
200 sh->insert(std::pair<irc::string,int>(name,1));
202 this->changed = true;
205 void OnUserPart(User* user, Channel* channel, const std::string &partmessage, bool &silent)
207 StatsIter a = sh->find(channel->name);
212 this->changed = true;
215 void OnUserQuit(User* user, const std::string &message, const std::string &oper_message)
217 for (UCListIter v = user->chans.begin(); v != user->chans.end(); v++)
219 Channel* c = v->first;
220 StatsIter a = sh->find(c->name);
226 this->changed = true;
229 const char* OnRequest(Request* request)
235 virtual ~ModuleHttpStats()
241 virtual Version GetVersion()
243 return Version(1, 2, 0, 0, VF_VENDOR, API_VERSION);
247 MODULE_INIT(ModuleHttpStats)