]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_httpd_stats.cpp
01d77d806c3da39987f7a5a914fe15954c245d0f
[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 /* $ModDep: httpd.h */
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(ServerInstance);
32                 this->stylesheet = c.ReadValue("httpstats", "stylesheet", 0);
33         }
34
35         ModuleHttpStats(InspIRCd* Me) : Module(Me)
36         {
37                 ReadConfig();
38                 this->changed = true;
39                 Implementation eventlist[] = { I_OnEvent, I_OnRequest };
40                 ServerInstance->Modules->Attach(eventlist, this, 2);
41         }
42
43         std::string Sanitize(const std::string &str)
44         {
45                 std::string ret;
46                 ret.reserve(str.length() * 2);
47
48                 for (std::string::const_iterator x = str.begin(); x != str.end(); ++x)
49                 {
50                         std::map<char, char const*>::const_iterator it = entities.find(*x);
51
52                         if (it != entities.end())
53                         {
54                                 ret += '&';
55                                 ret += it->second;
56                                 ret += ';';
57                         }
58                         else if (*x < 32 || *x > 126)
59                         {
60                                 int n = (unsigned char)*x;
61                                 ret += ("&#" + ConvToStr(n) + ";");
62                         }
63                         else
64                         {
65                                 ret += *x;
66                         }
67                 }
68                 return ret;
69         }
70
71         void OnEvent(Event* event)
72         {
73                 std::stringstream data("");
74
75                 if (event->GetEventID() == "httpd_url")
76                 {
77                         ServerInstance->Logs->Log("m_http_stats", DEBUG,"Handling httpd event");
78                         HTTPRequest* http = (HTTPRequest*)event->GetData();
79
80                         if ((http->GetURI() == "/stats") || (http->GetURI() == "/stats/"))
81                         {
82                                 data << "<inspircdstats>";
83
84                                 data << "<server><name>" << ServerInstance->Config->ServerName << "</name><gecos>" << Sanitize(ServerInstance->Config->ServerDesc) << "</gecos></server>";
85
86                                 data << "<general>";
87                                 data << "<usercount>" << ServerInstance->Users->clientlist->size() << "</usercount>";
88                                 data << "<channelcount>" << ServerInstance->chanlist->size() << "</channelcount>";
89                                 data << "<opercount>" << ServerInstance->Users->all_opers.size() << "</opercount>";
90                                 data << "<socketcount>" << (ServerInstance->SE->GetUsedFds()) << "</socketcount><socketmax>" << ServerInstance->SE->GetMaxFds() << "</socketmax><socketengine>" << ServerInstance->SE->GetName() << "</socketengine>";
91
92                                 time_t current_time = 0;
93                                 current_time = ServerInstance->Time();
94                                 time_t server_uptime = current_time - ServerInstance->startup_time;
95                                 struct tm* stime;
96                                 stime = gmtime(&server_uptime);
97                                 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>";
98
99
100                                 data << "</general>";
101                                 data << "<modulelist>";
102                                 std::vector<std::string> module_names = ServerInstance->Modules->GetAllModuleNames(0);
103
104                                 for (std::vector<std::string>::iterator i = module_names.begin(); i != module_names.end(); ++i)
105                                 {
106                                         Module* m = ServerInstance->Modules->Find(i->c_str());
107                                         Version v = m->GetVersion();
108                                         data << "<module><name>" << *i << "</name><version>" << v.version << "</version></module>";
109                                 }
110                                 data << "</modulelist>";
111                                 data << "<channellist>";
112
113                                 for (chan_hash::const_iterator a = ServerInstance->chanlist->begin(); a != ServerInstance->chanlist->end(); ++a)
114                                 {
115                                         Channel* c = a->second;
116
117                                         data << "<channel>";
118                                         data << "<usercount>" << c->GetUsers()->size() << "</usercount><channelname>" << c->name << "</channelname>";
119                                         data << "<channeltopic>";
120                                         data << "<topictext>" << Sanitize(c->topic) << "</topictext>";
121                                         data << "<setby>" << Sanitize(c->setby) << "</setby>";
122                                         data << "<settime>" << c->topicset << "</settime>";
123                                         data << "</channeltopic>";
124                                         data << "<channelmodes>" << Sanitize(c->ChanModes(true)) << "</channelmodes>";
125                                         const UserMembList* ulist = c->GetUsers();
126
127                                         for (UserMembCIter x = ulist->begin(); x != ulist->end(); ++x)
128                                         {
129                                                 data << "<channelmember><uid>" << x->first->uuid << "</uid><privs>" << Sanitize(c->GetAllPrefixChars(x->first)) << "</privs></channelmember>";
130                                         }
131
132                                         data << "</channel>";
133                                 }
134
135                                 data << "</channellist><userlist>";
136
137                                 for (user_hash::const_iterator a = ServerInstance->Users->clientlist->begin(); a != ServerInstance->Users->clientlist->end(); ++a)
138                                 {
139                                         User* u = a->second;
140
141                                         data << "<user>";
142                                         data << "<nickname>" << u->nick << "</nickname><uuid>" << u->uuid << "</uuid><realhost>" << u->host << "</realhost><displayhost>" << u->dhost << "</displayhost>";
143                                         data << "<gecos>" << Sanitize(u->fullname) << "</gecos><server>" << u->server << "</server><away>" << Sanitize(u->awaymsg) << "</away><opertype>" << Sanitize(u->oper) << "</opertype><modes>";
144                                         std::string modes;
145                                         for (unsigned char n = 'A'; n <= 'z'; ++n)
146                                                 if (u->IsModeSet(n))
147                                                         modes += n;
148
149                                         data << modes << "</modes><ident>" << Sanitize(u->ident) << "</ident><port>" << u->GetServerPort() << "</port><ipaddress>" << u->GetIPString() << "</ipaddress>";
150                                         data << "</user>";
151                                 }
152
153                                 data << "</userlist><serverlist>";
154
155                                 ProtoServerList sl;
156                                 ServerInstance->PI->GetServerList(sl);
157
158                                 for (ProtoServerList::iterator b = sl.begin(); b != sl.end(); ++b)
159                                 {
160                                         data << "<server>";
161                                         data << "<servername>" << b->servername << "</servername>";
162                                         data << "<parentname>" << b->parentname << "</parentname>";
163                                         data << "<gecos>" << b->gecos << "</gecos>";
164                                         data << "<usercount>" << b->usercount << "</usercount>";
165 // This is currently not implemented, so, commented out.
166 //                                      data << "<opercount>" << b->opercount << "</opercount>";
167                                         data << "<lagmillisecs>" << b->latencyms << "</lagmillisecs>";
168                                         data << "</server>";
169                                 }
170
171                                 data << "</serverlist>";
172
173                                 data << "</inspircdstats>";
174
175                                 /* Send the document back to m_httpd */
176                                 HTTPDocument response(http->sock, &data, 200);
177                                 response.headers.SetHeader("X-Powered-By", "m_httpd_stats.so");
178                                 response.headers.SetHeader("Content-Type", "text/xml");
179                                 Request req((char*)&response, (Module*)this, event->GetSource());
180                                 req.Send();
181                         }
182                 }
183         }
184
185         const char* OnRequest(Request* request)
186         {
187                 return NULL;
188         }
189
190
191         virtual ~ModuleHttpStats()
192         {
193         }
194
195         virtual Version GetVersion()
196         {
197                 return Version("Provides statistics over HTTP via m_httpd.so", VF_VENDOR, API_VERSION);
198         }
199 };
200
201 static std::map<char, char const*> const &init_entities()
202 {
203         static std::map<char, char const*> entities;
204         entities['<'] = "lt";
205         entities['>'] = "gt";
206         entities['&'] = "amp";
207         entities['"'] = "quot";
208         return entities;
209 }
210
211 std::map<char, char const*> const &ModuleHttpStats::entities = init_entities ();
212
213 MODULE_INIT(ModuleHttpStats)