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 install -y \
libncurses-dev \
libmicrohttpd-dev;\
apt-get clean
libmicrohttpd-dev \
libcurl4-openssl-dev \
zlib1g-dev \
prometheus-cpp-dev
RUN set -eux; \
git clone https://github.com/jelmd/libprom.git; \
cd libprom; \
make build; \
cd prom/build; \
make install; \
cd ../../promhttp/build; \
make install;
apt-get update; \
apt-get install -y \
libpaho-mqtt-dev
RUN set -eux; \
apt-get update; \
apt-get install -y \
librabbitmq4 \
librabbitmq-dev;\
apt-get clean
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": [
{
"type": "cppbuild",
"label": "C/C++: gcc build active file",
"command": "/usr/bin/gcc",
"args": [
"-fdiagnostics-color=always",
"-g",
"${workspaceFolder}${pathSeparator}main.cpp",
"-lm",
"-lncursesw",
"-lprom",
"-lpromhttp",
"-lmicrohttpd",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"label": "CMake: build",
"type": "shell",
"command": "cmake --build build --config Debug",
"group": {
"kind": "build",
"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 "AutomForArduino.cpp"
extern "C" {
#include "libprom/prom.h"
#include "libprom/promhttp.h"
}
#include <prometheus/counter.h>
#include <prometheus/gauge.h>
#include <prometheus/registry.h>
#include <prometheus/exposer.h>
// Constantes de fonctionnement
#define LEVEL_MIN 2
@@ -34,10 +34,11 @@ TemporisationRetardMontee tempo4(2000);
// Prometheus
// ************************************************************
struct MHD_Daemon *server;
using namespace prometheus;
prom_counter_t *pm_pompe;
prom_gauge_t *pm_debit;
std::shared_ptr<Registry> registry;
Gauge* debit_entree = nullptr;
Gauge* debit_sortie = nullptr;
// ************************************************************
int main(int argc, char *argv[])
@@ -83,8 +84,7 @@ int main(int argc, char *argv[])
endwin(); // Termine ncurses et rétablit le terminal
puts("Fin du programme");
pcr_destroy(PROM_COLLECTOR_REGISTRY);
promhttp_stop_daemon(server);
return 0;
}
@@ -781,6 +781,23 @@ void AffichageGraphe(int y, int x, double value)
*/
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_");
std::array<const char*, 1> labels = { "numero" };
@@ -802,18 +819,18 @@ void InitPrometheus()
printf("Impossible de démarrer le serveur HTTP\n");
exit(1);
}
*/
}
void ProcessPrometheus()
{
// raising = 1 => front montant sur la sortie
/*
if (_digital[OUT_PUMP_1].raising) {
std::array<const char*, 1> labels1 = { "1" };
prom_counter_inc(pm_pompe, labels1.data());
}
std::array<const char*, 1> labels1 = { "sortie" };
std::array<const char*, 1> labels2 = { "entree" };
prom_gauge_set(pm_debit, _digital[IN_FLOW_OUT].dvalue, labels1.data());
prom_gauge_set(pm_debit, _digital[IN_FLOW_IN].dvalue, labels2.data());
*/
debit_entree->Set(_digital[IN_FLOW_OUT].dvalue);
debit_sortie->Set(_digital[IN_FLOW_IN].dvalue);
}