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