2016-01-15 04:13:00 +00:00
|
|
|
|
#include <daemon/GraphiteWriter.h>
|
2016-02-09 17:06:50 +00:00
|
|
|
|
#include <daemon/BaseDaemon.h>
|
2016-01-15 03:55:07 +00:00
|
|
|
|
#include <Poco/Util/LayeredConfiguration.h>
|
|
|
|
|
#include <Poco/Util/Application.h>
|
|
|
|
|
#include <Poco/Net/DNS.h>
|
|
|
|
|
|
|
|
|
|
#include <mutex>
|
|
|
|
|
#include <iomanip>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
GraphiteWriter::GraphiteWriter(const std::string & config_name, const std::string & sub_path)
|
|
|
|
|
{
|
|
|
|
|
Poco::Util::LayeredConfiguration & config = Poco::Util::Application::instance().config();
|
|
|
|
|
port = config.getInt(config_name + ".port", 42000);
|
|
|
|
|
host = config.getString(config_name + ".host", "127.0.0.1");
|
|
|
|
|
timeout = config.getDouble(config_name + ".timeout", 0.1);
|
|
|
|
|
|
|
|
|
|
root_path = config.getString(config_name + ".root_path", "one_min");
|
|
|
|
|
if (root_path.size())
|
|
|
|
|
root_path += ".";
|
|
|
|
|
|
2016-03-31 20:13:56 +00:00
|
|
|
|
/** Что использовать в качестве имени сервера в названии метрики?
|
|
|
|
|
*
|
|
|
|
|
* По-умолчанию (для совместимости с существовавшим ранее поведением),
|
|
|
|
|
* в качестве имени сервера берётся строка, аналогичная uname -n,
|
|
|
|
|
* а затем к нему приписывается то, что указано в hostname_suffix, если есть.
|
|
|
|
|
* Часто серверы настроены так, что, например, для сервера example01-01-1.yandex.ru, uname -n будет выдавать example01-01-1
|
|
|
|
|
* Впрочем, uname -n может быть настроен произвольным образом. Он также может совпадать с FQDN.
|
|
|
|
|
*
|
|
|
|
|
* Если указано use_fqdn со значением true,
|
|
|
|
|
* то в качестве имени сервера берётся FQDN (uname -f),
|
|
|
|
|
* а значение hostname_suffix игнорируется.
|
|
|
|
|
*/
|
|
|
|
|
bool use_fqdn = config.getBool(config_name + ".use_fqdn", false);
|
|
|
|
|
|
|
|
|
|
std::string hostname_in_path = use_fqdn
|
|
|
|
|
? Poco::Net::DNS::thisHost().name() /// То же, что uname -f
|
|
|
|
|
: Poco::Net::DNS::hostName(); /// То же, что uname -n
|
2016-01-15 03:55:07 +00:00
|
|
|
|
|
|
|
|
|
/// Заменяем точки на подчёркивания, чтобы Graphite не интерпретировал их, как разделители пути.
|
|
|
|
|
std::replace(std::begin(hostname_in_path), std::end(hostname_in_path), '.', '_');
|
|
|
|
|
|
2016-03-31 20:13:56 +00:00
|
|
|
|
root_path += hostname_in_path;
|
|
|
|
|
|
|
|
|
|
if (!use_fqdn)
|
|
|
|
|
root_path += config.getString(config_name + ".hostname_suffix", "");
|
2016-01-15 03:55:07 +00:00
|
|
|
|
|
|
|
|
|
if (sub_path.size())
|
|
|
|
|
root_path += "." + sub_path;
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-21 08:01:41 +00:00
|
|
|
|
std::string getPostfix()
|
2016-01-15 03:55:07 +00:00
|
|
|
|
{
|
|
|
|
|
/// Угадываем имя среды по имени машинки
|
|
|
|
|
/// машинки имеют имена вида example01dt.yandex.ru
|
|
|
|
|
/// t - test
|
|
|
|
|
/// dev - development
|
|
|
|
|
/// никакого суффикса - production
|
|
|
|
|
|
|
|
|
|
std::stringstream path_full;
|
|
|
|
|
|
|
|
|
|
std::string hostname = Poco::Net::DNS::hostName();
|
|
|
|
|
hostname = hostname.substr(0, hostname.find('.'));
|
|
|
|
|
|
|
|
|
|
const std::string development_suffix = "dev";
|
|
|
|
|
if (hostname.back() == 't')
|
|
|
|
|
path_full << "test.";
|
|
|
|
|
else if (hostname.size() > development_suffix.size() &&
|
|
|
|
|
hostname.substr(hostname.size() - development_suffix.size()) == development_suffix)
|
|
|
|
|
path_full << "development.";
|
|
|
|
|
else
|
|
|
|
|
path_full << "production.";
|
|
|
|
|
|
2016-02-09 17:06:50 +00:00
|
|
|
|
const BaseDaemon & daemon = BaseDaemon::instance();
|
2016-01-15 03:55:07 +00:00
|
|
|
|
|
|
|
|
|
if (daemon.getLayer())
|
|
|
|
|
path_full << "layer" << std::setfill('0') << std::setw(3) << *daemon.getLayer() << ".";
|
|
|
|
|
|
|
|
|
|
/// Когда несколько демонов запускается на одной машине
|
|
|
|
|
/// к имени демона добавляется цифра.
|
|
|
|
|
/// Удалим последнюю цифру
|
|
|
|
|
std::locale locale;
|
|
|
|
|
std::string command_name = daemon.commandName();
|
|
|
|
|
if (std::isdigit(command_name.back(), locale))
|
|
|
|
|
command_name = command_name.substr(0, command_name.size() - 1);
|
|
|
|
|
path_full << command_name;
|
|
|
|
|
|
|
|
|
|
return path_full.str();
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-21 08:01:41 +00:00
|
|
|
|
std::string GraphiteWriter::getPerLayerPath(const std::string & prefix)
|
2016-01-15 03:55:07 +00:00
|
|
|
|
{
|
2016-01-21 08:01:41 +00:00
|
|
|
|
const std::string static postfix = getPostfix();
|
|
|
|
|
return prefix + "." + postfix;
|
2016-01-15 03:55:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string GraphiteWriter::getPerServerPath(const std::string & server_name, const std::string & root_path)
|
|
|
|
|
{
|
|
|
|
|
std::string path = root_path + "." + server_name;
|
|
|
|
|
std::replace(path.begin() + root_path.size() + 1, path.end(), '.', '_');
|
|
|
|
|
return path;
|
|
|
|
|
}
|