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