]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_httpd_stats.cpp
541e080f56a7ad999f4c7632fe85f88f5dfe5533
[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><socketengine>" INSPIRCD_SOCKETENGINE_NAME "</socketengine>";
109                                 data << "<uptime><boot_time_t>" << ServerInstance->startup_time << "</boot_time_t></uptime>";
110
111                                 data << "<isupport>";
112                                 const std::vector<std::string>& isupport = ServerInstance->ISupport.GetLines();
113                                 for (std::vector<std::string>::const_iterator it = isupport.begin(); it != isupport.end(); it++)
114                                 {
115                                         data << Sanitize(*it) << std::endl;
116                                 }
117                                 data << "</isupport></general><xlines>";
118                                 std::vector<std::string> xltypes = ServerInstance->XLines->GetAllTypes();
119                                 for (std::vector<std::string>::iterator it = xltypes.begin(); it != xltypes.end(); ++it)
120                                 {
121                                         XLineLookup* lookup = ServerInstance->XLines->GetAll(*it);
122
123                                         if (!lookup)
124                                                 continue;
125                                         for (LookupIter i = lookup->begin(); i != lookup->end(); ++i)
126                                         {
127                                                 data << "<xline type=\"" << it->c_str() << "\"><mask>"
128                                                         << Sanitize(i->second->Displayable()) << "</mask><settime>"
129                                                         << i->second->set_time << "</settime><duration>" << i->second->duration
130                                                         << "</duration><reason>" << Sanitize(i->second->reason)
131                                                         << "</reason></xline>";
132                                         }
133                                 }
134
135                                 data << "</xlines><modulelist>";
136                                 const ModuleManager::ModuleMap& mods = ServerInstance->Modules->GetModules();
137
138                                 for (ModuleManager::ModuleMap::const_iterator i = mods.begin(); i != mods.end(); ++i)
139                                 {
140                                         Version v = i->second->GetVersion();
141                                         data << "<module><name>" << i->first << "</name><description>" << Sanitize(v.description) << "</description></module>";
142                                 }
143                                 data << "</modulelist><channellist>";
144
145                                 const chan_hash& chans = ServerInstance->GetChans();
146                                 for (chan_hash::const_iterator i = chans.begin(); i != chans.end(); ++i)
147                                 {
148                                         Channel* c = i->second;
149
150                                         data << "<channel>";
151                                         data << "<usercount>" << c->GetUsers().size() << "</usercount><channelname>" << Sanitize(c->name) << "</channelname>";
152                                         data << "<channeltopic>";
153                                         data << "<topictext>" << Sanitize(c->topic) << "</topictext>";
154                                         data << "<setby>" << Sanitize(c->setby) << "</setby>";
155                                         data << "<settime>" << c->topicset << "</settime>";
156                                         data << "</channeltopic>";
157                                         data << "<channelmodes>" << Sanitize(c->ChanModes(true)) << "</channelmodes>";
158
159                                         const Channel::MemberMap& ulist = c->GetUsers();
160                                         for (Channel::MemberMap::const_iterator x = ulist.begin(); x != ulist.end(); ++x)
161                                         {
162                                                 Membership* memb = x->second;
163                                                 data << "<channelmember><uid>" << memb->user->uuid << "</uid><privs>"
164                                                         << Sanitize(memb->GetAllPrefixChars()) << "</privs><modes>"
165                                                         << memb->modes << "</modes>";
166                                                 DumpMeta(data, memb);
167                                                 data << "</channelmember>";
168                                         }
169
170                                         DumpMeta(data, c);
171
172                                         data << "</channel>";
173                                 }
174
175                                 data << "</channellist><userlist>";
176
177                                 const user_hash& users = ServerInstance->Users->GetUsers();
178                                 for (user_hash::const_iterator i = users.begin(); i != users.end(); ++i)
179                                 {
180                                         User* u = i->second;
181
182                                         data << "<user>";
183                                         data << "<nickname>" << u->nick << "</nickname><uuid>" << u->uuid << "</uuid><realhost>"
184                                                 << u->host << "</realhost><displayhost>" << u->dhost << "</displayhost><gecos>"
185                                                 << Sanitize(u->fullname) << "</gecos><server>" << u->server->GetName() << "</server>";
186                                         if (u->IsAway())
187                                                 data << "<away>" << Sanitize(u->awaymsg) << "</away><awaytime>" << u->awaytime << "</awaytime>";
188                                         if (u->IsOper())
189                                                 data << "<opertype>" << Sanitize(u->oper->name) << "</opertype>";
190                                         data << "<modes>" << u->FormatModes() << "</modes><ident>" << Sanitize(u->ident) << "</ident>";
191                                         LocalUser* lu = IS_LOCAL(u);
192                                         if (lu)
193                                                 data << "<port>" << lu->GetServerPort() << "</port><servaddr>"
194                                                         << lu->server_sa.str() << "</servaddr>";
195                                         data << "<ipaddress>" << u->GetIPString() << "</ipaddress>";
196
197                                         DumpMeta(data, u);
198
199                                         data << "</user>";
200                                 }
201
202                                 data << "</userlist><serverlist>";
203
204                                 ProtocolInterface::ServerList sl;
205                                 ServerInstance->PI->GetServerList(sl);
206
207                                 for (ProtocolInterface::ServerList::const_iterator b = sl.begin(); b != sl.end(); ++b)
208                                 {
209                                         data << "<server>";
210                                         data << "<servername>" << b->servername << "</servername>";
211                                         data << "<parentname>" << b->parentname << "</parentname>";
212                                         data << "<gecos>" << b->gecos << "</gecos>";
213                                         data << "<usercount>" << b->usercount << "</usercount>";
214 // This is currently not implemented, so, commented out.
215 //                                      data << "<opercount>" << b->opercount << "</opercount>";
216                                         data << "<lagmillisecs>" << b->latencyms << "</lagmillisecs>";
217                                         data << "</server>";
218                                 }
219
220                                 data << "</serverlist><commandlist>";
221
222                                 const CommandParser::CommandMap& commands = ServerInstance->Parser.GetCommands();
223                                 for (CommandParser::CommandMap::const_iterator i = commands.begin(); i != commands.end(); ++i)
224                                 {
225                                         data << "<command><name>" << i->second->name << "</name><usecount>" << i->second->use_count << "</usecount></command>";
226                                 }
227
228                                 data << "</commandlist></inspircdstats>";
229
230                                 /* Send the document back to m_httpd */
231                                 HTTPDocumentResponse response(this, *http, &data, 200);
232                                 response.headers.SetHeader("X-Powered-By", MODNAME);
233                                 response.headers.SetHeader("Content-Type", "text/xml");
234                                 API->SendResponse(response);
235                                 return MOD_RES_DENY; // Handled
236                         }
237                 }
238                 return MOD_RES_PASSTHRU;
239         }
240
241         ModResult OnHTTPRequest(HTTPRequest& req) CXX11_OVERRIDE
242         {
243                 return HandleRequest(&req);
244         }
245
246         Version GetVersion() CXX11_OVERRIDE
247         {
248                 return Version("Provides statistics over HTTP via m_httpd.so", VF_VENDOR);
249         }
250 };
251
252 static const insp::flat_map<char, char const*>& init_entities()
253 {
254         static insp::flat_map<char, char const*> entities;
255         entities['<'] = "lt";
256         entities['>'] = "gt";
257         entities['&'] = "amp";
258         entities['"'] = "quot";
259         return entities;
260 }
261
262 const insp::flat_map<char, char const*>& ModuleHttpStats::entities = init_entities();
263
264 MODULE_INIT(ModuleHttpStats)