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