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;
32 this->stylesheet = c.ReadValue("httpstats", "stylesheet", 0);
38 Implementation eventlist[] = { I_OnEvent, I_OnRequest };
39 ServerInstance->Modules->Attach(eventlist, this, 2);
42 std::string Sanitize(const std::string &str)
45 ret.reserve(str.length() * 2);
47 for (std::string::const_iterator x = str.begin(); x != str.end(); ++x)
49 std::map<char, char const*>::const_iterator it = entities.find(*x);
51 if (it != entities.end())
57 else if (*x < 32 || *x > 126)
59 int n = (unsigned char)*x;
60 ret += ("&#" + ConvToStr(n) + ";");
70 void OnEvent(Event* event)
72 std::stringstream data("");
74 if (event->GetEventID() == "httpd_url")
76 ServerInstance->Logs->Log("m_http_stats", DEBUG,"Handling httpd event");
77 HTTPRequest* http = (HTTPRequest*)event->GetData();
79 if ((http->GetURI() == "/stats") || (http->GetURI() == "/stats/"))
81 data << "<inspircdstats>";
83 data << "<server><name>" << ServerInstance->Config->ServerName << "</name><gecos>" << Sanitize(ServerInstance->Config->ServerDesc) << "</gecos></server>";
86 data << "<usercount>" << ServerInstance->Users->clientlist->size() << "</usercount>";
87 data << "<channelcount>" << ServerInstance->chanlist->size() << "</channelcount>";
88 data << "<opercount>" << ServerInstance->Users->all_opers.size() << "</opercount>";
89 data << "<socketcount>" << (ServerInstance->SE->GetUsedFds()) << "</socketcount><socketmax>" << ServerInstance->SE->GetMaxFds() << "</socketmax><socketengine>" << ServerInstance->SE->GetName() << "</socketengine>";
91 time_t current_time = 0;
92 current_time = ServerInstance->Time();
93 time_t server_uptime = current_time - ServerInstance->startup_time;
95 stime = gmtime(&server_uptime);
96 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 << "<modulelist>";
101 std::vector<std::string> module_names = ServerInstance->Modules->GetAllModuleNames(0);
103 for (std::vector<std::string>::iterator i = module_names.begin(); i != module_names.end(); ++i)
105 Module* m = ServerInstance->Modules->Find(i->c_str());
106 Version v = m->GetVersion();
107 data << "<module><name>" << *i << "</name><version>" << v.version << "</version></module>";
109 data << "</modulelist>";
110 data << "<channellist>";
112 for (chan_hash::const_iterator a = ServerInstance->chanlist->begin(); a != ServerInstance->chanlist->end(); ++a)
114 Channel* c = a->second;
117 data << "<usercount>" << c->GetUsers()->size() << "</usercount><channelname>" << c->name << "</channelname>";
118 data << "<channeltopic>";
119 data << "<topictext>" << Sanitize(c->topic) << "</topictext>";
120 data << "<setby>" << Sanitize(c->setby) << "</setby>";
121 data << "<settime>" << c->topicset << "</settime>";
122 data << "</channeltopic>";
123 data << "<channelmodes>" << Sanitize(c->ChanModes(true)) << "</channelmodes>";
124 const UserMembList* ulist = c->GetUsers();
126 for (UserMembCIter x = ulist->begin(); x != ulist->end(); ++x)
128 data << "<channelmember><uid>" << x->first->uuid << "</uid><privs>" << Sanitize(c->GetAllPrefixChars(x->first)) << "</privs></channelmember>";
131 data << "</channel>";
134 data << "</channellist><userlist>";
136 for (user_hash::const_iterator a = ServerInstance->Users->clientlist->begin(); a != ServerInstance->Users->clientlist->end(); ++a)
141 data << "<nickname>" << u->nick << "</nickname><uuid>" << u->uuid << "</uuid><realhost>" << u->host << "</realhost><displayhost>" << u->dhost << "</displayhost>";
142 data << "<gecos>" << Sanitize(u->fullname) << "</gecos><server>" << u->server << "</server><away>" << Sanitize(u->awaymsg) << "</away><opertype>" << Sanitize(u->oper) << "</opertype><modes>";
144 for (unsigned char n = 'A'; n <= 'z'; ++n)
148 data << modes << "</modes><ident>" << Sanitize(u->ident) << "</ident><port>" << u->GetServerPort() << "</port><ipaddress>" << u->GetIPString() << "</ipaddress>";
152 data << "</userlist><serverlist>";
155 ServerInstance->PI->GetServerList(sl);
157 for (ProtoServerList::iterator b = sl.begin(); b != sl.end(); ++b)
160 data << "<servername>" << b->servername << "</servername>";
161 data << "<parentname>" << b->parentname << "</parentname>";
162 data << "<gecos>" << b->gecos << "</gecos>";
163 data << "<usercount>" << b->usercount << "</usercount>";
164 // This is currently not implemented, so, commented out.
165 // data << "<opercount>" << b->opercount << "</opercount>";
166 data << "<lagmillisecs>" << b->latencyms << "</lagmillisecs>";
170 data << "</serverlist>";
172 data << "</inspircdstats>";
174 /* Send the document back to m_httpd */
175 HTTPDocument response(http->sock, &data, 200);
176 response.headers.SetHeader("X-Powered-By", "m_httpd_stats.so");
177 response.headers.SetHeader("Content-Type", "text/xml");
178 Request req((char*)&response, (Module*)this, event->GetSource());
184 const char* OnRequest(Request* request)
190 virtual ~ModuleHttpStats()
194 virtual Version GetVersion()
196 return Version("Provides statistics over HTTP via m_httpd.so", VF_VENDOR, API_VERSION);
200 static std::map<char, char const*> const &init_entities()
202 static std::map<char, char const*> entities;
203 entities['<'] = "lt";
204 entities['>'] = "gt";
205 entities['&'] = "amp";
206 entities['"'] = "quot";
210 std::map<char, char const*> const &ModuleHttpStats::entities = init_entities ();
212 MODULE_INIT(ModuleHttpStats)