<commit_before>/*
 * Copyright (c) 2015 <NAME>.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, version 2.1.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Lesser Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
// -------------------------------------------------------------------------
#include <sstream>
#include <iomanip>
#include "Exceptions.h"
#include <Poco/Net/NetException.h>
#include "BackendOpenTSDB.h"
// -----------------------------------------------------------------------------
using namespace std;
using namespace uniset;
using namespace uniset::extensions;
// -----------------------------------------------------------------------------
BackendOpenTSDB::BackendOpenTSDB( uniset::ObjectId objId, xmlNode* cnode,
								  uniset::ObjectId shmId, const std::shared_ptr<SharedMemory>& ic,
								  const string& prefix ):
	UObject_SK(objId, cnode, string(prefix + "-")),
	prefix(prefix)
{
	auto conf = uniset_conf();

	if( ic )
		ic->logAgregator()->add(logAgregator());

	shm = make_shared<SMInterface>(shmId, ui, objId, ic);

	init(cnode);

	if( smTestID == DefaultObjectId && !tsdbParams.empty() )
	{
		// берём первый датчик из списка
		smTestID = tsdbParams.begin()->first;
	}
}
// -----------------------------------------------------------------------------
BackendOpenTSDB::~BackendOpenTSDB()
{
}
// -----------------------------------------------------------------------------
void BackendOpenTSDB::init( xmlNode* cnode )
{
	UniXML::iterator it(cnode);

	auto conf = uniset_conf();

	host = conf->getArg2Param("--" + prefix + "-host", it.getProp("host"), "localhost");
	port = conf->getArgPInt("--" + prefix + "-port", it.getProp("port"), port);
	tsdbPrefix = conf->getArg2Param("--" + prefix + "-prefix", it.getProp("prefix"), "");
	tsdbTags = conf->getArg2Param("--" + prefix + "-tags", it.getProp("tags"), "");
	reconnectTime = conf->getArgPInt("--" + prefix + "-reconnect-time", it.getProp("reconnectTime"), reconnectTime);
	bufSize = conf->getArgPInt("--" + prefix + "-buf-size", it.getProp("bufMaxSize"), bufSize);
	bufSyncTime = conf->getArgPInt("--" + prefix + "-buf-sync-time", it.getProp("bufSyncTimeout"), bufSyncTime);

	const string ff = conf->getArg2Param("--" + prefix + "-filter-field", it.getProp("filter_field"), "" );
	const string fv = conf->getArg2Param("--" + prefix + "-filter-value", it.getProp("filter_value"), "" );

	myinfo << myname << "(init): opentsdb host=" << host << ":" << port
		   << " " << ff << "='" << fv << "'"
		   << " prefix='" << tsdbPrefix << "'"
		   << " tags='" << tsdbTags << "'"
		   << endl;

	// try
	{
		auto conf = uniset_conf();

		xmlNode* snode = conf->getXMLSensorsSection();

		if( !snode )
		{
			ostringstream err;
			err << myname << "(init): Not found section <sensors>";
			mycrit << err.str() << endl;
			throw SystemError(err.str());
		}

		UniXML::iterator it1(snode);

		if( !it1.goChildren() )
		{
			ostringstream err;
			err << myname << "(init): section <sensors> empty?!";
			mycrit << err.str() << endl;
			throw SystemError(err.str());
		}

		for(; it1.getCurrent(); it1.goNext() )
		{
			if( !uniset::check_filter(it1, ff, fv) )
				continue;

			std::string pname = it1.getProp2("tsdb_name", it1.getProp("name"));

			if( pname.empty() )
			{
				ostringstream err;
				err << myname << "(init): Unknown name sensor id = '" << it1.getProp("id") << "'";
				mycrit << err.str() << endl;
				throw SystemError(err.str());
			}

			ObjectId sid = conf->getSensorID( it1.getProp("name") );

			if( sid == DefaultObjectId )
			{
				ostringstream err;
				err << myname << "(init): Unknown SensorID for '" << it1.getProp("name") << "'";
				mycrit << err.str();
				throw SystemError(err.str());
			}

			tsdbParams.emplace( sid, ParamInfo(pname, it1.getProp("tsdb_tags")) );
		}

		if( tsdbParams.empty() )
		{
			ostringstream err;
			err << myname << "(init): Not found items for send to OpenTSDB...";
			mycrit << err.str() << endl;
			throw SystemError(err.str());
		}

	}
}
//--------------------------------------------------------------------------------
void BackendOpenTSDB::help_print( int argc, const char* const* argv )
{
	cout << " Default prefix='opentsdb'" << endl;
	cout << "--prefix-name                - ID. Default: BackendOpenTSDB." << endl;
	cout << "--prefix-confnode            - configuration section name. Default: <NAME name='NAME'...> " << endl;
	cout << endl;
	cout << " OpenTSDB: " << endl;
	cout << "--prefix-host  ip                         - OpenTSDB: host. Default: localhost" << endl;
	cout << "--prefix-port  num                        - OpenTSDB: port. Default: 4242" << endl;
	cout << "--prefix-prefix name                      - OpenTSDB: prefix for data" << endl;
	cout << "--prefix-tags  'TAG1=VAL1 TAG2=VAL2...'   - OpenTSDB: tags for data" << endl;
	cout << "--prefix-reconnect-time msec              - Time for attempts to connect to DB. Default: 5 sec" << endl;
	cout << endl;
	cout << "--prefix-buf-size  sz        - Buffer before save to DB. Default: 100" << endl;
	cout << "--prefix-buf-sync-time msec  - Time period for forced data writing to DB. Default: 5 sec" << endl;
	cout << endl;
	cout << "--prefix-heartbeat-id name   - ID for heartbeat sensor." << endl;
	cout << "--prefix-heartbeat-max val   - max value for heartbeat sensor." << endl;
	cout << endl;
	cout << " Logs: " << endl;
	cout << "--prefix-log-...            - log control" << endl;
	cout << "             add-levels ...  " << endl;
	cout << "             del-levels ...  " << endl;
	cout << "             set-levels ...  " << endl;
	cout << "             logfile filanme " << endl;
	cout << "             no-debug " << endl;
	cout << " Base oprtions: " << endl;
	//	cout << UObject_SK::help() << endl;
	cout << " LogServer: " << endl;
	cout << "--prefix-run-logserver      - run logserver. Default: localhost:id" << endl;
	cout << "--prefix-logserver-host ip  - listen ip. Default: localhost" << endl;
	cout << "--prefix-logserver-port num - listen port. Default: ID" << endl;
	cout << LogServer::help_print("prefix-logserver") << endl;
}
// -----------------------------------------------------------------------------
std::shared_ptr<BackendOpenTSDB> BackendOpenTSDB::init_opendtsdb( int argc,
		const char* const* argv,
		uniset::ObjectId icID, const std::shared_ptr<SharedMemory>& ic,
		const std::string& prefix )
{
	auto conf = uniset_conf();

	string name = conf->getArgParam("--" + prefix + "-name", "BackendOpenTSDB");<commit_msg>(TSDB): minor fixes in help<commit_after>/*
 * Copyright (c) 2015 <NAME>.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, version 2.1.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Lesser Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
// -------------------------------------------------------------------------
#include <sstream>
#include <iomanip>
#include "Exceptions.h"
#include <Poco/Net/NetException.h>
#include "BackendOpenTSDB.h"
// -----------------------------------------------------------------------------
using namespace std;
using namespace uniset;
using namespace uniset::extensions;
// -----------------------------------------------------------------------------
BackendOpenTSDB::BackendOpenTSDB( uniset::ObjectId objId, xmlNode* cnode,
								  uniset::ObjectId shmId, const std::shared_ptr<SharedMemory>& ic,
								  const string& prefix ):
	UObject_SK(objId, cnode, string(prefix + "-")),
	prefix(prefix)
{
	auto conf = uniset_conf();

	if( ic )
		ic->logAgregator()->add(logAgregator());

	shm = make_shared<SMInterface>(shmId, ui, objId, ic);

	init(cnode);

	if( smTestID == DefaultObjectId && !tsdbParams.empty() )
	{
		// берём первый датчик из списка
		smTestID = tsdbParams.begin()->first;
	}
}
// -----------------------------------------------------------------------------
BackendOpenTSDB::~BackendOpenTSDB()
{
}
// -----------------------------------------------------------------------------
void BackendOpenTSDB::init( xmlNode* cnode )
{
	UniXML::iterator it(cnode);

	auto conf = uniset_conf();

	host = conf->getArg2Param("--" + prefix + "-host", it.getProp("host"), "localhost");
	port = conf->getArgPInt("--" + prefix + "-port", it.getProp("port"), port);
	tsdbPrefix = conf->getArg2Param("--" + prefix + "-prefix", it.getProp("prefix"), "");
	tsdbTags = conf->getArg2Param("--" + prefix + "-tags", it.getProp("tags"), "");
	reconnectTime = conf->getArgPInt("--" + prefix + "-reconnect-time", it.getProp("reconnectTime"), reconnectTime);
	bufSize = conf->getArgPInt("--" + prefix + "-buf-size", it.getProp("bufMaxSize"), bufSize);
	bufSyncTime = conf->getArgPInt("--" + prefix + "-buf-sync-time", it.getProp("bufSyncTimeout"), bufSyncTime);

	const string ff = conf->getArg2Param("--" + prefix + "-filter-field", it.getProp("filter_field"), "" );
	const string fv = conf->getArg2Param("--" + prefix + "-filter-value", it.getProp("filter_value"), "" );

	myinfo << myname << "(init): opentsdb host=" << host << ":" << port
		   << " " << ff << "='" << fv << "'"
		   << " prefix='" << tsdbPrefix << "'"
		   << " tags='" << tsdbTags << "'"
		   << endl;

	// try
	{
		auto conf = uniset_conf();

		xmlNode* snode = conf->getXMLSensorsSection();

		if( !snode )
		{
			ostringstream err;
			err << myname << "(init): Not found section <sensors>";
			mycrit << err.str() << endl;
			throw SystemError(err.str());
		}

		UniXML::iterator it1(snode);

		if( !it1.goChildren() )
		{
			ostringstream err;
			err << myname << "(init): section <sensors> empty?!";
			mycrit << err.str() << endl;
			throw SystemError(err.str());
		}

		for(; it1.getCurrent(); it1.goNext() )
		{
			if( !uniset::check_filter(it1, ff, fv) )
				continue;

			std::string pname = it1.getProp2("tsdb_name", it1.getProp("name"));

			if( pname.empty() )
			{
				ostringstream err;
				err << myname << "(init): Unknown name sensor id = '" << it1.getProp("id") << "'";
				mycrit << err.str() << endl;
				throw SystemError(err.str());
			}

			ObjectId sid = conf->getSensorID( it1.getProp("name") );

			if( sid == DefaultObjectId )
			{
				ostringstream err;
				err << myname << "(init): Unknown SensorID for '" << it1.getProp("name") << "'";
				mycrit << err.str();
				throw SystemError(err.str());
			}

			tsdbParams.emplace( sid, ParamInfo(pname, it1.getProp("tsdb_tags")) );
		}

		if( tsdbParams.empty() )
		{
			ostringstream err;
			err << myname << "(init): Not found items for send to OpenTSDB...";
			mycrit << err.str() << endl;
			throw SystemError(err.str());
		}

	}
}
//--------------------------------------------------------------------------------
void BackendOpenTSDB::help_print( int argc, const char* const* argv )
{
	cout << " Default prefix='opentsdb'" << endl;
	cout << "--prefix-name                - ID. Default: BackendOpenTSDB." << endl;
	cout << "--prefix-confnode            - configuration section name. Default: <NAME name='NAME'...> " << endl;
	cout << endl;
	cout << " OpenTSDB: " << endl;
	cout << "--prefix-host  ip                         - OpenTSDB: host. Default: localhost" << endl;
	cout << "--prefix-port  num                        - OpenTSDB: port. Default: 4242" << endl;
	cout << "--prefix-prefix name                      - OpenTSDB: prefix for data" << endl;
	cout << "--prefix-tags  'TAG1=VAL1 TAG2=VAL2...'   - OpenTSDB: tags for data" << endl;
	cout << "--prefix-reconnect-time msec              - Time for attempts to connect to DB. Default: 5 sec" << endl;
	cout << endl;
	cout << "--prefix-buf-size  sz        - Buffer before save to DB. Default: 100" << endl;
	cout << "--prefix-buf-sync-time msec  - Time period for forced data writing to DB. Default: 5 sec" << endl;
	cout << endl;
	cout << "--prefix-heartbeat-id name   - ID for heartbeat sensor." << endl;
	cout << "--prefix-heartbeat-max val   - max value for heartbeat sensor." << endl;
	cout << endl;
	cout << " Logs: " << endl;
	cout << "--prefix-log-...            - log control" << endl;
	cout << "             add-levels ...  " << endl;
	cout << "             del-levels ...  " << endl;
	cout << "             set-levels ...  " << endl;
	cout << "             logfile filanme " << endl;
	cout << "             no-debug " << endl;
	cout << endl;
	cout << " LogServer: " << endl;
	cout << "--prefix-run-logserver      - run logserver. Default: localhost:id" << endl;
	cout << "--prefix-logserver-host ip  - listen ip. Default: localhost" << endl;
	cout << "--prefix-logserver-port num - listen port. Default: ID" << endl;
	cout << LogServer::help_print("prefix-logserver") << endl;
}
// -----------------------------------------------------------------------------
std::shared_ptr<BackendOpenTSDB> BackendOpenTSDB::init_opendtsdb( int argc,
		const char* const* argv,
		uniset::ObjectId icID, const std::shared_ptr<SharedMemory>& ic,
		const std::string& prefix )
{
	auto conf = uniset_conf();

	string name = conf->getArgParam("--" + prefix + "-name", "BackendOpenTSDB");
{'fraction_non_alphanumeric': 0.24404589021202439, 'fraction_numerical': 0.00522799883822248, 'mean_word_length': 2.313206639403416, 'pattern_counts': {'":': 2, '<': 413, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7050639', 'n_tokens_mistral': 4545, 'n_tokens_neox': 4230, 'n_words': 1116}