Adding dnsmasq leases file parser
This commit is contained in:
parent
022eb229ee
commit
1f0ebfaad7
@ -5,5 +5,7 @@ set(CMAKE_CXX_STANDARD 20)
|
|||||||
|
|
||||||
find_package(prometheus-cpp CONFIG REQUIRED)
|
find_package(prometheus-cpp CONFIG REQUIRED)
|
||||||
|
|
||||||
add_executable(aliencat_exporter main.cpp)
|
add_executable(aliencat_exporter main.cpp
|
||||||
|
DataSources/DnsmasqSource.cpp
|
||||||
|
DataSources/DnsmasqSource.h)
|
||||||
target_link_libraries(aliencat_exporter PRIVATE prometheus-cpp::pull)
|
target_link_libraries(aliencat_exporter PRIVATE prometheus-cpp::pull)
|
||||||
|
|||||||
38
DataSources/DnsmasqSource.cpp
Normal file
38
DataSources/DnsmasqSource.cpp
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
//
|
||||||
|
// Created by Мустафаев Селим Мустафаевич on 01.01.2024.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "DnsmasqSource.h"
|
||||||
|
#include <exception>
|
||||||
|
#include <sstream>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
DnsmasqSource::DnsmasqSource() {
|
||||||
|
_leases.open("/var/lib/misc/dnsmasq.leases");
|
||||||
|
if(_leases.fail()) {
|
||||||
|
throw std::runtime_error("failed to open leases file");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<prometheus::Labels> DnsmasqSource::readLeases() {
|
||||||
|
_leases.clear();
|
||||||
|
_leases.seekg(0);
|
||||||
|
|
||||||
|
std::string line, time, mac, ip, host, id;
|
||||||
|
std::stringstream ss;
|
||||||
|
std::vector<prometheus::Labels> result;
|
||||||
|
|
||||||
|
while(std::getline(_leases, line)) {
|
||||||
|
ss << line;
|
||||||
|
ss >> time >> mac >> ip >> host >> id;
|
||||||
|
result.push_back({
|
||||||
|
{"time", time},
|
||||||
|
{"mac", mac},
|
||||||
|
{"ip", ip},
|
||||||
|
{"host", host},
|
||||||
|
{"id", id}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
22
DataSources/DnsmasqSource.h
Normal file
22
DataSources/DnsmasqSource.h
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
//
|
||||||
|
// Created by Мустафаев Селим Мустафаевич on 01.01.2024.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef ALIENCAT_EXPORTER_DNSMASQSOURCE_H
|
||||||
|
#define ALIENCAT_EXPORTER_DNSMASQSOURCE_H
|
||||||
|
|
||||||
|
#include <prometheus/counter.h>
|
||||||
|
#include <fstream>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
class DnsmasqSource {
|
||||||
|
private:
|
||||||
|
std::ifstream _leases;
|
||||||
|
|
||||||
|
public:
|
||||||
|
DnsmasqSource();
|
||||||
|
std::vector<prometheus::Labels> readLeases();
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#endif //ALIENCAT_EXPORTER_DNSMASQSOURCE_H
|
||||||
18
main.cpp
18
main.cpp
@ -1,3 +1,5 @@
|
|||||||
|
#include "DataSources/DnsmasqSource.h"
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
@ -7,20 +9,22 @@
|
|||||||
#include <prometheus/counter.h>
|
#include <prometheus/counter.h>
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
prometheus::Exposer exposer{"127.0.0.1:9000"};
|
prometheus::Exposer exposer{"0.0.0.0:9000"};
|
||||||
prometheus::Registry registry;
|
auto registry = std::make_shared<prometheus::Registry>();
|
||||||
|
DnsmasqSource dnsmasq;
|
||||||
|
|
||||||
auto& gouge = prometheus::BuildGauge()
|
auto& gouge = prometheus::BuildGauge()
|
||||||
.Name("aliencat_dnsmasq_leases")
|
.Name("aliencat_dnsmasq_leases")
|
||||||
.Register(registry);
|
.Help("Dnsmasq leases detail info")
|
||||||
|
.Register(*registry);
|
||||||
|
|
||||||
auto& counter = prometheus::BuildCounter()
|
exposer.RegisterCollectable(registry);
|
||||||
.Name("aliencat_dnsmasq_leases")
|
|
||||||
.Register(registry);
|
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||||
gouge.Add({"mac", ""}).Set(0);
|
for(const auto& leaseLabels: dnsmasq.readLeases()) {
|
||||||
|
gouge.Add(leaseLabels).Set(1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user