This commit is contained in:
2005 2024-05-20 00:47:07 +02:00
commit ad0548e956
12 changed files with 2674 additions and 0 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
/target
result*

1962
Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

23
Cargo.toml Normal file
View file

@ -0,0 +1,23 @@
[package]
name = "i2pdmetrics"
version = "0.1.0"
edition = "2021"
[dependencies]
async-std = { version = "1.12.0", features = ["attributes", "tokio1"] }
derive-getters = "0.3.0"
derive_builder = "0.20.0"
dotenv = "0.15.0"
env_logger = "0.11.2"
envy = "0.4.2"
jsonrpc = "0.17.0"
log = "0.4.20"
metrics = "0.22.1"
metrics-exporter-prometheus = { version = "0.13.1", features = [
"http-listener",
] }
metrics-util = "0.16.2"
reqwest = "0.11.24"
serde = "1.0.197"
serde_json = "1.0.114"

4
deny.toml Normal file
View file

@ -0,0 +1,4 @@
[licenses]
allow = [
"MIT"
]

11
dockerfile Normal file
View file

@ -0,0 +1,11 @@
# 1. This tells docker to use the Rust official image
FROM rust:latest
# 2. Copy the files in your machine to the Docker image
COPY ./ ./
# Build your program for release
RUN cargo build --release
# Run the binary
CMD ["./target/release/i2pdmetrics"]

121
flake.lock Normal file
View file

@ -0,0 +1,121 @@
{
"nodes": {
"advisory-db": {
"flake": false,
"locked": {
"lastModified": 1714183630,
"narHash": "sha256-1BVft7ggSN2XXFeXQjazU3jN9wVECd9qp2mZx/8GDMk=",
"owner": "rustsec",
"repo": "advisory-db",
"rev": "35e7459a331d3e0c585e56dabd03006b9b354088",
"type": "github"
},
"original": {
"owner": "rustsec",
"repo": "advisory-db",
"type": "github"
}
},
"crane": {
"inputs": {
"nixpkgs": [
"nixpkgs"
]
},
"locked": {
"lastModified": 1716155989,
"narHash": "sha256-waAI5EvvISkQyw44awtrzdzqTVKsrSLbgiUHP6RM6BE=",
"owner": "ipetkov",
"repo": "crane",
"rev": "b7a1655564f96c28fa4c7a0d4888034c47f5ceb0",
"type": "github"
},
"original": {
"owner": "ipetkov",
"repo": "crane",
"type": "github"
}
},
"fenix": {
"inputs": {
"nixpkgs": [
"nixpkgs"
],
"rust-analyzer-src": []
},
"locked": {
"lastModified": 1716099865,
"narHash": "sha256-GrNswS37mF+Jj/GNb2uNapd11sR9IWf7j9WexybunPs=",
"owner": "nix-community",
"repo": "fenix",
"rev": "f7737feef42fa8abe70de20b9a13b845a113cfeb",
"type": "github"
},
"original": {
"owner": "nix-community",
"repo": "fenix",
"type": "github"
}
},
"flake-utils": {
"inputs": {
"systems": "systems"
},
"locked": {
"lastModified": 1710146030,
"narHash": "sha256-SZ5L6eA7HJ/nmkzGG7/ISclqe6oZdOZTNoesiInkXPQ=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "b1d9ab70662946ef0850d488da1c9019f3a9752a",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1716128955,
"narHash": "sha256-3DNg/PV+X2V7yn8b/fUR2ppakw7D9N4sjVBGk6nDwII=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "f9256de8281f2ccd04985ac5c30d8f69aefadbe8",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixpkgs-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"advisory-db": "advisory-db",
"crane": "crane",
"fenix": "fenix",
"flake-utils": "flake-utils",
"nixpkgs": "nixpkgs"
}
},
"systems": {
"locked": {
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"type": "github"
},
"original": {
"owner": "nix-systems",
"repo": "default",
"type": "github"
}
}
},
"root": "root",
"version": 7
}

217
flake.nix Normal file
View file

