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