]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_httpd_stats.cpp
ef1b87cb88fd4fd28636d2407a16f222e3c0f5eb
[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                 Implementation eventlist[] = { I_OnEvent, I_OnRequest, I_OnChannelDelete, I_OnUserJoin, I_OnUserPart, I_OnUserQuit };
50                 ServerInstance->Modules->Attach(eventlist, this, 6);
51         }
52
53         void InsertOrder(irc::string channel, int count)
54         {
55                 /* This function figures out where in the sorted list to put an item from the hash */
56                 SortedIter a;
57                 for (a = so->begin(); a != so->end(); a++)
58                 {
59                         /* Found an item equal to or less than, we insert our item before it */
60                         if (a->first <= count)
61                         {
62                                 so->insert(a,std::pair<int,irc::string>(count,channel));
63                                 return;
64                         }
65                 }
66                 /* There are no items in the list yet, insert something at the beginning */
67                 so->insert(so->begin(), std::pair<int,irc::string>(count,channel));
68         }
69
70         void SortList()
71         {
72                 /* Sorts the hash into the sorted list using an insertion sort */
73                 so->clear();
74                 for (StatsIter a = sh->begin(); a != sh->end(); a++)
75                         InsertOrder(a->first, a->second);
76                 for (user_hash::iterator u = ServerInstance->clientlist->begin(); u != ServerInstance->clientlist->end(); u++)
77                 {
78                         StatsHash::iterator n = Servers->find(u->second->server);
79                         if (n != Servers->end())
80                         {
81                                 n->second++;
82                         }
83                         else
84                         {
85                                 Servers->insert(std::make_pair<irc::string,int>(u->second->server,1));
86                         }
87                 }
88                 this->changed = false;
89         }
90
91         void OnEvent(Event* event)
92         {
93                 std::stringstream data("");
94
95                 if (event->GetEventID() == "httpd_url")
96                 {
97                         HTTPRequest* http = (HTTPRequest*)event->GetData();
98
99                         if ((http->GetURI() == "/stats") || (http->GetURI() == "/stats/"))
100                         {
101                                 data << "<inspircdstats>";
102
103                                 data << "<server><name>" << ServerInstance->Config->ServerName << "</name><gecos>" << ServerInstance->Config->ServerDesc << "</gecos></server>";
104
105                                 data << "<general>";
106                                 data << "<usercount>" << ServerInstance->clientlist->size() << "</usercount>";
107                                 data << "<channelcount>" << ServerInstance->chanlist->size() << "</channelcount>";
108                                 data << "<opercount>" << ServerInstance->all_opers.size() << "</opercount>";
109                                 data << "<socketcount>" << (ServerInstance->SE->GetMaxFds() - ServerInstance->SE->GetRemainingFds()) << "</socketcount><socketmax>" << ServerInstance->SE->GetMaxFds() <<
110                                         "</socketmax><socketengine>" << ServerInstance->SE->GetName() << "</socketengine>";
111
112                                 time_t current_time = 0;
113                                 current_time = ServerInstance->Time();
114                                 time_t server_uptime = current_time - ServerInstance->startup_time;
115                                 struct tm* stime;
116                                 stime = gmtime(&server_uptime);
117                                 data << "<uptime><days>" << stime->tm_yday << "</days><hours>" << stime->tm_hour << "</hours><mins>" << stime->tm_min << "</mins><secs>" << stime->tm_sec << "</secs></uptime>";
118
119
120                                 data << "</general>";
121                                 data << "<modulelist>";
122                                 /* XXX FIXME
123                                  * for (int i = 0; i <= ServerInstance->Modules->GetCount(); i++)
124                                 {
125                                         if (!ServerInstance->Config->module_names[i].empty())
126                                         {
127                                                 Version v = ServerInstance->Modules->modules[i]->GetVersion();
128                                                 data << "<module><name>" << ServerInstance->Config->module_names[i] << "</name><version>" << 
129                                                         v.Major << "." <<  v.Minor << "." << v.Revision << "." << v.Build << "</version></module>";
130                                         }
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                                         Channel* 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);
175                                 response.headers.SetHeader("X-Powered-By", "m_httpd_stats.so");
176                                 response.headers.SetHeader("Content-Type", "text/xml");
177                                 Request req((char*)&response, (Module*)this, event->GetSource());
178                                 req.Send();
179                         }
180                 }
181         }
182
183         void OnChannelDelete(Channel* chan)
184         {
185                 StatsIter a = sh->find(chan->name);
186                 if (a != sh->end())
187                 {
188                         sh->erase(a);
189                 }
190                 this->changed = true;
191         }
192
193         void OnUserJoin(User* user, Channel* channel, bool &silent)
194         {
195                 StatsIter a = sh->find(channel->name);
196                 if (a != sh->end())
197                 {
198                         a->second++;
199                 }
200                 else
201                 {
202                         irc::string name = channel->name;
203                         sh->insert(std::pair<irc::string,int>(name,1));
204                 }
205                 this->changed = true;
206         }
207
208         void OnUserPart(User* user, Channel* channel, const std::string &partmessage, bool &silent)
209         {
210                 StatsIter a = sh->find(channel->name);
211                 if (a != sh->end())
212                 {
213                         a->second--;
214                 }
215                 this->changed = true;
216         }
217
218         void OnUserQuit(User* user, const std::string &message, const std::string &oper_message)
219         {
220                 for (UCListIter v = user->chans.begin(); v != user->chans.end(); v++)
221                 {
222                         Channel* c = v->first;
223                         StatsIter a = sh->find(c->name);
224                         if (a != sh->end())
225                         {
226                                 a->second--;
227                         }
228                 }
229                 this->changed = true;
230         }
231
232         char* OnRequest(Request* request)
233         {
234                 return NULL;
235         }
236
237         void Implements(char* List)
238         {
239                 List[I_OnEvent] = List[I_OnRequest] = List[I_OnChannelDelete] = List[I_OnUserJoin] = List[I_OnUserPart] = List[I_OnUserQuit] = 1;
240         }
241
242         virtual ~ModuleHttpStats()
243         {
244                 delete sh;
245                 delete so;
246         }
247
248         virtual Version GetVersion()
249         {
250                 return Version(1, 1, 0, 0, VF_VENDOR, API_VERSION);
251         }
252 };
253
254 MODULE_INIT(ModuleHttpStats)