Export booted system and kernel hash as well
This commit is contained in:
parent
7b674a8262
commit
6c006f6384
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -450,7 +450,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "nixos-exporter"
|
name = "nixos-exporter"
|
||||||
version = "0.2.0"
|
version = "0.3.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"axum",
|
"axum",
|
||||||
"reqwest",
|
"reqwest",
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "nixos-exporter"
|
name = "nixos-exporter"
|
||||||
version = "0.2.0"
|
version = "0.3.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
31
src/main.rs
31
src/main.rs
@ -59,12 +59,12 @@ impl AppState{
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn parse_nix_store_path(path: std::path::PathBuf) -> Result<(String, String), String> {
|
fn parse_nix_store_path(path: std::path::PathBuf) -> Result<(String, String), String> {
|
||||||
let (hash, name) = path.file_name()
|
let (hash, name) = path.iter().nth(3)
|
||||||
.ok_or_else(String::default)?
|
.ok_or_else(|| String::from("Can't read store path name"))?
|
||||||
.to_str()
|
.to_str()
|
||||||
.ok_or_else(String::default)?
|
.ok_or_else(|| String::from("Failed converting store path name to string"))?
|
||||||
.split_once("-")
|
.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()));
|
return Ok((hash.to_string(), name.to_string()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -143,20 +143,33 @@ async fn main() {
|
|||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_current_system() -> Result<(String, String), String> {
|
fn parse_symlink(path: String) -> Result<(String, String), String> {
|
||||||
let symlink = std::fs::read_link("/run/current-system").map_err(|err| err.to_string())?;
|
let symlink = std::fs::read_link(path).map_err(|err| err.to_string())?;
|
||||||
|
|
||||||
let (hash, name) = parse_nix_store_path(symlink)?;
|
let (hash, name) = parse_nix_store_path(symlink)?;
|
||||||
|
|
||||||
Ok((String::from(hash), String::from(name)))
|
Ok((String::from(hash), String::from(name)))
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn metrics() -> Result<(StatusCode, impl IntoResponse), (StatusCode, impl IntoResponse)> {
|
fn gen_prometheus_metric(path: String, infix: String) -> Result<String, String> {
|
||||||
let (hash, name) = get_current_system().map_err(|_err| (StatusCode::INTERNAL_SERVER_ERROR, ""))?;
|
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((
|
return Ok((
|
||||||
StatusCode::OK,
|
StatusCode::OK,
|
||||||
format!("nixos_current_system_hash{{hash=\"{}\"}} 1\nnixos_current_system_name{{name=\"{}\"}} 1\n", hash, name)
|
out,
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user