traffic-controls: add API/CLI to show current traffic

Signed-off-by: Dietmar Maurer <dietmar@proxmox.com>
This commit is contained in:
Dietmar Maurer
2021-11-14 17:20:55 +01:00
parent 09f999337a
commit a0172d766b
5 changed files with 120 additions and 5 deletions

View File

@ -35,6 +35,7 @@ use proxmox_backup::rrd_cache::{
initialize_rrd_cache, rrd_update_gauge, rrd_update_derive, rrd_sync_journal,
};
use proxmox_backup::{
TRAFFIC_CONTROL_CACHE,
server::{
auth::check_pbs_auth,
jobstate::{
@ -71,7 +72,6 @@ use proxmox_backup::api2::pull::do_sync_job;
use proxmox_backup::api2::tape::backup::do_tape_backup_job;
use proxmox_backup::server::do_verification_job;
use proxmox_backup::server::do_prune_job;
use proxmox_backup::TrafficControlCache;
fn main() -> Result<(), Error> {
proxmox_backup::tools::setup_safe_path_env();
@ -329,6 +329,7 @@ async fn run() -> Result<(), Error> {
start_task_scheduler();
start_stat_generator();
start_traffic_control_updater();
server.await?;
log::info!("server shutting down, waiting for active workers to complete");
@ -465,6 +466,13 @@ fn start_task_scheduler() {
tokio::spawn(task.map(|_| ()));
}
fn start_traffic_control_updater() {
let abort_future = proxmox_rest_server::shutdown_future();
let future = Box::pin(run_traffic_control_updater());
let task = futures::future::select(future, abort_future);
tokio::spawn(task.map(|_| ()));
}
use std::time::{SystemTime, Instant, Duration, UNIX_EPOCH};
fn next_minute() -> Result<Instant, Error> {
@ -1086,9 +1094,19 @@ fn gather_disk_stats(disk_manager: Arc<DiskManage>, path: &Path, rrd_prefix: &st
// Test WITH
// proxmox-backup-client restore vm/201/2021-10-22T09:55:56Z drive-scsi0.img img1.img --repository localhost:store2
lazy_static::lazy_static!{
static ref TRAFFIC_CONTROL_CACHE: Arc<Mutex<TrafficControlCache>> =
Arc::new(Mutex::new(TrafficControlCache::new()));
async fn run_traffic_control_updater() {
loop {
let delay_target = Instant::now() + Duration::from_secs(1);
{
let mut cache = TRAFFIC_CONTROL_CACHE.lock().unwrap();
cache.compute_current_rates();
}
tokio::time::sleep_until(tokio::time::Instant::from_std(delay_target)).await;
}
}
fn lookup_rate_limiter(

View File

@ -7,6 +7,7 @@ use proxmox_schema::api;
use pbs_api_types::TRAFFIC_CONTROL_ID_SCHEMA;
use proxmox_backup::api2;
use proxmox_backup::client_helpers::connect_to_localhost;
#[api(
@ -75,10 +76,44 @@ fn show_traffic_control(param: Value, rpcenv: &mut dyn RpcEnvironment) -> Result
Ok(Value::Null)
}
#[api(
input: {
properties: {
"output-format": {
schema: OUTPUT_FORMAT,
optional: true,
},
}
}
)]
/// Show current traffic for all rules.
async fn show_current_traffic(param: Value) -> Result<Value, Error> {
let output_format = get_output_format(&param);
let client = connect_to_localhost()?;
let mut result = client.get(&"api2/json/admin/traffic-control", Some(param)).await?;
let mut data = result["data"].take();
let info = &api2::admin::traffic_control::API_METHOD_SHOW_CURRENT_TRAFFIC;
let options = default_table_format_options()
.column(ColumnConfig::new("name"))
.column(ColumnConfig::new("rate-in"))
.column(ColumnConfig::new("rate-out"));
format_and_print_result_full(&mut data, &info.returns, &output_format, &options);
Ok(Value::Null)
}
pub fn traffic_control_commands() -> CommandLineInterface {
let cmd_def = CliCommandMap::new()
.insert("list", CliCommand::new(&API_METHOD_LIST_TRAFFIC_CONTROLS))
.insert("traffic", CliCommand::new(&API_METHOD_SHOW_CURRENT_TRAFFIC))
.insert(
"show",
CliCommand::new(&API_METHOD_SHOW_TRAFFIC_CONTROL)