@ -0,0 +1,217 @@
{
description = "Build a cargo project";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
crane = {
url = "github:ipetkov/crane";
inputs.nixpkgs.follows = "nixpkgs";
};
fenix = {
url = "github:nix-community/fenix";
inputs.nixpkgs.follows = "nixpkgs";
inputs.rust-analyzer-src.follows = "";
};
flake-utils.url = "github:numtide/flake-utils";
advisory-db = {
url = "github:rustsec/advisory-db";
flake = false;
};
};
outputs =
{ self
, nixpkgs
, crane
, fenix
, flake-utils
, advisory-db
, ...
}:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = nixpkgs.legacyPackages.${system};
inherit (pkgs) lib;
craneLib = crane.mkLib pkgs;
src = craneLib.cleanCargoSource (craneLib.path ./.);
# Common arguments can be set here to avoid repeating them later
commonArgs = {
inherit src;
strictDeps = true;
buildInputs = with pkgs; [
pkg-config
openssl
] ++ lib.optionals pkgs.stdenv.isDarwin [
pkgs.libiconv
];
};
craneLibLLvmTools = craneLib.overrideToolchain
(fenix.packages.${system}.complete.withComponents [
"cargo"
"llvm-tools"
"rustc"
]);
# Build *just* the cargo dependencies, so we can reuse
# all of that work (e.g. via cachix) when running in CI
cargoArtifacts = craneLib.buildDepsOnly commonArgs;
# Build the actual crate itself, reusing the dependency
# artifacts from above.
i2pd-exporter = craneLib.buildPackage (commonArgs // {
inherit cargoArtifacts;
});
in
{
checks = {
# Build the crate as part of `nix flake check` for convenience
inherit i2pd-exporter;
# Run clippy (and deny all warnings) on the crate source,
# again, reusing the dependency artifacts from above.
#
# Note that this is done as a separate derivation so that
# we can block the CI if there are issues here, but not
# prevent downstream consumers from building our crate by itself.
i2pd-exporter-clippy = craneLib.cargoClippy (commonArgs // {
inherit cargoArtifacts;
cargoClippyExtraArgs = "--all-targets -- --deny warnings";
});
i2pd-exporter-doc = craneLib.cargoDoc (commonArgs // {
inherit cargoArtifacts;
});
# Check formatting
i2pd-exporter-fmt = craneLib.cargoFmt {
inherit src;
};
# Audit dependencies
i2pd-exporter-audit = craneLib.cargoAudit {
inherit src advisory-db;
};
# Audit licenses
i2pd-exporter-deny = craneLib.cargoDeny {
inherit src;
};
# Run tests with cargo-nextest
# Consider setting `doCheck = false` on `i2pd-exporter` if you do not want
# the tests to run twice
i2pd-exporter-nextest = craneLib.cargoNextest (commonArgs // {
inherit cargoArtifacts;
partitions = 1;
partitionType = "count";
});
};
packages = {
default = i2pd-exporter;
} // lib.optionalAttrs (!pkgs.stdenv.isDarwin) {
i2pd-exporter-llvm-coverage = craneLibLLvmTools.cargoLlvmCov (commonArgs // {
inherit cargoArtifacts;
});
};
apps.default = flake-utils.lib.mkApp {
drv = i2pd-exporter;
};
devShells.default = craneLib.devShell {
# Inherit inputs from checks.
checks = self.checks.${system};
LIBCLANG_PATH = "${pkgs.llvmPackages_17.libclang.lib}/lib";
RUST_BACKTRACE = 1;
RUST_SRC_PATH = "${pkgs.rust.packages.stable.rustPlatform.rustLibSrc}";
packages = with pkgs; [
rustfmt
rust-analyzer
clippy
openssl
sqlite
];
};
});
nixosModules.default =
{ config
, lib
, pkgs
, ...
}:
let
cfg = config.services.prometheus.exporters.i2pd-exporter;
in
{
options.services.prometheus.exporters.i2pd-exporter = {
enable = mkEnableOption (lib.mdDoc "lemmy a federated alternative to reddit in rust");
listenAddress = mkOption {
type = with types; nullOr str;
default = "127.0.0.1";
description = lib.mdDoc "The connection URI to use. Takes priority over the configuration file if set.";
};
port = mkOption {
type = types.port;
default = 5733;
description = lib.mdDoc "The connection URI to use. Takes priority over the configuration file if set.";
};
routerAddress = mkOption {
type = with types; nullOr str;
default = "http://127.0.0.1:7650";
description = lib.mdDoc "The connection URI to use. Takes priority over the configuration file if set.";
};
routerPassword = mkOption {
type = with types; nullOr str;
default = "itoopie";
description = lib.mdDoc "The connection URI to use. Takes priority over the configuration file if set.";
};
systemd.services.i2pd-exporter =
{
description = "I2PD prometheus exporter";
environment = {
IP = cfg.services.prometheus.exporters.i2pd-exporter.listenAddress;
PORT = cfg.services.prometheus.exporters.i2pd-exporter.port;
ADDRESS = cfg.services.prometheus.exporters.i2pd-exporter.routerAddress;
PASSWORD = cfg.services.prometheus.exporters.i2pd-exporter.routerPassword;
};
wantedBy = [ "multi-user.target" ];
serviceConfig = {
DynamicUser = true;
RuntimeDirectory = "i2pd-exporter";
ExecStart = "${cfg.server.package}/bin/i2pdexporter";
PrivateTmp = true;
MemoryDenyWriteExecute = true;
NoNewPrivileges = true;
};
};
};
};
}

