1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2010 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 * ---------------------------------------------------
19 /* $ModDesc: Provides statistics over HTTP via m_httpd.so */
21 class ModuleHttpStats : public Module
23 static std::map<char, char const*> const &entities;
28 Implementation eventlist[] = { I_OnEvent };
29 ServerInstance->Modules->Attach(eventlist, this, 1);
32 std::string Sanitize(const std::string &str)
35 ret.reserve(str.length() * 2);
37 for (std::string::const_iterator x = str.begin(); x != str.end(); ++x)
39 std::map<char, char const*>::const_iterator it = entities.find(*x);
41 if (it != entities.end())
47 else if (*x < 32 || *x > 126)
49 int n = (unsigned char)*x;
50 ret += ("&#" + ConvToStr(n) + ";");
60 void DumpMeta(std::stringstream& data, Extensible* ext)
63 for(Extensible::ExtensibleStore::const_iterator i = ext->GetExtList().begin(); i != ext->GetExtList().end(); i++)
65 ExtensionItem* item = i->first;
66 std::string value = item->serialize(FORMAT_USER, ext, i->second);
68 data << "<meta name=\"" << item->name << "\">" << Sanitize(value) << "</meta>";
69 else if (!item->name.empty())
70 data << "<meta name=\"" << item->name << "\"/>";
72 data << "</metadata>";
75 void OnEvent(Event& event)
77 std::stringstream data("");
79 if (event.id == "httpd_url")
81 ServerInstance->Logs->Log("m_http_stats", DEBUG,"Handling httpd event");
82 HTTPRequest* http = (HTTPRequest*)&event;
84 if ((http->GetURI() == "/stats") || (http->GetURI() == "/stats/"))
86 data << "<inspircdstats><server><name>" << ServerInstance->Config->ServerName << "</name><gecos>"
87 << Sanitize(ServerInstance->Config->ServerDesc) << "</gecos><version>"
88 << Sanitize(ServerInstance->GetVersionString()) << "</version></server>";
91 data << "<usercount>" << ServerInstance->Users->clientlist->size() << "</usercount>";
92 data << "<channelcount>" << ServerInstance->chanlist->size() << "</channelcount>";
93 data << "<opercount>" << ServerInstance->Users->all_opers.size() << "</opercount>";
94 data << "<socketcount>" << (ServerInstance->SE->GetUsedFds()) << "</socketcount><socketmax>" << ServerInstance->SE->GetMaxFds() << "</socketmax><socketengine>" << ServerInstance->SE->GetName() << "</socketengine>";
96 time_t current_time = 0;
97 current_time = ServerInstance->Time();
98 time_t server_uptime = current_time - ServerInstance->startup_time;
100 stime = gmtime(&server_uptime);
101 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>";
103 data << "<isupport>" << Sanitize(ServerInstance->Config->data005) << "</isupport></general><xlines>";
104 std::vector<std::string> xltypes = ServerInstance->XLines->GetAllTypes();
105 for (std::vector<std::string>::iterator it = xltypes.begin(); it != xltypes.end(); ++it)
107 XLineLookup* lookup = ServerInstance->XLines->GetAll(*it);
111 for (LookupIter i = lookup->begin(); i != lookup->end(); ++i)
113 data << "<xline type=\"" << it->c_str() << "\"><mask>"
114 << Sanitize(i->second->Displayable()) << "</mask><settime>"
115 << i->second->set_time << "</settime><duration>" << i->second->duration
116 << "</duration><reason>" << Sanitize(i->second->reason)
117 << "</reason></xline>";
121 data << "</xlines><modulelist>";
122 std::vector<std::string> module_names = ServerInstance->Modules->GetAllModuleNames(0);
124 for (std::vector<std::string>::iterator i = module_names.begin(); i != module_names.end(); ++i)
126 Module* m = ServerInstance->Modules->Find(i->c_str());
127 Version v = m->GetVersion();
128 data << "<module><name>" << *i << "</name><description>" << Sanitize(v.description) << "</description></module>";
130 data << "</modulelist><channellist>";
132 for (chan_hash::const_iterator a = ServerInstance->chanlist->begin(); a != ServerInstance->chanlist->end(); ++a)
134 Channel* c = a->second;
137 data << "<usercount>" << c->GetUsers()->size() << "</usercount><channelname>" << c->name << "</channelname>";
138 data << "<channeltopic>";
139 data << "<topictext>" << Sanitize(c->topic) << "</topictext>";
140 data << "<setby>" << Sanitize(c->setby) << "</setby>";
141 data << "<settime>" << c->topicset << "</settime>";
142 data << "</channeltopic>";
143 data << "<channelmodes>" << Sanitize(c->ChanModes(true)) << "</channelmodes>";
144 const UserMembList* ulist = c->GetUsers();
146 for (UserMembCIter x = ulist->begin(); x != ulist->end(); ++x)
148 Membership* memb = x->second;
149 data << "<channelmember><uid>" << memb->user->uuid << "</uid><privs>"
150 << Sanitize(c->GetAllPrefixChars(x->first)) << "</privs><modes>"
151 << memb->modes << "</modes>";
152 DumpMeta(data, memb);
153 data << "</channelmember>";
158 data << "</channel>";
161 data << "</channellist><userlist>";
163 for (user_hash::const_iterator a = ServerInstance->Users->clientlist->begin(); a != ServerInstance->Users->clientlist->end(); ++a)
168 data << "<nickname>" << u->nick << "</nickname><uuid>" << u->uuid << "</uuid><realhost>"
169 << u->host << "</realhost><displayhost>" << u->dhost << "</displayhost><gecos>"
170 << Sanitize(u->fullname) << "</gecos><server>" << u->server << "</server>";
172 data << "<away>" << Sanitize(u->awaymsg) << "</away><awaytime>" << u->awaytime << "</awaytime>";
174 data << "<opertype>" << Sanitize(u->oper->NameStr()) << "</opertype>";
175 data << "<modes>" << u->FormatModes() << "</modes><ident>" << Sanitize(u->ident) << "</ident>";
176 LocalUser* lu = IS_LOCAL(u);
178 data << "<port>" << lu->GetServerPort() << "</port><servaddr>"
179 << irc::sockets::satouser(lu->server_sa) << "</servaddr>";
180 data << "<ipaddress>" << u->GetIPString() << "</ipaddress>";
187 data << "</userlist><serverlist>";
190 ServerInstance->PI->GetServerList(sl);
192 for (ProtoServerList::iterator b = sl.begin(); b != sl.end(); ++b)
195 data << "<servername>" << b->servername << "</servername>";
196 data << "<parentname>" << b->parentname << "</parentname>";
197 data << "<gecos>" << b->gecos << "</gecos>";
198 data << "<usercount>" << b->usercount << "</usercount>";
199 // This is currently not implemented, so, commented out.
200 // data << "<opercount>" << b->opercount << "</opercount>";
201 data << "<lagmillisecs>" << b->latencyms << "</lagmillisecs>";
205 data << "</serverlist></inspircdstats>";
207 /* Send the document back to m_httpd */
208 HTTPDocumentResponse response(this, *http, &data, 200);
209 response.headers.SetHeader("X-Powered-By", "m_httpd_stats.so");
210 response.headers.SetHeader("Content-Type", "text/xml");
216 virtual ~ModuleHttpStats()
220 virtual Version GetVersion()
222 return Version("Provides statistics over HTTP via m_httpd.so", VF_VENDOR);
226 static std::map<char, char const*> const &init_entities()
228 static std::map<char, char const*> entities;
229 entities['<'] = "lt";
230 entities['>'] = "gt";
231 entities['&'] = "amp";
232 entities['"'] = "quot";
236 std::map<char, char const*> const &ModuleHttpStats::entities = init_entities ();
238 MODULE_INIT(ModuleHttpStats)