The InspIRCd Project
Home | Developers | Wiki | Forums | Bug Tracker | SVN | Download
Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

m_httpd_stats.cpp

Go to the documentation of this file.
00001 /*       +------------------------------------+
00002  *       | Inspire Internet Relay Chat Daemon |
00003  *       +------------------------------------+
00004  *
00005  *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
00006  * See: http://www.inspircd.org/wiki/index.php/Credits
00007  *
00008  * This program is free but copyrighted software; see
00009  *          the file COPYING for details.
00010  *
00011  * ---------------------------------------------------
00012  */
00013 
00014 #include "inspircd.h"
00015 #include "httpd.h"
00016 #include "protocol.h"
00017 
00018 /* $ModDesc: Provides statistics over HTTP via m_httpd.so */
00019 /* $ModDep: httpd.h */
00020 
00021 class ModuleHttpStats : public Module
00022 {
00023         static std::map<char, char const*> const &entities;
00024         std::string stylesheet;
00025         bool changed;
00026 
00027  public:
00028 
00029         void ReadConfig()
00030         {
00031                 ConfigReader c(ServerInstance);
00032                 this->stylesheet = c.ReadValue("httpstats", "stylesheet", 0);
00033         }
00034 
00035         ModuleHttpStats(InspIRCd* Me) : Module(Me)
00036         {
00037                 ReadConfig();
00038                 this->changed = true;
00039                 Implementation eventlist[] = { I_OnEvent, I_OnRequest };
00040                 ServerInstance->Modules->Attach(eventlist, this, 2);
00041         }
00042 
00043         std::string Sanitize(const std::string &str)
00044         {
00045                 std::string ret;
00046                 ret.reserve(str.length() * 2);
00047 
00048                 for (std::string::const_iterator x = str.begin(); x != str.end(); ++x)
00049                 {
00050                         std::map<char, char const*>::const_iterator it = entities.find(*x);
00051 
00052                         if (it != entities.end())
00053                         {
00054                                 ret += '&';
00055                                 ret += it->second;
00056                                 ret += ';';
00057                         }
00058                         else if (*x < 32 || *x > 126)
00059                         {
00060                                 int n = (unsigned char)*x;
00061                                 ret += ("&#" + ConvToStr(n) + ";");
00062                         }
00063                         else
00064                         {
00065                                 ret += *x;
00066                         }
00067                 }
00068                 return ret;
00069         }
00070 
00071         void OnEvent(Event* event)
00072         {
00073                 std::stringstream data("");
00074 
00075                 if (event->GetEventID() == "httpd_url")
00076                 {
00077                         ServerInstance->Logs->Log("m_http_stats", DEBUG,"Handling httpd event");
00078                         HTTPRequest* http = (HTTPRequest*)event->GetData();
00079 
00080                         if ((http->GetURI() == "/stats") || (http->GetURI() == "/stats/"))
00081                         {
00082                                 data << "<inspircdstats>";
00083 
00084                                 data << "<server><name>" << ServerInstance->Config->ServerName << "</name><gecos>" << Sanitize(ServerInstance->Config->ServerDesc) << "</gecos></server>";
00085 
00086                                 data << "<general>";
00087                                 data << "<usercount>" << ServerInstance->Users->clientlist->size() << "</usercount>";
00088                                 data << "<channelcount>" << ServerInstance->chanlist->size() << "</channelcount>";
00089                                 data << "<opercount>" << ServerInstance->Users->all_opers.size() << "</opercount>";
00090                                 data << "<socketcount>" << (ServerInstance->SE->GetMaxFds() - ServerInstance->SE->GetRemainingFds()) << "</socketcount><socketmax>" << ServerInstance->SE->GetMaxFds() << "</socketmax><socketengine>" << ServerInstance->SE->GetName() << "</socketengine>";
00091 
00092                                 time_t current_time = 0;
00093                                 current_time = ServerInstance->Time();
00094                                 time_t server_uptime = current_time - ServerInstance->startup_time;
00095                                 struct tm* stime;
00096                                 stime = gmtime(&server_uptime);
00097                                 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>";
00098 
00099 
00100                                 data << "</general>";
00101                                 data << "<modulelist>";
00102                                 std::vector<std::string> module_names = ServerInstance->Modules->GetAllModuleNames(0);
00103 
00104                                 for (std::vector<std::string>::iterator i = module_names.begin(); i != module_names.end(); ++i)
00105                                 {
00106                                         Module* m = ServerInstance->Modules->Find(i->c_str());
00107                                         Version v = m->GetVersion();
00108                                         data << "<module><name>" << *i << "</name><version>" << v.version << "</version></module>";
00109                                 }
00110                                 data << "</modulelist>";
00111                                 data << "<channellist>";
00112 
00113                                 for (chan_hash::const_iterator a = ServerInstance->chanlist->begin(); a != ServerInstance->chanlist->end(); ++a)
00114                                 {
00115                                         Channel* c = a->second;
00116 
00117                                         data << "<channel>";
00118                                         data << "<usercount>" << c->GetUsers()->size() << "</usercount><channelname>" << c->name << "</channelname>";
00119                                         data << "<channelops>" << c->GetOppedUsers()->size() << "</channelops>";
00120                                         data << "<channelhalfops>" << c->GetHalfoppedUsers()->size() << "</channelhalfops>";
00121                                         data << "<channelvoices>" << c->GetVoicedUsers()->size() << "</channelvoices>";
00122                                         data << "<channeltopic>";
00123                                         data << "<topictext>" << Sanitize(c->topic) << "</topictext>";
00124                                         data << "<setby>" << Sanitize(c->setby) << "</setby>";
00125                                         data << "<settime>" << c->topicset << "</settime>";
00126                                         data << "</channeltopic>";
00127                                         data << "<channelmodes>" << Sanitize(c->ChanModes(true)) << "</channelmodes>";
00128                                         CUList* ulist = c->GetUsers();
00129 
00130                                         for (CUList::iterator x = ulist->begin(); x != ulist->end(); ++x)
00131                                         {
00132                                                 data << "<channelmember><uid>" << x->first->uuid << "</uid><privs>" << Sanitize(c->GetAllPrefixChars(x->first)) << "</privs></channelmember>";
00133                                         }
00134 
00135                                         data << "</channel>";
00136                                 }
00137 
00138                                 data << "</channellist><userlist>";
00139 
00140                                 for (user_hash::const_iterator a = ServerInstance->Users->clientlist->begin(); a != ServerInstance->Users->clientlist->end(); ++a)
00141                                 {
00142                                         User* u = a->second;
00143 
00144                                         data << "<user>";
00145                                         data << "<nickname>" << u->nick << "</nickname><uuid>" << u->uuid << "</uuid><realhost>" << u->host << "</realhost><displayhost>" << u->dhost << "</displayhost>";
00146                                         data << "<gecos>" << Sanitize(u->fullname) << "</gecos><server>" << u->server << "</server><away>" << Sanitize(u->awaymsg) << "</away><opertype>" << Sanitize(u->oper) << "</opertype><modes>";
00147                                         std::string modes;
00148                                         for (unsigned char n = 'A'; n <= 'z'; ++n)
00149                                                 if (u->IsModeSet(n))
00150                                                         modes += n;
00151 
00152                                         data << modes << "</modes><ident>" << Sanitize(u->ident) << "</ident><port>" << u->GetPort() << "</port><ipaddress>" << u->GetIPString() << "</ipaddress>";
00153                                         data << "</user>";
00154                                 }
00155 
00156                                 data << "</userlist><serverlist>";
00157 
00158                                 ProtoServerList sl;
00159                                 ServerInstance->PI->GetServerList(sl);
00160 
00161                                 for (ProtoServerList::iterator b = sl.begin(); b != sl.end(); ++b)
00162                                 {
00163                                         data << "<server>";
00164                                         data << "<servername>" << b->servername << "</servername>";
00165                                         data << "<parentname>" << b->parentname << "</parentname>";
00166                                         data << "<gecos>" << b->gecos << "</gecos>";
00167                                         data << "<usercount>" << b->usercount << "</usercount>";
00168                                         data << "<opercount>" << b->opercount << "</opercount>";
00169                                         data << "<lagmillisecs>" << b->latencyms << "</lagmillisecs>";
00170                                         data << "</server>";
00171                                 }
00172 
00173                                 data << "</serverlist>";
00174 
00175                                 data << "</inspircdstats>";
00176 
00177                                 /* Send the document back to m_httpd */
00178                                 HTTPDocument response(http->sock, &data, 200);
00179                                 response.headers.SetHeader("X-Powered-By", "m_httpd_stats.so");
00180                                 response.headers.SetHeader("Content-Type", "text/xml");
00181                                 Request req((char*)&response, (Module*)this, event->GetSource());
00182                                 req.Send();
00183                         }
00184                 }
00185         }
00186 
00187         const char* OnRequest(Request* request)
00188         {
00189                 return NULL;
00190         }
00191 
00192 
00193         virtual ~ModuleHttpStats()
00194         {
00195         }
00196 
00197         virtual Version GetVersion()
00198         {
00199                 return Version("$Id: m_httpd_stats.cpp 10291 2008-08-25 20:35:51Z w00t $", VF_VENDOR, API_VERSION);
00200         }
00201 };
00202 
00203 static std::map<char, char const*> const &init_entities()
00204 {
00205         static std::map<char, char const*> entities;
00206         entities['<'] = "lt";
00207         entities['>'] = "gt";
00208         entities['&'] = "amp";
00209         entities['"'] = "quot";
00210         return entities;
00211 }
00212 
00213 std::map<char, char const*> const &ModuleHttpStats::entities = init_entities ();
00214 
00215 MODULE_INIT(ModuleHttpStats)