3
readme.md Normal file
View file

@ -0,0 +1,3 @@
# I2PD exporter
A basic prometheus exporter that exports miscellaneous data using the i2pcontrol protocol such as peers, data sent & received and so on...

66
service.nix Normal file
View file

@ -0,0 +1,66 @@
{ lib, pkgs, config, utils, ... }:
with lib;
let
cfg = config.services.prometheus.exporters.i2pd-exporter;
settingsFormat = pkgs.formats.json { };
in
{
options.services.prometheus.exporters.i2pd-exporter = {
enable = mkEnableOption (lib.mdDoc "lemmy a federated alternative to reddit in rust");
listenAddress = mkOption {
type = with types; nullOr str;
default = "127.0.0.1";
description = lib.mdDoc "The connection URI to use. Takes priority over the configuration file if set.";
};
port = mkOption {
type = types.port;
default = 5733;
description = lib.mdDoc "The connection URI to use. Takes priority over the configuration file if set.";
};
routerAddress = mkOption {
type = with types; nullOr str;
default = "http://127.0.0.1:7650";
description = lib.mdDoc "The connection URI to use. Takes priority over the configuration file if set.";
};
routerPassword = mkOption {
type = with types; nullOr str;
default = "itoopie";
description = lib.mdDoc "The connection URI to use. Takes priority over the configuration file if set.";
};
systemd.services.i2pd-exporter =
{
description = "I2PD prometheus exporter";
environment = {
IP = cfg.services.prometheus.exporters.i2pd-exporter.listenAddress;
PORT = cfg.services.prometheus.exporters.i2pd-exporter.port;
ADDRESS = cfg.services.prometheus.exporters.i2pd-exporter.routerAddress;
PASSWORD = cfg.services.prometheus.exporters.i2pd-exporter.routerPassword;
};
wantedBy = [ "multi-user.target" ];
serviceConfig = {
DynamicUser = true;
RuntimeDirectory = "i2pd-exporter";
ExecStart = "${cfg.server.package}/bin/i2pdexporter";
PrivateTmp = true;
MemoryDenyWriteExecute = true;
NoNewPrivileges = true;
};
};
};
}

146
src/api.rs Normal file
View file

@ -0,0 +1,146 @@
use std::{default, f32::consts::E};
use derive_builder::Builder;
use serde::Deserialize;
use serde_json::json;
use crate::i2pmetrics::Metrics;
#[derive(Builder, Debug)]
pub struct I2PControl {
//
endpoint: reqwest::Url,
client: reqwest::Client,
#[builder(default = "false")]
authenticated: bool,
#[builder(default = "0")]
token: i64,
password: String,
}
#[derive(Debug)]
pub enum I2PControlError {
HostUnreachable,
InvalidResponse,
InvalidPassword,
}
impl I2PControl {
pub async fn auth(&mut self) -> Result<(), I2PControlError> {
let req = self
.client
.post(self.endpoint.clone())
.body(
json!({
"id": "1",
"method": "Authenticate",
"params": {
"API": 1,
"Password": self.password
},
"jsonrpc": "2.0"
})
.to_string(),
)
.send()
.await;
let response = match req {
Ok(data) => data,
Err(_) => return Err(I2PControlError::HostUnreachable),
};
let text = match response.text().await {
Ok(data) => data,
Err(_) => return Err(I2PControlError::InvalidResponse),
};
let data: RouterResponse = match serde_json::from_str(text.as_str()) {
Ok(dat) => dat,
Err(_) => return Err(I2PControlError::InvalidResponse),
};
match data.result.pointer("/token") {
None => return Err(I2PControlError::InvalidPassword),
Some(token) => match token.as_i64() {
Some(token) => {
self.authenticated = true;
self.token = token;
}
None => return Err(I2PControlError::InvalidResponse),
},
}
return Ok(());
}
pub async fn fetch(&self) -> Result<Metrics, I2PControlError> {
//
let response = self
.client
.post(self.endpoint.clone())
.body(
json!({
"id": "3",
"method": "RouterInfo",
"params": {
"i2p.router.uptime": "",
"i2p.router.version": "",
"i2p.router.net.bw.inbound.15s": "",
"i2p.router.net.bw.outbound.15s": "",
"i2p.router.net.status": "",
"i2p.router.net.tunnels.participating": "",
"i2p.router.net.tunnels.successrate": "",
"i2p.router.netdb.activepeers": "",
"i2p.router.netdb.knownpeers": "",
"i2p.router.net.total.received.bytes": "",
"i2p.router.net.total.sent.bytes": "",
"Token": self.token
},
"jsonrpc": "2.0"
})
.to_string(),
)
.send()
.await;
match response {
Ok(res) => {
let text = match res.text().await {
Ok(data) => data,
Err(error) => {
log::debug!("Failed to convert into text {}", error);
return Err(I2PControlError::InvalidResponse);
}
};
let data: RouterResponse = match serde_json::from_str(text.as_str()) {
Ok(data) => data,
Err(error) => {
log::debug!("Failed to convert into routerresponse {}", error);
return Err(I2PControlError::InvalidResponse);
}
};
let metrics: Metrics = match serde_json::from_value(data.result) {
Ok(metrics) => metrics,
Err(error) => {
return {
log::debug!("Failed to convert into metrics: {}", error);
Err(I2PControlError::InvalidResponse)
}
}
};
return Ok(metrics);
}
Err(error) => {
println!("error: {}", error);
return Err(I2PControlError::HostUnreachable);
}
}
}
}
#[derive(Deserialize)]
struct RouterResponse {
id: i32,
result: serde_json::Value,
jsonrpc: String,
}

