]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_httpd_stats.cpp
00cab319790fb769a56911d2dd14b1509a3164d9
[user/henk/code/inspircd.git] / src / modules / m_httpd_stats.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
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>
9  *
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.
13  *
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
17  * details.
18  *
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/>.
21  */
22
23
24 #include "inspircd.h"
25 #include "modules/httpd.h"
26 #include "xline.h"
27
28 class ModuleHttpStats : public Module, public HTTPRequestEventListener
29 {
30         static const insp::flat_map<char, char const*>& entities;
31         HTTPdAPI API;
32
33  public:
34         ModuleHttpStats()
35                 : HTTPRequestEventListener(this)
36                 , API(this)
37         {
38         }
39
40         std::string Sanitize(const std::string &str)
41         {
42                 std::string ret;
43                 ret.reserve(str.length() * 2);
44
45                 for (std::string::const_iterator x = str.begin(); x != str.end(); ++x)
46                 {
47                         insp::flat_map<char, char const*>::const_iterator it = entities.find(*x);
48
49                         if (it != entities.end())
50                         {
51                                 ret += '&';
52                                 ret += it->second;
53                                 ret += ';';
54                         }
55                         else if (*x == 0x09 ||  *x == 0x0A || *x == 0x0D || ((*x >= 0x20) && (*x <= 0x7e)))
56                         {
57                                 // The XML specification defines the following characters as valid inside an XML document:
58                                 // Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]
59                                 ret += *x;
60                         }
61                         else
62                         {
63                                 // If we reached this point then the string contains characters which can
64                                 // not be represented in XML, even using a numeric escape. Therefore, we
65                                 // Base64 encode the entire string and wrap it in a CDATA.
66                                 ret.clear();
67                                 ret += "<![CDATA[";
68                                 ret += BinToBase64(str);
69                                 ret += "]]>";
70                                 break;
71                         }
72                 }
73                 return ret;
74         }
75
76         void DumpMeta(std::stringstream& data, Extensible* ext)
77         {
78                 data << "<metadata>";
79                 for(Extensible::ExtensibleStore::const_iterator i = ext->GetExtList().begin(); i != ext->GetExtList().end(); i++)
80                 {
81                         ExtensionItem* item = i->first;
82                         std::string value = item->serialize(FORMAT_USER, ext, i->second);
83                         if (!value.empty())
84                                 data << "<meta name=\"" << item->name << "\">" << Sanitize(value) << "</meta>";
85                         else if (!item->name.empty())
86                                 data << "<meta name=\"" << item->name << "\"/>";
87                 }
88                 data << "</metadata>";
89         }
90
91         ModResult HandleRequest(HTTPRequest* http)
92         {
93                 std::stringstream data("");
94
95                 {
96                         ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Handling httpd event");
97
98                         if ((http->GetURI() == "/stats") || (http->GetURI() == "/stats/"))
99                         {
100                                 data << "<inspircdstats><server><name>" << ServerInstance->Config->ServerName << "</name><gecos>"
101                                         << Sanitize(ServerInstance->Config->ServerDesc) << "</gecos><version>"
102                                         << Sanitize(ServerInstance->GetVersionString()) << "</version></server>";
103
104                                 data << "<general>";
105                                 data << "<usercount>" << ServerInstance->Users->GetUsers().size() << "</usercount>";
106                                 data << "<channelcount>" << ServerInstance->GetChans().size() << "</channelcount>";
107                                 data << "<opercount>" << ServerInstance->Users->all_opers.size() << "</opercount>";
108                                 data << "<socketcount>" << (SocketEngine::GetUsedFds()) << "</socketcount><socketmax>" << SocketEngine::GetMaxFds() << "</socketmax>";
109                                 data << "<uptime><boot_time_t>" << ServerInstance->startup_time << "</boot_time_t></uptime>";
110
111                                 data << "<isupport>";
112                                 const std::vector<Numeric::Numeric>& isupport = ServerInstance->ISupport.GetLines();
113                                 for (std::vector<Numeric::Numeric>::const_iterator i = isupport.begin(); i != isupport.end(); ++i)
114                                 {
115                                         const Numeric::Numeric& num = *i;
116                                         for (std::vector<std::string>::const_iterator j = num.GetParams().begin(); j != num.GetParams().end()-1; ++j)
117                                                 data << "<token>" << Sanitize(*j) << "</token>" << std::endl;
118                                 }
119                                 data << "</isupport></general><xlines>";
120                                 std::vector<std::string> xltypes = ServerInstance->XLines->GetAllTypes();
121                                 for (std::vector<std::string>::iterator it = xltypes.begin(); it != xltypes.end(); ++it)
122                                 {
123                                         XLineLookup* lookup = ServerInstance->XLines->GetAll(*it);
124
125                                         if (!lookup)
126                                                 continue;
127                                         for (LookupIter i = lookup->begin(); i != lookup->end(); ++i)
128                                         {
129                                                 data << "<xline type=\"" << it->c_str() << "\"><mask>"
130                                                         << Sanitize(i->second->Displayable()) << "</mask><settime>"
131                                                         << i->second->set_time << "</settime><duration>" << i->second->duration
132                                                         << "</duration><reason>" << Sanitize(i->second->reason)
133                                                         << "</reason></xline>";
134                                         }
135                                 }
136
137                                 data << "</xlines><modulelist>";
138                                 const ModuleManager::ModuleMap& mods = ServerInstance->Modules->GetModules();
139
140                                 for (ModuleManager::ModuleMap::const_iterator i = mods.begin(); i != mods.end(); ++i)
141                                 {
142                                         Version v = i->second->GetVersion();
143                                         data << "<module><name>" << i->first << "</name><description>" << Sanitize(v.description) << "</description></module>";
144                                 }
145                                 data << "</modulelist><channellist>";
146
147                                 const chan_hash& chans = ServerInstance->GetChans();
148                                 for (chan_hash::const_iterator i = chans.begin(); i != chans.end(); ++i)
149                                 {
150                                         Channel* c = i->second;
151
152                                         data << "<channel>";
153                                         data << "<usercount>" << c->GetUsers().size() << "</usercount><channelname>" << Sanitize(c->name) << "</channelname>";
154                                         data << "<channeltopic>";
155                                         data << "<topictext>" << Sanitize(c->topic) << "</topictext>";
156                                         data << "<setby>" << Sanitize(c->setby) << "</setby>";
157                                         data << "<settime>" << c->topicset << "</settime>";
158                                         data << "</channeltopic>";
159                                         data << "<channelmodes>" << Sanitize(c->ChanModes(true)) << "</channelmodes>";
160
161                                         const Channel::MemberMap& ulist = c->GetUsers();
162                                         for (Channel::MemberMap::const_iterator x = ulist.begin(); x != ulist.end(); ++x)
163                                         {
164                                                 Membership* memb = x->second;
165                                                 data << "<channelmember><uid>" << memb->user->uuid << "</uid><privs>"
166                                                         << Sanitize(memb->GetAllPrefixChars()) << "</privs><modes>"
167                                                         << memb->modes << "</modes>";
168                                                 DumpMeta(data, memb);
169                                                 data << "</channelmember>";
170                                         }
171
172                                         DumpMeta(data, c);
173
174                                         data << "</channel>";
175                                 }
176
177                                 data << "</channellist><userlist>";
178
179                                 const user_hash& users = ServerInstance->Users->GetUsers();
180                                 for (user_hash::const_iterator i = users.begin(); i != users.end(); ++i)
181                                 {
182                                         User* u = i->second;
183
184                                         data << "<user>";
185                                         data << "<nickname>" << u->nick << "</nickname><uuid>" << u->uuid << "</uuid><realhost>"
186                                                 << u->GetRealHost() << "</realhost><displayhost>" << u->GetDisplayedHost() << "</displayhost><gecos>"
187                                                 << Sanitize(u->fullname) << "</gecos><server>" << u->server->GetName() << "</server>";
188                                         if (u->IsAway())
189                                                 data << "<away>" << Sanitize(u->awaymsg) << "</away><awaytime>" << u->awaytime << "</awaytime>";
190                                         if (u->IsOper())
191                                                 data << "<opertype>" << Sanitize(u->oper->name) << "</opertype>";
192                                         data << "<modes>" << u->GetModeLetters().substr(1) << "</modes><ident>" << Sanitize(u->ident) << "</ident>";
193                                         LocalUser* lu = IS_LOCAL(u);
194                                         if (lu)
195                                                 data << "<port>" << lu->GetServerPort() << "</port><servaddr>"
196                                                         << lu->server_sa.str() << "</servaddr>";
197                                         data << "<ipaddress>" << u->GetIPString() << "</ipaddress>";
198
199                                         DumpMeta(data, u);
200
201                                         data << "</user>";
202                                 }
203
204                                 data << "</userlist><serverlist>";
205
206                                 ProtocolInterface::ServerList sl;
207                                 ServerInstance->PI->GetServerList(sl);
208
209                                 for (ProtocolInterface::ServerList::const_iterator b = sl.begin(); b != sl.end(); ++b)
210                                 {
211                                         data << "<server>";
212                                         data << "<servername>" << b->servername << "</servername>";
213                                         data << "<parentname>" << b->parentname << "</parentname>";
214                                         data << "<gecos>" << Sanitize(b->gecos) << "</gecos>";
215                                         data << "<usercount>" << b->usercount << "</usercount>";
216 // This is currently not implemented, so, commented out.
217 //                                      data << "<opercount>" << b->opercount << "</opercount>";
218                                         data << "<lagmillisecs>" << b->latencyms << "</lagmillisecs>";
219                                         data << "</server>";
220                                 }
221
222                                 data << "</serverlist><commandlist>";
223
224                                 const CommandParser::CommandMap& commands = ServerInstance->Parser.GetCommands();
225                                 for (CommandParser::CommandMap::const_iterator i = commands.begin(); i != commands.end(); ++i)
226                                 {
227                                         data << "<command><name>" << i->second->name << "</name><usecount>" << i->second->use_count << "</usecount></command>";
228                                 }
229
230                                 data << "</commandlist></inspircdstats>";
231
232                                 /* Send the document back to m_httpd */
233                                 HTTPDocumentResponse response(this, *http, &data, 200);
234                                 response.headers.SetHeader("X-Powered-By", MODNAME);
235                                 response.headers.SetHeader("Content-Type", "text/xml");
236                                 API->SendResponse(response);
237                                 return MOD_RES_DENY; // Handled
238                         }
239                 }
240                 return MOD_RES_PASSTHRU;
241         }
242
243         ModResult OnHTTPRequest(HTTPRequest& req) CXX11_OVERRIDE
244         {
245                 return HandleRequest(&req);
246         }
247
248         Version GetVersion() CXX11_OVERRIDE
249         {
250                 return Version("Provides statistics over HTTP via m_httpd", VF_VENDOR);
251         }
252 };
253
254 static const insp::flat_map<char, char const*>& init_entities()
255 {
256         static insp::flat_map<char, char const*> entities;
257         entities['<'] = "lt";
258         entities['>'] = "gt";
259         entities['&'] = "amp";
260         entities['"'] = "quot";
261         return entities;
262 }
263
264 const insp::flat_map<char, char const*>& ModuleHttpStats::entities = init_entities();
265
266 MODULE_INIT(ModuleHttpStats)