Compare commits
No commits in common. "3c3936e677974f72ba8b2225472e0a91315a3f7a" and "5aed8cf0ec7b27b0a1678c46e1765727317cc471" have entirely different histories.
3c3936e677
...
5aed8cf0ec
100
src/main.rs
100
src/main.rs
@ -1,5 +1,5 @@
|
|||||||
use axum::{
|
use axum::{
|
||||||
extract::{State, Query},
|
extract::Query,
|
||||||
http::StatusCode,
|
http::StatusCode,
|
||||||
response::{IntoResponse, Response},
|
response::{IntoResponse, Response},
|
||||||
routing::get,
|
routing::get,
|
||||||
@ -10,54 +10,12 @@ use std::collections::HashMap;
|
|||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
|
||||||
#[derive(Clone, PartialEq)]
|
#[derive(PartialEq)]
|
||||||
enum OperationMode {
|
enum OperationMode {
|
||||||
None,
|
|
||||||
Exporter,
|
Exporter,
|
||||||
Validator,
|
Validator,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
struct AppState {
|
|
||||||
listen: String,
|
|
||||||
operationmode: OperationMode,
|
|
||||||
prometheus_url: String,
|
|
||||||
prometheus_query_tag_template: String,
|
|
||||||
hydra_url: String,
|
|
||||||
hydra_job_template: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl AppState{
|
|
||||||
pub fn new() -> Self {
|
|
||||||
Self {
|
|
||||||
listen: String::from("[::]:9152"),
|
|
||||||
operationmode: OperationMode::None,
|
|
||||||
prometheus_url: String::from(""),
|
|
||||||
prometheus_query_tag_template: String::from("instance=\"{}\""),
|
|
||||||
hydra_url: String::from(""),
|
|
||||||
hydra_job_template: String::from("nixfiles/nixfiles/nixosConfigurations.{}"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn is_valid(self) -> bool {
|
|
||||||
let mut valid = true;
|
|
||||||
if self.operationmode == OperationMode::None {
|
|
||||||
println!("operationmode is not set");
|
|
||||||
valid = false;
|
|
||||||
}
|
|
||||||
if self.prometheus_url == String::from("") {
|
|
||||||
println!("Prometheus url is not specified");
|
|
||||||
valid = false;
|
|
||||||
}
|
|
||||||
if self.hydra_url == String::from("") {
|
|
||||||
println!("Hydra url is not specified");
|
|
||||||
valid = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return valid;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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.file_name()
|
||||||
.ok_or_else(String::default)?
|
.ok_or_else(String::default)?
|
||||||
@ -68,13 +26,12 @@ fn parse_nix_store_path(path: std::path::PathBuf) -> Result<(String, String), St
|
|||||||
return Ok((hash.to_string(), name.to_string()));
|
return Ok((hash.to_string(), name.to_string()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
let mut app_state = AppState::new();
|
let mut listen = String::from("[::]:9152");
|
||||||
|
let mut operationmode = OperationMode::Exporter;
|
||||||
let mut args = std::env::args();
|
let mut args = std::env::args();
|
||||||
let name = args.next().unwrap();
|
args.next();
|
||||||
loop {
|
loop {
|
||||||
let arg = if let Some(arg) = args.next() {
|
let arg = if let Some(arg) = args.next() {
|
||||||
arg
|
arg
|
||||||
@ -90,25 +47,13 @@ async fn main() {
|
|||||||
std::process::exit(0);
|
std::process::exit(0);
|
||||||
}
|
}
|
||||||
"--listen" => {
|
"--listen" => {
|
||||||
app_state.listen = args.next().unwrap();
|
listen = args.next().unwrap();
|
||||||
}
|
|
||||||
"--prometheus-url" => {
|
|
||||||
app_state.prometheus_url = args.next().unwrap();
|
|
||||||
}
|
|
||||||
"--prometheus-query-tag-template" => {
|
|
||||||
app_state.prometheus_query_tag_template = args.next().unwrap();
|
|
||||||
}
|
|
||||||
"--hydra-url" => {
|
|
||||||
app_state.hydra_url = args.next().unwrap();
|
|
||||||
}
|
|
||||||
"--hydra-job-template" => {
|
|
||||||
app_state.hydra_job_template = args.next().unwrap();
|
|
||||||
}
|
}
|
||||||
"exporter" => {
|
"exporter" => {
|
||||||
app_state.operationmode = OperationMode::Exporter;
|
operationmode = OperationMode::Exporter;
|
||||||
}
|
}
|
||||||
"validator" => {
|
"validator" => {
|
||||||
app_state.operationmode = OperationMode::Validator;
|
operationmode = OperationMode::Validator;
|
||||||
}
|
}
|
||||||
unknown => {
|
unknown => {
|
||||||
println!("unknown option: {}", unknown);
|
println!("unknown option: {}", unknown);
|
||||||
@ -117,25 +62,16 @@ async fn main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !app_state.clone().is_valid() {
|
|
||||||
std::process::exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut app = Router::new();
|
let mut app = Router::new();
|
||||||
if app_state.operationmode == OperationMode::Exporter {
|
if operationmode == OperationMode::Exporter {
|
||||||
println!("Running NixOS Exporter in Exporter mode");
|
println!("Running NixOS Exporter in Exporter mode");
|
||||||
app = app.route("/metrics", get(metrics));
|
app = app.route("/metrics", get(metrics));
|
||||||
} else if app_state.operationmode == OperationMode::Validator {
|
} else if operationmode == OperationMode::Validator {
|
||||||
println!("Running NixOS Exporter in Validator mode");
|
println!("Running NixOS Exporter in Validator mode");
|
||||||
app = app.route("/metrics", get(check));
|
app = app.route("/metrics", get(check));
|
||||||
} else {
|
}
|
||||||
println!("Run mode not specified, do {} --help", name);
|
|
||||||
std::process::exit(1);
|
|
||||||
};
|
|
||||||
|
|
||||||
let app = app.with_state(app_state.clone());
|
let addr = SocketAddr::from_str(&listen).unwrap();
|
||||||
|
|
||||||
let addr = SocketAddr::from_str(&app_state.listen.clone()).unwrap();
|
|
||||||
println!("listening on http://{}", addr);
|
println!("listening on http://{}", addr);
|
||||||
axum::Server::bind(&addr)
|
axum::Server::bind(&addr)
|
||||||
.serve(app.into_make_service())
|
.serve(app.into_make_service())
|
||||||
@ -170,7 +106,7 @@ async fn metrics() -> Response {
|
|||||||
).into_response()
|
).into_response()
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn check(State(app_state): State<AppState>, Query(params): Query<HashMap<String, String>>) -> Response {
|
async fn check(Query(params): Query<HashMap<String, String>>) -> Response {
|
||||||
let target = match params.get("target") {
|
let target = match params.get("target") {
|
||||||
Some(target) => target,
|
Some(target) => target,
|
||||||
None => {
|
None => {
|
||||||
@ -178,13 +114,9 @@ async fn check(State(app_state): State<AppState>, Query(params): Query<HashMap<S
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
if target.contains("\"") {
|
|
||||||
return (StatusCode::INTERNAL_SERVER_ERROR, "Invalid target name").into_response();
|
|
||||||
}
|
|
||||||
|
|
||||||
let client = reqwest::Client::new();
|
let client = reqwest::Client::new();
|
||||||
|
|
||||||
let prometheus_req = match client.get(format!("{}/api/v1/query?query=nixos_nixos_current_system_hash{{{}}}", app_state.prometheus_url, app_state.prometheus_query_tag_template.clone().replace("{}", target)))
|
let prometheus_req = match client.get(format!("https://prometheus.monitoring.clerie.de/api/v1/query?query=nixos_nixos_current_system_hash{{job=%22nixos-exporter%22,instance=%22{}.mon.clerie.de:9152%22}}", target))
|
||||||
.header("Accept", "application/json")
|
.header("Accept", "application/json")
|
||||||
.send().await {
|
.send().await {
|
||||||
Ok(req) => req,
|
Ok(req) => req,
|
||||||
@ -215,7 +147,7 @@ async fn check(State(app_state): State<AppState>, Query(params): Query<HashMap<S
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
let hydra_req = match client.get(format!("{}/job/{}/latest", app_state.hydra_url, app_state.hydra_job_template.clone().replace("{}", target)))
|
let hydra_req = match client.get(format!("https://hydra.clerie.de/job/nixfiles/nixfiles/nixosConfigurations.{}/latest", target))
|
||||||
.header("Accept", "application/json")
|
.header("Accept", "application/json")
|
||||||
.send().await {
|
.send().await {
|
||||||
Ok(req) => req,
|
Ok(req) => req,
|
||||||
@ -260,6 +192,6 @@ async fn check(State(app_state): State<AppState>, Query(params): Query<HashMap<S
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
StatusCode::OK,
|
StatusCode::OK,
|
||||||
format!("nixos_current_system_valid{{{}}} {}\n", app_state.prometheus_query_tag_template.clone().replace("{}", target), status)
|
format!("nixos_current_system_valid{{target=\"{}.net.clerie.de:9152\"}} {}\n", target, status)
|
||||||
).into_response();
|
).into_response();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user