]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_httpd_stats.cpp
e94ee25049c95d22a55866dff14160210782c7a8
[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 #include "protocol.h"
28
29 class ModuleHttpStats : public Module
30 {
31         static std::map<char, char const*> const &entities;
32         HTTPdAPI API;
33
34  public:
35         ModuleHttpStats()
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                         std::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         void OnEvent(Event& event) CXX11_OVERRIDE
92         {
93                 std::stringstream data("");
94
95                 if (event.id == "httpd_url")
96                 {
97                         ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Handling httpd event");
98                         HTTPRequest* http = (HTTPRequest*)&event;
99
100                         if ((http->GetURI() == "/stats") || (http->GetURI() == "/stats/"))
101                         {
102                                 data << "<inspircdstats><server><name>" << ServerInstance->Config->ServerName << "</name><gecos>"
103                                         << Sanitize(ServerInstance->Config->ServerDesc) << "</gecos><version>"
104                                         << Sanitize(ServerInstance->GetVersionString()) << "</version></server>";
105
106                                 data << "<general>";
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->GetUsedFds()) << "</socketcount><socketmax>" << ServerInstance->SE->GetMaxFds() << "</socketmax><socketengine>" INSPIRCD_SOCKETENGINE_NAME "</socketengine>";
111
112                                 time_t current_time = 0;
113                                 current_time = ServerInstance->Time();
114                                 time_t server_uptime = current_time - ServerInstance->startup_time;
115                                 struct tm* stime;
116                                 stime = gmtime(&server_uptime);
117                                 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>";
118
119                                 data << "<isupport>";
120                                 const std::vector<std::string>& isupport = ServerInstance->ISupport.GetLines();
121                                 for (std::vector<std::string>::const_iterator it = isupport.begin(); it != isupport.end(); it++)
122                                 {
123                                         data << Sanitize(*it) << std::endl;
124                                 }
125                                 data << "</isupport></general><xlines>";
126                                 std::vector<std::string> xltypes = ServerInstance->XLines->GetAllTypes();
127                                 for (std::vector<std::string>::iterator it = xltypes.begin(); it != xltypes.end(); ++it)
128                                 {
129                                         XLineLookup* lookup = ServerInstance->XLines->GetAll(*it);
130
131                                         if (!lookup)
132                                                 continue;
133                                         for (LookupIter i = lookup->begin(); i != lookup->end(); ++i)
134                                         {
135                                                 data << "<xline type=\"" << it->c_str() << "\"><mask>"
136                                                         << Sanitize(i->second->Displayable()) << "</mask><settime>"
137                                                         << i->second->set_time << "</settime><duration>" << i->second->duration
138                                                         << "</duration><reason>" << Sanitize(i->second->reason)
139                                                         << "</reason></xline>";
140                                         }
141                                 }
142
143                                 data << "</xlines><modulelist>";
144                                 const ModuleManager::ModuleMap& mods = ServerInstance->Modules->GetModules();
145
146                                 for (ModuleManager::ModuleMap::const_iterator i = mods.begin(); i != mods.end(); ++i)
147                                 {
148                                         Version v = i->second->GetVersion();
149                                         data << "<module><name>" << i->first << "</name><description>" << Sanitize(v.description) << "</description></module>";
150                                 }
151                                 data << "</modulelist><channellist>";
152
153                                 for (chan_hash::const_iterator a = ServerInstance->chanlist->begin(); a != ServerInstance->chanlist->end(); ++a)
154                                 {
155                                         Channel* c = a->second;
156
157                                         data << "<channel>";
158                                         data << "<usercount>" << c->GetUsers()->size() << "</usercount><channelname>" << Sanitize(c->name) << "</channelname>";
159                                         data << "<channeltopic>";
160                                         data << "<topictext>" << Sanitize(c->topic) << "</topictext>";
161                                         data << "<setby>" << Sanitize(c->setby) << "</setby>";
162                                         data << "<settime>" << c->topicset << "</settime>";
163                                         data << "</channeltopic>";
164                                         data << "<channelmodes>" << Sanitize(c->ChanModes(true)) << "</channelmodes>";
165                                         const UserMembList* ulist = c->GetUsers();
166
167                                         for (UserMembCIter x = ulist->begin(); x != ulist->end(); ++x)
168                                         {
169                                                 Membership* memb = x->second;
170                                                 data << "<channelmember><uid>" << memb->user->uuid << "</uid><privs>"
171                                                         << Sanitize(c->GetAllPrefixChars(x->first)) << "</privs><modes>"
172                                                         << memb->modes << "</modes>";
173                                                 DumpMeta(data, memb);
174                                                 data << "</channelmember>";
175                                         }
176
177                                         DumpMeta(data, c);
178
179                                         data << "</channel>";
180                                 }
181
182                                 data << "</channellist><userlist>";
183
184                                 for (user_hash::const_iterator a = ServerInstance->Users->clientlist->begin(); a != ServerInstance->Users->clientlist->end(); ++a)
185                                 {
186                                         User* u = a->second;
187
188                                         data << "<user>";
189                                         data << "<nickname>" << u->nick << "</nickname><uuid>" << u->uuid << "</uuid><realhost>"
190                                                 << u->host << "</realhost><displayhost>" << u->dhost << "</displayhost><gecos>"
191                                                 << Sanitize(u->fullname) << "</gecos><server>" << u->server << "</server>";
192                                         if (u->IsAway())
193                                                 data << "<away>" << Sanitize(u->awaymsg) << "</away><awaytime>" << u->awaytime << "</awaytime>";
194                                         if (u->IsOper())
195                                                 data << "<opertype>" << Sanitize(u->oper->name) << "</opertype>";
196                                         data << "<modes>" << u->FormatModes() << "</modes><ident>" << Sanitize(u->ident) << "</ident>";
197                                         LocalUser* lu = IS_LOCAL(u);
198                                         if (lu)
199                                                 data << "<port>" << lu->GetServerPort() << "</port><servaddr>"
200                                                         << lu->server_sa.str() << "</servaddr>";
201                                         data << "<ipaddress>" << u->GetIPString() << "</ipaddress>";
202
203                                         DumpMeta(data, u);
204
205                                         data << "</user>";
206                                 }
207
208                                 data << "</userlist><serverlist>";
209
210                                 ProtocolInterface::ServerList sl;
211                                 ServerInstance->PI->GetServerList(sl);
212
213                                 for (ProtocolInterface::ServerList::const_iterator b = sl.begin(); b != sl.end(); ++b)
214                                 {
215                                         data << "<server>";
216                                         data << "<servername>" << b->servername << "</servername>";
217                                         data << "<parentname>" << b->parentname << "</parentname>";
218                                         data << "<gecos>" << b->gecos << "</gecos>";
219                                         data << "<usercount>" << b->usercount << "</usercount>";
220 // This is currently not implemented, so, commented out.
221 //                                      data << "<opercount>" << b->opercount << "</opercount>";
222                                         data << "<lagmillisecs>" << b->latencyms << "</lagmillisecs>";
223                                         data << "</server>";
224                                 }
225
226                                 data << "</serverlist></inspircdstats>";
227
228                                 /* Send the document back to m_httpd */
229                                 HTTPDocumentResponse response(this, *http, &data, 200);
230                                 response.headers.SetHeader("X-Powered-By", MODNAME);
231                                 response.headers.SetHeader("Content-Type", "text/xml");
232                                 API->SendResponse(response);
233                         }
234                 }
235         }
236
237         Version GetVersion() CXX11_OVERRIDE
238         {
239                 return Version("Provides statistics over HTTP via m_httpd.so", VF_VENDOR);
240         }
241 };
242
243 static std::map<char, char const*> const &init_entities()
244 {
245         static std::map<char, char const*> entities;
246         entities['<'] = "lt";
247         entities['>'] = "gt";
248         entities['&'] = "amp";
249         entities['"'] = "quot";
250         return entities;
251 }
252
253 std::map<char, char const*> const &ModuleHttpStats::entities = init_entities ();
254
255 MODULE_INIT(ModuleHttpStats)