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 */
20 class ModuleHttpStats : public Module
22 static std::map<char, char const*> const &entities;
23 std::string stylesheet;
31 this->stylesheet = c.ReadValue("httpstats", "stylesheet", 0);
37 Implementation eventlist[] = { I_OnEvent };
38 ServerInstance->Modules->Attach(eventlist, this, 1);
41 std::string Sanitize(const std::string &str)
44 ret.reserve(str.length() * 2);
46 for (std::string::const_iterator x = str.begin(); x != str.end(); ++x)
48 std::map<char, char const*>::const_iterator it = entities.find(*x);
50 if (it != entities.end())
56 else if (*x < 32 || *x > 126)
58 int n = (unsigned char)*x;
59 ret += ("&#" + ConvToStr(n) + ";");
69 void DumpMeta(std::stringstream& data, Extensible* ext)
72 for(Extensible::ExtensibleStore::const_iterator i = ext->GetExtList().begin(); i != ext->GetExtList().end(); i++)
74 ExtensionItem* item = i->first;
75 std::string value = item->serialize(FORMAT_USER, ext, i->second);
77 data << "<meta name=\"" << item->key << "\">" << Sanitize(value) << "</meta>";
78 else if (!item->key.empty())
79 data << "<meta name=\"" << item->key << "\"/>";
81 data << "</metadata>";
84 void OnEvent(Event& event)
86 std::stringstream data("");
88 if (event.id == "httpd_url")
90 ServerInstance->Logs->Log("m_http_stats", DEBUG,"Handling httpd event");
91 HTTPRequest* http = (HTTPRequest*)&event;
93 if ((http->GetURI() == "/stats") || (http->GetURI() == "/stats/"))
95 data << "<inspircdstats>";
97 data << "<server><name>" << ServerInstance->Config->ServerName << "</name><gecos>"
98 << Sanitize(ServerInstance->Config->ServerDesc) << "</gecos><version>"
99 << Sanitize(ServerInstance->GetVersionString()) << "</version><revision>"
100 << Sanitize(ServerInstance->GetRevision()) << "</revision></server>";
103 data << "<usercount>" << ServerInstance->Users->clientlist->size() << "</usercount>";
104 data << "<channelcount>" << ServerInstance->chanlist->size() << "</channelcount>";
105 data << "<opercount>" << ServerInstance->Users->all_opers.size() << "</opercount>";
106 data << "<socketcount>" << (ServerInstance->SE->GetUsedFds()) << "</socketcount><socketmax>" << ServerInstance->SE->GetMaxFds() << "</socketmax><socketengine>" << ServerInstance->SE->GetName() << "</socketengine>";
108 time_t current_time = 0;
109 current_time = ServerInstance->Time();
110 time_t server_uptime = current_time - ServerInstance->startup_time;
112 stime = gmtime(&server_uptime);
113 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>";
115 data << "<isupport>" << Sanitize(ServerInstance->Config->data005) << "</isupport></general>";
116 data << "<modulelist>";
117 std::vector<std::string> module_names = ServerInstance->Modules->GetAllModuleNames(0);
119 for (std::vector<std::string>::iterator i = module_names.begin(); i != module_names.end(); ++i)
121 Module* m = ServerInstance->Modules->Find(i->c_str());
122 Version v = m->GetVersion();
123 data << "<module><name>" << *i << "</name><version>" << v.version << "</version><description>" << Sanitize(v.description) << "</description></module>";
125 data << "</modulelist>";
126 data << "<channellist>";
128 for (chan_hash::const_iterator a = ServerInstance->chanlist->begin(); a != ServerInstance->chanlist->end(); ++a)
130 Channel* c = a->second;
133 data << "<usercount>" << c->GetUsers()->size() << "</usercount><channelname>" << c->name << "</channelname>";
134 data << "<channeltopic>";
135 data << "<topictext>" << Sanitize(c->topic) << "</topictext>";
136 data << "<setby>" << Sanitize(c->setby) << "</setby>";
137 data << "<settime>" << c->topicset << "</settime>";
138 data << "</channeltopic>";
139 data << "<channelmodes>" << Sanitize(c->ChanModes(true)) << "</channelmodes>";
140 const UserMembList* ulist = c->GetUsers();
142 for (UserMembCIter x = ulist->begin(); x != ulist->end(); ++x)
144 Membership* memb = x->second;
145 data << "<channelmember><uid>" << memb->user->uuid << "</uid><privs>"
146 << Sanitize(c->GetAllPrefixChars(x->first)) << "</privs><modes>"
147 << memb->modes << "</modes>";
148 DumpMeta(data, memb);
149 data << "</channelmember>";
154 data << "</channel>";
157 data << "</channellist><userlist>";
159 for (user_hash::const_iterator a = ServerInstance->Users->clientlist->begin(); a != ServerInstance->Users->clientlist->end(); ++a)
164 data << "<nickname>" << u->nick << "</nickname><uuid>" << u->uuid << "</uuid><realhost>"
165 << u->host << "</realhost><displayhost>" << u->dhost << "</displayhost><gecos>"
166 << Sanitize(u->fullname) << "</gecos><server>" << u->server << "</server>";
168 data << "<away>" << Sanitize(u->awaymsg) << "</away><awaytime>" << u->awaytime << "</awaytime>";
170 data << "<opertype>" << Sanitize(u->oper) << "</opertype>";
171 data << "<modes>" << u->FormatModes() << "</modes><ident>" << Sanitize(u->ident) << "</ident>";
172 LocalUser* lu = IS_LOCAL(u);
174 data << "<port>" << lu->GetServerPort() << "</port><servaddr>"
175 << irc::sockets::satouser(&lu->server_sa) << "</servaddr>";
176 data << "<ipaddress>" << u->GetIPString() << "</ipaddress>";
183 data << "</userlist><serverlist>";
186 ServerInstance->PI->GetServerList(sl);
188 for (ProtoServerList::iterator b = sl.begin(); b != sl.end(); ++b)
191 data << "<servername>" << b->servername << "</servername>";
192 data << "<parentname>" << b->parentname << "</parentname>";
193 data << "<gecos>" << b->gecos << "</gecos>";
194 data << "<usercount>" << b->usercount << "</usercount>";
195 // This is currently not implemented, so, commented out.
196 // data << "<opercount>" << b->opercount << "</opercount>";
197 data << "<lagmillisecs>" << b->latencyms << "</lagmillisecs>";
201 data << "</serverlist>";
203 data << "</inspircdstats>";
205 /* Send the document back to m_httpd */
206 HTTPDocumentResponse response(this, *http, &data, 200);
207 response.headers.SetHeader("X-Powered-By", "m_httpd_stats.so");
208 response.headers.SetHeader("Content-Type", "text/xml");
214 virtual ~ModuleHttpStats()
218 virtual Version GetVersion()
220 return Version("Provides statistics over HTTP via m_httpd.so", VF_VENDOR);
224 static std::map<char, char const*> const &init_entities()
226 static std::map<char, char const*> entities;
227 entities['<'] = "lt";
228 entities['>'] = "gt";
229 entities['&'] = "amp";
230 entities['"'] = "quot";
234 std::map<char, char const*> const &ModuleHttpStats::entities = init_entities ();
236 MODULE_INIT(ModuleHttpStats)