Skip to content

Commit 4a5a4d2

Browse files
authored
v3.19.6
adding o2-monitoring-send utility
2 parents fcfe139 + d451b5f commit 4a5a4d2

File tree

3 files changed

+151
-1
lines changed

3 files changed

+151
-1
lines changed

CMakeLists.txt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ endif()
3131

3232
# Define project
3333
project(Monitoring
34-
VERSION 3.19.5
34+
VERSION 3.19.6
3535
DESCRIPTION "O2 Monitoring library"
3636
LANGUAGES CXX
3737
)
@@ -279,6 +279,17 @@ if(RdKafka_FOUND)
279279
endif()
280280

281281

282+
# executable: o2-monitoring-send
283+
add_executable(
284+
o2-monitoring-send
285+
src/sendMetric.cxx
286+
)
287+
target_link_libraries(
288+
o2-monitoring-send
289+
Monitoring
290+
)
291+
install(TARGETS o2-monitoring-send)
292+
282293
####################################
283294
# Generate protobuf
284295
####################################

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ send(Metric{"throughput"}.addValue(100, "tx").addValue(200, "rx"))
110110
111111
See how it works in the example: [examples/1-Basic.cxx](examples/1-Basic.cxx).
112112
113+
Metrics can also be injected from the command line using the o2-monitoring-send utility (self-documented).
114+
113115
## Advanced features
114116
115117
### Metric verbosity

src/sendMetric.cxx

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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

Comments
 (0)