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