]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_httpd_stats.cpp
3339a9ca83a73027de9f653ac43d58cbc99ee143
[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 << "\">" << Sanitize(value) << "</meta>";
78                         else if (!item->key.empty())
79                                 data << "<meta name=\"" << item->key << "\"/>";
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>";
148                                                 DumpMeta(data, memb);
149                                                 data << "</channelmember>";
150                                         }
151
152                                         DumpMeta(data, c);
153
154                                         data << "</channel>";
155                                 }
156
157                                 data << "</channellist><userlist>";
158
159                                 for (user_hash::const_iterator a = ServerInstance->Users->clientlist->begin(); a != ServerInstance->Users->clientlist->end(); ++a)
160                                 {
161                                         User* u = a->second;
162
163                                         data << "<user>";
164                                         data << "<nickname>" << u->nick << "</nickname><uuid>" << u->uuid << "</uuid><realhost>"
165                                                 << u->host << "</realhost><displayhost>" << u->dhost << "</displayhost><gecos>"
166                                                 << Sanitize(u->fullname) << "</gecos><server>" << u->server << "</server>";
167                                         if (IS_AWAY(u))
168                                                 data << "<away>" << Sanitize(u->awaymsg) << "</away><awaytime>" << u->awaytime << "</awaytime>";
169                                         if (IS_OPER(u))
170                                                 data << "<opertype>" << Sanitize(u->oper) << "</opertype>";
171                                         data << "<modes>" << u->FormatModes() << "</modes><ident>" << Sanitize(u->ident) << "</ident>";
172                                         LocalUser* lu = IS_LOCAL(u);
173                                         if (lu)
174                                                 data << "<port>" << lu->GetServerPort() << "</port><servaddr>"
175                                                         << irc::sockets::satouser(&lu->server_sa) << "</servaddr>";
176                                         data << "<ipaddress>" << u->GetIPString() << "</ipaddress>";
177
178                                         DumpMeta(data, u);
179
180                                         data << "</user>";
181                                 }
182
183                                 data << "</userlist><serverlist>";
184
185                                 ProtoServerList sl;
186                                 ServerInstance->PI->GetServerList(sl);
187
188                                 for (ProtoServerList::iterator b = sl.begin(); b != sl.end(); ++b)
189                                 {
190                                         data << "<server>";
191                                         data << "<servername>" << b->servername << "</servername>";
192                                         data << "<parentname>" << b->parentname << "</parentname>";
193                                         data << "<gecos>" << b->gecos << "</gecos>";
194                                         data << "<usercount>" << b->usercount << "</usercount>";
195 // This is currently not implemented, so, commented out.
196 //                                      data << "<opercount>" << b->opercount << "</opercount>";
197                                         data << "<lagmillisecs>" << b->latencyms << "</lagmillisecs>";
198                                         data << "</server>";
199                                 }
200
201                                 data << "</serverlist>";
202
203                                 data << "</inspircdstats>";
204
205                                 /* Send the document back to m_httpd */
206                                 HTTPDocumentResponse response(this, *http, &data, 200);
207                                 response.headers.SetHeader("X-Powered-By", "m_httpd_stats.so");
208                                 response.headers.SetHeader("Content-Type", "text/xml");
209                                 response.Send();
210                         }
211                 }
212         }
213
214         virtual ~ModuleHttpStats()
215         {
216         }
217
218         virtual Version GetVersion()
219         {
220                 return Version("Provides statistics over HTTP via m_httpd.so", VF_VENDOR);
221         }
222 };
223
224 static std::map<char, char const*> const &init_entities()
225 {
226         static std::map<char, char const*> entities;
227         entities['<'] = "lt";
228         entities['>'] = "gt";
229         entities['&'] = "amp";
230         entities['"'] = "quot";
231         return entities;
232 }
233
234 std::map<char, char const*> const &ModuleHttpStats::entities = init_entities ();
235
236 MODULE_INIT(ModuleHttpStats)