41
src/i2pmetrics.rs Normal file
View file

@ -0,0 +1,41 @@
use derive_getters::Getters;
use serde::Deserialize;
#[derive(Deserialize, Debug, Getters, Clone)]
pub struct Metrics {
//
#[serde(alias = "i2p.router.uptime")]
uptime: i32,
#[serde(alias = "i2p.router.version")]
version: String,
#[serde(alias = "i2p.router.net.bw.inbound.15s")]
inbound_15s: f64,
#[serde(alias = "i2p.router.net.bw.outbound.15s")]
outbound_15s: f64,
#[serde(alias = "i2p.router.net.status")]
status: i32,
#[serde(alias = "i2p.router.net.tunnels.participating")]
transit_tunnels: i32,
#[serde(alias = "i2p.router.netdb.activepeers")]
active_peers: i32,
#[serde(alias = "i2p.router.netdb.knownpeers")]
known_peers: i32,
#[serde(alias = "i2p.router.net.tunnels.successrate")]
success_rate: i32,
#[serde(alias = "i2p.router.net.total.received.bytes")]
total_bytes_received: f64,
#[serde(alias = "i2p.router.net.total.sent.bytes")]
total_bytes_sent: f64,
}

78
src/main.rs Normal file
View file

@ -0,0 +1,78 @@
use std::{
net::{Ipv4Addr, SocketAddr, SocketAddrV4},
thread,
time::Duration,
};
use api::{I2PControl, I2PControlBuilder};
use dotenv::dotenv;
use metrics_exporter_prometheus::PrometheusBuilder;
use reqwest::Url;
use metrics::{counter, describe_counter, describe_gauge, describe_histogram, gauge, histogram};
use serde::Deserialize;
mod api;
mod i2pmetrics;
#[derive(Deserialize)]
struct Configuration {
ip: String,
port: i32,
router: String,
password: String,
}
#[async_std::main]
async fn main() {
let _ = env_logger::init();
let _ = dotenv();
let c = envy::from_env::<Configuration>().expect("Invalid variables given");
let mut client = I2PControlBuilder::default()
.client(
reqwest::ClientBuilder::new()
.danger_accept_invalid_certs(true)
.build()
.unwrap(),
)
.endpoint(Url::parse(&c.router).unwrap())
.password(c.password.into())
.build()
.unwrap();
match client.auth().await {
Ok(_) => (),
Err(error) => {
println!("Failed to authenticate: {:?}", error)
}
};
// Bulding metrics
let builder = PrometheusBuilder::new();
builder
.with_http_listener(SocketAddr::new(
std::net::IpAddr::V4(c.ip.parse().unwrap()),
c.port as u16,
))
.install()
.expect("failed to install Prometheus recorder");
// Setting gauges
loop {
thread::sleep(Duration::from_secs(1));
match client.fetch().await {
Ok(data) => {
counter!("i2p_uptime").absolute(*data.uptime() as u64);
gauge!("i2p_inbound_15s").set(*data.inbound_15s());
gauge!("i2p_outbound_15s").set(*data.outbound_15s());
gauge!("i2p_status").set(*data.status() as f64);
gauge!("i2p_transit_tunnels").set(*data.transit_tunnels() as f64);
gauge!("i2p_active_peers").set(*data.active_peers() as f64);
gauge!("i2p_known_peers").set(*data.known_peers() as f64);
gauge!("i2p_success_rate").set(*data.success_rate() as f64);
gauge!("i2p_total_bytes_received").set(*data.total_bytes_received());
gauge!("i2p_total_bytes_sent").set(*data.total_bytes_sent());
}
Err(_) => (),
}
}
}