]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_httpd_stats.cpp
Add <gecos> field to <server> in XML stats output, also add to ProtoServer. Fixes...
[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         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                         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 OnEvent(Event* event)
71         {
72                 std::stringstream data("");
73
74                 if (event->GetEventID() == "httpd_url")
75                 {
76                         ServerInstance->Logs->Log("m_http_stats", DEBUG,"Handling httpd event");
77                         HTTPRequest* http = (HTTPRequest*)event->GetData();
78
79                         if ((http->GetURI() == "/stats") || (http->GetURI() == "/stats/"))
80                         {
81                                 data << "<inspircdstats>";
82
83                                 data << "<server><name>" << ServerInstance->Config->ServerName << "</name><gecos>" << Sanitize(ServerInstance->Config->ServerDesc) << "</gecos></server>";
84
85                                 data << "<general>";
86                                 data << "<usercount>" << ServerInstance->Users->clientlist->size() << "</usercount>";
87                                 data << "<channelcount>" << ServerInstance->chanlist->size() << "</channelcount>";
88                                 data << "<opercount>" << ServerInstance->Users->all_opers.size() << "</opercount>";
89                                 data << "<socketcount>" << (ServerInstance->SE->GetMaxFds() - ServerInstance->SE->GetRemainingFds()) << "</socketcount><socketmax>" << ServerInstance->SE->GetMaxFds() <<
90                                         "</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                                 for (std::vector<std::string>::iterator i = module_names.begin(); i != module_names.end(); ++i)
104                                 {
105                                         Module* m = ServerInstance->Modules->Find(i->c_str());
106                                         Version v = m->GetVersion();
107                                         data << "<module><name>" << *i << "</name><version>" << v.Major << "." <<  v.Minor << "." << v.Revision << "." << v.Build << "</version></module>";
108                                 }
109                                 data << "</modulelist>";
110                                 data << "<channellist>";
111
112                                 for (chan_hash::const_iterator a = ServerInstance->chanlist->begin(); a != ServerInstance->chanlist->end(); ++a)
113                                 {
114                                         Channel* c = a->second;
115
116                                         data << "<channel>";
117                                         data << "<usercount>" << c->GetUsers()->size() << "</usercount><channelname>" << c->name << "</channelname>";
118                                         data << "<channelops>" << c->GetOppedUsers()->size() << "</channelops>";
119                                         data << "<channelhalfops>" << c->GetHalfoppedUsers()->size() << "</channelhalfops>";
120                                         data << "<channelvoices>" << c->GetVoicedUsers()->size() << "</channelvoices>";
121                                         data << "<channeltopic>";
122                                                 data << "<topictext>" << Sanitize(c->topic) << "</topictext>";
123                                                 data << "<setby>" << Sanitize(c->setby) << "</setby>";
124                                                 data << "<settime>" << c->topicset << "</settime>";
125                                         data << "</channeltopic>";
126                                         data << "<channelmodes>" << Sanitize(c->ChanModes(true)) << "</channelmodes>";
127                                         CUList* ulist = c->GetUsers();
128
129                                         for (CUList::iterator x = ulist->begin(); x != ulist->end(); ++x)
130                                         {
131                                                 data << "<channelmember><uid>" << x->first->uuid << "</uid><privs>" << Sanitize(c->GetAllPrefixChars(x->first)) << "</privs></channelmember>";
132                                         }
133                                         data << "</channel>";
134                                 }
135
136                                 data << "</channellist><userlist>";
137
138                                 for (user_hash::const_iterator a = ServerInstance->Users->clientlist->begin(); a != ServerInstance->Users->clientlist->end(); ++a)
139                                 {
140                                         User* u = a->second;
141
142                                         data << "<user>";
143                                         data << "<nickname>" << u->nick << "</nickname><uuid>" << u->uuid << "</uuid><realhost>" << u->host << "</realhost><displayhost>" << u->dhost << "</displayhost>";
144                                         data << "<gecos>" << Sanitize(u->fullname) << "</gecos><server>" << u->server << "</server><away>" << Sanitize(u->awaymsg) << "</away><opertype>" << Sanitize(u->oper) << "</opertype><modes>";
145                                         std::string modes;
146                                         for (unsigned char n = 'A'; n <= 'z'; ++n)
147                                                 if (u->IsModeSet(n))
148                                                         modes += n;
149
150                                         data << modes << "</modes><ident>" << Sanitize(u->ident) << "</ident><port>" << u->GetPort() << "</port><ipaddress>" << u->GetIPString() << "</ipaddress>";
151                                         data << "</user>";
152                                 }
153
154                                 data << "</userlist><serverlist>";
155
156                                 ProtoServerList sl;
157                                 ServerInstance->PI->GetServerList(sl);
158
159                                 for (ProtoServerList::iterator b = sl.begin(); b != sl.end(); ++b)
160                                 {
161                                         data << "<server>";
162                                         data << "<servername>" << b->servername << "</servername>";
163                                         data << "<parentname>" << b->parentname << "</parentname>";
164                                         data << "<gecos>" << b->gecos << "</gecos>";
165                                         data << "<usercount>" << b->usercount << "</usercount>";
166                                         data << "<opercount>" << b->opercount << "</opercount>";
167                                         data << "<lagmillisecs>" << b->latencyms << "</lagmillisecs>";
168                                         data << "</server>";
169                                 }
170                                 data << "</serverlist>";
171
172                                 data << "</inspircdstats>";
173
174                                 /* Send the document back to m_httpd */
175                                 HTTPDocument response(http->sock, &data, 200);
176                                 response.headers.SetHeader("X-Powered-By", "m_httpd_stats.so");
177                                 response.headers.SetHeader("Content-Type", "text/xml");
178                                 Request req((char*)&response, (Module*)this, event->GetSource());
179                                 req.Send();
180                         }
181                 }
182         }
183
184         const char* OnRequest(Request* request)
185         {
186                 return NULL;
187         }
188
189
190         virtual ~ModuleHttpStats()
191         {
192         }
193
194         virtual Version GetVersion()
195         {
196                 return Version(1, 2, 0, 0, VF_VENDOR, API_VERSION);
197         }
198 };
199
200 static std::map<char, char const*> const &init_entities()
201 {
202         static std::map<char, char const*> entities;
203         entities['<'] = "lt";
204         entities['>'] = "gt";
205         entities['&'] = "amp";
206         entities['"'] = "quot";
207         return entities;
208 }
209
210 std::map<char, char const*> const &ModuleHttpStats::entities = init_entities ();
211
212 MODULE_INIT(ModuleHttpStats)