Fawkes API  Fawkes Development Version
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
rrdweb_processor.cpp
1 
2 /***************************************************************************
3  * rrdweb_processor.cpp - RRD web request processor
4  *
5  * Created: Tue Dec 21 01:12:58 2010
6  * Copyright 2006-2010 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Library General Public License for more details.
19  *
20  * Read the full text in the LICENSE.GPL file in the doc directory.
21  */
22 
23 #include "rrdweb_processor.h"
24 #include <plugins/rrd/aspect/rrd_manager.h>
25 #include <core/threading/scoped_rwlock.h>
26 #include <core/exception.h>
27 #include <webview/page_reply.h>
28 #include <webview/file_reply.h>
29 #include <webview/error_reply.h>
30 
31 #include <cstring>
32 
33 using namespace fawkes;
34 
35 /** @class RRDWebRequestProcessor "rrdweb_processor.h"
36  * RRD web request processor.
37  * Process web requests to the rrd URL space.
38  * @author Tim Niemueller
39  */
40 
41 /** Constructor.
42  * @param rrd_manager RRD manager to query
43  * @param logger logger to report problems
44  * @param baseurl base URL of the RRD webrequest processor
45  */
47  fawkes::Logger *logger,
48  const char *baseurl)
49  : WebRequestProcessor(/* handle session data */ false)
50 {
51  __rrd_man = rrd_manager;
52  __logger = logger;
53 
54  __baseurl = baseurl;
55  __baseurl_len = strlen(baseurl);
56 }
57 
58 
59 /** Destructor. */
61 {
62 }
63 
64 WebReply *
66  const char *method,
67  const char *version,
68  const char *upload_data,
69  size_t *upload_data_size,
70  void **session_data)
71 {
72  if ( strncmp(__baseurl, url, __baseurl_len) == 0 ) {
73  // It is in our URL prefix range
74  std::string subpath = std::string(url).substr(__baseurl_len);
75 
76  const RWLockVector<RRDGraphDefinition *> &graphs(__rrd_man->get_graphs());
78 
79  ScopedRWLock(graphs.rwlock(), ScopedRWLock::LOCK_READ);
80 
81  if (subpath.find("/graph/") == 0) {
82  std::string graph_name = subpath.substr(subpath.find_first_not_of("/", std::string("/graph/").length()));
83 
84  for (g = graphs.begin(); g != graphs.end(); ++g) {
85  if (strcmp((*g)->get_name(), graph_name.c_str()) == 0) {
86  try {
87  return new DynamicFileWebReply((*g)->get_filename());
88  } catch (Exception &e) {
89  return new WebErrorPageReply(WebReply::HTTP_NOT_FOUND, e.what());
90  }
91  }
92  }
93  return new WebErrorPageReply(WebReply::HTTP_NOT_FOUND, "Graph not found");
94  } else {
95  WebPageReply *r = new WebPageReply("RRD Graphs");
96  r->set_html_header(" <link rel=\"stylesheet\" type=\"text/css\" "
97  "href=\"/static/css/rrdweb.css\" />\n");
98  *r += "<h2>RRD Graphs</h2>\n";
99 
100  std::string subpath = std::string(url).substr(__baseurl_len);
101 
102  unsigned int i = 0;
103  *r += "<table class=\"rrdgrid\">";
104  for (g = graphs.begin(); g != graphs.end(); ++g) {
105  if ((i % 2) == 0) *r += " <tr>";
106  r->append_body("<td class=\"%s\"><img src=\"/rrd/graph/%s\" /></td>",
107  ((i % 2) == 0) ? "left" : "right",
108  (*g)->get_name());
109  if ((i++ % 2) == 1) *r += " </tr>\n";
110  }
111  *r += "</table>";
112 
113  return r;
114  }
115  } else {
116  return NULL;
117  }
118 }