1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
18 /* $ModDesc: Provides statistics over HTTP via m_httpd.so */
19 /* $ModDep: httpd.h */
21 class ModuleHttpStats : public Module
23 static std::map<char, char const*> const &entities;
24 std::string stylesheet;
31 ConfigReader c(ServerInstance);
32 this->stylesheet = c.ReadValue("httpstats", "stylesheet", 0);
35 ModuleHttpStats(InspIRCd* Me) : Module(Me)
39 Implementation eventlist[] = { I_OnEvent, I_OnRequest };
40 ServerInstance->Modules->Attach(eventlist, this, 2);
43 std::string Sanitize(const std::string &str)
46 ret.reserve(str.length() * 2);
48 for (std::string::const_iterator x = str.begin(); x != str.end(); ++x)
50 std::map<char, char const*>::const_iterator it = entities.find(*x);
52 if (it != entities.end())
58 else if (*x < 32 || *x > 126)
60 int n = (unsigned char)*x;
61 ret += ("&#" + ConvToStr(n) + ";");
71 void OnEvent(Event* event)
73 std::stringstream data("");
75 if (event->GetEventID() == "httpd_url")
77 ServerInstance->Logs->Log("m_http_stats", DEBUG,"Handling httpd event");
78 HTTPRequest* http = (HTTPRequest*)event->GetData();
80 if ((http->GetURI() == "/stats") || (http->GetURI() == "/stats/"))
82 data << "<inspircdstats>";
84 data << "<server><name>" << ServerInstance->Config->ServerName << "</name><gecos>" << Sanitize(ServerInstance->Config->ServerDesc) << "</gecos></server>";
87 data << "<usercount>" << ServerInstance->Users->clientlist->size() << "</usercount>";
88 data << "<channelcount>" << ServerInstance->chanlist->size() << "</channelcount>";
89 data << "<opercount>" << ServerInstance->Users->all_opers.size() << "</opercount>";
90 data << "<socketcount>" << (ServerInstance->SE->GetMaxFds() - ServerInstance->SE->GetRemainingFds()) << "</socketcount><socketmax>" << ServerInstance->SE->GetMaxFds() << "</socketmax><socketengine>" << ServerInstance->SE->GetName() << "</socketengine>";
92 time_t current_time = 0;
93 current_time = ServerInstance->Time();
94 time_t server_uptime = current_time - ServerInstance->startup_time;
96 stime = gmtime(&server_uptime);
97 data << "<uptime><days>" << stime->tm_yday << "</days><hours>" << stime->tm_hour << "</hours><mins>" << stime->tm_min << "</mins><secs>" << stime->tm_sec << "</secs><boot_time_t>" << ServerInstance->startup_time << "</boot_time_t></uptime>";
100 data << "</general>";
101 data << "<modulelist>";
102 std::vector<std::string> module_names = ServerInstance->Modules->GetAllModuleNames(0);
104 for (std::vector<std::string>::iterator i = module_names.begin(); i != module_names.end(); ++i)
106 Module* m = ServerInstance->Modules->Find(i->c_str());
107 Version v = m->GetVersion();
108 data << "<module><name>" << *i << "</name><version>" << v.version << "</version></module>";
110 data << "</modulelist>";
111 data << "<channellist>";
113 for (chan_hash::const_iterator a = ServerInstance->chanlist->begin(); a != ServerInstance->chanlist->end(); ++a)
115 Channel* c = a->second;
118 data << "<usercount>" << c->GetUsers()->size() << "</usercount><channelname>" << c->name << "</channelname>";
119 data << "<channelops>" << c->GetOppedUsers()->size() << "</channelops>";
120 data << "<channelhalfops>" << c->GetHalfoppedUsers()->size() << "</channelhalfops>";
121 data << "<channelvoices>" << c->GetVoicedUsers()->size() << "</channelvoices>";
122 data << "<channeltopic>";
123 data << "<topictext>" << Sanitize(c->topic) << "</topictext>";
124 data << "<setby>" << Sanitize(c->setby) << "</setby>";
125 data << "<settime>" << c->topicset << "</settime>";
126 data << "</channeltopic>";
127 data << "<channelmodes>" << Sanitize(c->ChanModes(true)) << "</channelmodes>";
128 CUList* ulist = c->GetUsers();
130 for (CUList::iterator x = ulist->begin(); x != ulist->end(); ++x)
132 data << "<channelmember><uid>" << x->first->uuid << "</uid><privs>" << Sanitize(c->GetAllPrefixChars(x->first)) << "</privs></channelmember>";
135 data << "</channel>";
138 data << "</channellist><userlist>";
140 for (user_hash::const_iterator a = ServerInstance->Users->clientlist->begin(); a != ServerInstance->Users->clientlist->end(); ++a)
145 data << "<nickname>" << u->nick << "</nickname><uuid>" << u->uuid << "</uuid><realhost>" << u->host << "</realhost><displayhost>" << u->dhost << "</displayhost>";
146 data << "<gecos>" << Sanitize(u->fullname) << "</gecos><server>" << u->server << "</server><away>" << Sanitize(u->awaymsg) << "</away><opertype>" << Sanitize(u->oper) << "</opertype><modes>";
148 for (unsigned char n = 'A'; n <= 'z'; ++n)
152 data << modes << "</modes><ident>" << Sanitize(u->ident) << "</ident><port>" << u->GetPort() << "</port><ipaddress>" << u->GetIPString() << "</ipaddress>";
156 data << "</userlist><serverlist>";
159 ServerInstance->PI->GetServerList(sl);
161 for (ProtoServerList::iterator b = sl.begin(); b != sl.end(); ++b)
164 data << "<servername>" << b->servername << "</servername>";
165 data << "<parentname>" << b->parentname << "</parentname>";
166 data << "<gecos>" << b->gecos << "</gecos>";
167 data << "<usercount>" << b->usercount << "</usercount>";
168 // This is currently not implemented, so, commented out.
169 // data << "<opercount>" << b->opercount << "</opercount>";
170 data << "<lagmillisecs>" << b->latencyms << "</lagmillisecs>";
174 data << "</serverlist>";
176 data << "</inspircdstats>";
178 /* Send the document back to m_httpd */
179 HTTPDocument response(http->sock, &data, 200);
180 response.headers.SetHeader("X-Powered-By", "m_httpd_stats.so");
181 response.headers.SetHeader("Content-Type", "text/xml");
182 Request req((char*)&response, (Module*)this, event->GetSource());
188 const char* OnRequest(Request* request)
194 virtual ~ModuleHttpStats()
198 virtual Version GetVersion()
200 return Version("$Id$", VF_VENDOR, API_VERSION);
204 static std::map<char, char const*> const &init_entities()
206 static std::map<char, char const*> entities;
207 entities['<'] = "lt";
208 entities['>'] = "gt";
209 entities['&'] = "amp";
210 entities['"'] = "quot";
214 std::map<char, char const*> const &ModuleHttpStats::entities = init_entities ();
216 MODULE_INIT(ModuleHttpStats)