]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_httpd_stats.cpp
Allow support for multiple dns results per request. This is a significant change...
[user/henk/code/inspircd.git] / src / modules / m_httpd_stats.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 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 "users.h"
16 #include "channels.h"
17 #include "configreader.h"
18 #include "modules.h"
19 #include "inspsocket.h"
20 #include "httpd.h"
21
22 /* $ModDesc: Provides statistics over HTTP via m_httpd.so */
23 /* $ModDep: httpd.h */
24
25 typedef std::map<irc::string,int> StatsHash;
26 typedef StatsHash::iterator StatsIter;
27
28 typedef std::vector<std::pair<int,irc::string> > SortedList;
29 typedef SortedList::iterator SortedIter;
30
31 static StatsHash* sh = new StatsHash();
32 static SortedList* so = new SortedList();
33
34 static StatsHash* Servers = new StatsHash();
35
36 class ModuleHttpStats : public Module
37 {
38         
39         std::string stylesheet;
40         bool changed;
41
42  public:
43
44         void ReadConfig()
45         {
46                 ConfigReader c(ServerInstance);
47                 this->stylesheet = c.ReadValue("httpstats", "stylesheet", 0);
48         }
49
50         ModuleHttpStats(InspIRCd* Me) : Module(Me)
51         {
52                 ReadConfig();
53                 this->changed = true;
54         }
55
56         void InsertOrder(irc::string channel, int count)
57         {
58                 /* This function figures out where in the sorted list to put an item from the hash */
59                 SortedIter a;
60                 for (a = so->begin(); a != so->end(); a++)
61                 {
62                         /* Found an item equal to or less than, we insert our item before it */
63                         if (a->first <= count)
64                         {
65                                 so->insert(a,std::pair<int,irc::string>(count,channel));
66                                 return;
67                         }
68                 }
69                 /* There are no items in the list yet, insert something at the beginning */
70                 so->insert(so->begin(), std::pair<int,irc::string>(count,channel));
71         }
72
73         void SortList()
74         {
75                 /* Sorts the hash into the sorted list using an insertion sort */
76                 so->clear();
77                 for (StatsIter a = sh->begin(); a != sh->end(); a++)
78                         InsertOrder(a->first, a->second);
79                 for (user_hash::iterator u = ServerInstance->clientlist->begin(); u != ServerInstance->clientlist->end(); u++)
80                 {
81                         StatsHash::iterator n = Servers->find(u->second->server);
82                         if (n != Servers->end())
83                         {
84                                 n->second++;
85                         }
86                         else
87                         {
88                                 Servers->insert(std::make_pair<irc::string,int>(u->second->server,1));
89                         }
90                 }
91                 this->changed = false;
92         }
93
94         void OnEvent(Event* event)
95         {
96                 std::stringstream data("");
97
98                 if (event->GetEventID() == "httpd_url")
99                 {
100                         HTTPRequest* http = (HTTPRequest*)event->GetData();
101
102                         if ((http->GetURI() == "/stats") || (http->GetURI() == "/stats/"))
103                         {
104                                 data << "<inspircdstats>";
105
106                                 data << "<server><name>" << ServerInstance->Config->ServerName << "</name><gecos>" << ServerInstance->Config->ServerDesc << "</gecos></server>";
107
108                                 data << "<general>";
109                                 data << "<usercount>" << ServerInstance->clientlist->size() << "</usercount>";
110                                 data << "<channelcount>" << ServerInstance->chanlist->size() << "</channelcount>";
111                                 data << "<opercount>" << ServerInstance->all_opers.size() << "</opercount>";
112                                 data << "<socketcount>" << (ServerInstance->SE->GetMaxFds() - ServerInstance->SE->GetRemainingFds()) << "</socketcount><socketmax>" << ServerInstance->SE->GetMaxFds() <<
113                                         "</socketmax><socketengine>" << ServerInstance->SE->GetName() << "</socketengine>";
114
115                                 time_t current_time = 0;
116                                 current_time = ServerInstance->Time();
117                                 time_t server_uptime = current_time - ServerInstance->startup_time;
118                                 struct tm* stime;
119                                 stime = gmtime(&server_uptime);
120                                 data << "<uptime><days>" << stime->tm_yday << "</days><hours>" << stime->tm_hour << "</hours><mins>" << stime->tm_min << "</mins><secs>" << stime->tm_sec << "</secs></uptime>";
121
122
123                                 data << "</general>";
124                                 data << "<modulelist>";
125                                 for (int i = 0; i <= ServerInstance->GetModuleCount(); i++)
126                                 {
127                                         if (!ServerInstance->Config->module_names[i].empty())
128                                         {
129                                                 Version v = ServerInstance->modules[i]->GetVersion();
130                                                 data << "<module><name>" << ServerInstance->Config->module_names[i] << "</name><version>" << 
131                                                         v.Major << "." <<  v.Minor << "." << v.Revision << "." << v.Build << "</version></module>";
132                                         }
133                                 }
134                                 data << "</modulelist>";
135
136                                 data << "<channellist>";
137                                 /* If the list has changed since last time it was displayed, re-sort it
138                                  * this time only (not every time, as this would be moronic)
139                                  */
140                                 if (this->changed)
141                                         this->SortList();
142
143                                 for (SortedIter a = so->begin(); a != so->end(); a++)
144                                 {
145                                         chanrec* c = ServerInstance->FindChan(a->second.c_str());
146                                         if (c && !c->IsModeSet('s') && !c->IsModeSet('p'))
147                                         {
148                                                 data << "<channel>";
149                                                 data << "<usercount>" << c->GetUsers()->size() << "</usercount><channelname>" << c->name << "</channelname>";
150                                                 data << "<channelops>" << c->GetOppedUsers()->size() << "</channelops>";
151                                                 data << "<channelhalfops>" << c->GetHalfoppedUsers()->size() << "</channelhalfops>";
152                                                 data << "<channelvoices>" << c->GetVoicedUsers()->size() << "</channelvoices>";
153                                                 data << "<channeltopic>" << c->topic << "</channeltopic>";
154                                                 data << "<channelmodes>" << c->ChanModes(false) << "</channelmodes>";
155                                                 data << "</channel>";
156                                         }
157                                 }
158
159                                 data << "</channellist>";
160
161                                 data << "<serverlist>";
162                                 
163                                 for (StatsHash::iterator b = Servers->begin(); b != Servers->end(); b++)
164                                 {
165                                         data << "<server>";
166                                         data << "<servername>" << b->first << "</servername>";
167                                         data << "<usercount>" << b->second << "</usercount>";
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         void OnChannelDelete(chanrec* chan)
185         {
186                 StatsIter a = sh->find(chan->name);
187                 if (a != sh->end())
188                 {
189                         sh->erase(a);
190                 }
191                 this->changed = true;
192         }
193
194         void OnUserJoin(userrec* user, chanrec* channel, bool &silent)
195         {
196                 StatsIter a = sh->find(channel->name);
197                 if (a != sh->end())
198                 {
199                         a->second++;
200                 }
201                 else
202                 {
203                         irc::string name = channel->name;
204                         sh->insert(std::pair<irc::string,int>(name,1));
205                 }
206                 this->changed = true;
207         }
208
209         void OnUserPart(userrec* user, chanrec* channel, const std::string &partmessage, bool &silent)
210         {
211                 StatsIter a = sh->find(channel->name);
212                 if (a != sh->end())
213                 {
214                         a->second--;
215                 }
216                 this->changed = true;
217         }
218
219         void OnUserQuit(userrec* user, const std::string &message, const std::string &oper_message)
220         {
221                 for (UCListIter v = user->chans.begin(); v != user->chans.end(); v++)
222                 {
223                         chanrec* c = v->first;
224                         StatsIter a = sh->find(c->name);
225                         if (a != sh->end())
226                         {
227                                 a->second--;
228                         }
229                 }
230                 this->changed = true;
231         }
232
233         char* OnRequest(Request* request)
234         {
235                 return NULL;
236         }
237
238         void Implements(char* List)
239         {
240                 List[I_OnEvent] = List[I_OnRequest] = List[I_OnChannelDelete] = List[I_OnUserJoin] = List[I_OnUserPart] = List[I_OnUserQuit] = 1;
241         }
242
243         virtual ~ModuleHttpStats()
244         {
245                 delete sh;
246                 delete so;
247         }
248
249         virtual Version GetVersion()
250         {
251                 return Version(1, 1, 0, 0, VF_VENDOR, API_VERSION);
252         }
253 };
254
255 MODULE_INIT(ModuleHttpStats)