|
| 1 | +// Copyright 2019-2020 CERN and copyright holders of ALICE O2. |
| 2 | +// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders. |
| 3 | +// All rights not expressly granted are reserved. |
| 4 | +// |
| 5 | +// This software is distributed under the terms of the GNU General Public |
| 6 | +// License v3 (GPL Version 3), copied verbatim in the file "COPYING". |
| 7 | +// |
| 8 | +// In applying this license CERN does not waive the privileges and immunities |
| 9 | +// granted to it by virtue of its status as an Intergovernmental Organization |
| 10 | +// or submit itself to any jurisdiction. |
| 11 | + |
| 12 | +// simple command-line program used to send metrics using the monitoring lib |
| 13 | + |
| 14 | +#include <Monitoring/MonitoringFactory.h> |
| 15 | +#include <stdint.h> |
| 16 | +#include <stdio.h> |
| 17 | +#include <unistd.h> |
| 18 | +using namespace o2::monitoring; |
| 19 | + |
| 20 | +void print_usage() |
| 21 | +{ |
| 22 | + printf("Monitoring command line utility to inject metrics.\n"); |
| 23 | + printf("Usage: o2-monitoring-send [options]\n"); |
| 24 | + printf("Options: \n"); |
| 25 | + printf(" -u ... Set monitoring URI (for metric output).\n"); |
| 26 | + printf(" -m ... Set metric name.\n"); |
| 27 | + printf(" -i ... Set metric value (int).\n"); |
| 28 | + printf(" -v Verbose mode.\n"); |
| 29 | + printf(" -h This help.\n"); |
| 30 | + printf("\nExample: o2-monitoring-send -u influxdb-stdout:// -m test.metric -i 12345\n"); |
| 31 | +} |
| 32 | + |
| 33 | + |
| 34 | +int main(int argc, char** argv) |
| 35 | +{ |
| 36 | + |
| 37 | + bool verbose = 0; // if set, prints detailed messages |
| 38 | + |
| 39 | + std::unique_ptr<Monitoring> monitoringCollector; |
| 40 | + char option; |
| 41 | + |
| 42 | + bool isOk = 1; |
| 43 | + const char *monitoringURI = nullptr; |
| 44 | + const char *monitoringValue = nullptr; |
| 45 | + const char *monitoringMetric = nullptr; |
| 46 | + |
| 47 | + // read options |
| 48 | + while ((option = getopt(argc, argv, "hvu:i:m:")) != -1) { |
| 49 | + switch (option) { |
| 50 | + |
| 51 | + case 'u': { |
| 52 | + monitoringURI = optarg; |
| 53 | + } break; |
| 54 | + |
| 55 | + case 'i': { |
| 56 | + monitoringValue = optarg; |
| 57 | + } break; |
| 58 | + |
| 59 | + case 'm': { |
| 60 | + monitoringMetric = optarg; |
| 61 | + } break; |
| 62 | + |
| 63 | + case 'v': { |
| 64 | + verbose = 1; |
| 65 | + } break; |
| 66 | + |
| 67 | + case 'h': |
| 68 | + print_usage(); |
| 69 | + return 0; |
| 70 | + |
| 71 | + default: |
| 72 | + print_usage(); |
| 73 | + return -1; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + if (monitoringURI == nullptr) { |
| 78 | + printf("Unspecified monitoring URI.\n"); |
| 79 | + isOk = 0; |
| 80 | + } |
| 81 | + |
| 82 | + if (monitoringMetric == nullptr) { |
| 83 | + printf("Unspecified monitoring metric.\n"); |
| 84 | + isOk = 0; |
| 85 | + } |
| 86 | + |
| 87 | + if (monitoringValue == nullptr) { |
| 88 | + printf("Unspecified monitoring value.\n"); |
| 89 | + isOk = 0; |
| 90 | + } |
| 91 | + |
| 92 | + if (!isOk) { |
| 93 | + printf("Failed to send metric: bad parameters.\n\n\n"); |
| 94 | + print_usage(); |
| 95 | + return -1; |
| 96 | + } |
| 97 | + |
| 98 | + // conversions |
| 99 | + int monitoringValueI = atoi(monitoringValue); |
| 100 | + |
| 101 | + // disable logs from monitoring lib |
| 102 | + setenv("O2_INFOLOGGER_MODE", "none", 1); |
| 103 | + |
| 104 | + if (verbose) { |
| 105 | + // summarize status |
| 106 | + printf("URI = %s\n", monitoringURI); |
| 107 | + printf("Metric = %s\n", monitoringMetric); |
| 108 | + printf("Value = %d (int)\n", monitoringValueI); |
| 109 | + printf("\n"); |
| 110 | + } |
| 111 | + |
| 112 | + isOk = 0; |
| 113 | + try { |
| 114 | + monitoringCollector = MonitoringFactory::Get(monitoringURI); |
| 115 | + monitoringCollector->send({ monitoringValueI, monitoringMetric }); |
| 116 | + isOk = 1; |
| 117 | + } |
| 118 | + catch (const std::exception &exc) { |
| 119 | + printf("Exception: %s\n", exc.what()); |
| 120 | + } |
| 121 | + catch (...) { |
| 122 | + printf("Undefined exception\n"); |
| 123 | + } |
| 124 | + |
| 125 | + MonitoringMeric |
| 126 | + |
| 127 | + if (!isOk) { |
| 128 | + printf("Failed to send metric\n"); |
| 129 | + return -1; |
| 130 | + } |
| 131 | + |
| 132 | + if (verbose) { |
| 133 | + printf("\nSuccess\n"); |
| 134 | + } |
| 135 | + return 0; |
| 136 | +} |
| 137 | + |
0 commit comments