Fawkes API  Fawkes Development Version
rrd_thread.cpp
1 
2 /***************************************************************************
3  * rrd_thread.cpp - RRD Thread
4  *
5  * Created: Fri Dec 17 00:32:57 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 "rrd_thread.h"
24 
25 #include <core/exceptions/system.h>
26 #include <core/threading/scoped_rwlock.h>
27 #include <utils/misc/string_conversions.h>
28 #include <utils/system/file.h>
29 #include <cstdio>
30 #include <cstdlib>
31 #include <cstdarg>
32 #include <cstring>
33 #include <rrd.h>
34 
35 using namespace fawkes;
36 
37 /** @class RRDThread "rrd_thread.h"
38  * RRD Thread.
39  * This thread maintains an active connection to RRD and provides an
40  * aspect to access RRD to make it convenient for other threads to use
41  * RRD.
42  *
43  * @author Tim Niemueller
44  */
45 
46 /** Constructor. */
48  : Thread("RRDThread", Thread::OPMODE_CONTINUOUS),
49  AspectProviderAspect(&__rrd_aspect_inifin), __rrd_aspect_inifin(this)
50 {
52 }
53 
54 
55 /** Destructor. */
57 {
58 }
59 
60 
61 void
63 {
64  __cfg_graph_interval = 30.;
65  try {
66  __cfg_graph_interval = config->get_float("/plugins/rrd/graph_interval");
67  } catch (Exception &e) {}
68 
69  __time_wait = new TimeWait(clock, time_sec_to_usec(__cfg_graph_interval));
70 }
71 
72 
73 void
75 {
76 }
77 
78 
79 void
81 {
82  __time_wait->mark_start();
84  __time_wait->wait_systime();
85 }
86 
87 
88 /** Generate all graphs. */
89 void
91 {
92  ScopedRWLock lock(__graphs.rwlock(), ScopedRWLock::LOCK_READ);
93 
94  std::vector<fawkes::RRDGraphDefinition *>::iterator g;
95  for (g = __graphs.begin(); g != __graphs.end(); ++g) {
96  size_t argc = 0;
97  const char **argv = (*g)->get_argv(argc);
98 
99  //logger->log_debug(name(), "rrd_graph arguments:");
100  //for (size_t j = 0; j < argc; ++j) {
101  // logger->log_debug(name(), " %zu: %s", j, argv[j]);
102  //}
103 
104  rrd_clear_error();
105  rrd_info_t *i = rrd_graph_v(argc, (char **)argv);
106  if (i == NULL) {
107  throw Exception("Creating graph %s (for RRD %s) failed: %s",
108  (*g)->get_name(), (*g)->get_rrd_def()->get_name(),
109  rrd_get_error());
110  }
111  rrd_info_free(i);
112  }
113 }
114 
115 void
117 {
118  // generate filename
119  char *filename;
120  if (asprintf(&filename, "%s/%s.rrd", ".", rrd_def->get_name()) == -1) {
121  throw OutOfMemoryException("Failed to creat filename for RRD %s",
122  rrd_def->get_name());
123  }
124  rrd_def->set_filename(filename);
125  free(filename);
126 
127  if (! File::exists(rrd_def->get_filename()) || rrd_def->get_recreate()) {
128  std::string size_s = StringConversions::to_string(rrd_def->get_step_sec());
129 
130  // build parameter array
131  // "create" filename --step STEP --start START DS... RRA...
132  size_t rrd_argc = 6 + rrd_def->get_ds().size() + rrd_def->get_rra().size();
133  const char *rrd_argv[rrd_argc];
134  size_t i = 0;
135  rrd_argv[i++] = "create";
136  rrd_argv[i++] = rrd_def->get_filename();
137  rrd_argv[i++] = "--step";
138  rrd_argv[i++] = size_s.c_str();
139  rrd_argv[i++] = "--start";
140  rrd_argv[i++] = "0";
141 
142  std::vector<RRDDataSource>::const_iterator d;
143  for (d = rrd_def->get_ds().begin();
144  d != rrd_def->get_ds().end() && i < rrd_argc;
145  ++d)
146  {
147  rrd_argv[i++] = d->to_string();
148  }
149 
150  std::vector<RRDArchive>::const_iterator a;
151  for (a = rrd_def->get_rra().begin();
152  a != rrd_def->get_rra().end() && i < rrd_argc;
153  ++a)
154  {
155  rrd_argv[i++] = a->to_string();
156  }
157 
158  //logger->log_debug(name(), "rrd_create arguments:");
159  //for (size_t j = 0; j < i; ++j) {
160  // logger->log_debug(name(), " %zu: %s", j, rrd_argv[j]);
161  //}
162 
163  // Create RRD file
164  rrd_clear_error();
165  if (rrd_create(i, (char **)rrd_argv) == -1) {
166  throw Exception("Creating RRD %s failed: %s",
167  rrd_def->get_name(), rrd_get_error());
168  }
169  }
170 
171  ScopedRWLock lock(__rrds.rwlock());
173  for (r = __rrds.begin(); r != __rrds.end(); ++r) {
174  if (strcmp((*r)->get_name(), rrd_def->get_name()) == 0) {
175  throw Exception("RRD with name %s has already been registered",
176  rrd_def->get_name());
177  }
178  }
179 
180  rrd_def->set_rrd_manager(this);
181  __rrds.push_back(rrd_def);
182 }
183 
184 void
186 {
187  ScopedRWLock rrds_lock(__rrds.rwlock());
189  for (r = __rrds.begin(); r != __rrds.end(); ++r) {
190  if (strcmp((*r)->get_name(), rrd_def->get_name()) == 0) {
191  __rrds.erase(r);
192  break;
193  }
194  }
195 
196  ScopedRWLock graph_lock(__graphs.rwlock());
197  bool graphs_modified = false;
198  do {
199  graphs_modified = false;
201  for (g = __graphs.begin(); g != __graphs.end(); ++g) {
202  if (strcmp((*g)->get_rrd_def()->get_name(), rrd_def->get_name()) == 0) {
203  __graphs.erase(g);
204  graphs_modified = true;
205  break;
206  }
207  }
208  } while (graphs_modified);
209 }
210 
211 void
213 {
214  // generate filename
215  char *filename;
216  if (asprintf(&filename, "%s/%s.png", ".",
217  rrd_graph_def->get_name()) == -1) {
218  throw OutOfMemoryException("Failed to create filename for PNG %s",
219  rrd_graph_def->get_name());
220  }
221  rrd_graph_def->set_filename(filename);
222  free(filename);
223 
224  ScopedRWLock lock(__graphs.rwlock());
226  for (g = __graphs.begin(); g != __graphs.end(); ++g) {
227  if (strcmp((*g)->get_name(), rrd_graph_def->get_name()) == 0) {
228  throw Exception("RRD graph with name %s has already been registered",
229  rrd_graph_def->get_name());
230  }
231  }
232  __graphs.push_back(rrd_graph_def);
233 }
234 
235 void
236 RRDThread::add_data(const char *rrd_name, const char *format, ...)
237 {
238  ScopedRWLock lock(__rrds.rwlock(), ScopedRWLock::LOCK_READ);
239 
240  std::vector<RRDDefinition *>::const_iterator d;
241  for (d = __rrds.begin(); d != __rrds.end(); ++d) {
242  RRDDefinition *rrd_def = *d;
243  if (strcmp(rrd_name, rrd_def->get_name()) == 0) {
244  char *data;
245  va_list arg;
246  va_start(arg, format);
247  if (vasprintf(&data, format, arg) == -1) {
248  va_end(arg);
249  throw OutOfMemoryException("Failed to create data string for %s",
250  rrd_name);
251  }
252  va_end(arg);
253 
254  // filename data
255  size_t rrd_argc = 3;
256  const char *rrd_argv[rrd_argc];
257  size_t i = 0;
258  rrd_argv[i++] = "update";
259  rrd_argv[i++] = rrd_def->get_filename();
260  rrd_argv[i++] = data;
261 
262  //logger->log_debug(name(), "rrd_update arguments:");
263  //for (size_t j = 0; j < i; ++j) {
264  // logger->log_debug(name(), " %zu: %s", j, rrd_argv[j]);
265  //}
266 
267  rrd_clear_error();
268  if (rrd_update(i, (char **)rrd_argv) == -1) {
269  free(data);
270  throw Exception("Failed to update RRD %s: %s", rrd_name, rrd_get_error());
271  }
272 
273  free(data);
274  return;
275  }
276  }
277 
278  throw Exception("No RRD named %s registered", rrd_name);
279 }
280 
281 
284 {
285  return __rrds;
286 }
287 
288 
291 {
292  return __graphs;
293 }
long int time_sec_to_usec(double sec)
Convert seconds to micro seconds.
Definition: time.h:72
const char * get_name() const
Get graph definition name.
Fawkes library namespace.
RefPtr< ReadWriteLock > rwlock() const
Get access to the internal read/write lock.
virtual const fawkes::RWLockVector< fawkes::RRDDefinition * > & get_rrds() const
Get RRDs.
Definition: rrd_thread.cpp:283
Scoped read/write lock.
Definition: scoped_rwlock.h:33
virtual void add_rrd(fawkes::RRDDefinition *rrd_def)
Add RRD.
Definition: rrd_thread.cpp:116
void set_filename(const char *filename)
Set filename.
const std::vector< RRDDataSource > & get_ds() const
Get data sources.
Thread class encapsulation of pthreads.
Definition: thread.h:42
void set_prepfin_conc_loop(bool concurrent=true)
Set concurrent execution of prepare_finalize() and loop().
Definition: thread.cpp:727
virtual void finalize()
Finalize the thread.
Definition: rrd_thread.cpp:74
void wait_systime()
Wait until minimum loop time has been reached in real time.
Definition: wait.cpp:100
unsigned int get_step_sec() const
Get step size in sec.
Clock * clock
By means of this member access to the clock is given.
Definition: clock.h:45
virtual void loop()
Code to execute in the thread.
Definition: rrd_thread.cpp:80
virtual void add_graph(fawkes::RRDGraphDefinition *rrd_graph_def)
Add graph.
Definition: rrd_thread.cpp:212
virtual ~RRDThread()
Destructor.
Definition: rrd_thread.cpp:56
virtual void remove_rrd(fawkes::RRDDefinition *rrd_def)
Remove RRD.
Definition: rrd_thread.cpp:185
Base class for exceptions in Fawkes.
Definition: exception.h:36
Class representing a graph definition.
void set_filename(const char *filename)
Set filename.
const std::vector< RRDArchive > & get_rra() const
Get RRD archives.
virtual const fawkes::RWLockVector< fawkes::RRDGraphDefinition * > & get_graphs() const
Get graphs.
Definition: rrd_thread.cpp:290
Thread aspect provide a new aspect.
void mark_start()
Mark start of loop.
Definition: wait.cpp:70
const char * get_name() const
Get name.
virtual void init()
Initialize the thread.
Definition: rrd_thread.cpp:62
const char * get_filename() const
Get file name.
RRDThread()
Constructor.
Definition: rrd_thread.cpp:47
Configuration * config
This is the Configuration member used to access the configuration.
Definition: configurable.h:44
virtual float get_float(const char *path)=0
Get value from configuration which is of type float.
Time wait utility.
Definition: wait.h:32
bool get_recreate() const
Check recreation flag.
System ran out of memory and desired operation could not be fulfilled.
Definition: system.h:32
void set_rrd_manager(RRDManager *rrd_manager)
Set RRD manager.
Vector with a lock.
Definition: rwlock_vector.h:37
void generate_graphs()
Generate all graphs.
Definition: rrd_thread.cpp:90
virtual void add_data(const char *rrd_name, const char *format,...)
Add data.
Definition: rrd_thread.cpp:236