traffic-control api: return current traffic with config

Signed-off-by: Dietmar Maurer <dietmar@proxmox.com>
This commit is contained in:
Dietmar Maurer 2021-11-18 12:23:50 +01:00
parent ac7dbba458
commit 26e949d5fe
2 changed files with 45 additions and 29 deletions

View File

@ -1,15 +1,34 @@
use anyhow::Error;
use serde_json::{json, Value};
use serde::{Deserialize, Serialize};
use proxmox_router::{Router, Permission};
use proxmox_router::{Router, RpcEnvironment, Permission};
use proxmox_schema::api;
use pbs_api_types::{
TRAFFIC_CONTROL_ID_SCHEMA, PRIV_SYS_AUDIT,
TrafficControlRule, PRIV_SYS_AUDIT,
};
use crate::TRAFFIC_CONTROL_CACHE;
#[api(
properties: {
config: {
type: TrafficControlRule,
},
},
)]
#[derive(Serialize, Deserialize)]
#[serde(rename_all="kebab-case")]
/// Traffic control rule config with current rates
pub struct TrafficControlCurrentRate {
#[serde(flatten)]
config: TrafficControlRule,
/// Current ingress rate in bytes/second
cur_rate_in: u64,
/// Current egress rate in bytes/second
cur_rate_out: u64,
}
#[api(
input: {
properties: {},
@ -18,20 +37,7 @@ use crate::TRAFFIC_CONTROL_CACHE;
description: "Show current traffic control rates.",
type: Array,
items: {
description: "Current rates per rule.",
properties: {
name: {
schema: TRAFFIC_CONTROL_ID_SCHEMA,
},
"rate-in": {
description: "Current ingress rate in bytes/second",
type: u64,
},
"rate-out": {
description: "Current egress rate in bytes/second",
type: u64,
},
},
type: TrafficControlCurrentRate,
},
},
access: {
@ -39,20 +45,30 @@ use crate::TRAFFIC_CONTROL_CACHE;
},
)]
/// Show current traffic for all traffic control rules.
pub fn show_current_traffic() -> Result<Value, Error> {
pub fn show_current_traffic(
mut rpcenv: &mut dyn RpcEnvironment,
) -> Result<Vec<TrafficControlCurrentRate>, Error> {
let (config, digest) = pbs_config::traffic_control::config()?;
let rules: Vec<TrafficControlRule> = config.convert_to_typed_array("rule")?;
let cache = TRAFFIC_CONTROL_CACHE.lock().unwrap();
let mut list = Vec::new();
let cache = TRAFFIC_CONTROL_CACHE.lock().unwrap();
for (rule, stat) in cache.current_rate_map().iter() {
list.push(json!({
"name": rule,
"rate-in": stat.rate_in,
"rate-out": stat.rate_out,
}));
for config in rules {
let (cur_rate_in, cur_rate_out) = match cache.current_rate_map().get(&config.name) {
None => (0, 0),
Some(state) => (state.rate_in, state.rate_out),
};
list.push(TrafficControlCurrentRate {config, cur_rate_in, cur_rate_out});
}
Ok(list.into())
// also return the configuration digest
rpcenv["digest"] = proxmox::tools::digest_to_hex(&digest).into();
Ok(list)
}
pub const ROUTER: Router = Router::new()

View File

@ -93,7 +93,7 @@ async fn show_current_traffic(param: Value) -> Result<Value, Error> {
let client = connect_to_localhost()?;
let mut result = client.get(&"api2/json/admin/traffic-control", Some(param)).await?;
let mut result = client.get(&"api2/json/admin/traffic-control", None).await?;
let mut data = result["data"].take();
@ -101,8 +101,8 @@ async fn show_current_traffic(param: Value) -> Result<Value, Error> {
let options = default_table_format_options()
.column(ColumnConfig::new("name"))
.column(ColumnConfig::new("rate-in"))
.column(ColumnConfig::new("rate-out"));
.column(ColumnConfig::new("cur-rate-in"))
.column(ColumnConfig::new("cur-rate-out"));
format_and_print_result_full(&mut data, &info.returns, &output_format, &options);