Fawkes API  Fawkes Development Version
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 {
50  __rrd_man = rrd_manager;
51  __logger = logger;
52 
53  __baseurl = baseurl;
54  __baseurl_len = strlen(baseurl);
55 }
56 
57 
58 /** Destructor. */
60 {
61 }
62 
63 WebReply *
65 {
66  if ( strncmp(__baseurl, request->url().c_str(), __baseurl_len) == 0 ) {
67  // It is in our URL prefix range
68  std::string subpath = request->url().substr(__baseurl_len);
69 
70  const RWLockVector<RRDGraphDefinition *> &graphs(__rrd_man->get_graphs());
72 
73  ScopedRWLock(graphs.rwlock(), ScopedRWLock::LOCK_READ);
74 
75  if (subpath.find("/graph/") == 0) {
76  std::string graph_name = subpath.substr(subpath.find_first_not_of("/", std::string("/graph/").length()));
77 
78  for (g = graphs.begin(); g != graphs.end(); ++g) {
79  if (strcmp((*g)->get_name(), graph_name.c_str()) == 0) {
80  try {
81  return new DynamicFileWebReply((*g)->get_filename());
82  } catch (Exception &e) {
84  }
85  }
86  }
87  return new WebErrorPageReply(WebReply::HTTP_NOT_FOUND, "Graph not found");
88  } else {
89  WebPageReply *r = new WebPageReply("RRD Graphs");
90  r->set_html_header(" <link rel=\"stylesheet\" type=\"text/css\" "
91  "href=\"/static/css/rrdweb.css\" />\n");
92  *r += "<h2>RRD Graphs</h2>\n";
93 
94  std::string subpath = request->url().substr(__baseurl_len);
95 
96  unsigned int i = 0;
97  *r += "<table class=\"rrdgrid\">";
98  for (g = graphs.begin(); g != graphs.end(); ++g) {
99  if ((i % 2) == 0) *r += " <tr>";
100  r->append_body("<td class=\"%s\"><img src=\"/rrd/graph/%s\" /></td>",
101  ((i % 2) == 0) ? "left" : "right",
102  (*g)->get_name());
103  if ((i++ % 2) == 1) *r += " </tr>\n";
104  }
105  *r += "</table>";
106 
107  return r;
108  }
109  } else {
110  return NULL;
111  }
112 }
virtual void set_html_header(std::string h)
Set HTML header text.
Definition: page_reply.cpp:94
RRDWebRequestProcessor(fawkes::RRDManager *rrd_manager, fawkes::Logger *logger, const char *__baseurl)
Constructor.
Dynamic raw file transfer reply.
Definition: file_reply.h:34
Interface for a RRD connection creator.
Definition: rrd_manager.h:40
Fawkes library namespace.
Scoped read/write lock.
Definition: scoped_rwlock.h:33
virtual const char * what() const
Get primary string.
Definition: exception.cpp:661
virtual fawkes::WebReply * process_request(const fawkes::WebRequest *request)
Process a request.
Base class for exceptions in Fawkes.
Definition: exception.h:36
virtual ~RRDWebRequestProcessor()
Destructor.
Web request meta data carrier.
Definition: request.h:42
Basic page reply.
Definition: page_reply.h:36
Basic web reply.
Definition: reply.h:36
void append_body(const char *format,...)
Append to body.
Definition: reply.cpp:224
const std::string & url() const
Get URL.
Definition: request.h:69
Static error page reply.
Definition: error_reply.h:33
Vector with a lock.
Definition: rwlock_vector.h:37
Interface for logging.
Definition: logger.h:34