Utilisation de prometheus-cpp-dev

This commit is contained in:
2025-12-03 23:20:46 +01:00
parent 3b9d229559
commit 8c23e6b8f9
6 changed files with 170 additions and 77 deletions

View File

@@ -12,17 +12,22 @@ RUN set -eux; \
apt-get update; \ apt-get update; \
apt-get install -y \ apt-get install -y \
libncurses-dev \ libncurses-dev \
libmicrohttpd-dev;\ libmicrohttpd-dev \
apt-get clean libcurl4-openssl-dev \
zlib1g-dev \
prometheus-cpp-dev
RUN set -eux; \ RUN set -eux; \
git clone https://github.com/jelmd/libprom.git; \ apt-get update; \
cd libprom; \ apt-get install -y \
make build; \ libpaho-mqtt-dev
cd prom/build; \
make install; \ RUN set -eux; \
cd ../../promhttp/build; \ apt-get update; \
make install; apt-get install -y \
librabbitmq4 \
librabbitmq-dev;\
apt-get clean
WORKDIR /root WORKDIR /root

2
.gitignore vendored
View File

@@ -1 +1 @@
/output/ /build/

26
.vscode/launch.json vendored Normal file
View File

@@ -0,0 +1,26 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug geii_exporter",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/geii_exporter",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Activer les pretty-printers pour gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "CMake: build"
}
]
}

36
.vscode/tasks.json vendored
View File

@@ -1,33 +1,21 @@
{ {
"version": "2.0.0",
"tasks": [ "tasks": [
{ {
"type": "cppbuild", "label": "CMake: build",
"label": "C/C++: gcc build active file", "type": "shell",
"command": "/usr/bin/gcc", "command": "cmake --build build --config Debug",
"args": [
"-fdiagnostics-color=always",
"-g",
"${workspaceFolder}${pathSeparator}main.cpp",
"-lm",
"-lncursesw",
"-lprom",
"-lpromhttp",
"-lmicrohttpd",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": { "group": {
"kind": "build", "kind": "build",
"isDefault": true "isDefault": true
}, },
"detail": "Task generated by Debugger." "problemMatcher": ["$gcc"]
},
{
"label": "CMake: configure",
"type": "shell",
"command": "cmake -S . -B build -DCMAKE_EXPORT_COMPILE_COMMANDS=ON",
"problemMatcher": ["$gcc"]
} }
], ]
"version": "2.0.0"
} }

57
CMakeLists.txt Normal file
View File

@@ -0,0 +1,57 @@
cmake_minimum_required(VERSION 3.15)
project(geii_exporter LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
# Activer warnings utiles
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wpedantic")
# Executable principal
add_executable(geii_exporter
main.cpp
)
# -------------------------------
# Librairies systèmes
# -------------------------------
# CURL
find_package(CURL REQUIRED)
# Microhttpd
find_library(MICROHTTPD_LIB microhttpd REQUIRED)
# Threads et compression
find_package(Threads REQUIRED)
find_library(Z_LIB z REQUIRED)
# ncursesw
find_library(NCURSESW_LIB ncursesw REQUIRED)
# RabbitMQ C client
find_library(RABBITMQ_LIB rabbitmq REQUIRED)
# Paho MQTT C client
find_library(PAHO_MQTT3C_LIB paho-mqtt3c REQUIRED)
# -------------------------------
# Prometheus C++
# -------------------------------
find_package(prometheus-cpp REQUIRED)
# -------------------------------
# Lien des bibliothèques
# -------------------------------
target_link_libraries(geii_exporter
prometheus-cpp::core
prometheus-cpp::pull
${CURL_LIBRARIES}
${MICROHTTPD_LIB}
Threads::Threads
${Z_LIB}
${NCURSESW_LIB}
${RABBITMQ_LIB}
${PAHO_MQTT3C_LIB}
)

View File

@@ -6,10 +6,10 @@
#include "main.h" #include "main.h"
#include "AutomForArduino.cpp" #include "AutomForArduino.cpp"
extern "C" { #include <prometheus/counter.h>
#include "libprom/prom.h" #include <prometheus/gauge.h>
#include "libprom/promhttp.h" #include <prometheus/registry.h>
} #include <prometheus/exposer.h>
// Constantes de fonctionnement // Constantes de fonctionnement
#define LEVEL_MIN 2 #define LEVEL_MIN 2
@@ -34,10 +34,11 @@ TemporisationRetardMontee tempo4(2000);
// Prometheus // Prometheus
// ************************************************************ // ************************************************************
struct MHD_Daemon *server; using namespace prometheus;
prom_counter_t *pm_pompe; std::shared_ptr<Registry> registry;
prom_gauge_t *pm_debit; Gauge* debit_entree = nullptr;
Gauge* debit_sortie = nullptr;
// ************************************************************ // ************************************************************
int main(int argc, char *argv[]) int main(int argc, char *argv[])
@@ -83,8 +84,7 @@ int main(int argc, char *argv[])
endwin(); // Termine ncurses et rétablit le terminal endwin(); // Termine ncurses et rétablit le terminal
puts("Fin du programme"); puts("Fin du programme");
pcr_destroy(PROM_COLLECTOR_REGISTRY);
promhttp_stop_daemon(server);
return 0; return 0;
} }
@@ -781,6 +781,23 @@ void AffichageGraphe(int y, int x, double value)
*/ */
void InitPrometheus() void InitPrometheus()
{ {
static Exposer exposer{"0.0.0.0:8099"};
// Le registre central
registry = std::make_shared<Registry>();
exposer.RegisterCollectable(registry);
// Exemple : gauge (comme votre debit)
auto& gauge_family = BuildGauge()
.Name("geii_debit")
.Help("Débit en l/s")
.Register(*registry);
debit_entree = &gauge_family.Add({{"numero", "entree"}});
debit_sortie = &gauge_family.Add({{"numero", "sortie"}});
/*
int result = pcr_init(0,"geii_"); int result = pcr_init(0,"geii_");
std::array<const char*, 1> labels = { "numero" }; std::array<const char*, 1> labels = { "numero" };
@@ -802,18 +819,18 @@ void InitPrometheus()
printf("Impossible de démarrer le serveur HTTP\n"); printf("Impossible de démarrer le serveur HTTP\n");
exit(1); exit(1);
} }
*/
} }
void ProcessPrometheus() void ProcessPrometheus()
{ {
// raising = 1 => front montant sur la sortie // raising = 1 => front montant sur la sortie
/*
if (_digital[OUT_PUMP_1].raising) { if (_digital[OUT_PUMP_1].raising) {
std::array<const char*, 1> labels1 = { "1" }; std::array<const char*, 1> labels1 = { "1" };
prom_counter_inc(pm_pompe, labels1.data()); prom_counter_inc(pm_pompe, labels1.data());
} }
*/
std::array<const char*, 1> labels1 = { "sortie" }; debit_entree->Set(_digital[IN_FLOW_OUT].dvalue);
std::array<const char*, 1> labels2 = { "entree" }; debit_sortie->Set(_digital[IN_FLOW_IN].dvalue);
prom_gauge_set(pm_debit, _digital[IN_FLOW_OUT].dvalue, labels1.data());
prom_gauge_set(pm_debit, _digital[IN_FLOW_IN].dvalue, labels2.data());
} }