]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_httpd_stats.cpp
Give tons more information in the xml feed, should be enough to construct just about...
[user/henk/code/inspircd.git] / src / modules / m_httpd_stats.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 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 #include "protocol.h"
17
18 /* $ModDesc: Provides statistics over HTTP via m_httpd.so */
19 /* $ModDep: httpd.h */
20
21 class ModuleHttpStats : public Module
22 {
23         
24         std::string stylesheet;
25         bool changed;
26
27  public:
28
29         void ReadConfig()
30         {
31                 ConfigReader c(ServerInstance);
32                 this->stylesheet = c.ReadValue("httpstats", "stylesheet", 0);
33         }
34
35         ModuleHttpStats(InspIRCd* Me) : Module(Me)
36         {
37                 ReadConfig();
38                 this->changed = true;
39                 Implementation eventlist[] = { I_OnEvent, I_OnRequest };
40                 ServerInstance->Modules->Attach(eventlist, this, 2);
41         }
42
43         void OnEvent(Event* event)
44         {
45                 std::stringstream data("");
46
47                 if (event->GetEventID() == "httpd_url")
48                 {
49                         ServerInstance->Logs->Log("m_http_stats", DEBUG,"Handling httpd event");
50                         HTTPRequest* http = (HTTPRequest*)event->GetData();
51
52                         if ((http->GetURI() == "/stats") || (http->GetURI() == "/stats/"))
53                         {
54                                 data << "<inspircdstats>";
55
56                                 data << "<server><name>" << ServerInstance->Config->ServerName << "</name><gecos>" << ServerInstance->Config->ServerDesc << "</gecos></server>";
57
58                                 data << "<general>";
59                                 data << "<usercount>" << ServerInstance->Users->clientlist->size() << "</usercount>";
60                                 data << "<channelcount>" << ServerInstance->chanlist->size() << "</channelcount>";
61                                 data << "<opercount>" << ServerInstance->Users->all_opers.size() << "</opercount>";
62                                 data << "<socketcount>" << (ServerInstance->SE->GetMaxFds() - ServerInstance->SE->GetRemainingFds()) << "</socketcount><socketmax>" << ServerInstance->SE->GetMaxFds() <<
63                                         "</socketmax><socketengine>" << ServerInstance->SE->GetName() << "</socketengine>";
64
65                                 time_t current_time = 0;
66                                 current_time = ServerInstance->Time();
67                                 time_t server_uptime = current_time - ServerInstance->startup_time;
68                                 struct tm* stime;
69                                 stime = gmtime(&server_uptime);
70                                 data << "<uptime><days>" << stime->tm_yday << "</days><hours>" << stime->tm_hour << "</hours><mins>" << stime->tm_min << "</mins><secs>" << stime->tm_sec << "</secs></uptime>";
71
72
73                                 data << "</general>";
74                                 data << "<modulelist>";
75                                 std::vector<std::string> module_names = ServerInstance->Modules->GetAllModuleNames(0);
76                                 for (std::vector<std::string>::iterator i = module_names.begin(); i != module_names.end(); ++i)
77                                 {
78                                         Module* m = ServerInstance->Modules->Find(i->c_str());
79                                         Version v = m->GetVersion();
80                                         data << "<module><name>" << *i << "</name><version>" << v.Major << "." <<  v.Minor << "." << v.Revision << "." << v.Build << "</version></module>";
81                                 }
82                                 data << "</modulelist>";
83                                 data << "<channellist>";
84
85                                 for (chan_hash::const_iterator a = ServerInstance->chanlist->begin(); a != ServerInstance->chanlist->end(); ++a)
86                                 {
87                                         Channel* c = a->second;
88
89                                         data << "<channel>";
90                                         data << "<usercount>" << c->GetUsers()->size() << "</usercount><channelname>" << c->name << "</channelname>";
91                                         data << "<channelops>" << c->GetOppedUsers()->size() << "</channelops>";
92                                         data << "<channelhalfops>" << c->GetHalfoppedUsers()->size() << "</channelhalfops>";
93                                         data << "<channelvoices>" << c->GetVoicedUsers()->size() << "</channelvoices>";
94                                         data << "<channeltopic>" << c->topic << "</channeltopic>";
95                                         data << "<channelmodes>" << c->ChanModes(false) << "</channelmodes>";
96                                         CUList* ulist = c->GetUsers();
97
98                                         for (CUList::iterator x = ulist->begin(); x != ulist->end(); ++x)
99                                         {
100                                                 data << "<channelmember><uid>" << x->first->uuid << "</uid><privs>" << c->GetAllPrefixChars(x->first) << "</privs></channelmember>";
101                                         }
102                                         data << "</channel>";
103                                 }
104
105                                 data << "</channellist><userlist>";
106
107                                 for (user_hash::const_iterator a = ServerInstance->Users->clientlist->begin(); a != ServerInstance->Users->clientlist->end(); ++a)
108                                 {
109                                         User* u = a->second;
110
111                                         data << "<user>";
112                                         data << "<nickname>" << u->nick << "</nickname><uuid>" << u->uuid << "</uuid><realhost>" << u->host << "</realhost><displayhost>" << u->dhost << "</displayhost>";
113                                         data << "<gecos>" << u->fullname << "</gecos><server>" << u->server << "</server><away>" << u->awaymsg << "</away><opertype>" << u->oper << "</opertype><modes>";
114                                         std::string modes;
115                                         for (unsigned char n = 'A'; n <= 'z'; ++n)
116                                                 if (u->IsModeSet(n))
117                                                         modes += n;
118
119                                         data << modes << "</modes>";
120                                         data << "</user>";
121                                 }
122
123                                 data << "</userlist><serverlist>";
124                                 
125                                 ProtoServerList sl;
126                                 ServerInstance->PI->GetServerList(sl);
127
128                                 for (ProtoServerList::iterator b = sl.begin(); b != sl.end(); ++b)
129                                 {
130                                         data << "<server>";
131                                         data << "<servername>" << b->servername << "</servername>";
132                                         data << "<parentname>" << b->parentname << "</parentname>";
133                                         data << "<usercount>" << b->usercount << "</usercount>";
134                                         data << "<opercount>" << b->opercount << "</opercount>";
135                                         data << "<lagmillisecs>" << b->latencyms << "</lagmillisecs>";
136                                         data << "</server>";
137                                 }
138                                 data << "</serverlist>";
139
140                                 data << "</inspircdstats>";
141
142                                 /* Send the document back to m_httpd */
143                                 HTTPDocument response(http->sock, &data, 200);
144                                 response.headers.SetHeader("X-Powered-By", "m_httpd_stats.so");
145                                 response.headers.SetHeader("Content-Type", "text/xml");
146                                 Request req((char*)&response, (Module*)this, event->GetSource());
147                                 req.Send();
148                         }
149                 }
150         }
151
152         const char* OnRequest(Request* request)
153         {
154                 return NULL;
155         }
156
157
158         virtual ~ModuleHttpStats()
159         {
160         }
161
162         virtual Version GetVersion()
163         {
164                 return Version(1, 2, 0, 0, VF_VENDOR, API_VERSION);
165         }
166 };
167
168 MODULE_INIT(ModuleHttpStats)