]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_httpd_stats.cpp
Revert my fix and apply the flushevent, and a bit of debugging output.
[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
24 typedef std::map<irc::string,int> StatsHash;
25 typedef StatsHash::iterator StatsIter;
26
27 typedef std::vector<std::pair<int,irc::string> > SortedList;
28 typedef SortedList::iterator SortedIter;
29
30 static StatsHash* sh = new StatsHash();
31 static SortedList* so = new SortedList();
32
33 static StatsHash* Servers = new StatsHash();
34
35 class ModuleHttpStats : public Module
36 {
37         
38         std::string stylesheet;
39         bool changed;
40
41  public:
42
43         void ReadConfig()
44         {
45                 ConfigReader c(ServerInstance);
46                 this->stylesheet = c.ReadValue("httpstats", "stylesheet", 0);
47         }
48
49         ModuleHttpStats(InspIRCd* Me) : Module(Me)
50         {
51                 ReadConfig();
52                 this->changed = true;
53         }
54
55         void InsertOrder(irc::string channel, int count)
56         {
57                 /* This function figures out where in the sorted list to put an item from the hash */
58                 SortedIter a;
59                 for (a = so->begin(); a != so->end(); a++)
60                 {
61                         /* Found an item equal to or less than, we insert our item before it */
62                         if (a->first <= count)
63                         {
64                                 so->insert(a,std::pair<int,irc::string>(count,channel));
65                                 return;
66                         }
67                 }
68                 /* There are no items in the list yet, insert something at the beginning */
69                 so->insert(so->begin(), std::pair<int,irc::string>(count,channel));
70         }
71
72         void SortList()
73         {
74                 /* Sorts the hash into the sorted list using an insertion sort */
75                 so->clear();
76                 for (StatsIter a = sh->begin(); a != sh->end(); a++)
77                         InsertOrder(a->first, a->second);
78                 for (user_hash::iterator u = ServerInstance->clientlist->begin(); u != ServerInstance->clientlist->end(); u++)
79                 {
80                         StatsHash::iterator n = Servers->find(u->second->server);
81                         if (n != Servers->end())
82                         {
83                                 n->second++;
84                         }
85                         else
86                         {
87                                 Servers->insert(std::make_pair<irc::string,int>(u->second->server,1));
88                         }
89                 }
90                 this->changed = false;
91         }
92
93         void OnEvent(Event* event)
94         {
95                 std::stringstream data("");
96
97                 if (event->GetEventID() == "httpd_url")
98                 {
99                         HTTPRequest* http = (HTTPRequest*)event->GetData();
100
101                         if ((http->GetURI() == "/stats") || (http->GetURI() == "/stats/"))
102                         {
103                                 data << "<inspircdstats>";
104
105                                 data << "<server><name>" << ServerInstance->Config->ServerName << "</name><gecos>" << ServerInstance->Config->ServerDesc << "</gecos></server>";
106
107                                 data << "<general>";
108                                 data << "<usercount>" << ServerInstance->clientlist->size() << "</usercount>";
109                                 data << "<channelcount>" << ServerInstance->chanlist->size() << "</channelcount>";
110                                 data << "<opercount>" << ServerInstance->all_opers.size() << "</opercount>";
111                                 data << "<socketcount>" << (ServerInstance->SE->GetMaxFds() - ServerInstance->SE->GetRemainingFds()) << "</socketcount><socketmax>" << ServerInstance->SE->GetMaxFds() <<
112                                         "</socketmax><socketengine>" << ServerInstance->SE->GetName() << "</socketengine>";
113
114                                 time_t current_time = 0;
115                                 current_time = ServerInstance->Time();
116                                 time_t server_uptime = current_time - ServerInstance->startup_time;
117                                 struct tm* stime;
118                                 stime = gmtime(&server_uptime);
119                                 data << "<uptime><days>" << stime->tm_yday << "</days><hours>" << stime->tm_hour << "</hours><mins>" << stime->tm_min << "</mins><secs>" << stime->tm_sec << "</secs></uptime>";
120
121
122                                 data << "</general>";
123                                 data << "<modulelist>";
124                                 for (int i = 0; i <= ServerInstance->GetModuleCount(); i++)
125                                 {
126                                         if (!ServerInstance->Config->module_names[i].empty())
127                                         {
128                                                 Version v = ServerInstance->modules[i]->GetVersion();
129                                                 data << "<module><name>" << ServerInstance->Config->module_names[i] << "</name><version>" << 
130                                                         v.Major << "." <<  v.Minor << "." << v.Revision << "." << v.Build << "</version></module>";
131                                         }
132                                 }
133                                 data << "</modulelist>";
134
135                                 data << "<channellist>";
136                                 /* If the list has changed since last time it was displayed, re-sort it
137                                  * this time only (not every time, as this would be moronic)
138                                  */
139                                 if (this->changed)
140                                         this->SortList();
141
142                                 for (SortedIter a = so->begin(); a != so->end(); a++)
143                                 {
144                                         chanrec* c = ServerInstance->FindChan(a->second.c_str());
145                                         if (c && !c->IsModeSet('s') && !c->IsModeSet('p'))
146                                         {
147                                                 data << "<channel>";
148                                                 data << "<usercount>" << c->GetUsers()->size() << "</usercount><channelname>" << c->name << "</channelname>";
149                                                 data << "<channelops>" << c->GetOppedUsers()->size() << "</channelops>";
150                                                 data << "<channelhalfops>" << c->GetHalfoppedUsers()->size() << "</channelhalfops>";
151                                                 data << "<channelvoices>" << c->GetVoicedUsers()->size() << "</channelvoices>";
152                                                 data << "<channeltopic>" << c->topic << "</channeltopic>";
153                                                 data << "<channelmodes>" << c->ChanModes(false) << "</channelmodes>";
154                                                 data << "</channel>";
155                                         }
156                                 }
157
158                                 data << "</channellist>";
159
160                                 data << "<serverlist>";
161                                 
162                                 for (StatsHash::iterator b = Servers->begin(); b != Servers->end(); b++)
163                                 {
164                                         data << "<server>";
165                                         data << "<servername>" << b->first << "</servername>";
166                                         data << "<usercount>" << b->second << "</usercount>";
167                                         data << "</server>";
168                                 }
169                                 data << "</serverlist>";
170
171                                 data << "</inspircdstats>";
172
173                                 /* Send the document back to m_httpd */
174                                 HTTPDocument response(http->sock, &data, 200, "X-Powered-By: m_http_stats.so\r\nContent-Type: text/xml\r\n");
175                                 Request req((char*)&response, (Module*)this, event->GetSource());
176                                 req.Send();
177                         }
178                 }
179         }
180
181         void OnChannelDelete(chanrec* chan)
182         {
183                 StatsIter a = sh->find(chan->name);
184                 if (a != sh->end())
185                 {
186                         sh->erase(a);
187                 }
188                 this->changed = true;
189         }
190
191         void OnUserJoin(userrec* user, chanrec* channel, bool &silent)
192         {
193                 StatsIter a = sh->find(channel->name);
194                 if (a != sh->end())
195                 {
196                         a->second++;
197                 }
198                 else
199                 {
200                         irc::string name = channel->name;
201                         sh->insert(std::pair<irc::string,int>(name,1));
202                 }
203                 this->changed = true;
204         }
205
206         void OnUserPart(userrec* user, chanrec* channel, const std::string &partmessage, bool &silent)
207         {
208                 StatsIter a = sh->find(channel->name);
209                 if (a != sh->end())
210                 {
211                         a->second--;
212                 }
213                 this->changed = true;
214         }
215
216         void OnUserQuit(userrec* user, const std::string &message, const std::string &oper_message)
217         {
218                 for (UCListIter v = user->chans.begin(); v != user->chans.end(); v++)
219                 {
220                         chanrec* c = v->first;
221                         StatsIter a = sh->find(c->name);
222                         if (a != sh->end())
223                         {
224                                 a->second--;
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                 delete so;
244         }
245
246         virtual Version GetVersion()
247         {
248                 return Version(1, 1, 0, 0, VF_VENDOR, API_VERSION);
249         }
250 };
251
252 MODULE_INIT(ModuleHttpStats)