2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5 * Copyright (C) 2007-2008 Robin Burchell <robin+git@viroteck.net>
6 * Copyright (C) 2008 Pippijn van Steenhoven <pip88nl@gmail.com>
7 * Copyright (C) 2006-2008 Craig Edwards <craigedwards@brainbox.cc>
8 * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
10 * This file is part of InspIRCd. InspIRCd is free software: you can
11 * redistribute it and/or modify it under the terms of the GNU General Public
12 * License as published by the Free Software Foundation, version 2.
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
29 /* $ModDesc: Provides statistics over HTTP via m_httpd.so */
31 class ModuleHttpStats : public Module
33 static std::map<char, char const*> const &entities;
39 Implementation eventlist[] = { I_OnEvent };
40 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
43 std::string Sanitize(const std::string &str)
46 ret.reserve(str.length() * 2);
48 for (std::string::const_iterator x = str.begin(); x != str.end(); ++x)
50 std::map<char, char const*>::const_iterator it = entities.find(*x);
52 if (it != entities.end())
58 else if (*x == 0x9 || *x == 0xA || *x == 0xD ||
59 (*x >= 0x20 && *x <= 0xD7FF) || (*x >= 0xE000 && *x <= 0x10FFFF))
61 // The XML specification defines the following characters as valid inside an XML document:
62 // Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]
67 // If we reached this point then the string contains characters which can
68 // not be represented in XML, even using a numeric escape. Therefore, we
69 // Base64 encode the entire string and wrap it in a CDATA.
72 ret += BinToBase64(str);
80 void DumpMeta(std::stringstream& data, Extensible* ext)
83 for(Extensible::ExtensibleStore::const_iterator i = ext->GetExtList().begin(); i != ext->GetExtList().end(); i++)
85 ExtensionItem* item = i->first;
86 std::string value = item->serialize(FORMAT_USER, ext, i->second);
88 data << "<meta name=\"" << item->name << "\">" << Sanitize(value) << "</meta>";
89 else if (!item->name.empty())
90 data << "<meta name=\"" << item->name << "\"/>";
92 data << "</metadata>";
95 void OnEvent(Event& event)
97 std::stringstream data("");
99 if (event.id == "httpd_url")
101 ServerInstance->Logs->Log("m_http_stats", DEBUG,"Handling httpd event");
102 HTTPRequest* http = (HTTPRequest*)&event;
104 if ((http->GetURI() == "/stats") || (http->GetURI() == "/stats/"))
106 data << "<inspircdstats><server><name>" << ServerInstance->Config->ServerName << "</name><gecos>"
107 << Sanitize(ServerInstance->Config->ServerDesc) << "</gecos><version>"
108 << Sanitize(ServerInstance->GetVersionString()) << "</version></server>";
111 data << "<usercount>" << ServerInstance->Users->clientlist->size() << "</usercount>";
112 data << "<channelcount>" << ServerInstance->chanlist->size() << "</channelcount>";
113 data << "<opercount>" << ServerInstance->Users->all_opers.size() << "</opercount>";
114 data << "<socketcount>" << (ServerInstance->SE->GetUsedFds()) << "</socketcount><socketmax>" << ServerInstance->SE->GetMaxFds() << "</socketmax><socketengine>" << ServerInstance->SE->GetName() << "</socketengine>";
116 time_t current_time = 0;
117 current_time = ServerInstance->Time();
118 time_t server_uptime = current_time - ServerInstance->startup_time;
120 stime = gmtime(&server_uptime);
121 data << "<uptime><days>" << stime->tm_yday << "</days><hours>" << stime->tm_hour << "</hours><mins>" << stime->tm_min << "</mins><secs>" << stime->tm_sec << "</secs><boot_time_t>" << ServerInstance->startup_time << "</boot_time_t></uptime>";
123 data << "<isupport>" << Sanitize(ServerInstance->Config->data005) << "</isupport></general><xlines>";
124 std::vector<std::string> xltypes = ServerInstance->XLines->GetAllTypes();
125 for (std::vector<std::string>::iterator it = xltypes.begin(); it != xltypes.end(); ++it)
127 XLineLookup* lookup = ServerInstance->XLines->GetAll(*it);
131 for (LookupIter i = lookup->begin(); i != lookup->end(); ++i)
133 data << "<xline type=\"" << it->c_str() << "\"><mask>"
134 << Sanitize(i->second->Displayable()) << "</mask><settime>"
135 << i->second->set_time << "</settime><duration>" << i->second->duration
136 << "</duration><reason>" << Sanitize(i->second->reason)
137 << "</reason></xline>";
141 data << "</xlines><modulelist>";
142 std::vector<std::string> module_names = ServerInstance->Modules->GetAllModuleNames(0);
144 for (std::vector<std::string>::iterator i = module_names.begin(); i != module_names.end(); ++i)
146 Module* m = ServerInstance->Modules->Find(i->c_str());
147 Version v = m->GetVersion();
148 data << "<module><name>" << *i << "</name><description>" << Sanitize(v.description) << "</description></module>";
150 data << "</modulelist><channellist>";
152 for (chan_hash::const_iterator a = ServerInstance->chanlist->begin(); a != ServerInstance->chanlist->end(); ++a)
154 Channel* c = a->second;
157 data << "<usercount>" << c->GetUsers()->size() << "</usercount><channelname>" << Sanitize(c->name) << "</channelname>";
158 data << "<channeltopic>";
159 data << "<topictext>" << Sanitize(c->topic) << "</topictext>";
160 data << "<setby>" << Sanitize(c->setby) << "</setby>";
161 data << "<settime>" << c->topicset << "</settime>";
162 data << "</channeltopic>";
163 data << "<channelmodes>" << Sanitize(c->ChanModes(true)) << "</channelmodes>";
164 const UserMembList* ulist = c->GetUsers();
166 for (UserMembCIter x = ulist->begin(); x != ulist->end(); ++x)
168 Membership* memb = x->second;
169 data << "<channelmember><uid>" << memb->user->uuid << "</uid><privs>"
170 << Sanitize(c->GetAllPrefixChars(x->first)) << "</privs><modes>"
171 << memb->modes << "</modes>";
172 DumpMeta(data, memb);
173 data << "</channelmember>";
178 data << "</channel>";
181 data << "</channellist><userlist>";
183 for (user_hash::const_iterator a = ServerInstance->Users->clientlist->begin(); a != ServerInstance->Users->clientlist->end(); ++a)
188 data << "<nickname>" << u->nick << "</nickname><uuid>" << u->uuid << "</uuid><realhost>"
189 << u->host << "</realhost><displayhost>" << u->dhost << "</displayhost><gecos>"
190 << Sanitize(u->fullname) << "</gecos><server>" << u->server << "</server>";
192 data << "<away>" << Sanitize(u->awaymsg) << "</away><awaytime>" << u->awaytime << "</awaytime>";
194 data << "<opertype>" << Sanitize(u->oper->NameStr()) << "</opertype>";
195 data << "<modes>" << u->FormatModes() << "</modes><ident>" << Sanitize(u->ident) << "</ident>";
196 LocalUser* lu = IS_LOCAL(u);
198 data << "<port>" << lu->GetServerPort() << "</port><servaddr>"
199 << irc::sockets::satouser(lu->server_sa) << "</servaddr>";
200 data << "<ipaddress>" << u->GetIPString() << "</ipaddress>";
207 data << "</userlist><serverlist>";
210 ServerInstance->PI->GetServerList(sl);
212 for (ProtoServerList::iterator b = sl.begin(); b != sl.end(); ++b)
215 data << "<servername>" << b->servername << "</servername>";
216 data << "<parentname>" << b->parentname << "</parentname>";
217 data << "<gecos>" << b->gecos << "</gecos>";
218 data << "<usercount>" << b->usercount << "</usercount>";
219 // This is currently not implemented, so, commented out.
220 // data << "<opercount>" << b->opercount << "</opercount>";
221 data << "<lagmillisecs>" << b->latencyms << "</lagmillisecs>";
225 data << "</serverlist></inspircdstats>";
227 /* Send the document back to m_httpd */
228 HTTPDocumentResponse response(this, *http, &data, 200);
229 response.headers.SetHeader("X-Powered-By", "m_httpd_stats.so");
230 response.headers.SetHeader("Content-Type", "text/xml");
236 virtual ~ModuleHttpStats()
240 virtual Version GetVersion()
242 return Version("Provides statistics over HTTP via m_httpd.so", VF_VENDOR);
246 static std::map<char, char const*> const &init_entities()
248 static std::map<char, char const*> entities;
249 entities['<'] = "lt";
250 entities['>'] = "gt";
251 entities['&'] = "amp";
252 entities['"'] = "quot";
256 std::map<char, char const*> const &ModuleHttpStats::entities = init_entities ();
258 MODULE_INIT(ModuleHttpStats)