|
| 1 | +/// |
| 2 | +/// \file Zabbix.cxx |
| 3 | +/// \author Adam Wegrzynek <[email protected]> |
| 4 | +/// |
| 5 | + |
| 6 | +#include "Zabbix.h" |
| 7 | +#include <boost/lexical_cast.hpp> |
| 8 | +#include <boost/property_tree/json_parser.hpp> |
| 9 | +#include <string> |
| 10 | +#include "../Transports/TCP.h" |
| 11 | +#include "../Exceptions/MonitoringInternalException.h" |
| 12 | + |
| 13 | +namespace AliceO2 |
| 14 | +{ |
| 15 | +/// ALICE O2 Monitoring system |
| 16 | +namespace Monitoring |
| 17 | +{ |
| 18 | +/// Monitoring backends |
| 19 | +namespace Backends |
| 20 | +{ |
| 21 | + |
| 22 | +Zabbix::Zabbix(const std::string &hostname, int port) |
| 23 | +{ |
| 24 | + transport = std::make_unique<Transports::TCP>(hostname, port); |
| 25 | + MonLogger::Get() << "Zabbix/TCP backend initialized" |
| 26 | + << " ("<< hostname << ":" << port << ")" << MonLogger::End(); |
| 27 | +} |
| 28 | + |
| 29 | +inline std::string Zabbix::convertTimestamp(const std::chrono::time_point<std::chrono::system_clock>& timestamp) |
| 30 | +{ |
| 31 | + std::string converted = std::to_string( |
| 32 | + std::chrono::duration_cast <std::chrono::nanoseconds>( |
| 33 | + timestamp.time_since_epoch() |
| 34 | + ).count()); |
| 35 | + converted.erase(converted.begin()+10, converted.end()); |
| 36 | + return converted; |
| 37 | +} |
| 38 | + |
| 39 | +std::string Zabbix::metricToZabbix(const Metric& metric) |
| 40 | +{ |
| 41 | + // create JSON payload |
| 42 | + boost::property_tree::ptree request, dataArray, data; |
| 43 | + data.put<std::string>("host", hostname); |
| 44 | + data.put<std::string>("key", metric.getName()); |
| 45 | + data.put<std::string>("value", boost::lexical_cast<std::string>(metric.getValue())); |
| 46 | + data.put<std::string>("clock", convertTimestamp(metric.getTimestamp())); |
| 47 | + |
| 48 | + dataArray.push_back(std::make_pair("", data)); |
| 49 | + request.put<std::string>("request", "sender data"); |
| 50 | + request.add_child("data", dataArray); |
| 51 | + |
| 52 | + std::stringstream ss; |
| 53 | + write_json(ss, request); |
| 54 | + |
| 55 | + std::string noWhiteSpaces = ss.str(); |
| 56 | + noWhiteSpaces.erase(std::remove_if( noWhiteSpaces.begin(), noWhiteSpaces.end(), |
| 57 | + [](char c){ return (c =='\r' || c =='\t' || c == ' ' || c == '\n');}), noWhiteSpaces.end() ); |
| 58 | + noWhiteSpaces.insert(18, " "); |
| 59 | + |
| 60 | + // prepare Zabbix header |
| 61 | + uint32_t length = noWhiteSpaces.length(); |
| 62 | + std::vector<unsigned char> header = { |
| 63 | + 'Z', 'B', 'X', 'D', '\x01', |
| 64 | + static_cast<unsigned char>(length & 0xFF), |
| 65 | + static_cast<unsigned char>((length >> 8) & 0x00FF), |
| 66 | + static_cast<unsigned char>((length >> 16) & 0x0000FF), |
| 67 | + static_cast<unsigned char>((length >> 24) & 0x000000FF), |
| 68 | + '\x00','\x00','\x00','\x00' |
| 69 | + }; |
| 70 | + |
| 71 | + return std::string(header.begin(), header.end()) + noWhiteSpaces; |
| 72 | +} |
| 73 | + |
| 74 | +void Zabbix::send(const Metric& metric) { |
| 75 | + try { |
| 76 | + transport->send(metricToZabbix(metric)); |
| 77 | + } catch (MonitoringInternalException&) { |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +void Zabbix::addGlobalTag(std::string name, std::string value) |
| 82 | +{ |
| 83 | + if (name == "hostname") { |
| 84 | + hostname = value; |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +} // namespace Backends |
| 89 | +} // namespace Monitoring |
| 90 | +} // namespace AliceO2 |
0 commit comments