]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_httpd_stats.cpp
Remove the intercomm system since sqlite is synchronous.
[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></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>";
115                                 data << "<modulelist>";
116                                 std::vector<std::string> module_names = ServerInstance->Modules->GetAllModuleNames(0);
117
118                                 for (std::vector<std::string>::iterator i = module_names.begin(); i != module_names.end(); ++i)
119                                 {
120                                         Module* m = ServerInstance->Modules->Find(i->c_str());
121                                         Version v = m->GetVersion();
122                                         data << "<module><name>" << *i << "</name><version>" << v.version << "</version><description>" << Sanitize(v.description) << "</description></module>";
123                                 }
124                                 data << "</modulelist>";
125                                 data << "<channellist>";
126
127                                 for (chan_hash::const_iterator a = ServerInstance->chanlist->begin(); a != ServerInstance->chanlist->end(); ++a)
128                                 {
129                                         Channel* c = a->second;
130
131                                         data << "<channel>";
132                                         data << "<usercount>" << c->GetUsers()->size() << "</usercount><channelname>" << c->name << "</channelname>";
133                                         data << "<channeltopic>";
134                                         data << "<topictext>" << Sanitize(c->topic) << "</topictext>";
135                                         data << "<setby>" << Sanitize(c->setby) << "</setby>";
136                                         data << "<settime>" << c->topicset << "</settime>";
137                                         data << "</channeltopic>";
138                                         data << "<channelmodes>" << Sanitize(c->ChanModes(true)) << "</channelmodes>";
139                                         const UserMembList* ulist = c->GetUsers();
140
141                                         for (UserMembCIter x = ulist->begin(); x != ulist->end(); ++x)
142                                         {
143                                                 Membership* memb = x->second;
144                                                 data << "<channelmember><uid>" << memb->user->uuid << "</uid><privs>"
145                                                         << Sanitize(c->GetAllPrefixChars(x->first)) << "</privs><modes>"
146                                                         << memb->modes << "</modes>";
147                                                 DumpMeta(data, memb);
148                                                 data << "</channelmember>";
149                                         }
150
151                                         DumpMeta(data, c);
152
153                                         data << "</channel>";
154                                 }
155
156                                 data << "</channellist><userlist>";
157
158                                 for (user_hash::const_iterator a = ServerInstance->Users->clientlist->begin(); a != ServerInstance->Users->clientlist->end(); ++a)
159                                 {
160                                         User* u = a->second;
161
162                                         data << "<user>";
163                                         data << "<nickname>" << u->nick << "</nickname><uuid>" << u->uuid << "</uuid><realhost>"
164                                                 << u->host << "</realhost><displayhost>" << u->dhost << "</displayhost><gecos>"
165                                                 << Sanitize(u->fullname) << "</gecos><server>" << u->server << "</server>";
166                                         if (IS_AWAY(u))
167                                                 data << "<away>" << Sanitize(u->awaymsg) << "</away><awaytime>" << u->awaytime << "</awaytime>";
168                                         if (IS_OPER(u))
169                                                 data << "<opertype>" << Sanitize(u->oper->NameStr()) << "</opertype>";
170                                         data << "<modes>" << u->FormatModes() << "</modes><ident>" << Sanitize(u->ident) << "</ident>";
171                                         LocalUser* lu = IS_LOCAL(u);
172                                         if (lu)
173                                                 data << "<port>" << lu->GetServerPort() << "</port><servaddr>"
174                                                         << irc::sockets::satouser(&lu->server_sa) << "</servaddr>";
175                                         data << "<ipaddress>" << u->GetIPString() << "</ipaddress>";
176
177                                         DumpMeta(data, u);
178
179                                         data << "</user>";
180                                 }
181
182                                 data << "</userlist><serverlist>";
183
184                                 ProtoServerList sl;
185                                 ServerInstance->PI->GetServerList(sl);
186
187                                 for (ProtoServerList::iterator b = sl.begin(); b != sl.end(); ++b)
188                                 {
189                                         data << "<server>";
190                                         data << "<servername>" << b->servername << "</servername>";
191                                         data << "<parentname>" << b->parentname << "</parentname>";
192                                         data << "<gecos>" << b->gecos << "</gecos>";
193                                         data << "<usercount>" << b->usercount << "</usercount>";
194 // This is currently not implemented, so, commented out.
195 //                                      data << "<opercount>" << b->opercount << "</opercount>";
196                                         data << "<lagmillisecs>" << b->latencyms << "</lagmillisecs>";
197                                         data << "</server>";
198                                 }
199
200                                 data << "</serverlist>";
201
202                                 data << "</inspircdstats>";
203
204                                 /* Send the document back to m_httpd */
205                                 HTTPDocumentResponse response(this, *http, &data, 200);
206                                 response.headers.SetHeader("X-Powered-By", "m_httpd_stats.so");
207                                 response.headers.SetHeader("Content-Type", "text/xml");
208                                 response.Send();
209                         }
210                 }
211         }
212
213         virtual ~ModuleHttpStats()
214         {
215         }
216
217         virtual Version GetVersion()
218         {
219                 return Version("Provides statistics over HTTP via m_httpd.so", VF_VENDOR);
220         }
221 };
222
223 static std::map<char, char const*> const &init_entities()
224 {
225         static std::map<char, char const*> entities;
226         entities['<'] = "lt";
227         entities['>'] = "gt";
228         entities['&'] = "amp";
229         entities['"'] = "quot";
230         return entities;
231 }
232
233 std::map<char, char const*> const &ModuleHttpStats::entities = init_entities ();
234
235 MODULE_INIT(ModuleHttpStats)