add lsblk parser

This commit is contained in:
lukas-heiligenbrunner 2022-12-05 23:18:32 +01:00
parent 9333489ff4
commit a0068579b4
3 changed files with 75 additions and 2 deletions

View File

@ -4,6 +4,7 @@ mod parser;
extern crate rocket;
use rocket::serde::json::Json;
use crate::parser::lsblk_parser::{Disk, parse_lsblk};
use crate::parser::mdstat_parser::{MdRaidSystem, parse_mdstat};
#[get("/raiddevices")]
@ -11,11 +12,16 @@ fn get_raid_devices() -> Json<MdRaidSystem> {
Json(parse_mdstat())
}
#[get("/disks")]
fn get_disks() -> Json<Vec<Disk>> {
Json(parse_lsblk())
}
#[rocket::main]
async fn main() -> Result<(), rocket::Error> {
println!("init");
let _rocket = rocket::build()
.mount("/api", routes![get_raid_devices])
.mount("/api", routes![get_raid_devices, get_disks])
.launch()
.await?;

View File

@ -0,0 +1,66 @@
use std::process::Command;
use rocket::serde::json::{serde_json};
use rocket::serde::{Serialize, Deserialize};
#[derive(Serialize)]
#[serde(crate = "rocket::serde")]
pub struct Disk {
name: String,
size: u64,
}
#[derive(Serialize, Deserialize)]
#[serde(crate = "rocket::serde")]
struct ChildDevice {
name: String,
#[serde(rename = "maj:min")]
majmin: String,
rm: bool,
size: u64,
ro: bool,
#[serde(rename = "type")]
kind: String,
mountpoints: Vec<Option<String>>,
}
#[derive(Serialize, Deserialize)]
#[serde(crate = "rocket::serde")]
struct Blockdevice {
name: String,
#[serde(rename = "type")]
kind: String,
size: u64,
#[serde(rename = "maj:min")]
majmin: String,
rm: bool,
ro: bool,
mountpoints: Vec<Option<String>>,
children: Option<Vec<ChildDevice>>,
}
#[derive(Serialize, Deserialize)]
#[serde(crate = "rocket::serde")]
struct LsblkT {
blockdevices: Vec<Blockdevice>,
}
pub fn parse_lsblk() -> Vec<Disk> {
let output = Command::new("lsblk")
.arg("-J")
.arg("-b")// sizes in bytes
.arg("-p") // complete device path
.output()
.expect("failed to execute process");
let out = output.stdout;
let out = String::from_utf8(out).unwrap();
let v: LsblkT = serde_json::from_str(out.as_str()).unwrap();
let mut disks: Vec<Disk> = Vec::new();
for d in v.blockdevices {
if d.kind != "disk" { continue; };
disks.push(Disk { name: d.name, size: d.size });
}
disks
}

View File

@ -1 +1,2 @@
pub mod mdstat_parser;
pub mod mdstat_parser;
pub mod lsblk_parser;