Fawkes API  Fawkes Development Version
rrd_example_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_example_thread.h"
24 
25 #include <core/exceptions/system.h>
26 #include <plugins/rrd/aspect/rrd_manager.h>
27 #include <utils/misc/string_conversions.h>
28 #include <utils/system/file.h>
29 
30 #include <cstdarg>
31 #include <cstdio>
32 #include <cstdlib>
33 #include <rrd.h>
34 
35 using namespace fawkes;
36 
37 /** @class RRDExampleThread "rrd_example_thread.h"
38  * RRD Example Thread.
39  * This thread creates a simple RRD and stores random values.
40  *
41  * @author Tim Niemueller
42  */
43 
44 /** Constructor. */
46 : Thread("RRDExampleThread", Thread::OPMODE_WAITFORWAKEUP),
48 {
49 }
50 
51 /** Destructor. */
53 {
54 }
55 
56 void
58 {
59  std::vector<RRDDataSource> rrds;
60  rrds.push_back(RRDDataSource("value", RRDDataSource::COUNTER));
61  test_rrd_def_ = new RRDDefinition("test", rrds);
62  rrd_manager->add_rrd(test_rrd_def_);
63 
64  std::vector<RRDGraphDataDefinition> defs;
65  std::vector<RRDGraphElement> els;
66 
67  defs.push_back(RRDGraphDataDefinition("value", RRDArchive::AVERAGE, _test_rrd_def));
68 
69  els.push_back(RRDGraphLine("value", 1, "FF0000", "Value", false));
70  els.push_back(RRDGraphGPrint("value", RRDArchive::LAST, "Current\\:%8.2lf %s"));
71  els.push_back(RRDGraphGPrint("value", RRDArchive::AVERAGE, "Average\\:%8.2lf %s"));
72  els.push_back(RRDGraphGPrint("value", RRDArchive::MAX, "Maximum\\:%8.2lf %s\\n"));
73 
74  test_graph_def_ = new RRDGraphDefinition(
75  "testgraph", test_rrd_def_, -600, -10, 10, "Test Value", "Foo", 10, false, defs, els);
76 
77  rrd_manager->add_graph(test_graph_def_);
78 
79  loop_count_ = 0;
80  counter_ = 0;
81 }
82 
83 void
85 {
86  rrd_manager->remove_rrd(test_rrd_def_);
87 }
88 
89 void
91 {
92  loop_count_++;
93  if (rand() > RAND_MAX / 2)
94  counter_++;
95  if (loop_count_ == 10) {
96  try {
97  logger->log_debug(name(), "Adding data N:%u", counter_);
98  rrd_manager->add_data(test_rrd_def_->get_name(), "N:%u", counter_);
99  } catch (Exception &e) {
100  logger->log_warn(name(),
101  "Adding data to %s failed, exception follows",
102  test_rrd_def_->get_name());
103  logger->log_warn(name(), e);
104  }
105  loop_count_ = 0;
106  }
107 }
RRDExampleThread()
Constructor.
virtual void finalize()
Finalize the thread.
virtual ~RRDExampleThread()
Destructor.
virtual void init()
Initialize the thread.
virtual void loop()
Code to execute in the thread.
Thread aspect to use blocked timing.
Base class for exceptions in Fawkes.
Definition: exception.h:36
virtual void log_debug(const char *component, const char *format,...)=0
Log debug message.
virtual void log_warn(const char *component, const char *format,...)=0
Log warning message.
Logger * logger
This is the Logger member used to access the logger.
Definition: logging.h:41
RRDManager * rrd_manager
Manager class to access RRD features.
Definition: rrd.h:44
Class to represent a RRD data source.
const char * get_name() const
Get name.
Represent data definition in graph arguments.
Class representing a graph definition.
Print string inside graph.
Print graph line.
virtual void add_rrd(RRDDefinition *rrd_def)=0
Add RRD.
virtual void add_graph(RRDGraphDefinition *rrd_graph_def)=0
Add graph.
virtual void add_data(const char *rrd_name, const char *format,...)=0
Add data.
virtual void remove_rrd(RRDDefinition *rrd_def)=0
Remove RRD.
Thread class encapsulation of pthreads.
Definition: thread.h:46
const char * name() const
Get name of thread.
Definition: thread.h:100
Fawkes library namespace.