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