traffic-control api: return current traffic with config
Signed-off-by: Dietmar Maurer <dietmar@proxmox.com>
This commit is contained in:
		@ -1,15 +1,34 @@
 | 
				
			|||||||
use anyhow::Error;
 | 
					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 proxmox_schema::api;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use pbs_api_types::{
 | 
					use pbs_api_types::{
 | 
				
			||||||
    TRAFFIC_CONTROL_ID_SCHEMA, PRIV_SYS_AUDIT,
 | 
					    TrafficControlRule, PRIV_SYS_AUDIT,
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use crate::TRAFFIC_CONTROL_CACHE;
 | 
					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(
 | 
					#[api(
 | 
				
			||||||
    input: {
 | 
					    input: {
 | 
				
			||||||
        properties: {},
 | 
					        properties: {},
 | 
				
			||||||
@ -18,20 +37,7 @@ use crate::TRAFFIC_CONTROL_CACHE;
 | 
				
			|||||||
        description: "Show current traffic control rates.",
 | 
					        description: "Show current traffic control rates.",
 | 
				
			||||||
        type: Array,
 | 
					        type: Array,
 | 
				
			||||||
        items: {
 | 
					        items: {
 | 
				
			||||||
            description: "Current rates per rule.",
 | 
					            type: TrafficControlCurrentRate,
 | 
				
			||||||
            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,
 | 
					 | 
				
			||||||
                },
 | 
					 | 
				
			||||||
            },
 | 
					 | 
				
			||||||
        },
 | 
					        },
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
    access: {
 | 
					    access: {
 | 
				
			||||||
@ -39,20 +45,30 @@ use crate::TRAFFIC_CONTROL_CACHE;
 | 
				
			|||||||
    },
 | 
					    },
 | 
				
			||||||
)]
 | 
					)]
 | 
				
			||||||
/// Show current traffic for all traffic control rules.
 | 
					/// 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 mut list = Vec::new();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let cache = TRAFFIC_CONTROL_CACHE.lock().unwrap();
 | 
					    for config in rules {
 | 
				
			||||||
    for (rule, stat) in cache.current_rate_map().iter() {
 | 
					        let (cur_rate_in, cur_rate_out) = match cache.current_rate_map().get(&config.name) {
 | 
				
			||||||
        list.push(json!({
 | 
					            None => (0, 0),
 | 
				
			||||||
            "name": rule,
 | 
					            Some(state) => (state.rate_in, state.rate_out),
 | 
				
			||||||
            "rate-in": stat.rate_in,
 | 
					        };
 | 
				
			||||||
            "rate-out": stat.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()
 | 
					pub const ROUTER: Router = Router::new()
 | 
				
			||||||
 | 
				
			|||||||
@ -93,7 +93,7 @@ async fn show_current_traffic(param: Value) -> Result<Value, Error> {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    let client = connect_to_localhost()?;
 | 
					    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();
 | 
					    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()
 | 
					    let options = default_table_format_options()
 | 
				
			||||||
        .column(ColumnConfig::new("name"))
 | 
					        .column(ColumnConfig::new("name"))
 | 
				
			||||||
        .column(ColumnConfig::new("rate-in"))
 | 
					        .column(ColumnConfig::new("cur-rate-in"))
 | 
				
			||||||
        .column(ColumnConfig::new("rate-out"));
 | 
					        .column(ColumnConfig::new("cur-rate-out"));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    format_and_print_result_full(&mut data, &info.returns, &output_format, &options);
 | 
					    format_and_print_result_full(&mut data, &info.returns, &output_format, &options);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user