]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_httpd_stats.cpp
fixed some indentation and spacing in modules
[user/henk/code/inspircd.git] / src / modules / m_httpd_stats.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/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
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
47                 for (std::string::const_iterator x = str.begin(); x != str.end(); ++x)
48                 {
49                         switch (*x)
50                         {
51                                 case '<':
52                                         ret += "&lt;";
53                                 break;
54                                 case '>':
55                                         ret += "&gt;";
56                                 break;
57                                 case '&':
58                                         ret += "&amp;";
59                                 break;
60                                 case '"':
61                                         ret += "&quot;";
62                                 break;
63                                 default:
64                                         if (*x < 32 || *x > 126)
65                                         {
66                                                 int n = *x;
67                                                 ret += ("&#" + ConvToStr(n) + ";");
68                                         }
69                                         else
70                                                 ret += *x;
71                                 break;
72                         }
73                 }
74                 return ret;
75         }
76
77         void OnEvent(Event* event)
78         {
79                 std::stringstream data("");
80
81                 if (event->GetEventID() == "httpd_url")
82                 {
83                         ServerInstance->Logs->Log("m_http_stats", DEBUG,"Handling httpd event");
84                         HTTPRequest* http = (HTTPRequest*)event->GetData();
85
86                         if ((http->GetURI() == "/stats") || (http->GetURI() == "/stats/"))
87                         {
88                                 data << "<inspircdstats>";
89
90                                 data << "<server><name>" << ServerInstance->Config->ServerName << "</name><gecos>" << Sanitize(ServerInstance->Config->ServerDesc) << "</gecos></server>";
91
92                                 data << "<general>";
93                                 data << "<usercount>" << ServerInstance->Users->clientlist->size() << "</usercount>";
94                                 data << "<channelcount>" << ServerInstance->chanlist->size() << "</channelcount>";
95                                 data << "<opercount>" << ServerInstance->Users->all_opers.size() << "</opercount>";
96                                 data << "<socketcount>" << (ServerInstance->SE->GetMaxFds() - ServerInstance->SE->GetRemainingFds()) << "</socketcount><socketmax>" << ServerInstance->SE->GetMaxFds() <<
97                                         "</socketmax><socketengine>" << ServerInstance->SE->GetName() << "</socketengine>";
98
99                                 time_t current_time = 0;
100                                 current_time = ServerInstance->Time();
101                                 time_t server_uptime = current_time - ServerInstance->startup_time;
102                                 struct tm* stime;
103                                 stime = gmtime(&server_uptime);
104                                 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>";
105
106
107                                 data << "</general>";
108                                 data << "<modulelist>";
109                                 std::vector<std::string> module_names = ServerInstance->Modules->GetAllModuleNames(0);
110                                 for (std::vector<std::string>::iterator i = module_names.begin(); i != module_names.end(); ++i)
111                                 {
112                                         Module* m = ServerInstance->Modules->Find(i->c_str());
113                                         Version v = m->GetVersion();
114                                         data << "<module><name>" << *i << "</name><version>" << v.Major << "." <<  v.Minor << "." << v.Revision << "." << v.Build << "</version></module>";
115                                 }
116                                 data << "</modulelist>";
117                                 data << "<channellist>";
118
119                                 for (chan_hash::const_iterator a = ServerInstance->chanlist->begin(); a != ServerInstance->chanlist->end(); ++a)
120                                 {
121                                         Channel* c = a->second;
122
123                                         data << "<channel>";
124                                         data << "<usercount>" << c->GetUsers()->size() << "</usercount><channelname>" << c->name << "</channelname>";
125                                         data << "<channelops>" << c->GetOppedUsers()->size() << "</channelops>";
126                                         data << "<channelhalfops>" << c->GetHalfoppedUsers()->size() << "</channelhalfops>";
127                                         data << "<channelvoices>" << c->GetVoicedUsers()->size() << "</channelvoices>";
128                                         data << "<channeltopic>" << Sanitize(c->topic) << "</channeltopic>";
129                                         data << "<channelmodes>" << Sanitize(c->ChanModes(true)) << "</channelmodes>";
130                                         CUList* ulist = c->GetUsers();
131
132                                         for (CUList::iterator x = ulist->begin(); x != ulist->end(); ++x)
133                                         {
134                                                 data << "<channelmember><uid>" << x->first->uuid << "</uid><privs>" << Sanitize(c->GetAllPrefixChars(x->first)) << "</privs></channelmember>";
135                                         }
136                                         data << "</channel>";
137                                 }
138
139                                 data << "</channellist><userlist>";
140
141                                 for (user_hash::const_iterator a = ServerInstance->Users->clientlist->begin(); a != ServerInstance->Users->clientlist->end(); ++a)
142                                 {
143                                         User* u = a->second;
144
145                                         data << "<user>";
146                                         data << "<nickname>" << u->nick << "</nickname><uuid>" << u->uuid << "</uuid><realhost>" << u->host << "</realhost><displayhost>" << u->dhost << "</displayhost>";
147                                         data << "<gecos>" << Sanitize(u->fullname) << "</gecos><server>" << u->server << "</server><away>" << Sanitize(u->awaymsg) << "</away><opertype>" << Sanitize(u->oper) << "</opertype><modes>";
148                                         std::string modes;
149                                         for (unsigned char n = 'A'; n <= 'z'; ++n)
150                                                 if (u->IsModeSet(n))
151                                                         modes += n;
152
153                                         data << modes << "</modes><ident>" << Sanitize(u->ident) << "</ident><port>" << u->GetPort() << "</port><ipaddress>" << u->GetIPString() << "</ipaddress>";
154                                         data << "</user>";
155                                 }
156
157                                 data << "</userlist><serverlist>";
158
159                                 ProtoServerList sl;
160                                 ServerInstance->PI->GetServerList(sl);
161
162                                 for (ProtoServerList::iterator b = sl.begin(); b != sl.end(); ++b)
163                                 {
164                                         data << "<server>";
165                                         data << "<servername>" << b->servername << "</servername>";
166                                         data << "<parentname>" << b->parentname << "</parentname>";
167                                         data << "<usercount>" << b->usercount << "</usercount>";
168                                         data << "<opercount>" << b->opercount << "</opercount>";
169                                         data << "<lagmillisecs>" << b->latencyms << "</lagmillisecs>";
170                                         data << "</server>";
171                                 }
172                                 data << "</serverlist>";
173
174                                 data << "</inspircdstats>";
175
176                                 /* Send the document back to m_httpd */
177                                 HTTPDocument response(http->sock, &data, 200);
178                                 response.headers.SetHeader("X-Powered-By", "m_httpd_stats.so");
179                                 response.headers.SetHeader("Content-Type", "text/xml");
180                                 Request req((char*)&response, (Module*)this, event->GetSource());
181                                 req.Send();
182                         }
183                 }
184         }
185
186         const char* OnRequest(Request* request)
187         {
188                 return NULL;
189         }
190
191
192         virtual ~ModuleHttpStats()
193         {
194         }
195
196         virtual Version GetVersion()
197         {
198                 return Version(1, 2, 0, 0, VF_VENDOR, API_VERSION);
199         }
200 };
201
202 MODULE_INIT(ModuleHttpStats)