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