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