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