diff --git a/CMakeLists.txt b/CMakeLists.txt index ea561567..f72725a3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -31,7 +31,7 @@ endif() # Define project project(Monitoring - VERSION 3.19.5 + VERSION 3.19.6 DESCRIPTION "O2 Monitoring library" LANGUAGES CXX ) @@ -279,6 +279,17 @@ if(RdKafka_FOUND) endif() +# executable: o2-monitoring-send +add_executable( + o2-monitoring-send + src/sendMetric.cxx +) +target_link_libraries( + o2-monitoring-send + Monitoring +) +install(TARGETS o2-monitoring-send) + #################################### # Generate protobuf #################################### diff --git a/README.md b/README.md index 2801b819..82066f6d 100644 --- a/README.md +++ b/README.md @@ -110,6 +110,8 @@ send(Metric{"throughput"}.addValue(100, "tx").addValue(200, "rx")) See how it works in the example: [examples/1-Basic.cxx](examples/1-Basic.cxx). +Metrics can also be injected from the command line using the o2-monitoring-send utility (self-documented). + ## Advanced features ### Metric verbosity diff --git a/src/sendMetric.cxx b/src/sendMetric.cxx new file mode 100644 index 00000000..81acea39 --- /dev/null +++ b/src/sendMetric.cxx @@ -0,0 +1,137 @@ +// Copyright 2019-2020 CERN and copyright holders of ALICE O2. +// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders. +// All rights not expressly granted are reserved. +// +// This software is distributed under the terms of the GNU General Public +// License v3 (GPL Version 3), copied verbatim in the file "COPYING". +// +// In applying this license CERN does not waive the privileges and immunities +// granted to it by virtue of its status as an Intergovernmental Organization +// or submit itself to any jurisdiction. + +// simple command-line program used to send metrics using the monitoring lib + +#include +#include +#include +#include +using namespace o2::monitoring; + +void print_usage() +{ + printf("Monitoring command line utility to inject metrics.\n"); + printf("Usage: o2-monitoring-send [options]\n"); + printf("Options: \n"); + printf(" -u ... Set monitoring URI (for metric output).\n"); + printf(" -m ... Set metric name.\n"); + printf(" -i ... Set metric value (int).\n"); + printf(" -v Verbose mode.\n"); + printf(" -h This help.\n"); + printf("\nExample: o2-monitoring-send -u influxdb-stdout:// -m test.metric -i 12345\n"); +} + + +int main(int argc, char** argv) +{ + + bool verbose = 0; // if set, prints detailed messages + + std::unique_ptr monitoringCollector; + char option; + + bool isOk = 1; + const char *monitoringURI = nullptr; + const char *monitoringValue = nullptr; + const char *monitoringMetric = nullptr; + + // read options + while ((option = getopt(argc, argv, "hvu:i:m:")) != -1) { + switch (option) { + + case 'u': { + monitoringURI = optarg; + } break; + + case 'i': { + monitoringValue = optarg; + } break; + + case 'm': { + monitoringMetric = optarg; + } break; + + case 'v': { + verbose = 1; + } break; + + case 'h': + print_usage(); + return 0; + + default: + print_usage(); + return -1; + } + } + + if (monitoringURI == nullptr) { + printf("Unspecified monitoring URI.\n"); + isOk = 0; + } + + if (monitoringMetric == nullptr) { + printf("Unspecified monitoring metric.\n"); + isOk = 0; + } + + if (monitoringValue == nullptr) { + printf("Unspecified monitoring value.\n"); + isOk = 0; + } + + if (!isOk) { + printf("Failed to send metric: bad parameters.\n\n\n"); + print_usage(); + return -1; + } + + // conversions + int monitoringValueI = atoi(monitoringValue); + + // disable logs from monitoring lib + setenv("O2_INFOLOGGER_MODE", "none", 1); + + if (verbose) { + // summarize status + printf("URI = %s\n", monitoringURI); + printf("Metric = %s\n", monitoringMetric); + printf("Value = %d (int)\n", monitoringValueI); + printf("\n"); + } + + isOk = 0; + try { + monitoringCollector = MonitoringFactory::Get(monitoringURI); + monitoringCollector->send({ monitoringValueI, monitoringMetric }); + isOk = 1; + } + catch (const std::exception &exc) { + printf("Exception: %s\n", exc.what()); + } + catch (...) { + printf("Undefined exception\n"); + } + + MonitoringMeric + + if (!isOk) { + printf("Failed to send metric\n"); + return -1; + } + + if (verbose) { + printf("\nSuccess\n"); + } + return 0; +} +