1
0
Fork 0

Export booted system and kernel hash as well

This commit is contained in:
clerie 2023-03-23 20:50:08 +01:00
parent 7b674a8262
commit 0c468b4953
4 changed files with 25 additions and 12 deletions

2
Cargo.lock generated
View File

@ -450,7 +450,7 @@ dependencies = [
[[package]]
name = "nixos-exporter"
version = "0.2.0"
version = "0.3.0"
dependencies = [
"axum",
"reqwest",

View File

@ -1,6 +1,6 @@
[package]
name = "nixos-exporter"
version = "0.2.0"
version = "0.3.0"
edition = "2021"
[dependencies]

View File

@ -10,7 +10,7 @@
in {
nixos-exporter = pkgs.rustPlatform.buildRustPackage rec {
pname = "nixos-exporter";
version = "0.1.0";
version = "0.3.0";
src = ./.;

View File

@ -59,12 +59,12 @@ impl AppState{
}
fn parse_nix_store_path(path: std::path::PathBuf) -> Result<(String, String), String> {
let (hash, name) = path.file_name()
.ok_or_else(String::default)?
let (hash, name) = path.iter().nth(3)
.ok_or_else(|| String::from("Can't read store path name"))?
.to_str()
.ok_or_else(String::default)?
.ok_or_else(|| String::from("Failed converting store path name to string"))?
.split_once("-")
.ok_or_else(String::default)?;
.ok_or_else(|| String::from("Failed splitting store path name for hash and name"))?;
return Ok((hash.to_string(), name.to_string()));
}
@ -143,20 +143,33 @@ async fn main() {
.unwrap();
}
fn get_current_system() -> Result<(String, String), String> {
let symlink = std::fs::read_link("/run/current-system").map_err(|err| err.to_string())?;
fn parse_symlink(path: String) -> Result<(String, String), String> {
let symlink = std::fs::read_link(path).map_err(|err| err.to_string())?;
let (hash, name) = parse_nix_store_path(symlink)?;
Ok((String::from(hash), String::from(name)))
}
async fn metrics() -> Result<(StatusCode, impl IntoResponse), (StatusCode, impl IntoResponse)> {
let (hash, name) = get_current_system().map_err(|_err| (StatusCode::INTERNAL_SERVER_ERROR, ""))?;
fn gen_prometheus_metric(path: String, infix: String) -> Result<String, String> {
let (hash, name) = parse_symlink(path).map_err(|err| err)?;
return Ok(format!("nixos_{}_hash{{hash=\"{}\"}} 1\nnixos_{}_name{{name=\"{}\"}} 1\n", infix, hash, infix, name));
}
async fn metrics() -> Result<(StatusCode, impl IntoResponse), (StatusCode, impl IntoResponse)> {
let mut out = String::new();
out.push_str(&gen_prometheus_metric(String::from("/run/current-system"), String::from("current_system"))
.map_err(|err| (StatusCode::INTERNAL_SERVER_ERROR, err))?);
out.push_str(&gen_prometheus_metric(String::from("/run/current-system/kernel"), String::from("current_system_kernel"))
.map_err(|err| (StatusCode::INTERNAL_SERVER_ERROR, err))?);
out.push_str(&gen_prometheus_metric(String::from("/run/booted-system"), String::from("booted_system"))
.map_err(|err| (StatusCode::INTERNAL_SERVER_ERROR, err))?);
out.push_str(&gen_prometheus_metric(String::from("/run/booted-system/kernel"), String::from("booted_system_kernel"))
.map_err(|err| (StatusCode::INTERNAL_SERVER_ERROR, err))?);
return Ok((
StatusCode::OK,
format!("nixos_current_system_hash{{hash=\"{}\"}} 1\nnixos_current_system_name{{name=\"{}\"}} 1\n", hash, name)
out,
));
}