]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_httpd_stats.cpp
ed1d6a57ebc752c14f8405f7379d967ad0099b28
[user/henk/code/inspircd.git] / src / modules / m_httpd_stats.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *                     E-mail:
7  *              <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *          the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 using namespace std;
18
19 #include <stdio.h>
20 #include "users.h"
21 #include "channels.h"
22 #include "configreader.h"
23 #include "modules.h"
24 #include "inspsocket.h"
25 #include "helperfuncs.h"
26 #include "httpd.h"
27 #include "inspircd.h"
28
29 /* $ModDesc: Provides statistics over HTTP via m_httpd.so */
30
31 extern user_hash clientlist;
32 extern chan_hash chanlist;
33 extern std::vector<userrec*> all_opers;
34 extern InspIRCd* ServerInstance;
35 extern ServerConfig* Config;
36
37 extern int MODCOUNT;
38
39 typedef std::map<irc::string,int> StatsHash;
40 typedef StatsHash::iterator StatsIter;
41
42 typedef std::vector<std::pair<int,irc::string> > SortedList;
43 typedef SortedList::iterator SortedIter;
44
45 static StatsHash* sh = new StatsHash();
46 static SortedList* so = new SortedList();
47
48 class ModuleHttpStats : public Module
49 {
50         Server* Srv;
51         std::string stylesheet;
52         bool changed;
53
54  public:
55
56         void ReadConfig()
57         {
58                 ConfigReader c;
59                 this->stylesheet = c.ReadValue("httpstats", "stylesheet", 0);
60         }
61
62         ModuleHttpStats(Server* Me) : Module::Module(Me)
63         {
64                 Srv = Me;
65                 ReadConfig();
66                 this->changed = false;
67         }
68
69         void InsertOrder(irc::string channel, int count)
70         {
71                 /* This function figures out where in the sorted list to put an item from the hash */
72                 SortedIter a;
73                 for (a = so->begin(); a != so->end(); a++)
74                 {
75                         /* Found an item equal to or less than, we insert our item before it */
76                         if (a->first <= count)
77                         {
78                                 so->insert(a,std::pair<int,irc::string>(count,channel));
79                                 return;
80                         }
81                 }
82                 /* There are no items in the list yet, insert something at the beginning */
83                 so->insert(so->begin(), std::pair<int,irc::string>(count,channel));
84         }
85
86         void SortList()
87         {
88                 /* Sorts the hash into the sorted list using an insertion sort */
89                 so->clear();
90                 for (StatsIter a = sh->begin(); a != sh->end(); a++)
91                 {
92                         log(DEBUG, "InsertOrder on %d %s",a->second,a->first.c_str());
93                         InsertOrder(a->first, a->second);
94                 }
95                 this->changed = false;
96         }
97
98         void OnEvent(Event* event)
99         {
100                 std::stringstream data("");
101
102                 if (event->GetEventID() == "httpd_url")
103                 {
104                         HTTPRequest* http = (HTTPRequest*)event->GetData();
105
106                         if ((http->GetURI() == "/stats") || (http->GetURI() == "/stats/"))
107                         {
108                                 log(DEBUG,"HTTP URL!");
109
110                                 data << "<HTML><HEAD>";
111                                 data << "<TITLE>InspIRCd server statisitics for " << Srv->GetServerName() << " (" << Srv->GetServerDescription() << ")</TITLE>";
112                                 data << "</HEAD><BODY>";
113                                 data << "<H1>InspIRCd server statisitics for " << Srv->GetServerName() << " (" << Srv->GetServerDescription() << ")</H1>";
114
115                                 data << "<DIV ID='TOTALS'>";
116                                 data << "<H2>Totals</H2>";
117                                 data << "<TABLE>";
118                                 data << "<TR><TD>Users</TD><TD>" << clientlist.size() << "</TD></TR>";
119                                 data << "<TR><TD>Channels</TD><TD>" << chanlist.size() << "</TD></TR>";
120                                 data << "<TR><TD>Opers</TD><TD>" << all_opers.size() << "</TD></TR>";
121                                 data << "<TR><TD>Sockets</TD><TD>" << (ServerInstance->SE->GetMaxFds() - ServerInstance->SE->GetRemainingFds()) << " (Max: " << ServerInstance->SE->GetMaxFds() << " via socket engine '" << ServerInstance->SE->GetName() << "')</TD></TR>";
122                                 data << "</TABLE>";
123                                 data << "</DIV>";
124
125                                 data << "<DIV ID='MODULES'>";
126                                 data << "<H2>Modules</H2>";
127                                 data << "<TABLE>";
128                                 for (int i = 0; i <= MODCOUNT; i++)
129                                 {
130                                         if (Config->module_names[i] != "")
131                                                 data << "<TR><TD>" << Config->module_names[i] << "</TD></TR>";
132                                 }
133                                 data << "</TABLE>";
134                                 data << "</DIV>";
135
136                                 data << "<DIV ID='CHANNELS'>";
137                                 data << "<H2>Channels</H2>";
138                                 data << "<TABLE>";
139                                 data << "<TR><TH>Users</TH><TH>Name</TH><TH>@</TH><TH>%</TH><TH>+</TH></TR>";
140
141                                 /* If the list has changed since last time it was displayed, re-sort it
142                                  * this time only (not every time, as this would be moronic)
143                                  */
144                                 if (this->changed)
145                                         this->SortList();
146
147                                 int n = 0;
148                                 for (SortedIter a = so->begin(); ((a != so->end()) && (n < 25)); a++, n++)
149                                 {
150                                         chanrec* c = Srv->FindChannel(a->second.c_str());
151                                         if (c)
152                                         {
153                                                 data << "<TR><TD>" << a->first << "</TD><TD>" << a->second << "</TD>";
154                                                 data << "<TD>" << c->GetOppedUsers()->size() << "</TD>";
155                                                 data << "<TD>" << c->GetHalfoppedUsers()->size() << "</TD>";
156                                                 data << "<TD>" << c->GetVoicedUsers()->size() << "</TD>";
157                                                 data << "</TR>";
158                                         }
159                                 }
160
161                                 data << "</TABLE>";
162                                 data << "</DIV>";
163
164                                 
165                                 data << "</BODY>";
166                                 data << "</HTML>";
167
168                                 /* Send the document back to m_httpd */
169                                 HTTPDocument response(http->sock, &data, 200, "X-Powered-By: m_http_stats.so\r\nContent-Type: text/html\r\n");
170                                 Request req((char*)&response, (Module*)this, event->GetSource());
171                                 req.Send();
172
173                                 log(DEBUG,"Sent");
174                         }
175                 }
176         }
177
178         void OnChannelDelete(chanrec* chan)
179         {
180                 StatsIter a = sh->find(chan->name);
181                 if (a != sh->end())
182                 {
183                         sh->erase(a);
184                 }
185                 this->changed = true;
186         }
187
188         void OnUserJoin(userrec* user, chanrec* channel)
189         {
190                 StatsIter a = sh->find(channel->name);
191                 if (a != sh->end())
192                 {
193                         a->second++;
194                 }
195                 else
196                 {
197                         irc::string name = channel->name;
198                         sh->insert(std::pair<irc::string,int>(name,1));
199                 }
200                 this->changed = true;
201         }
202
203         void OnUserPart(userrec* user, chanrec* channel, const std::string &partmessage)
204         {
205                 StatsIter a = sh->find(channel->name);
206                 if (a != sh->end())
207                 {
208                         a->second--;
209                 }
210                 this->changed = true;
211         }
212
213         void OnUserQuit(userrec* user, const std::string &message)
214         {
215                 for (std::vector<ucrec*>::const_iterator v = user->chans.begin(); v != user->chans.end(); v++)
216                 {
217                         if (((ucrec*)(*v))->channel)
218                         {
219                                 chanrec* c = ((ucrec*)(*v))->channel;
220                                 StatsIter a = sh->find(c->name);
221                                 if (a != sh->end())
222                                 {
223                                         a->second--;
224                                 }
225                         }
226                 }
227                 this->changed = true;
228         }
229
230         char* OnRequest(Request* request)
231         {
232                 return NULL;
233         }
234
235         void Implements(char* List)
236         {
237                 List[I_OnEvent] = List[I_OnRequest] = List[I_OnChannelDelete] = List[I_OnUserJoin] = List[I_OnUserPart] = List[I_OnUserQuit] = 1;
238         }
239
240         virtual ~ModuleHttpStats()
241         {
242                 delete sh;
243         }
244
245         virtual Version GetVersion()
246         {
247                 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
248         }
249 };
250
251
252 class ModuleHttpStatsFactory : public ModuleFactory
253 {
254  public:
255         ModuleHttpStatsFactory()
256         {
257         }
258         
259         ~ModuleHttpStatsFactory()
260         {
261         }
262         
263         virtual Module * CreateModule(Server* Me)
264         {
265                 return new ModuleHttpStats(Me);
266         }
267 };
268
269
270 extern "C" void * init_module( void )
271 {
272         return new ModuleHttpStatsFactory;
273 }