Fawkes API  Fawkes Development Version
rrd_example_thread.cpp
00001 
00002 /***************************************************************************
00003  *  rrd_thread.cpp - RRD Thread
00004  *
00005  *  Created: Fri Dec 17 00:32:57 2010
00006  *  Copyright  2006-2010  Tim Niemueller [www.niemueller.de]
00007  *
00008  ****************************************************************************/
00009 
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version.
00014  *
00015  *  This program is distributed in the hope that it will be useful,
00016  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  *  GNU Library General Public License for more details.
00019  *
00020  *  Read the full text in the LICENSE.GPL file in the doc directory.
00021  */
00022 
00023 #include "rrd_example_thread.h"
00024 
00025 #include <core/exceptions/system.h>
00026 #include <utils/misc/string_conversions.h>
00027 #include <utils/system/file.h>
00028 #include <plugins/rrd/aspect/rrd_manager.h>
00029 #include <cstdio>
00030 #include <cstdlib>
00031 #include <cstdarg>
00032 #include <rrd.h>
00033 
00034 using namespace fawkes;
00035 
00036 /** @class RRDExampleThread "rrd_example_thread.h"
00037  * RRD Example Thread.
00038  * This thread creates a simple RRD and stores random values.
00039  *
00040  * @author Tim Niemueller
00041  */
00042 
00043 /** Constructor. */
00044 RRDExampleThread::RRDExampleThread()
00045   : Thread("RRDExampleThread", Thread::OPMODE_WAITFORWAKEUP),
00046     BlockedTimingAspect(BlockedTimingAspect::WAKEUP_HOOK_ACT)
00047 {
00048 }
00049 
00050 
00051 /** Destructor. */
00052 RRDExampleThread::~RRDExampleThread()
00053 {
00054 }
00055 
00056 
00057 void
00058 RRDExampleThread::init()
00059 {
00060   std::vector<RRDDataSource> rrds;
00061   rrds.push_back(RRDDataSource("value", RRDDataSource::COUNTER));
00062   __test_rrd_def = new RRDDefinition("test", rrds);
00063   rrd_manager->add_rrd(__test_rrd_def);
00064 
00065   std::vector<RRDGraphDataDefinition> defs;
00066   std::vector<RRDGraphElement> els;
00067 
00068   defs.push_back(RRDGraphDataDefinition("value", RRDArchive::AVERAGE,
00069                                         _test_rrd_def));
00070   
00071   els.push_back(RRDGraphLine("value", 1, "FF0000", "Value", false));
00072   els.push_back(RRDGraphGPrint("value", RRDArchive::LAST,
00073                                "Current\\:%8.2lf %s"));
00074   els.push_back(RRDGraphGPrint("value", RRDArchive::AVERAGE,
00075                                "Average\\:%8.2lf %s"));
00076   els.push_back(RRDGraphGPrint("value", RRDArchive::MAX,
00077                                "Maximum\\:%8.2lf %s\\n"));
00078 
00079   __test_graph_def = new RRDGraphDefinition("testgraph", __test_rrd_def,
00080                                             -600, -10, 10,
00081                                             "Test Value", "Foo", 10,
00082                                             false, defs, els);
00083 
00084   rrd_manager->add_graph(__test_graph_def);
00085 
00086   __loop_count = 0;
00087   __counter = 0;
00088 }
00089 
00090 
00091 void
00092 RRDExampleThread::finalize()
00093 {
00094   rrd_manager->remove_rrd(__test_rrd_def);
00095 }
00096 
00097 
00098 void
00099 RRDExampleThread::loop()
00100 {
00101   __loop_count++;
00102   if (rand() > RAND_MAX/2) __counter++;
00103   if (__loop_count == 10) {
00104     try {
00105       logger->log_debug(name(), "Adding data N:%u", __counter);
00106       rrd_manager->add_data(__test_rrd_def->get_name(), "N:%u", __counter);
00107     } catch (Exception &e) {
00108       logger->log_warn(name(), "Adding data to %s failed, exception follows",
00109                        __test_rrd_def->get_name());
00110       logger->log_warn(name(), e);
00111     }
00112     __loop_count = 0;
00113   }
00114 }