Fawkes API
Fawkes Development Version
|
00001 00002 /*************************************************************************** 00003 * rrdweb_processor.cpp - RRD web request processor 00004 * 00005 * Created: Tue Dec 21 01:12:58 2010 00006 * Copyright 2006-2010 Tim Niemueller [www.niemueller.de] 00007 * 00008 ****************************************************************************/ 00009 00010 /* This program is free software; you can redistribute it and/or modify 00011 * it under the terms of the GNU General Public License as published by 00012 * the Free Software Foundation; either version 2 of the License, or 00013 * (at your option) any later version. 00014 * 00015 * This program is distributed in the hope that it will be useful, 00016 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00018 * GNU Library General Public License for more details. 00019 * 00020 * Read the full text in the LICENSE.GPL file in the doc directory. 00021 */ 00022 00023 #include "rrdweb_processor.h" 00024 #include <plugins/rrd/aspect/rrd_manager.h> 00025 #include <core/threading/scoped_rwlock.h> 00026 #include <core/exception.h> 00027 #include <webview/page_reply.h> 00028 #include <webview/file_reply.h> 00029 #include <webview/error_reply.h> 00030 00031 #include <cstring> 00032 00033 using namespace fawkes; 00034 00035 /** @class RRDWebRequestProcessor "rrdweb_processor.h" 00036 * RRD web request processor. 00037 * Process web requests to the rrd URL space. 00038 * @author Tim Niemueller 00039 */ 00040 00041 /** Constructor. 00042 * @param rrd_manager RRD manager to query 00043 * @param logger logger to report problems 00044 * @param baseurl base URL of the RRD webrequest processor 00045 */ 00046 RRDWebRequestProcessor::RRDWebRequestProcessor(fawkes::RRDManager *rrd_manager, 00047 fawkes::Logger *logger, 00048 const char *baseurl) 00049 : WebRequestProcessor(/* handle session data */ false) 00050 { 00051 __rrd_man = rrd_manager; 00052 __logger = logger; 00053 00054 __baseurl = baseurl; 00055 __baseurl_len = strlen(baseurl); 00056 } 00057 00058 00059 /** Destructor. */ 00060 RRDWebRequestProcessor::~RRDWebRequestProcessor() 00061 { 00062 } 00063 00064 WebReply * 00065 RRDWebRequestProcessor::process_request(const char *url, 00066 const char *method, 00067 const char *version, 00068 const char *upload_data, 00069 size_t *upload_data_size, 00070 void **session_data) 00071 { 00072 if ( strncmp(__baseurl, url, __baseurl_len) == 0 ) { 00073 // It is in our URL prefix range 00074 std::string subpath = std::string(url).substr(__baseurl_len); 00075 00076 const RWLockVector<RRDGraphDefinition *> &graphs(__rrd_man->get_graphs()); 00077 RWLockVector<RRDGraphDefinition *>::const_iterator g; 00078 00079 ScopedRWLock(graphs.rwlock(), ScopedRWLock::LOCK_READ); 00080 00081 if (subpath.find("/graph/") == 0) { 00082 std::string graph_name = subpath.substr(subpath.find_first_not_of("/", std::string("/graph/").length())); 00083 00084 for (g = graphs.begin(); g != graphs.end(); ++g) { 00085 if (strcmp((*g)->get_name(), graph_name.c_str()) == 0) { 00086 try { 00087 return new DynamicFileWebReply((*g)->get_filename()); 00088 } catch (Exception &e) { 00089 return new WebErrorPageReply(WebReply::HTTP_NOT_FOUND, e.what()); 00090 } 00091 } 00092 } 00093 return new WebErrorPageReply(WebReply::HTTP_NOT_FOUND, "Graph not found"); 00094 } else { 00095 WebPageReply *r = new WebPageReply("RRD Graphs"); 00096 r->set_html_header(" <link rel=\"stylesheet\" type=\"text/css\" " 00097 "href=\"/static/css/rrdweb.css\" />\n"); 00098 *r += "<h2>RRD Graphs</h2>\n"; 00099 00100 std::string subpath = std::string(url).substr(__baseurl_len); 00101 00102 unsigned int i = 0; 00103 *r += "<table class=\"rrdgrid\">"; 00104 for (g = graphs.begin(); g != graphs.end(); ++g) { 00105 if ((i % 2) == 0) *r += " <tr>"; 00106 r->append_body("<td class=\"%s\"><img src=\"/rrd/graph/%s\" /></td>", 00107 ((i % 2) == 0) ? "left" : "right", 00108 (*g)->get_name()); 00109 if ((i++ % 2) == 1) *r += " </tr>\n"; 00110 } 00111 *r += "</table>"; 00112 00113 return r; 00114 } 00115 } else { 00116 return NULL; 00117 } 00118 }