23#include "rrd_thread.h"
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>
51 rrd_aspect_inifin_(this)
64 cfg_graph_interval_ = 30.;
93 std::vector<fawkes::RRDGraphDefinition *>::iterator g;
94 for (g = graphs_.begin(); g != graphs_.end(); ++g) {
96 const char **argv = (*g)->get_argv(argc);
104 rrd_info_t *i = rrd_graph_v(argc, (
char **)argv);
106 throw Exception(
"Creating graph %s (for RRD %s) failed: %s",
108 (*g)->get_rrd_def()->get_name(),
120 if (asprintf(&filename,
"%s/%s.rrd",
".", rrd_def->
get_name()) == -1) {
127 std::string size_s = StringConversions::to_string(rrd_def->
get_step_sec());
131 size_t rrd_argc = 6 + rrd_def->
get_ds().size() + rrd_def->
get_rra().size();
132 const char *rrd_argv[rrd_argc];
134 rrd_argv[i++] =
"create";
136 rrd_argv[i++] =
"--step";
137 rrd_argv[i++] = size_s.c_str();
138 rrd_argv[i++] =
"--start";
141 std::vector<RRDDataSource>::const_iterator d;
142 for (d = rrd_def->
get_ds().begin(); d != rrd_def->
get_ds().end() && i < rrd_argc; ++d) {
143 rrd_argv[i++] = d->to_string();
146 std::vector<RRDArchive>::const_iterator a;
147 for (a = rrd_def->
get_rra().begin(); a != rrd_def->
get_rra().end() && i < rrd_argc; ++a) {
148 rrd_argv[i++] = a->to_string();
158 if (rrd_create(i, (
char **)rrd_argv) == -1) {
159 throw Exception(
"Creating RRD %s failed: %s", rrd_def->
get_name(), rrd_get_error());
165 for (r = rrds_.begin(); r != rrds_.end(); ++r) {
166 if (strcmp((*r)->get_name(), rrd_def->
get_name()) == 0) {
167 throw Exception(
"RRD with name %s has already been registered", rrd_def->
get_name());
172 rrds_.push_back(rrd_def);
180 for (r = rrds_.begin(); r != rrds_.end(); ++r) {
181 if (strcmp((*r)->get_name(), rrd_def->
get_name()) == 0) {
188 bool graphs_modified =
false;
190 graphs_modified =
false;
192 for (g = graphs_.begin(); g != graphs_.end(); ++g) {
193 if (strcmp((*g)->get_rrd_def()->get_name(), rrd_def->
get_name()) == 0) {
195 graphs_modified =
true;
199 }
while (graphs_modified);
207 if (asprintf(&filename,
"%s/%s.png",
".", rrd_graph_def->
get_name()) == -1) {
215 for (g = graphs_.begin(); g != graphs_.end(); ++g) {
216 if (strcmp((*g)->get_name(), rrd_graph_def->
get_name()) == 0) {
217 throw Exception(
"RRD graph with name %s has already been registered",
221 graphs_.push_back(rrd_graph_def);
229 std::vector<RRDDefinition *>::const_iterator d;
230 for (d = rrds_.begin(); d != rrds_.end(); ++d) {
232 if (strcmp(rrd_name, rrd_def->
get_name()) == 0) {
235 va_start(arg, format);
236 if (vasprintf(&data, format, arg) == -1) {
244 const char *rrd_argv[rrd_argc];
246 rrd_argv[i++] =
"update";
248 rrd_argv[i++] = data;
256 if (rrd_update(i, (
char **)rrd_argv) == -1) {
258 throw Exception(
"Failed to update RRD %s: %s", rrd_name, rrd_get_error());
266 throw Exception(
"No RRD named %s registered", rrd_name);
virtual void add_data(const char *rrd_name, const char *format,...)
Add data.
virtual void add_rrd(fawkes::RRDDefinition *rrd_def)
Add RRD.
virtual ~RRDThread()
Destructor.
virtual void remove_rrd(fawkes::RRDDefinition *rrd_def)
Remove RRD.
void generate_graphs()
Generate all graphs.
virtual void add_graph(fawkes::RRDGraphDefinition *rrd_graph_def)
Add graph.
virtual void init()
Initialize the thread.
virtual void loop()
Code to execute in the thread.
virtual void finalize()
Finalize the thread.
virtual const fawkes::RWLockVector< fawkes::RRDGraphDefinition * > & get_graphs() const
Get graphs.
virtual const fawkes::RWLockVector< fawkes::RRDDefinition * > & get_rrds() const
Get RRDs.
Thread aspect provide a new aspect.
Clock * clock
By means of this member access to the clock is given.
Configuration * config
This is the Configuration member used to access the configuration.
virtual float get_float(const char *path)=0
Get value from configuration which is of type float.
Base class for exceptions in Fawkes.
System ran out of memory and desired operation could not be fulfilled.
void set_rrd_manager(RRDManager *rrd_manager)
Set RRD manager.
const char * get_filename() const
Get file name.
void set_filename(const char *filename)
Set filename.
const std::vector< RRDArchive > & get_rra() const
Get RRD archives.
const char * get_name() const
Get name.
bool get_recreate() const
Check recreation flag.
unsigned int get_step_sec() const
Get step size in sec.
const std::vector< RRDDataSource > & get_ds() const
Get data sources.
Class representing a graph definition.
const char * get_name() const
Get graph definition name.
void set_filename(const char *filename)
Set filename.
RefPtr< ReadWriteLock > rwlock() const
Get access to the internal read/write lock.
Thread class encapsulation of pthreads.
void set_prepfin_conc_loop(bool concurrent=true)
Set concurrent execution of prepare_finalize() and loop().
void mark_start()
Mark start of loop.
void wait_systime()
Wait until minimum loop time has been reached in real time.
Fawkes library namespace.
long int time_sec_to_usec(double sec)
Convert seconds to micro seconds.