Commit 6a552c9a authored by Pavel Vainerman's avatar Pavel Vainerman

По аналогии с другими процессами (MySQL,SQLite) переименовал RRDStorage --> RRDServer

parent a8384a95
...@@ -262,8 +262,8 @@ AC_CONFIG_FILES([Makefile ...@@ -262,8 +262,8 @@ AC_CONFIG_FILES([Makefile
extensions/SharedMemory/Makefile extensions/SharedMemory/Makefile
extensions/SharedMemory/libUniSetSharedMemory.pc extensions/SharedMemory/libUniSetSharedMemory.pc
extensions/SharedMemoryPlus/Makefile extensions/SharedMemoryPlus/Makefile
extensions/RRDStorage/Makefile extensions/RRDServer/Makefile
extensions/RRDStorage/libUniSetRRDStorage.pc extensions/RRDServer/libUniSetRRDServer.pc
extensions/tests/Makefile extensions/tests/Makefile
python/lib/Makefile python/lib/Makefile
python/lib/pyUniSet/Makefile python/lib/pyUniSet/Makefile
......
if DISABLE_RRD
else
bin_PROGRAMS = @PACKAGE@-rrdserver bin_PROGRAMS = @PACKAGE@-rrdserver
RRD_VER=@LIBVER@ RRD_VER=@LIBVER@
...@@ -35,5 +31,3 @@ pkgconfig_DATA = libUniSetRRDServer.pc ...@@ -35,5 +31,3 @@ pkgconfig_DATA = libUniSetRRDServer.pc
all-local: all-local:
ln -sf ../RRDServer/$(devel_include_HEADERS) ../include ln -sf ../RRDServer/$(devel_include_HEADERS) ../include
endif
\ No newline at end of file
...@@ -335,4 +335,3 @@ void RRDServer::timerInfo( UniSetTypes::TimerMessage* tm ) ...@@ -335,4 +335,3 @@ void RRDServer::timerInfo( UniSetTypes::TimerMessage* tm )
} }
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
...@@ -4,4 +4,3 @@ ln -s -f ../../Utilities/scripts/uniset-start.sh ...@@ -4,4 +4,3 @@ ln -s -f ../../Utilities/scripts/uniset-start.sh
ln -s -f ../../Utilities/scripts/uniset-stop.sh stop.sh ln -s -f ../../Utilities/scripts/uniset-stop.sh stop.sh
ln -s -f ../../Utilities/scripts/uniset-functions.sh ln -s -f ../../Utilities/scripts/uniset-functions.sh
ln -s -f ../../conf/test.xml test.xml ln -s -f ../../conf/test.xml test.xml
...@@ -3,4 +3,3 @@ ...@@ -3,4 +3,3 @@
uniset-start.sh -f ./uniset-rrdstorage --confile test.xml \ uniset-start.sh -f ./uniset-rrdstorage --confile test.xml \
--rrd-name RRDStorage1 \ --rrd-name RRDStorage1 \
--dlog-add-levels info,crit,warn --dlog-add-levels info,crit,warn
bin_PROGRAMS = @PACKAGE@-rrdstorage
RRD_VER=@LIBVER@
lib_LTLIBRARIES = libUniSetRRDStorage.la
libUniSetRRDStorage_la_LDFLAGS = -version-info $(RRD_VER)
libUniSetRRDStorage_la_LIBADD = $(top_builddir)/lib/libUniSet.la \
$(top_builddir)/extensions/SharedMemory/libUniSetSharedMemory.la \
$(top_builddir)/extensions/lib/libUniSetExtensions.la \
$(SIGC_LIBS) $(RRD_LIBS)
libUniSetRRDStorage_la_CXXFLAGS = -I$(top_builddir)/extensions/include \
-I$(top_builddir)/extensions/SharedMemory \
$(SIGC_CFLAGS) $(RRD_CFLAGS)
libUniSetRRDStorage_la_SOURCES = RRDStorage.cc
@PACKAGE@_rrdstorage_SOURCES = main.cc
@PACKAGE@_rrdstorage_LDADD = libUniSetRRDStorage.la $(top_builddir)/lib/libUniSet.la \
$(top_builddir)/extensions/SharedMemory/libUniSetSharedMemory.la \
$(top_builddir)/extensions/lib/libUniSetExtensions.la \
$(SIGC_LIBS) $(RRD_LIBS)
@PACKAGE@_rrdstorage_CXXFLAGS = -I$(top_builddir)/extensions/include \
-I$(top_builddir)/extensions/SharedMemory \
$(SIGC_CFLAGS) $(RRD_CFLAGS)
# install
devel_include_HEADERS = *.h
devel_includedir = $(pkgincludedir)/extensions
pkgconfigdir = $(libdir)/pkgconfig
pkgconfig_DATA = libUniSetRRDStorage.pc
all-local:
ln -sf ../RRDStorage/$(devel_include_HEADERS) ../include
// -----------------------------------------------------------------------------
extern "C" {
#include <rrd.h>
}
#include <cmath>
#include <sstream>
#include "Exceptions.h"
#include "RRDStorage.h"
// -----------------------------------------------------------------------------
using namespace std;
using namespace UniSetTypes;
using namespace UniSetExtensions;
// -----------------------------------------------------------------------------
RRDStorage::RRDStorage( UniSetTypes::ObjectId objId, xmlNode* cnode, UniSetTypes::ObjectId shmId, SharedMemory* ic,
const string prefix, DebugStream& log ):
UObject_SK(objId,cnode),
shm( new SMInterface(shmId,&ui,objId,ic) ),
prefix(prefix)
{
dlog = log;
UniXML::iterator it(cnode);
UniXML::iterator it1(cnode);
if( !it1.goChildren() )
{
ostringstream err;
err << myname << "(init): empty rrd list...";
RRDStorage::dlog[Debug::CRIT] << err.str() << endl;
throw NameNotFound(err.str());
}
int tmID=1;
for( ;it1.getCurrent(); it1++,tmID++ )
initRRD(it1,tmID);
}
// -----------------------------------------------------------------------------
RRDStorage::~RRDStorage()
{
}
// -----------------------------------------------------------------------------
void RRDStorage::step()
{
}
//--------------------------------------------------------------------------------
void RRDStorage::initRRD( xmlNode* cnode, int tmID )
{
UniXML::iterator it(cnode);
string fname( it.getProp("filename") );
string ff( it.getProp("filter_field") );
string fv( it.getProp("filter_value") );
string cf( it.getProp("ds_field") );
if( cf.empty() )
cf = ff+fv+"_ds";
int rrdstep = it.getPIntProp("step",5);
int lastup = it.getPIntProp("lastup",0);
bool overwrite = it.getPIntProp("overwrite",0);
if( RRDStorage::dlog.debugging(Debug::INFO) )
RRDStorage::dlog[Debug::INFO] << myname << "(init): add rrd: file='" << fname
<< " " << ff << "='" << fv
<< "' create='" << cf << "'"
<< " step=" << rrdstep
<< endl;
std::list<std::string> rralist;
UniXML::iterator it_rra(cnode);
if( !it_rra.goChildren() )
{
ostringstream err;
err << myname << "(init): rrd='" << fname << "' Unknown RRA list";
RRDStorage::dlog[Debug::CRIT] << err.str();
throw SystemError(err.str());
}
for(;it_rra.getCurrent(); it_rra++ )
{
string rra( it_rra.getProp("rra") );
if( rra.empty() )
{
ostringstream err;
err << myname << "(init): rrd='" << fname << "' Unkown RRA item.. <item rra='...'";
RRDStorage::dlog[Debug::CRIT] << err.str();
throw SystemError(err.str());
}
rralist.push_back(rra);
}
if( rralist.empty() )
{
ostringstream err;
err << myname << "(init): Not found RRA items...";
RRDStorage::dlog[Debug::CRIT] << err.str() << endl;
throw SystemError(err.str());
}
// try
{
xmlNode* snode = conf->getXMLSensorsSection();
if(!snode)
{
ostringstream err;
err << myname << "(init): Not found section <sensors>";
RRDStorage::dlog[Debug::CRIT] << err.str();
throw SystemError(err.str());
}
UniXML_iterator it1(snode);
if( !it1.goChildren() )
{
ostringstream err;
err << myname << "(init): section <sensors> empty?!";
RRDStorage::dlog[Debug::CRIT] << err.str();
throw SystemError(err.str());
}
DSMap dsmap;
// список параметров входящих в RRD
std::list<std::string> dslist;
for(;it1.getCurrent(); it1.goNext() )
{
if( !UniSetTypes::check_filter(it1,ff,fv) )
continue;
std::string a(it1.getProp(cf));
if( a.empty() )
{
ostringstream err;
err << myname << "(init): Unknown create parameters ('" << cf << "')";
RRDStorage::dlog[Debug::CRIT] << err.str();
throw SystemError(err.str());
}
std::string dsname(it1.getProp("name"));
ostringstream nm;
nm << "DS:" << dsname << ":" << a;
dslist.push_back(nm.str());
ObjectId sid = conf->getSensorID( dsname );
if( sid == DefaultObjectId )
{
ostringstream err;
err << myname << "(init): Unknown SensorID for '" << dsname << "'";
RRDStorage::dlog[Debug::CRIT] << err.str();
throw SystemError(err.str());
}
DSInfo ds(dsname,it1.getIntProp("default"));
dsmap.insert( DSMap::value_type(sid,ds) );
}
if( dslist.empty() )
{
ostringstream err;
err << myname << "(init): Not found RRD items...";
RRDStorage::dlog[Debug::CRIT] << err.str() << endl;
throw SystemError(err.str());
}
char argc = dslist.size() + rralist.size();
char** argv = new char*[ argc ];
int k=0;
for( std::list<std::string>::iterator i=dslist.begin(); i!=dslist.end(); ++i,k++ )
argv[k] = strdup(i->c_str());
for( std::list<std::string>::iterator i=rralist.begin(); i!=rralist.end(); ++i,k++ )
argv[k] = strdup(i->c_str());
// for( k=0; k<argc; k++ )
// cout << "*** argv[" << k << "]='" << argv[k] << "'" << endl;
// Собственно создаём RRD
if( !overwrite && file_exist(fname) )
{
if( RRDStorage::dlog.debugging(Debug::INFO) )
RRDStorage::dlog[Debug::INFO] << myname << "(init): ignore create file='" << fname
<< "'. File exist... overwrite=0." << endl;
}
else
{
rrd_clear_error();
if( rrd_create_r(fname.c_str(),rrdstep,lastup,argc,(const char**)argv) < 0 )
{
ostringstream err;
err << myname << "(init): Can`t create RRD ('" << fname << "'): err: " << string(rrd_get_error());
RRDStorage::dlog[Debug::CRIT] << err.str() << endl;
throw SystemError(err.str());
}
}
// Чистим выделенную память
for( k=0; k<argc; k++ )
free( argv[k] );
delete[] argv;
RRDInfo rrd(fname,tmID,rrdstep,dsmap);
rrdlist.push_back(rrd);
}
/* catch( Exception& ex )
{
RRDStorage::dlog[Debug::CRIT] << myname << "(init) " << ex << std::endl;
}
catch( ... )
{
RRDStorage::dlog[Debug::CRIT] << myname << "(init): catch ..." << std::endl;
}
*/
}
//--------------------------------------------------------------------------------
void RRDStorage::help_print( int argc, const char* const* argv )
{
cout << " Default prefix='rrd'" << endl;
cout << "--prefix-name - ID for rrdstorage. Default: RRDStorage1. " << endl;
cout << "--prefix-confnode - configuration section name. Default: <NAME name='NAME'...> " << endl;
cout << "--prefix-heartbeat-id name - ID for heartbeat sensor." << endl;
cout << "--prefix-heartbeat-max val - max value for heartbeat sensor." << endl;
}
// -----------------------------------------------------------------------------
RRDStorage* RRDStorage::init_rrdstorage( int argc, const char* const* argv,
UniSetTypes::ObjectId icID, SharedMemory* ic,
const std::string prefix )
{
string name = conf->getArgParam("--" + prefix + "-name","RRDStorage");
if( name.empty() )
{
UniSetExtensions::dlog[Debug::CRIT] << "(RRDStorage): Unknown name. Usage: --" << prefix << "-name" << endl;
return 0;
}
ObjectId ID = conf->getObjectID(name);
if( ID == UniSetTypes::DefaultObjectId )
{
UniSetExtensions::dlog[Debug::CRIT] << "(RRDStorage): Not found ID for '" << name
<< " in '" << conf->getObjectsSection() << "' section" << endl;
return 0;
}
string confname = conf->getArgParam("--" + prefix + "-confnode",name);
xmlNode* cnode = conf->getNode(confname);
if( !cnode )
{
UniSetExtensions::dlog[Debug::CRIT] << "(RRDStorage): " << name << "(init): Not found <" + confname + ">" << endl;
return 0;
}
UniSetExtensions::dlog[Debug::INFO] << "(RRDStorage): name = " << name << "(" << ID << ")" << endl;
return new RRDStorage(ID,cnode,icID,ic,prefix);
}
// -----------------------------------------------------------------------------
void RRDStorage::askSensors( UniversalIO::UIOCommand cmd )
{
UObject_SK::askSensors(cmd);
for( RRDList::iterator it=rrdlist.begin(); it!=rrdlist.end(); ++it )
{
for( DSMap::iterator s=it->dsmap.begin(); s!=it->dsmap.end(); ++s )
{
try
{
shm->askSensor(s->first,cmd);
}
catch( std::exception& ex )
{
RRDStorage::dlog[Debug::CRIT] << myname << "(askSensors): " << ex.what() << endl;
}
}
}
}
// -----------------------------------------------------------------------------
void RRDStorage::sysCommand( UniSetTypes::SystemMessage* sm )
{
UObject_SK::sysCommand(sm);
if( sm->command == SystemMessage::StartUp || sm->command == SystemMessage::WatchDog )
{
for( RRDList::iterator it=rrdlist.begin(); it!=rrdlist.end(); ++it )
{
try
{
askTimer(it->tid,it->sec*1000);
}
catch( std::exception& ex )
{
RRDStorage::dlog[Debug::CRIT] << myname << "(askTimer): " << ex.what() << endl;
}
}
}
}
// -----------------------------------------------------------------------------
void RRDStorage::sensorInfo( UniSetTypes::SensorMessage* sm )
{
for( RRDList::iterator it=rrdlist.begin(); it!=rrdlist.end(); ++it )
{
DSMap::iterator s = it->dsmap.find(sm->id);
if( s!=it->dsmap.end() )
s->second.value = sm->value;
// продолжаем искать по другим rrd, т.к. датчик может входить в несколько..
}
}
// -----------------------------------------------------------------------------
void RRDStorage::timerInfo( UniSetTypes::TimerMessage* tm )
{
for( RRDList::iterator it=rrdlist.begin(); it!=rrdlist.end(); ++it )
{
if( it->tid == tm->id )
{
ostringstream v;
v << time(0);
for( DSMap::iterator s=it->dsmap.begin(); s!=it->dsmap.end(); ++s )
v << ":" << s->second.value;
if( RRDStorage::dlog.debugging(Debug::INFO) )
RRDStorage::dlog[Debug::INFO] << myname << "(update): '" << it->filename << "' " << v.str() << endl;
rrd_clear_error();
const char* argv = v.str().c_str();
if( rrd_update_r(it->filename.c_str(),NULL,1,&argv) < 0 )
{
ostringstream err;
err << myname << "(update): Can`t update RRD ('" << it->filename << "'): err: " << string(rrd_get_error());
RRDStorage::dlog[Debug::CRIT] << err.str() << endl;
}
break;
}
}
}
// -----------------------------------------------------------------------------
#ifndef _RRDStorage_H_
#define _RRDStorage_H_
// -----------------------------------------------------------------------------
#include "UObject_SK.h"
#include "SMInterface.h"
#include "SharedMemory.h"
#include "extensions/Extensions.h"
// -----------------------------------------------------------------------------
/*!
"RoundRobinDatabase" - реализация циклического хранилища.
Процесс реализует циклическое хранение данных (от датчиков) и позволяет
конфигурировать любое количество rrd-баз и входящих в них "источников".
Пример секции конфигурации:
\code
<RRDStorage1 name="RRDStorage1">
<rrd filename="rrdtest.rrd" filter_field="rrd" filter_value="1" step="5" ds_field="rrd1_ds" overwrite="0">
<item rra="RRA:AVERAGE:0.5:1:4320"/>
<item rra="RRA:MAX:0.5:1:4320"/>
</rrd>
<rrd filename="rrdtest2.rrd" filter_field="rrd" filter_value="2" step="10" ds_field="rrd2_ds" overwrite="0">
<item rra="RRA:AVERAGE:0.5:1:4320"/>
<item rra="RRA:MAX:0.5:1:4320"/>
</rrd>
</RRDStorage1>
\endcode
Где:
\b filename - имя создаваемого rrd-файла
\b filter_field - поле у датчика, определяющее, что его нужно сохранять в БД
\b filter_value - значение filter_field, определяющее, что датчик нужно сохранять в БД
\b ds_field - поле определяющее, параметр задающий формат хранения. Если \a ds_field не задано,
то будет браться filter_field+filter_value+'_ds'.
\b step - период обновления данных (в секундах)
\b overwrite - [0,1]. Пересоздавать ли БД, если файл уже существует.
При этом в секции <sensors> у датчиков прописываются параметры относящиеся к источнику:
\code
<sensors>
...
<item id="54" iotype="AI" name="AI54_S" textname="AI sensor 54" rrd="1" rrd1_ds="GAUGE:20:U:U"/>
<item id="55" iotype="AI" name="AI55_S" textname="AI sensor 55" rrd="1" rrd1_ds="GAUGE:20:U:U"/>
<item id="56" iotype="AI" name="AI56_S" textname="AI sensor 56" rrd="2" rrd2_ds="COUNTER:20:U:U"/>
<item id="57" iotype="AI" name="AI57_S" textname="AI sensor 57" rrd="2" rrd2_ds="DERIVE:20:U:U"/>
...
</sensors>
\endcode
*/
class RRDStorage:
public UObject_SK
{
public:
RRDStorage( UniSetTypes::ObjectId objId, xmlNode* cnode, UniSetTypes::ObjectId shmID, SharedMemory* ic=0,
const std::string prefix="rrd", DebugStream& log=UniSetExtensions::dlog );
virtual ~RRDStorage();
/*! глобальная функция для инициализации объекта */
static RRDStorage* init_rrdstorage( int argc, const char* const* argv,
UniSetTypes::ObjectId shmID, SharedMemory* ic=0,
const std::string prefix="rrd" );
/*! глобальная функция для вывода help-а */
static void help_print( int argc, const char* const* argv );
protected:
RRDStorage();
virtual void askSensors( UniversalIO::UIOCommand cmd );
virtual void sensorInfo( UniSetTypes::SensorMessage* sm );
virtual void timerInfo( UniSetTypes::TimerMessage* tm );
virtual void sysCommand( UniSetTypes::SystemMessage* sm );
virtual void initRRD( xmlNode* cnode, int tmID );
void step();
SMInterface* shm;
struct DSInfo
{
std::string dsname;
long value;
DSInfo( const std::string& dsname, long defval ):
dsname(dsname),value(defval){}
};
typedef std::map<UniSetTypes::ObjectId,DSInfo> DSMap;
struct RRDInfo
{
std::string filename;
long tid;
long sec;
DSMap dsmap;
RRDInfo( const std::string& fname, long tmID, long sec, const DSMap& ds ):
filename(fname),tid(tmID),sec(sec),dsmap(ds){}
};
typedef std::list<RRDInfo> RRDList;
RRDList rrdlist;
private:
std::string prefix;
};
// -----------------------------------------------------------------------------
#endif // _RRDStorage_H_
// -----------------------------------------------------------------------------
#!/bin/sh
ln -s -f ../../Utilities/scripts/uniset-start.sh
ln -s -f ../../Utilities/scripts/uniset-stop.sh stop.sh
ln -s -f ../../Utilities/scripts/uniset-functions.sh
ln -s -f ../../conf/test.xml test.xml
prefix=@prefix@
exec_prefix=@exec_prefix@
libdir=@libdir@
includedir=@includedir@
Name: libUniSetRRDStorage
Description: Support library for UniSetRRDStorage
Requires: libUniSetExtensions librrd
Version: @VERSION@
Libs: -L${libdir} -lUniSetRRDStorage
Cflags: -I${includedir}/uniset
#include <sstream>
#include "RRDStorage.h"
#include "Configuration.h"
#include "Debug.h"
#include "ObjectsActivator.h"
#include "Extensions.h"
// -----------------------------------------------------------------------------
using namespace std;
using namespace UniSetTypes;
using namespace UniSetExtensions;
// -----------------------------------------------------------------------------
int main( int argc, const char** argv )
{
if( argc>1 && (!strcmp(argv[1],"--help") || !strcmp(argv[1],"-h")) )
{
cout << "--smemory-id objectName - SharedMemory objectID. Default: autodetect" << endl;
cout << "--confile filename - configuration file. Default: configure.xml" << endl;
cout << "--rrdstorage-logfile filename - logfilename. Default: rrdstorage.log" << endl;
cout << endl;
RRDStorage::help_print(argc, argv);
return 0;
}
try
{
string confile=UniSetTypes::getArgParam("--confile",argc, argv, "configure.xml");
conf = new Configuration( argc, argv, confile );
string logfilename(conf->getArgParam("--rrdstorage-logfile"));
if( logfilename.empty() )
logfilename = "rrdstorage.log";
conf->initDebug(dlog,"dlog");
std::ostringstream logname;
string dir(conf->getLogDir());
logname << dir << logfilename;
unideb.logFile( logname.str() );
dlog.logFile( logname.str() );
ObjectId shmID = DefaultObjectId;
string sID = conf->getArgParam("--smemory-id");
if( !sID.empty() )
shmID = conf->getControllerID(sID);
else
shmID = getSharedMemoryID();
if( shmID == DefaultObjectId )
{
cerr << sID << "? SharedMemoryID not found in " << conf->getControllersSection() << " section" << endl;
return 1;
}
RRDStorage* db = RRDStorage::init_rrdstorage(argc,argv,shmID);
if( !db )
{
dlog[Debug::CRIT] << "(rrdstorage): init не прошёл..." << endl;
return 1;
}
ObjectsActivator act;
act.addObject(static_cast<class UniSetObject*>(db));
SystemMessage sm(SystemMessage::StartUp);
act.broadcast( sm.transport_msg() );
unideb(Debug::ANY) << "\n\n\n";
unideb[Debug::ANY] << "(main): -------------- RRDStorage START -------------------------\n\n";
dlog(Debug::ANY) << "\n\n\n";
dlog[Debug::ANY] << "(main): -------------- RRDStorage START -------------------------\n\n";
act.run(false);
return 0;
}
catch( UniSetTypes::Exception& ex )
{
dlog[Debug::CRIT] << "(rrdstorage): " << ex << std::endl;
}
catch(...)
{
dlog[Debug::CRIT] << "(rrdstorage): catch ..." << std::endl;
}
return 1;
}
#!/bin/sh
uniset-start.sh -f ./uniset-rrdstorage --confile test.xml \
--rrd-name RRDStorage1 \
--dlog-add-levels info,crit,warn
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment