From cf59e2b394958e0760d7ff145d96c948aea12868 Mon Sep 17 00:00:00 2001 From: jean-christophe81 <98889244+jean-christophe81@users.noreply.github.com> Date: Fri, 6 Dec 2024 11:11:19 +0100 Subject: [PATCH] rename memory base to native base, add help on native checks (#1919) --- .github/scripts/agent_installer_test.ps1 | 185 +++++--- agent/inc/com/centreon/agent/drive_size.hh | 10 +- ...ck_memory_base.hh => native_check_base.hh} | 64 +-- agent/installer/silent.nsi | 5 + agent/native_linux/src/check_cpu.cc | 30 ++ .../inc/com/centreon/agent/check_memory.hh | 37 +- agent/native_windows/src/check_cpu.cc | 50 +++ agent/native_windows/src/check_drive_size.cc | 6 +- agent/native_windows/src/check_memory.cc | 416 +++++++++++------- agent/native_windows/src/check_uptime.cc | 24 + agent/src/drive_size.cc | 53 ++- agent/src/main.cc | 3 + agent/src/main_win.cc | 24 + ...ck_memory_base.cc => native_check_base.cc} | 70 +-- agent/src/scheduler.cc | 4 +- agent/test/check_exec_test.cc | 6 +- agent/test/check_windows_memory_test.cc | 14 +- agent/test/test_main.cc | 3 +- 18 files changed, 666 insertions(+), 338 deletions(-) rename agent/inc/com/centreon/agent/{native_check_memory_base.hh => native_check_base.hh} (70%) rename agent/src/{native_check_memory_base.cc => native_check_base.cc} (70%) diff --git a/.github/scripts/agent_installer_test.ps1 b/.github/scripts/agent_installer_test.ps1 index a52b126550d..2d0bbfa43b7 100644 --- a/.github/scripts/agent_installer_test.ps1 +++ b/.github/scripts/agent_installer_test.ps1 @@ -18,9 +18,45 @@ # This script test CMA installer in silent mode +Set-PSDebug -Trace 2 + +function f_start_process([string]$sProcess, [string]$sArgs, [ref]$pOutPut) { + <# + .SYNOPSIS + Starts a new process with the specified executable and arguments. + + .DESCRIPTION + The `f_start_process` function initiates a new process using the provided executable path and arguments. + + .PARAMETER sProcess + The path to the executable file to start. + + .PARAMETER sArgs + The arguments to pass to the executable. + + .PARAMETER pOutPut + variable where we will store stdout and stderr + #> + $oProcessInfo = New-Object System.Diagnostics.ProcessStartInfo + $oProcessInfo.FileName = $sProcess + $oProcessInfo.RedirectStandardError = $true + $oProcessInfo.RedirectStandardOutput = $true + $oProcessInfo.UseShellExecute = $false + $oProcessInfo.Arguments = $sArgs + $oProcess = New-Object System.Diagnostics.Process + $oProcess.StartInfo = $oProcessInfo + $oProcess.Start() | Out-Null + $oProcess.WaitForExit() | Out-Null + $sSTDOUT = $oProcess.StandardOutput.ReadToEnd() + $sSTDERR = $oProcess.StandardError.ReadToEnd() + $pOutPut.Value = "Commandline: $sProcess $sArgs`r`n" + $pOutPut.Value += "STDOUT: " + $sSTDOUT + "`r`n" + $pOutPut.Value += "STDERR: " + $sSTDERR + "`r`n" + return $oProcess.ExitCode +} -function test_args_to_registry { -<# +function test_args_to_registry ([string] $exe_path, [string[]] $exe_args, $expected_registry_values) { + <# .SYNOPSIS start a program and check values in registry @@ -33,17 +69,13 @@ function test_args_to_registry { .PARAMETER expected_registry_values hash_table as @{'host'='host_1';'endpoint'='127.0.0.1'} #> - param ( - [string] $exe_path, - [string[]] $exe_args, - $expected_registry_values - ) - Write-Host "arguments: $exe_args" + Write-Host "execute $exe_path arguments: $exe_args" + + $process_output = @{} + $exit_code = f_start_process $exe_path $exe_args ([ref]$process_output) - $process_info= Start-Process -PassThru $exe_path $exe_args - $process_info.WaitForExit() - if ($process_info.ExitCode -ne 0) { + if ($exit_code -ne 0) { Write-Host "fail to execute $exe_path with arguments $exe_args" Write-Host "exit status = " $process_info.ExitCode exit 1 @@ -60,21 +92,40 @@ function test_args_to_registry { } } - foreach ($value_name in $expected_registry_values.Keys) { - $expected_value = $($expected_registry_values[$value_name]) - $real_value = (Get-ItemProperty -Path HKLM:\Software\Centreon\CentreonMonitoringAgent -Name $value_name).$value_name - if ($expected_value -ne $real_value) { - Write-Host "unexpected value for $value_name, expected: $expected_value, read: $real_value" - exit 1 + for (($i = 0); $i -lt 10; $i++) { + Start-Sleep -Seconds 1 + $read_success = 1 + foreach ($value_name in $expected_registry_values.Keys) { + $expected_value = $($expected_registry_values[$value_name]) + try { + $real_value = (Get-ItemProperty -Path HKLM:\Software\Centreon\CentreonMonitoringAgent -Name $value_name).$value_name + if ($expected_value -ne $real_value) { + Write-Host "unexpected value for $value_name, expected: $expected_value, read: $real_value" + $read_success = 0 + break + } + } + catch { + $read_success = 0 + break + } } + if ($read_success -eq 1) { + break + } + } + + if ($read_success -ne 1) { + Write-Host "fail to read expected registry values in 10s installer output: $process_output" + exit 1 } } Write-Host "############################ all install uninstall ############################" -$args = '/S','--install_cma', '--install_plugins', '--hostname', "my_host_name_1", "--endpoint","127.0.0.1:4317" -$expected = @{ 'endpoint'='127.0.0.1:4317';'host'='my_host_name_1';'log_type'='EventLog'; 'log_level' = 'error'; 'encryption' = 0;'reversed_grpc_streaming'= 0 } -test_args_to_registry "agent/installer/centreon-monitoring-agent.exe" $args $expected +$exe_args = '/S', '--install_cma', '--install_plugins', '--hostname', "my_host_name_1", "--endpoint", "127.0.0.1:4317" +$expected = @{ 'endpoint' = '127.0.0.1:4317'; 'host' = 'my_host_name_1'; 'log_type' = 'EventLog'; 'log_level' = 'error'; 'encryption' = 0; 'reversed_grpc_streaming' = 0 } +test_args_to_registry "agent/installer/centreon-monitoring-agent.exe" $exe_args $expected if (!(Get-ItemProperty -Path HKLM:\Software\Centreon\CentreonMonitoringAgent)) { Write-Host "no registry entry created" @@ -96,7 +147,7 @@ if (![System.Io.File]::Exists("C:\Program Files\Centreon\Plugins\centreon_plugin exit 1 } -$process_info= Start-Process -PassThru "C:\Program Files\Centreon\CentreonMonitoringAgent\uninstall.exe" "/S", "--uninstall_cma","--uninstall_plugins" +$process_info = Start-Process -PassThru "C:\Program Files\Centreon\CentreonMonitoringAgent\uninstall.exe" "/S", "--uninstall_cma", "--uninstall_plugins" Wait-Process -Id $process_info.Id if ($process_info.ExitCode -ne 0) { Write-Host "bad uninstaller exit code" @@ -107,7 +158,7 @@ if ($process_info.ExitCode -ne 0) { for (($i = 0); $i -lt 10; $i++) { Start-Sleep -Seconds 1 $info = Get-Process | Select-Object -Property ProcessName | Select-String centagent - if (! $info) { + if (! $info) { break } } @@ -141,14 +192,14 @@ if ($key_found) { Write-Host "############################ installer test ############################" -$process_info= Start-Process -PassThru "agent/installer/centreon-monitoring-agent.exe" "/S", "--help" +$process_info = Start-Process -PassThru "agent/installer/centreon-monitoring-agent.exe" "/S", "--help" Wait-Process -Id $process_info.Id if ($process_info.ExitCode -ne 2) { Write-Host "bad --help exit code" exit 1 } -$process_info= Start-Process -PassThru "agent/installer/centreon-monitoring-agent.exe" "/S", "--version" +$process_info = Start-Process -PassThru "agent/installer/centreon-monitoring-agent.exe" "/S", "--version" Wait-Process -Id $process_info.Id if ($process_info.ExitCode -ne 2) { Write-Host "bad --version exit code" @@ -156,70 +207,70 @@ if ($process_info.ExitCode -ne 2) { } #missing mandatory parameters -$process_info= Start-Process -PassThru "agent/installer/centreon-monitoring-agent.exe" "/S", "--install_cma" +$process_info = Start-Process -PassThru "agent/installer/centreon-monitoring-agent.exe" "/S", "--install_cma" Wait-Process -Id $process_info.Id if ($process_info.ExitCode -ne 1) { Write-Host "bad no parameter exit code " $process_info.ExitCode exit 1 } -$process_info= Start-Process -PassThru "agent/installer/centreon-monitoring-agent.exe" "/S", "--install_cma","--hostname","toto" +$process_info = Start-Process -PassThru "agent/installer/centreon-monitoring-agent.exe" "/S", "--install_cma", "--hostname", "toto" Wait-Process -Id $process_info.Id if ($process_info.ExitCode -ne 1) { Write-Host "bad no endpoint exit code " $process_info.ExitCode exit 1 } -$process_info= Start-Process -PassThru "agent/installer/centreon-monitoring-agent.exe" "/S", "--install_cma","--hostname","toto","--endpoint","turlututu" +$process_info = Start-Process -PassThru "agent/installer/centreon-monitoring-agent.exe" "/S", "--install_cma", "--hostname", "toto", "--endpoint", "turlututu" Wait-Process -Id $process_info.Id if ($process_info.ExitCode -ne 1) { Write-Host "bad wrong endpoint exit code " $process_info.ExitCode exit 1 } -$process_info= Start-Process -PassThru "agent/installer/centreon-monitoring-agent.exe" "/S", "--install_cma","--hostname","toto","--endpoint","127.0.0.1:4317","--log_type","file" +$process_info = Start-Process -PassThru "agent/installer/centreon-monitoring-agent.exe" "/S", "--install_cma", "--hostname", "toto", "--endpoint", "127.0.0.1:4317", "--log_type", "file" Wait-Process -Id $process_info.Id if ($process_info.ExitCode -ne 1) { Write-Host "bad no log file path " $process_info.ExitCode exit 1 } -$process_info= Start-Process -PassThru "agent/installer/centreon-monitoring-agent.exe" "/S", "--install_cma","--hostname","toto","--endpoint","127.0.0.1:4317","--log_type","file","--log_file","C:" +$process_info = Start-Process -PassThru "agent/installer/centreon-monitoring-agent.exe" "/S", "--install_cma", "--hostname", "toto", "--endpoint", "127.0.0.1:4317", "--log_type", "file", "--log_file", "C:" Wait-Process -Id $process_info.Id if ($process_info.ExitCode -ne 1) { Write-Host "bad log file path " $process_info.ExitCode exit 1 } -$process_info= Start-Process -PassThru "agent/installer/centreon-monitoring-agent.exe" "/S", "--install_cma","--hostname","toto","--endpoint","127.0.0.1:4317","--log_level","dsfsfd" +$process_info = Start-Process -PassThru "agent/installer/centreon-monitoring-agent.exe" "/S", "--install_cma", "--hostname", "toto", "--endpoint", "127.0.0.1:4317", "--log_level", "dsfsfd" Wait-Process -Id $process_info.Id if ($process_info.ExitCode -ne 1) { Write-Host "bad log level " $process_info.ExitCode exit 1 } -$process_info= Start-Process -PassThru "agent/installer/centreon-monitoring-agent.exe" "/S", "--install_cma","--hostname","toto","--endpoint","127.0.0.1:4317","--reverse","--log_type","file","--log_file","C:\Users\Public\cma.log","--encryption" +$process_info = Start-Process -PassThru "agent/installer/centreon-monitoring-agent.exe" "/S", "--install_cma", "--hostname", "toto", "--endpoint", "127.0.0.1:4317", "--reverse", "--log_type", "file", "--log_file", "C:\Users\Public\cma.log", "--encryption" Wait-Process -Id $process_info.Id if ($process_info.ExitCode -ne 1) { Write-Host "reverse mode, encryption and no private_key " $process_info.ExitCode exit 1 } -$process_info= Start-Process -PassThru "agent/installer/centreon-monitoring-agent.exe" "/S", "--install_cma","--hostname","toto","--endpoint","127.0.0.1:4317","--reverse","--log_type","file","--log_file","C:\Users\Public\cma.log","--encryption","--private_key","C:" +$process_info = Start-Process -PassThru "agent/installer/centreon-monitoring-agent.exe" "/S", "--install_cma", "--hostname", "toto", "--endpoint", "127.0.0.1:4317", "--reverse", "--log_type", "file", "--log_file", "C:\Users\Public\cma.log", "--encryption", "--private_key", "C:" Wait-Process -Id $process_info.Id if ($process_info.ExitCode -ne 1) { Write-Host "reverse mode, encryption and bad private_key path" $process_info.ExitCode exit 1 } -$process_info= Start-Process -PassThru "agent/installer/centreon-monitoring-agent.exe" "/S", "--install_cma","--hostname","toto","--endpoint","127.0.0.1:4317","--reverse","--log_type","file","--log_file","C:\Users\Public\cma.log","--encryption","--private_key","C:\Users\Public\private_key.key" +$process_info = Start-Process -PassThru "agent/installer/centreon-monitoring-agent.exe" "/S", "--install_cma", "--hostname", "toto", "--endpoint", "127.0.0.1:4317", "--reverse", "--log_type", "file", "--log_file", "C:\Users\Public\cma.log", "--encryption", "--private_key", "C:\Users\Public\private_key.key" Wait-Process -Id $process_info.Id if ($process_info.ExitCode -ne 1) { Write-Host "reverse mode, encryption and no certificate" $process_info.ExitCode exit 1 } -$process_info= Start-Process -PassThru "agent/installer/centreon-monitoring-agent.exe" "/S", "--install_cma","--hostname","toto","--endpoint","127.0.0.1:4317","--reverse","--log_type","file","--log_file","C:\Users\Public\cma.log","--encryption","--private_key","C:\Users\Public\private_key.key", "--public_cert", "C:" +$process_info = Start-Process -PassThru "agent/installer/centreon-monitoring-agent.exe" "/S", "--install_cma", "--hostname", "toto", "--endpoint", "127.0.0.1:4317", "--reverse", "--log_type", "file", "--log_file", "C:\Users\Public\cma.log", "--encryption", "--private_key", "C:\Users\Public\private_key.key", "--public_cert", "C:" Wait-Process -Id $process_info.Id if ($process_info.ExitCode -ne 1) { Write-Host "reverse mode, encryption and bad certificate path" $process_info.ExitCode @@ -227,52 +278,52 @@ if ($process_info.ExitCode -ne 1) { } -$args = '/S','--install_cma','--hostname', "my_host_name_1", "--endpoint","127.0.0.1:4317" -$expected = @{ 'endpoint'='127.0.0.1:4317';'host'='my_host_name_1';'log_type'='EventLog'; 'log_level' = 'error'; 'encryption' = 0;'reversed_grpc_streaming'= 0 } -test_args_to_registry "agent/installer/centreon-monitoring-agent.exe" $args $expected +$exe_args = '/S', '--install_cma', '--hostname', "my_host_name_1", "--endpoint", "127.0.0.1:4317" +$expected = @{ 'endpoint' = '127.0.0.1:4317'; 'host' = 'my_host_name_1'; 'log_type' = 'EventLog'; 'log_level' = 'error'; 'encryption' = 0; 'reversed_grpc_streaming' = 0 } +test_args_to_registry "agent/installer/centreon-monitoring-agent.exe" $exe_args $expected -$args = '/S','--install_cma','--hostname', "my_host_name_2", "--endpoint","127.0.0.2:4317", "--log_type", "file", "--log_file", "C:\Users\Public\cma.log", "--log_level", "trace", "--log_max_file_size", "15", "--log_max_files", "10" -$expected = @{ 'endpoint'='127.0.0.2:4317';'host'='my_host_name_2';'log_type'='File'; 'log_level' = 'trace'; 'log_file'='C:\Users\Public\cma.log'; 'encryption' = 0;'reversed_grpc_streaming'= 0; 'log_max_file_size' = 15; 'log_max_files' = 10; } -test_args_to_registry "agent/installer/centreon-monitoring-agent.exe" $args $expected +$exe_args = '/S', '--install_cma', '--hostname', "my_host_name_2", "--endpoint", "127.0.0.2:4317", "--log_type", "file", "--log_file", "C:\Users\Public\cma.log", "--log_level", "trace", "--log_max_file_size", "15", "--log_max_files", "10" +$expected = @{ 'endpoint' = '127.0.0.2:4317'; 'host' = 'my_host_name_2'; 'log_type' = 'File'; 'log_level' = 'trace'; 'log_file' = 'C:\Users\Public\cma.log'; 'encryption' = 0; 'reversed_grpc_streaming' = 0; 'log_max_file_size' = 15; 'log_max_files' = 10; } +test_args_to_registry "agent/installer/centreon-monitoring-agent.exe" $exe_args $expected -$args = '/S','--install_cma','--hostname', "my_host_name_2", "--endpoint","127.0.0.3:4317", "--log_type", "file", "--log_file", "C:\Users\Public\cma.log", "--log_level", "trace", "--encryption" -$expected = @{ 'endpoint'='127.0.0.3:4317';'host'='my_host_name_2';'log_type'='File'; 'log_level' = 'trace'; 'log_file'='C:\Users\Public\cma.log'; 'encryption' = 1;'reversed_grpc_streaming'= 0 } -test_args_to_registry "agent/installer/centreon-monitoring-agent.exe" $args $expected +$exe_args = '/S', '--install_cma', '--hostname', "my_host_name_2", "--endpoint", "127.0.0.3:4317", "--log_type", "file", "--log_file", "C:\Users\Public\cma.log", "--log_level", "trace", "--encryption" +$expected = @{ 'endpoint' = '127.0.0.3:4317'; 'host' = 'my_host_name_2'; 'log_type' = 'File'; 'log_level' = 'trace'; 'log_file' = 'C:\Users\Public\cma.log'; 'encryption' = 1; 'reversed_grpc_streaming' = 0 } +test_args_to_registry "agent/installer/centreon-monitoring-agent.exe" $exe_args $expected -$args = '/S','--install_cma','--hostname', "my_host_name_2", "--endpoint","127.0.0.4:4317", "--log_type", "file", "--log_file", "C:\Users\Public\cma.log", "--log_level", "trace", "--encryption", "--private_key", "C:\Users crypto\private.key", "--public_cert", "D:\tutu\titi.crt", "--ca", "C:\Users\Public\ca.crt", "--ca_name", "tls_ca_name" -$expected = @{ 'endpoint'='127.0.0.4:4317';'host'='my_host_name_2';'log_type'='File'; 'log_level' = 'trace'; 'log_file'='C:\Users\Public\cma.log'; 'encryption' = 1;'reversed_grpc_streaming'= 0; 'certificate'='D:\tutu\titi.crt'; 'private_key'='C:\Users crypto\private.key'; 'ca_certificate' = 'C:\Users\Public\ca.crt'; 'ca_name' = 'tls_ca_name' } -test_args_to_registry "agent/installer/centreon-monitoring-agent.exe" $args $expected +$exe_args = '/S', '--install_cma', '--hostname', "my_host_name_2", "--endpoint", "127.0.0.4:4317", "--log_type", "file", "--log_file", "C:\Users\Public\cma.log", "--log_level", "trace", "--encryption", "--private_key", "C:\Users crypto\private.key", "--public_cert", "D:\tutu\titi.crt", "--ca", "C:\Users\Public\ca.crt", "--ca_name", "tls_ca_name" +$expected = @{ 'endpoint' = '127.0.0.4:4317'; 'host' = 'my_host_name_2'; 'log_type' = 'File'; 'log_level' = 'trace'; 'log_file' = 'C:\Users\Public\cma.log'; 'encryption' = 1; 'reversed_grpc_streaming' = 0; 'certificate' = 'D:\tutu\titi.crt'; 'private_key' = 'C:\Users crypto\private.key'; 'ca_certificate' = 'C:\Users\Public\ca.crt'; 'ca_name' = 'tls_ca_name' } +test_args_to_registry "agent/installer/centreon-monitoring-agent.exe" $exe_args $expected -$args = '/S','--install_cma','--hostname', "my_host_name_2", "--endpoint","127.0.0.5:4317", "--log_type", "file", "--log_file", "C:\Users\Public\cma_rev.log", "--log_level", "trace", "--encryption", "--reverse", "--private_key", "C:\Users crypto\private_rev.key", "--public_cert", "D:\tutu\titi_rev.crt", "--ca", "C:\Users\Public\ca_rev.crt", "--ca_name", "tls_ca_name_rev" -$expected = @{ 'endpoint'='127.0.0.5:4317';'host'='my_host_name_2';'log_type'='File'; 'log_level' = 'trace'; 'log_file'='C:\Users\Public\cma_rev.log'; 'encryption' = 1;'reversed_grpc_streaming'= 1; 'certificate'='D:\tutu\titi_rev.crt'; 'private_key'='C:\Users crypto\private_rev.key'; 'ca_certificate' = 'C:\Users\Public\ca_rev.crt'; 'ca_name' = 'tls_ca_name_rev' } -test_args_to_registry "agent/installer/centreon-monitoring-agent.exe" $args $expected +$exe_args = '/S', '--install_cma', '--hostname', "my_host_name_2", "--endpoint", "127.0.0.5:4317", "--log_type", "file", "--log_file", "C:\Users\Public\cma_rev.log", "--log_level", "trace", "--encryption", "--reverse", "--private_key", "C:\Users crypto\private_rev.key", "--public_cert", "D:\tutu\titi_rev.crt", "--ca", "C:\Users\Public\ca_rev.crt", "--ca_name", "tls_ca_name_rev" +$expected = @{ 'endpoint' = '127.0.0.5:4317'; 'host' = 'my_host_name_2'; 'log_type' = 'File'; 'log_level' = 'trace'; 'log_file' = 'C:\Users\Public\cma_rev.log'; 'encryption' = 1; 'reversed_grpc_streaming' = 1; 'certificate' = 'D:\tutu\titi_rev.crt'; 'private_key' = 'C:\Users crypto\private_rev.key'; 'ca_certificate' = 'C:\Users\Public\ca_rev.crt'; 'ca_name' = 'tls_ca_name_rev' } +test_args_to_registry "agent/installer/centreon-monitoring-agent.exe" $exe_args $expected Write-Host "############################ modifier test ############################" -$args = '/S','--hostname', "my_host_name_10", "--endpoint","127.0.0.10:4317", "--no_reverse" -$expected = @{ 'endpoint'='127.0.0.10:4317';'host'='my_host_name_10';'log_type'='File'; 'log_level' = 'trace'; 'log_file'='C:\Users\Public\cma_rev.log'; 'encryption' = 1;'reversed_grpc_streaming'= 0; 'certificate'='D:\tutu\titi_rev.crt'; 'private_key'='C:\Users crypto\private_rev.key'; 'ca_certificate' = 'C:\Users\Public\ca_rev.crt'; 'ca_name' = 'tls_ca_name_rev' } -test_args_to_registry "agent/installer/centreon-monitoring-agent-modify.exe" $args $expected +$exe_args = '/S', '--hostname', "my_host_name_10", "--endpoint", "127.0.0.10:4317", "--no_reverse" +$expected = @{ 'endpoint' = '127.0.0.10:4317'; 'host' = 'my_host_name_10'; 'log_type' = 'File'; 'log_level' = 'trace'; 'log_file' = 'C:\Users\Public\cma_rev.log'; 'encryption' = 1; 'reversed_grpc_streaming' = 0; 'certificate' = 'D:\tutu\titi_rev.crt'; 'private_key' = 'C:\Users crypto\private_rev.key'; 'ca_certificate' = 'C:\Users\Public\ca_rev.crt'; 'ca_name' = 'tls_ca_name_rev' } +test_args_to_registry "agent/installer/centreon-monitoring-agent-modify.exe" $exe_args $expected -$args = '/S',"--log_type", "file", "--log_file", "C:\Users\Public\cma_rev2.log", "--log_level", "debug", "--log_max_file_size", "50", "--log_max_files", "20" -$expected = @{ 'endpoint'='127.0.0.10:4317';'host'='my_host_name_10';'log_type'='File'; 'log_level' = 'debug'; 'log_file'='C:\Users\Public\cma_rev2.log'; 'encryption' = 1;'reversed_grpc_streaming'= 0; 'certificate'='D:\tutu\titi_rev.crt'; 'log_max_file_size' = 50; 'log_max_files' = 20;'private_key'='C:\Users crypto\private_rev.key'; 'ca_certificate' = 'C:\Users\Public\ca_rev.crt'; 'ca_name' = 'tls_ca_name_rev' } -test_args_to_registry "agent/installer/centreon-monitoring-agent-modify.exe" $args $expected +$exe_args = '/S', "--log_type", "file", "--log_file", "C:\Users\Public\cma_rev2.log", "--log_level", "debug", "--log_max_file_size", "50", "--log_max_files", "20" +$expected = @{ 'endpoint' = '127.0.0.10:4317'; 'host' = 'my_host_name_10'; 'log_type' = 'File'; 'log_level' = 'debug'; 'log_file' = 'C:\Users\Public\cma_rev2.log'; 'encryption' = 1; 'reversed_grpc_streaming' = 0; 'certificate' = 'D:\tutu\titi_rev.crt'; 'log_max_file_size' = 50; 'log_max_files' = 20; 'private_key' = 'C:\Users crypto\private_rev.key'; 'ca_certificate' = 'C:\Users\Public\ca_rev.crt'; 'ca_name' = 'tls_ca_name_rev' } +test_args_to_registry "agent/installer/centreon-monitoring-agent-modify.exe" $exe_args $expected -$args = '/S',"--log_type", "EventLog", "--log_level", "error" -$expected = @{ 'endpoint'='127.0.0.10:4317';'host'='my_host_name_10';'log_type'='event-log'; 'log_level' = 'error'; 'encryption' = 1;'reversed_grpc_streaming'= 0; 'certificate'='D:\tutu\titi_rev.crt'; 'private_key'='C:\Users crypto\private_rev.key'; 'ca_certificate' = 'C:\Users\Public\ca_rev.crt'; 'ca_name' = 'tls_ca_name_rev' } -test_args_to_registry "agent/installer/centreon-monitoring-agent-modify.exe" $args $expected +$exe_args = '/S', "--log_type", "EventLog", "--log_level", "error" +$expected = @{ 'endpoint' = '127.0.0.10:4317'; 'host' = 'my_host_name_10'; 'log_type' = 'event-log'; 'log_level' = 'error'; 'encryption' = 1; 'reversed_grpc_streaming' = 0; 'certificate' = 'D:\tutu\titi_rev.crt'; 'private_key' = 'C:\Users crypto\private_rev.key'; 'ca_certificate' = 'C:\Users\Public\ca_rev.crt'; 'ca_name' = 'tls_ca_name_rev' } +test_args_to_registry "agent/installer/centreon-monitoring-agent-modify.exe" $exe_args $expected -$args = '/S',"--private_key", "C:\Users crypto\private_rev2.key", "--public_cert", "D:\tutu\titi_rev2.crt" -$expected = @{ 'endpoint'='127.0.0.10:4317';'host'='my_host_name_10';'log_type'='event-log'; 'log_level' = 'error'; 'encryption' = 1;'reversed_grpc_streaming'= 0; 'certificate'='D:\tutu\titi_rev2.crt'; 'private_key'='C:\Users crypto\private_rev2.key'; 'ca_certificate' = 'C:\Users\Public\ca_rev.crt'; 'ca_name' = 'tls_ca_name_rev' } -test_args_to_registry "agent/installer/centreon-monitoring-agent-modify.exe" $args $expected +$exe_args = '/S', "--private_key", "C:\Users crypto\private_rev2.key", "--public_cert", "D:\tutu\titi_rev2.crt" +$expected = @{ 'endpoint' = '127.0.0.10:4317'; 'host' = 'my_host_name_10'; 'log_type' = 'event-log'; 'log_level' = 'error'; 'encryption' = 1; 'reversed_grpc_streaming' = 0; 'certificate' = 'D:\tutu\titi_rev2.crt'; 'private_key' = 'C:\Users crypto\private_rev2.key'; 'ca_certificate' = 'C:\Users\Public\ca_rev.crt'; 'ca_name' = 'tls_ca_name_rev' } +test_args_to_registry "agent/installer/centreon-monitoring-agent-modify.exe" $exe_args $expected -$args = '/S',"--ca", "C:\Users\Public\ca_rev2.crt", "--ca_name", "tls_ca_name_rev2" -$expected = @{ 'endpoint'='127.0.0.10:4317';'host'='my_host_name_10';'log_type'='event-log'; 'log_level' = 'error'; 'encryption' = 1;'reversed_grpc_streaming'= 0; 'certificate'='D:\tutu\titi_rev2.crt'; 'private_key'='C:\Users crypto\private_rev2.key'; 'ca_certificate' = 'C:\Users\Public\ca_rev2.crt'; 'ca_name' = 'tls_ca_name_rev2' } -test_args_to_registry "agent/installer/centreon-monitoring-agent-modify.exe" $args $expected +$exe_args = '/S', "--ca", "C:\Users\Public\ca_rev2.crt", "--ca_name", "tls_ca_name_rev2" +$expected = @{ 'endpoint' = '127.0.0.10:4317'; 'host' = 'my_host_name_10'; 'log_type' = 'event-log'; 'log_level' = 'error'; 'encryption' = 1; 'reversed_grpc_streaming' = 0; 'certificate' = 'D:\tutu\titi_rev2.crt'; 'private_key' = 'C:\Users crypto\private_rev2.key'; 'ca_certificate' = 'C:\Users\Public\ca_rev2.crt'; 'ca_name' = 'tls_ca_name_rev2' } +test_args_to_registry "agent/installer/centreon-monitoring-agent-modify.exe" $exe_args $expected -$args = '/S',"--no_encryption" -$expected = @{ 'endpoint'='127.0.0.10:4317';'host'='my_host_name_10';'log_type'='event-log'; 'log_level' = 'error'; 'encryption' = 0;'reversed_grpc_streaming'= 0; 'certificate'='D:\tutu\titi_rev2.crt'; 'private_key'='C:\Users crypto\private_rev2.key'; 'ca_certificate' = 'C:\Users\Public\ca_rev2.crt'; 'ca_name' = 'tls_ca_name_rev2' } -test_args_to_registry "agent/installer/centreon-monitoring-agent-modify.exe" $args $expected +$exe_args = '/S', "--no_encryption" +$expected = @{ 'endpoint' = '127.0.0.10:4317'; 'host' = 'my_host_name_10'; 'log_type' = 'event-log'; 'log_level' = 'error'; 'encryption' = 0; 'reversed_grpc_streaming' = 0; 'certificate' = 'D:\tutu\titi_rev2.crt'; 'private_key' = 'C:\Users crypto\private_rev2.key'; 'ca_certificate' = 'C:\Users\Public\ca_rev2.crt'; 'ca_name' = 'tls_ca_name_rev2' } +test_args_to_registry "agent/installer/centreon-monitoring-agent-modify.exe" $exe_args $expected diff --git a/agent/inc/com/centreon/agent/drive_size.hh b/agent/inc/com/centreon/agent/drive_size.hh index c47e4ad0359..8b33cf10c74 100644 --- a/agent/inc/com/centreon/agent/drive_size.hh +++ b/agent/inc/com/centreon/agent/drive_size.hh @@ -19,12 +19,6 @@ #ifndef CENTREON_AGENT_NATIVE_DRIVE_SIZE_BASE_HH #define CENTREON_AGENT_NATIVE_DRIVE_SIZE_BASE_HH -#include -#include -#include "absl/base/thread_annotations.h" -#include "absl/container/btree_set.h" -#include "absl/synchronization/mutex.h" -#include "boost/asio/io_context.hpp" #include "check.hh" #include "re2/re2.h" @@ -270,6 +264,8 @@ class check_drive_size : public check { check::shared_from_this()); } + static void help(std::ostream& help_stream); + void start_check(const duration& timeout) override; static void thread_kill(); @@ -277,4 +273,4 @@ class check_drive_size : public check { } // namespace com::centreon::agent -#endif // CENTREON_AGENT_NATIVE_DRIVE_SIZE_HH \ No newline at end of file +#endif // CENTREON_AGENT_NATIVE_DRIVE_SIZE_HH diff --git a/agent/inc/com/centreon/agent/native_check_memory_base.hh b/agent/inc/com/centreon/agent/native_check_base.hh similarity index 70% rename from agent/inc/com/centreon/agent/native_check_memory_base.hh rename to agent/inc/com/centreon/agent/native_check_base.hh index c2610b04763..a5c1d48ab8d 100644 --- a/agent/inc/com/centreon/agent/native_check_memory_base.hh +++ b/agent/inc/com/centreon/agent/native_check_base.hh @@ -23,7 +23,7 @@ namespace com::centreon::agent { -namespace check_memory_detail { +namespace native_check_detail { /** * @brief we store the result of a measure in this struct @@ -31,12 +31,12 @@ namespace check_memory_detail { * @tparam nb_metric */ template -class memory_info { +class snapshot { protected: std::array _metrics; public: - virtual ~memory_info() = default; + virtual ~snapshot() = default; uint64_t get_metric(unsigned data_index) const { return _metrics[data_index]; @@ -51,7 +51,7 @@ class memory_info { return (static_cast(_metrics[data_index]) / total); } - virtual void dump_to_output(std::string* output, unsigned flags) const = 0; + virtual void dump_to_output(std::string* output) const = 0; }; /** @@ -61,7 +61,7 @@ class memory_info { * @tparam nb_metric */ template -class mem_to_status { +class measure_to_status { e_status _status; unsigned _data_index; double _threshold; @@ -70,20 +70,22 @@ class mem_to_status { bool _free_threshold; public: - mem_to_status(e_status status, - unsigned data_index, - double threshold, - unsigned total_data_index, - bool _percent, - bool free_threshold); + measure_to_status(e_status status, + unsigned data_index, + double threshold, + unsigned total_data_index, + bool _percent, + bool free_threshold); + + virtual ~measure_to_status() = default; unsigned get_data_index() const { return _data_index; } unsigned get_total_data_index() const { return _total_data_index; } e_status get_status() const { return _status; } double get_threshold() const { return _threshold; } - void compute_status(const memory_info& to_test, - e_status* status) const; + virtual void compute_status(const snapshot& to_test, + e_status* status) const; }; /** @@ -97,13 +99,7 @@ struct metric_definition { bool percent; }; -/** - * @brief this must be defined and filled in final OS implementation - * - */ -extern const std::vector metric_definitions; - -} // namespace check_memory_detail +} // namespace native_check_detail /** * @brief native check base (to inherit) @@ -111,10 +107,10 @@ extern const std::vector metric_definitions; * @tparam nb_metric */ template -class check_memory_base : public check { +class native_check_base : public check { protected: /** - * @brief key used to store mem_to_status + * @brief key used to store measure_to_status * @tparam 1 index (phys, virtual..) * @tparam 2 total index (phys, virtual..) * @tparam 3 e_status warning or critical @@ -122,14 +118,15 @@ class check_memory_base : public check { */ using mem_to_status_key = std::tuple; - boost::container::flat_map> - _mem_to_status; + boost::container::flat_map< + mem_to_status_key, + std::unique_ptr>> + _measure_to_status; - unsigned _output_flags = 0; + const char* _no_percent_unit = nullptr; public: - check_memory_base(const std::shared_ptr& io_context, + native_check_base(const std::shared_ptr& io_context, const std::shared_ptr& logger, time_point first_start_expected, duration check_interval, @@ -140,19 +137,22 @@ class check_memory_base : public check { const engine_to_agent_request_ptr& cnf, check::completion_handler&& handler); - std::shared_ptr> shared_from_this() { - return std::static_pointer_cast>( + std::shared_ptr> shared_from_this() { + return std::static_pointer_cast>( check::shared_from_this()); } void start_check(const duration& timeout) override; - virtual std::shared_ptr> measure() - const = 0; + virtual std::shared_ptr> + measure() = 0; - e_status compute(const check_memory_detail::memory_info& data, + e_status compute(const native_check_detail::snapshot& data, std::string* output, std::list* perfs) const; + + virtual const std::vector& + get_metric_definitions() const = 0; }; } // namespace com::centreon::agent diff --git a/agent/installer/silent.nsi b/agent/installer/silent.nsi index 1ac6a4e43f1..cef6ecf1c33 100644 --- a/agent/installer/silent.nsi +++ b/agent/installer/silent.nsi @@ -125,6 +125,11 @@ Function cmd_line_to_registry Call silent_fatal_error ${EndIf} WriteRegStr HKLM ${CMA_REG_KEY} "host" "$0" + ${If} ${Errors} + StrCpy $1 "Failed to write registry key for host" + Call silent_fatal_error + ${EndIf} + ClearErrors ${GetOptions} $cmdline_parameters "--endpoint" $0 ${If} ${Errors} diff --git a/agent/native_linux/src/check_cpu.cc b/agent/native_linux/src/check_cpu.cc index 89995fda77b..679f518c013 100644 --- a/agent/native_linux/src/check_cpu.cc +++ b/agent/native_linux/src/check_cpu.cc @@ -285,3 +285,33 @@ e_status check_cpu::compute( return _compute(first_measure, second_measure, _sz_summary_labels.data(), _sz_perfdata_name.data(), output, perfs); } + +void check_cpu::help(std::ostream& help_stream) { + help_stream << R"( +- cpu params: + warning-core: threshold for warning status on core usage in percentage + critical-core: threshold for critical status on core usage in percentage + warning-average: threshold for warning status on average usage in percentage + critical-average: threshold for critical status on average usage in percentage + warning-core-user: threshold for warning status on core user usage in percentage + critical-core-user: threshold for critical status on core user usage in percentage + warning-average-user: threshold for warning status on average user usage in percentage + critical-average-user: threshold for critical status on average user usage in percentage + warning-core-nice: threshold for warning status on core nice usage in percentage + critical-core-nice: threshold for critical status on core nice usage in percentage + warning-average-nice: threshold for warning status on average nice usage in percentage + critical-average-nice: threshold for critical status on average nice usage in percentage + warning-core-system: threshold for warning status on core system usage in percentage + critical-core-system: threshold for critical status on core system usage in percentage + warning-average-system: threshold for warning status on average system usage in percentage + critical-average-system: threshold for critical status on average system usage in percentage + warning-core-iowait: threshold for warning status on core iowait usage in percentage + critical-core-iowait: threshold for critical status on core iowait usage in percentage + warning-average-iowait: threshold for warning status on average iowait usage in percentage + critical-average-iowait: threshold for critical status on average iowait usage in percentage + warning-core-guest: threshold for warning status on core guest usage in percentage + critical-core-guest: threshold for critical status on core guest usage in percentage + warning-average-guest: threshold for warning status on average guest usage in percentage + critical-average-guest: threshold for critical status on average guest usage in percentage + )"; +} diff --git a/agent/native_windows/inc/com/centreon/agent/check_memory.hh b/agent/native_windows/inc/com/centreon/agent/check_memory.hh index eedc504bfaf..f5b9c6aaaba 100644 --- a/agent/native_windows/inc/com/centreon/agent/check_memory.hh +++ b/agent/native_windows/inc/com/centreon/agent/check_memory.hh @@ -19,14 +19,14 @@ #ifndef CENTREON_AGENT_NATIVE_CHECK_MEMORY_HH #define CENTREON_AGENT_NATIVE_CHECK_MEMORY_HH -#include "native_check_memory_base.hh" +#include "native_check_base.hh" struct _PERFORMANCE_INFORMATION; namespace com::centreon::agent { -namespace check_memory_detail { +namespace native_check_detail { -enum e_metric : unsigned { +enum e_memory_metric : unsigned { phys_total, phys_free, phys_used, @@ -45,27 +45,33 @@ enum e_metric : unsigned { * */ class w_memory_info - : public memory_info { + : public snapshot { + unsigned _output_flags = 0; + public: enum output_flags : unsigned { dump_swap = 1, dump_virtual }; - w_memory_info(); + w_memory_info(unsigned flags); w_memory_info(const MEMORYSTATUSEX& mem_status, - const struct _PERFORMANCE_INFORMATION& perf_mem_status); + const struct _PERFORMANCE_INFORMATION& perf_mem_status, + unsigned flags = 0); void init(const MEMORYSTATUSEX& mem_status, const struct _PERFORMANCE_INFORMATION& perf_mem_status); - void dump_to_output(std::string* output, unsigned flags) const override; + void dump_to_output(std::string* output) const override; }; -} // namespace check_memory_detail +} // namespace native_check_detail /** * @brief native final check object * */ -class check_memory - : public check_memory_base { +class check_memory : public native_check_base< + native_check_detail::e_memory_metric::nb_metric> { + protected: + unsigned _output_flags = 0; + public: check_memory(const std::shared_ptr& io_context, const std::shared_ptr& logger, @@ -78,9 +84,14 @@ class check_memory const engine_to_agent_request_ptr& cnf, check::completion_handler&& handler); - std::shared_ptr> - measure() const override; + std::shared_ptr> + measure() override; + + static void help(std::ostream& help_stream); + + const std::vector& + get_metric_definitions() const override; }; } // namespace com::centreon::agent diff --git a/agent/native_windows/src/check_cpu.cc b/agent/native_windows/src/check_cpu.cc index 11e95510340..502f3b0161a 100644 --- a/agent/native_windows/src/check_cpu.cc +++ b/agent/native_windows/src/check_cpu.cc @@ -525,3 +525,53 @@ e_status check_cpu::compute( return _compute(first_measure, second_measure, _sz_summary_labels.data(), _sz_perfdata_name.data(), output, perfs); } + +void check_cpu::help(std::ostream& help_stream) { + help_stream << R"( +- cpu params: + use-nt-query-system-information (default true): true: use NtQuerySystemInformation instead of performance counters + cpu-detailed (default false): true: add detailed cpu usage metrics + warning-core: threshold for warning status on core usage in percentage + critical-core: threshold for critical status on core usage in percentage + warning-average: threshold for warning status on average usage in percentage + critical-average: threshold for critical status on average usage in percentage + warning-core-user: threshold for warning status on core user usage in percentage + critical-core-user: threshold for critical status on core user usage in percentage + warning-average-user: threshold for warning status on average user usage in percentage + critical-average-user: threshold for critical status on average user usage in percentage + warning-core-system: threshold for warning status on core system usage in percentage + critical-core-system: threshold for critical status on core system usage in percentage + warning-average-system: threshold for warning status on average system usage in percentage + critical-average-system: threshold for critical status on average system usage in percentage + An example of configuration: + { + "check": "cpu_percentage", + "args": { + "cpu-detailed": true, + "warning-core": 80, + "critical-core": 90, + "warning-average": 60, + "critical-average": 70 + } + } + Examples of output: + OK: CPU(s) average usage is 50.00% + WARNING: CPU'0' Usage: 40.00%, User 25.00%, System 10.00%, Idle 60.00%, Interrupt 5.00%, Dpc Interrupt 1.00% CRITICAL: CPU'1' Usage: 60.00%, User 45.00%, System 10.00%, Idle 40.00%, Interrupt 5.00%, Dpc Interrupt 0.00% WARNING: CPU(s) average Usage: 50.00%, User 35.00%, System 10.00%, Idle 50.00%, Interrupt 5.00%, Dpc Interrupt 0.50% + Metrics: + Normal mode: + #core.cpu.utilization.percentage + cpu.utilization.percentage + cpu-detailed mode: + ~user#core.cpu.utilization.percentage + ~system#core.cpu.utilization.percentage + ~idle#core.cpu.utilization.percentage + ~interrupt#core.cpu.utilization.percentage + ~dpc_interrupt#core.cpu.utilization.percentage + ~used#core.cpu.utilization.percentage + user#cpu.utilization.percentage + system#cpu.utilization.percentage + idle#cpu.utilization.percentage + interrupt#cpu.utilization.percentage + dpc_interrupt#cpu.utilization.percentage +)"; +} diff --git a/agent/native_windows/src/check_drive_size.cc b/agent/native_windows/src/check_drive_size.cc index 8b81b695a90..9a37f6e7feb 100644 --- a/agent/native_windows/src/check_drive_size.cc +++ b/agent/native_windows/src/check_drive_size.cc @@ -56,6 +56,7 @@ static e_drive_fs_type get_fs_type( fs_type = e_drive_fs_type::hr_storage_ram_disk; break; default: + fs_type = e_drive_fs_type::hr_unknown; SPDLOG_LOGGER_ERROR(logger, "{} unknown drive type {}", fs_root, drive_type); break; @@ -78,6 +79,7 @@ static e_drive_fs_type get_fs_type( if (fs_search != _sz_filesystem_map.end()) { fs_type |= fs_search->second; } else { + fs_type |= e_drive_fs_type::hr_fs_unknown; SPDLOG_LOGGER_ERROR(logger, "{} unknown file system type {}", fs_root, file_system_name); } @@ -130,8 +132,8 @@ std::list os_fs_stats(filter& filt, if (success) { SPDLOG_LOGGER_TRACE(logger, "{} total: {}, free {}", fs_to_test, - total_number_of_bytes.QuadPart , - total_number_of_free_bytes.QuadPart); + total_number_of_bytes.QuadPart, + total_number_of_free_bytes.QuadPart); result.emplace_back(std::move(fs_to_test), total_number_of_bytes.QuadPart - diff --git a/agent/native_windows/src/check_memory.cc b/agent/native_windows/src/check_memory.cc index 36e7862a013..f22eed67be1 100644 --- a/agent/native_windows/src/check_memory.cc +++ b/agent/native_windows/src/check_memory.cc @@ -19,38 +19,39 @@ #include #include -#include "agent/native_windows/inc/com/centreon/agent/check_memory.hh" #include "check_memory.hh" -#include "native_check_memory_base.cc" +#include "native_check_base.cc" using namespace com::centreon::agent; -using namespace com::centreon::agent::check_memory_detail; +using namespace com::centreon::agent::native_check_detail; -namespace com::centreon::agent::check_memory_detail { +namespace com::centreon::agent::native_check_detail { /** * @brief little struct used to format memory output (B, KB, MB or GB) * */ -struct byte_metric { +struct byte_memory_metric { uint64_t byte_value; }; -} // namespace com::centreon::agent::check_memory_detail +} // namespace com::centreon::agent::native_check_detail namespace fmt { /** - * @brief formatter of byte_metric + * @brief formatter of byte_memory_metric * * @tparam */ template <> -struct formatter { +struct formatter< + com::centreon::agent::native_check_detail::byte_memory_metric> { constexpr auto parse(format_parse_context& ctx) -> format_parse_context::iterator { return ctx.begin(); } - auto format(const com::centreon::agent::check_memory_detail::byte_metric& v, - format_context& ctx) const -> format_context::iterator { + auto format( + const com::centreon::agent::native_check_detail::byte_memory_metric& v, + format_context& ctx) const -> format_context::iterator { if (v.byte_value < 1024) { return fmt::format_to(ctx.out(), "{} B", v.byte_value); } @@ -79,14 +80,14 @@ struct formatter { }; } // namespace fmt -namespace com::centreon::agent::check_memory_detail { +namespace com::centreon::agent::native_check_detail { /** * @brief Construct a new w_memory info * it measures memory usage and fill _metrics * */ -w_memory_info::w_memory_info() { +w_memory_info::w_memory_info(unsigned flags) : _output_flags(flags) { MEMORYSTATUSEX mem_status; mem_status.dwLength = sizeof(mem_status); if (!GlobalMemoryStatusEx(&mem_status)) { @@ -108,7 +109,9 @@ w_memory_info::w_memory_info() { * @param mem_status */ w_memory_info::w_memory_info(const MEMORYSTATUSEX& mem_status, - const PERFORMANCE_INFORMATION& perf_mem_status) { + const PERFORMANCE_INFORMATION& perf_mem_status, + unsigned flags) + : _output_flags(flags) { init(mem_status, perf_mem_status); } @@ -119,23 +122,24 @@ w_memory_info::w_memory_info(const MEMORYSTATUSEX& mem_status, */ void w_memory_info::init(const MEMORYSTATUSEX& mem_status, const PERFORMANCE_INFORMATION& perf_mem_status) { - _metrics[e_metric::phys_total] = mem_status.ullTotalPhys; - _metrics[e_metric::phys_free] = mem_status.ullAvailPhys; - _metrics[e_metric::phys_used] = + _metrics[e_memory_metric::phys_total] = mem_status.ullTotalPhys; + _metrics[e_memory_metric::phys_free] = mem_status.ullAvailPhys; + _metrics[e_memory_metric::phys_used] = mem_status.ullTotalPhys - mem_status.ullAvailPhys; - _metrics[e_metric::swap_total] = + _metrics[e_memory_metric::swap_total] = perf_mem_status.PageSize * (perf_mem_status.CommitLimit - perf_mem_status.PhysicalTotal); - _metrics[e_metric::swap_used] = + _metrics[e_memory_metric::swap_used] = perf_mem_status.PageSize * (perf_mem_status.CommitTotal + perf_mem_status.PhysicalAvailable - perf_mem_status.PhysicalTotal); - _metrics[e_metric::swap_free] = - _metrics[e_metric::swap_total] - _metrics[e_metric::swap_used]; - _metrics[e_metric::virtual_total] = mem_status.ullTotalPageFile; - _metrics[e_metric::virtual_free] = mem_status.ullAvailPageFile; - _metrics[e_metric::virtual_used] = - _metrics[e_metric::virtual_total] - _metrics[e_metric::virtual_free]; + _metrics[e_memory_metric::swap_free] = _metrics[e_memory_metric::swap_total] - + _metrics[e_memory_metric::swap_used]; + _metrics[e_memory_metric::virtual_total] = mem_status.ullTotalPageFile; + _metrics[e_memory_metric::virtual_free] = mem_status.ullAvailPageFile; + _metrics[e_memory_metric::virtual_used] = + _metrics[e_memory_metric::virtual_total] - + _metrics[e_memory_metric::virtual_free]; } /** @@ -143,72 +147,55 @@ void w_memory_info::init(const MEMORYSTATUSEX& mem_status, * * @param output */ -void w_memory_info::dump_to_output(std::string* output, unsigned flags) const { - fmt::format_to( - std::back_inserter(*output), - "Ram total: {}, used (-buffers/cache): {} ({:.2f}%), " - "free: {} ({:.2f}%)", - byte_metric{_metrics[e_metric::phys_total]}, - byte_metric{_metrics[e_metric::phys_used]}, - get_proportional_value(e_metric::phys_used, e_metric::phys_total) * 100, - byte_metric{_metrics[e_metric::phys_free]}, - get_proportional_value(e_metric::phys_free, e_metric::phys_total) * 100); +void w_memory_info::dump_to_output(std::string* output) const { + fmt::format_to(std::back_inserter(*output), + "Ram total: {}, used (-buffers/cache): {} ({:.2f}%), " + "free: {} ({:.2f}%)", + byte_memory_metric{_metrics[e_memory_metric::phys_total]}, + byte_memory_metric{_metrics[e_memory_metric::phys_used]}, + get_proportional_value(e_memory_metric::phys_used, + e_memory_metric::phys_total) * + 100, + byte_memory_metric{_metrics[e_memory_metric::phys_free]}, + get_proportional_value(e_memory_metric::phys_free, + e_memory_metric::phys_total) * + 100); - if (flags & output_flags::dump_swap) { - fmt::format_to( - std::back_inserter(*output), - " Swap total: {}, used: {} ({:.2f}%), free: {} ({:.2f}%)", - byte_metric{_metrics[e_metric::swap_total]}, - byte_metric{_metrics[e_metric::swap_used]}, - get_proportional_value(e_metric::swap_used, e_metric::swap_total) * 100, - byte_metric{_metrics[e_metric::swap_free]}, - get_proportional_value(e_metric::swap_free, e_metric::swap_total) * - 100); + if (_output_flags & output_flags::dump_swap) { + fmt::format_to(std::back_inserter(*output), + " Swap total: {}, used: {} ({:.2f}%), free: {} ({:.2f}%)", + byte_memory_metric{_metrics[e_memory_metric::swap_total]}, + byte_memory_metric{_metrics[e_memory_metric::swap_used]}, + get_proportional_value(e_memory_metric::swap_used, + e_memory_metric::swap_total) * + 100, + byte_memory_metric{_metrics[e_memory_metric::swap_free]}, + get_proportional_value(e_memory_metric::swap_free, + e_memory_metric::swap_total) * + 100); } - if (flags & output_flags::dump_virtual) { + if (_output_flags & output_flags::dump_virtual) { fmt::format_to(std::back_inserter(*output), " Virtual total: {}, used: {} ({:.2f}%), free: {} ({:.2f}%)", - byte_metric{_metrics[e_metric::virtual_total]}, - byte_metric{_metrics[e_metric::virtual_used]}, - get_proportional_value(e_metric::virtual_used, - e_metric::virtual_total) * + byte_memory_metric{_metrics[e_memory_metric::virtual_total]}, + byte_memory_metric{_metrics[e_memory_metric::virtual_used]}, + get_proportional_value(e_memory_metric::virtual_used, + e_memory_metric::virtual_total) * 100, - byte_metric{_metrics[e_metric::virtual_free]}, - get_proportional_value(e_metric::virtual_free, - e_metric::virtual_total) * + byte_memory_metric{_metrics[e_memory_metric::virtual_free]}, + get_proportional_value(e_memory_metric::virtual_free, + e_memory_metric::virtual_total) * 100); } } -/** - * @brief metric defines - * - */ -const std::vector metric_definitions = { - {"memory.usage.bytes", e_metric::phys_used, e_metric::phys_total, false}, - {"memory.free.bytes", e_metric::phys_free, e_metric::phys_total, false}, - {"memory.usage.percentage", e_metric::phys_used, e_metric::phys_total, - true}, - - {"swap.usage.bytes", e_metric::swap_used, e_metric::swap_total, false}, - {"swap.free.bytes", e_metric::swap_free, e_metric::swap_total, false}, - {"swap.usage.percentage", e_metric::swap_used, e_metric::swap_total, true}, - - {"virtual-memory.usage.bytes", e_metric::virtual_used, - e_metric::virtual_total, false}, - {"virtual-memory.free.bytes", e_metric::virtual_free, - e_metric::virtual_total, false}, - {"virtual-memory.usage.percentage", e_metric::virtual_used, - e_metric::virtual_total, true}, -}; - -} // namespace com::centreon::agent::check_memory_detail +} // namespace com::centreon::agent::native_check_detail -using windows_mem_to_status = mem_to_status; +using windows_mem_to_status = measure_to_status; using mem_to_status_constructor = - std::function; + std::function(double /*threshold*/)>; /** * @brief status threshold defines @@ -219,149 +206,149 @@ static const absl::flat_hash_map // phys {"critical-usage", [](double threshold) { - return windows_mem_to_status(e_status::critical, e_metric::phys_used, - threshold, e_metric::phys_total, false, - false); + return std::make_unique( + e_status::critical, e_memory_metric::phys_used, threshold, + e_memory_metric::phys_total, false, false); }}, {"warning-usage", [](double threshold) { - return windows_mem_to_status(e_status::warning, e_metric::phys_used, - threshold, e_metric::phys_total, false, - false); + return std::make_unique( + e_status::warning, e_memory_metric::phys_used, threshold, + e_memory_metric::phys_total, false, false); }}, {"critical-usage-free", [](double threshold) { - return windows_mem_to_status(e_status::critical, e_metric::phys_free, - threshold, e_metric::phys_total, false, - true); + return std::make_unique( + e_status::critical, e_memory_metric::phys_free, threshold, + e_memory_metric::phys_total, false, true); }}, {"warning-usage-free", [](double threshold) { - return windows_mem_to_status(e_status::warning, e_metric::phys_free, - threshold, e_metric::phys_total, false, - true); + return std::make_unique( + e_status::warning, e_memory_metric::phys_free, threshold, + e_memory_metric::phys_total, false, true); }}, {"critical-usage-prct", [](double threshold) { - return windows_mem_to_status(e_status::critical, e_metric::phys_used, - threshold / 100, e_metric::phys_total, - true, false); + return std::make_unique( + e_status::critical, e_memory_metric::phys_used, threshold / 100, + e_memory_metric::phys_total, true, false); }}, {"warning-usage-prct", [](double threshold) { - return windows_mem_to_status(e_status::warning, e_metric::phys_used, - threshold / 100, e_metric::phys_total, - true, false); + return std::make_unique( + e_status::warning, e_memory_metric::phys_used, threshold / 100, + e_memory_metric::phys_total, true, false); }}, {"critical-usage-free-prct", [](double threshold) { - return windows_mem_to_status(e_status::critical, e_metric::phys_free, - threshold / 100, e_metric::phys_total, - true, true); + return std::make_unique( + e_status::critical, e_memory_metric::phys_free, threshold / 100, + e_memory_metric::phys_total, true, true); }}, {"warning-usage-free-prct", [](double threshold) { - return windows_mem_to_status(e_status::warning, e_metric::phys_free, - threshold / 100, e_metric::phys_total, - true, true); + return std::make_unique( + e_status::warning, e_memory_metric::phys_free, threshold / 100, + e_memory_metric::phys_total, true, true); }}, // swap {"critical-swap", [](double threshold) { - return windows_mem_to_status(e_status::critical, e_metric::swap_used, - threshold, e_metric::swap_total, false, - false); + return std::make_unique( + e_status::critical, e_memory_metric::swap_used, threshold, + e_memory_metric::swap_total, false, false); }}, {"warning-swap", [](double threshold) { - return windows_mem_to_status(e_status::warning, e_metric::swap_used, - threshold, e_metric::swap_total, false, - false); + return std::make_unique( + e_status::warning, e_memory_metric::swap_used, threshold, + e_memory_metric::swap_total, false, false); }}, {"critical-swap-free", [](double threshold) { - return windows_mem_to_status(e_status::critical, e_metric::swap_free, - threshold, e_metric::swap_total, false, - true); + return std::make_unique( + e_status::critical, e_memory_metric::swap_free, threshold, + e_memory_metric::swap_total, false, true); }}, {"warning-swap-free", [](double threshold) { - return windows_mem_to_status(e_status::warning, e_metric::swap_free, - threshold, e_metric::swap_total, false, - true); + return std::make_unique( + e_status::warning, e_memory_metric::swap_free, threshold, + e_memory_metric::swap_total, false, true); }}, {"critical-swap-prct", [](double threshold) { - return windows_mem_to_status(e_status::critical, e_metric::swap_used, - threshold / 100, e_metric::swap_total, - true, false); + return std::make_unique( + e_status::critical, e_memory_metric::swap_used, threshold / 100, + e_memory_metric::swap_total, true, false); }}, {"warning-swap-prct", [](double threshold) { - return windows_mem_to_status(e_status::warning, e_metric::swap_used, - threshold / 100, e_metric::swap_total, - true, false); + return std::make_unique( + e_status::warning, e_memory_metric::swap_used, threshold / 100, + e_memory_metric::swap_total, true, false); }}, {"critical-swap-free-prct", [](double threshold) { - return windows_mem_to_status(e_status::critical, e_metric::swap_free, - threshold / 100, e_metric::swap_total, - true, true); + return std::make_unique( + e_status::critical, e_memory_metric::swap_free, threshold / 100, + e_memory_metric::swap_total, true, true); }}, {"warning-swap-free-prct", [](double threshold) { - return windows_mem_to_status(e_status::warning, e_metric::swap_free, - threshold / 100, e_metric::swap_total, - true, true); + return std::make_unique( + e_status::warning, e_memory_metric::swap_free, threshold / 100, + e_memory_metric::swap_total, true, true); }}, // virtual memory {"critical-virtual", [](double threshold) { - return windows_mem_to_status(e_status::critical, - e_metric::virtual_used, threshold, - e_metric::virtual_total, false, false); + return std::make_unique( + e_status::critical, e_memory_metric::virtual_used, threshold, + e_memory_metric::virtual_total, false, false); }}, {"warning-virtual", [](double threshold) { - return windows_mem_to_status(e_status::warning, - e_metric::virtual_used, threshold, - e_metric::virtual_total, false, false); + return std::make_unique( + e_status::warning, e_memory_metric::virtual_used, threshold, + e_memory_metric::virtual_total, false, false); }}, {"critical-virtual-free", [](double threshold) { - return windows_mem_to_status(e_status::critical, - e_metric::virtual_free, threshold, - e_metric::virtual_total, false, true); + return std::make_unique( + e_status::critical, e_memory_metric::virtual_free, threshold, + e_memory_metric::virtual_total, false, true); }}, {"warning-virtual-free", [](double threshold) { - return windows_mem_to_status(e_status::warning, - e_metric::virtual_free, threshold, - e_metric::virtual_total, false, true); + return std::make_unique( + e_status::warning, e_memory_metric::virtual_free, threshold, + e_memory_metric::virtual_total, false, true); }}, {"critical-virtual-prct", [](double threshold) { - return windows_mem_to_status(e_status::critical, - e_metric::virtual_used, threshold / 100, - e_metric::virtual_total, true, false); + return std::make_unique( + e_status::critical, e_memory_metric::virtual_used, + threshold / 100, e_memory_metric::virtual_total, true, false); }}, {"warning-virtual-prct", [](double threshold) { - return windows_mem_to_status(e_status::warning, - e_metric::virtual_used, threshold / 100, - e_metric::virtual_total, true, false); + return std::make_unique( + e_status::warning, e_memory_metric::virtual_used, + threshold / 100, e_memory_metric::virtual_total, true, false); }}, {"critical-virtual-free-prct", [](double threshold) { - return windows_mem_to_status(e_status::critical, - e_metric::virtual_free, threshold / 100, - e_metric::virtual_total, true, true); + return std::make_unique( + e_status::critical, e_memory_metric::virtual_free, + threshold / 100, e_memory_metric::virtual_total, true, true); }}, {"warning-virtual-free-prct", [](double threshold) { - return windows_mem_to_status(e_status::warning, - e_metric::virtual_free, threshold / 100, - e_metric::virtual_total, true, true); + return std::make_unique( + e_status::warning, e_memory_metric::virtual_free, + threshold / 100, e_memory_metric::virtual_total, true, true); }} }; @@ -390,7 +377,7 @@ check_memory::check_memory(const std::shared_ptr& io_context, const rapidjson::Value& args, const engine_to_agent_request_ptr& cnf, check::completion_handler&& handler) - : check_memory_base(io_context, + : native_check_base(io_context, logger, first_start_expected, check_interval, @@ -400,6 +387,7 @@ check_memory::check_memory(const std::shared_ptr& io_context, args, cnf, std::move(handler)) { + _no_percent_unit = "B"; if (args.IsObject()) { for (auto member_iter = args.MemberBegin(); member_iter != args.MemberEnd(); ++member_iter) { @@ -434,24 +422,24 @@ check_memory::check_memory(const std::shared_ptr& io_context, const rapidjson::Value& val = member_iter->value; if (val.IsFloat() || val.IsInt() || val.IsUint() || val.IsInt64() || val.IsUint64()) { - windows_mem_to_status mem_checker = + std::unique_ptr mem_checker = mem_to_status_search->second(member_iter->value.GetDouble()); - _mem_to_status.emplace( - std::make_tuple(mem_checker.get_data_index(), - mem_checker.get_total_data_index(), - mem_checker.get_status()), - mem_checker); + _measure_to_status.emplace( + std::make_tuple(mem_checker->get_data_index(), + mem_checker->get_total_data_index(), + mem_checker->get_status()), + std::move(mem_checker)); } else if (val.IsString()) { auto to_conv = val.GetString(); double dval; if (absl::SimpleAtod(to_conv, &dval)) { - windows_mem_to_status mem_checker = + std::unique_ptr mem_checker = mem_to_status_search->second(dval); - _mem_to_status.emplace( - std::make_tuple(mem_checker.get_data_index(), - mem_checker.get_total_data_index(), - mem_checker.get_status()), - mem_checker); + _measure_to_status.emplace( + std::make_tuple(mem_checker->get_data_index(), + mem_checker->get_total_data_index(), + mem_checker->get_status()), + std::move(mem_checker)); } else { SPDLOG_LOGGER_ERROR( logger, @@ -476,14 +464,106 @@ check_memory::check_memory(const std::shared_ptr& io_context, * @brief create a w_memory_info * * @return std::shared_ptr< - * check_memory_detail::memory_info> + * native_check_detail::snapshot> + */ +std::shared_ptr> +check_memory::measure() { + return std::make_shared(_output_flags); +} + +/** + * @brief metric defines + * */ -std::shared_ptr< - check_memory_detail::memory_info> -check_memory::measure() const { - return std::make_shared(); +static const std::vector + metric_definitions = { + {"memory.usage.bytes", e_memory_metric::phys_used, + e_memory_metric::phys_total, false}, + {"memory.free.bytes", e_memory_metric::phys_free, + e_memory_metric::phys_total, false}, + {"memory.usage.percentage", e_memory_metric::phys_used, + e_memory_metric::phys_total, true}, + + {"swap.usage.bytes", e_memory_metric::swap_used, + e_memory_metric::swap_total, false}, + {"swap.free.bytes", e_memory_metric::swap_free, + e_memory_metric::swap_total, false}, + {"swap.usage.percentage", e_memory_metric::swap_used, + e_memory_metric::swap_total, true}, + + {"virtual-memory.usage.bytes", e_memory_metric::virtual_used, + e_memory_metric::virtual_total, false}, + {"virtual-memory.free.bytes", e_memory_metric::virtual_free, + e_memory_metric::virtual_total, false}, + {"virtual-memory.usage.percentage", e_memory_metric::virtual_used, + e_memory_metric::virtual_total, true}, +}; + +const std::vector& +check_memory::get_metric_definitions() const { + return metric_definitions; +} + +void check_memory::help(std::ostream& help_stream) { + help_stream << R"( +- memory params: + swap (default false): true: add swap to output + virtual (default false): true: add virtual memory to output + critical-usage: threshold for critical status on physical memory usage in bytes + warning-usage: threshold for warning status on physyical memory usage in bytes + critical-usage-free: threshold for critical status on free physical memory in bytes, if free memory is lower than threshold, service is critical + warning-usage-free: threshold for warning status on free physical memory in bytes + critical-usage-prct: threshold for critical status on memory usage in percentage + warning-usage-prct: threshold for warning status on memory usage in percentage + critical-usage-free-prct: threshold for critical status on free memory in percentage + warning-usage-free-prct: threshold for warning status on free memory in percentage + critical-swap: threshold for critical status on swap usage in bytes + warning-swap: threshold for warning status on swap usage in bytes + critical-swap-free: threshold for critical status on free swap in bytes + warning-swap-free: threshold for warning status on free swap in bytes + critical-swap-prct: threshold for critical status on swap usage in percentage + warning-swap-prct: threshold for warning status on swap usage in percentage + critical-swap-free-prct: threshold for critical status on free swap in percentage + warning-swap-free-prct: threshold for warning status on free swap in percentage + critical-virtual: threshold for critical status on virtual memory usage in bytes + warning-virtual: threshold for warning status on virtual memory usage in bytes + critical-virtual-free: threshold for critical status on free virtual memory in bytes + warning-virtual-free: threshold for warning status on free virtual memory in bytes + critical-virtual-prct: threshold for critical status on virtual memory usage in percentage + warning-virtual-prct: threshold for warning status on virtual memory usage in percentage + critical-virtual-free-prct: threshold for critical status on free virtual memory in percentage + warning-virtual-free-prct: threshold for warning status on free virtual memory in percentage + An example of configuration: + { + "check": "memory", + "args: { + "swap": true, + "virtual": true, + "warning-usage-prct": 80, + "critical-usage-prct": 90 + } + } + Examples of output: + OK: Ram total: 16 GB, used (-buffers/cache): 15.99 GB (99.96%), free: 7 MB (0.04%) + With swap flag: + OK: Ram total: 16 GB, used (-buffers/cache): 15.99 GB (99.96%), free: 7 MB (0.04%) Swap total: 44 GB, used: 4 GB (9.11%), free: 39.99 GB (90.89%) + With swap and virtual flag: + OK: Ram total: 16 GB, used (-buffers/cache): 15.99 GB (99.96%), free: 7 MB (0.04%) Swap total: 44 GB, used: 4 GB (9.11%), free: 39.99 GB (90.89%) Virtual total: 24 GB, used: 18 GB (75.00%), free: 6 GB (25.00%) + Metrics: + memory.usage.bytes + memory.free.bytes + memory.usage.percentage + swap.usage.bytes + swap.free.bytes + swap.usage.percentage + virtual-memory.usage.bytes + virtual-memory.free.bytes + virtual-memory.usage.percentage +)"; } namespace com::centreon::agent { -template class check_memory_base; +template class native_check_base< + native_check_detail::e_memory_metric::nb_metric>; } diff --git a/agent/native_windows/src/check_uptime.cc b/agent/native_windows/src/check_uptime.cc index eb8066e4401..6aa7dd83e58 100644 --- a/agent/native_windows/src/check_uptime.cc +++ b/agent/native_windows/src/check_uptime.cc @@ -157,3 +157,27 @@ e_status check_uptime::compute(uint64_t ms_uptime, } return status; } + +void check_uptime::help(std::ostream& help_stream) { + help_stream << + R"( +- uptime params:" + unit (defaults s): can be s, second, m, minute, h, hour, d, day, w, week + warning-uptime: warning threshold, if computer has been up for less than this time, service will be in warning state + critical-uptime: critical threshold + An example of configuration: + { + "check": "uptime", + "args": { + "unit": "day", + "warning-uptime": 1, + "critical-uptime": 2 + } + } + Examples of output: + OK: System uptime is: 5d 1h 1m 1s + CRITICAL: System uptime is: 1d 4h 0m 0s + Metrics: + uptime +)"; +} diff --git a/agent/src/drive_size.cc b/agent/src/drive_size.cc index 4f7031e1c56..6bdd439bdff 100644 --- a/agent/src/drive_size.cc +++ b/agent/src/drive_size.cc @@ -17,7 +17,6 @@ */ #include "drive_size.hh" -#include "absl/synchronization/mutex.h" #include "check.hh" #include "com/centreon/common/perfdata.hh" #include "com/centreon/common/rapidjson_helper.hh" @@ -582,4 +581,54 @@ void check_drive_size::thread_kill() { delete _worker_thread; _worker_thread = nullptr; } -} \ No newline at end of file +} + +void check_drive_size::help(std::ostream& help_stream) { + help_stream << + R"( +- storage params:" + unit (default %): unit of threshold. If different from % threshold are in bytes + free (default used): true: threshold is applied on free space and service become warning if free sapce is lower than threshold + false: threshold is applied on used space and service become warning if used space is higher than threshold + warning: warning threshold + critical: critical threshold + filters: + filter-storage-type: case insensitive regex to filter storage type it includes drive type (fixed, network...) and also fs type (fat32, ntfs..) + types recognized by agent: + hrunknown + hrstoragefixeddisk + hrstorageremovabledisk + hrstoragecompactdisc + hrstorageramdisk + hrstoragenetworkdisk + hrfsunknown + hrfsfat + hrfsntfs + hrfsfat32 + hrfsexfat + filter-fs: regex to filter filesystem + Example: [C-D]:\\.* + exclude-fs: regex to exclude filesystem + An example of configuration: + { + "check": "storage", + "args": { + "unit": "%", + "free": false, + "warning": 80, + "critical": 90, + "filter-storage-type": "hrstoragefixeddisk", + "filter-fs": "[C-D]:\\" + } + } + Examples of output: + WARNING: C:\ Total: 322G Used: 39.54% Free: 60.46% CRITICAL: D:\ Total: 5G Used: 50.60% Free: 49.40% + Metrics: + if free flag = true + free_C:\ + free_D:\ + if free flag = false + used_C:\ + used_D:\ +)"; +} diff --git a/agent/src/main.cc b/agent/src/main.cc index b6f9ea65a58..8617d55f9f6 100644 --- a/agent/src/main.cc +++ b/agent/src/main.cc @@ -20,6 +20,7 @@ #include #include +#include "check_cpu.hh" #include "config.hh" #include "drive_size.hh" #include "streaming_client.hh" @@ -104,6 +105,8 @@ int main(int argc, char* argv[]) { "Usage: {} \nSchema of the config " "file is:\n{}", argv[0], config::config_schema); + std::cout << std::endl << "Native checks options:" << std::endl; + check_cpu::help(std::cout); return 1; } diff --git a/agent/src/main_win.cc b/agent/src/main_win.cc index fa9b35eac24..39063c4d0a4 100644 --- a/agent/src/main_win.cc +++ b/agent/src/main_win.cc @@ -17,6 +17,11 @@ */ #include +#include "check_cpu.hh" +#include "check_memory.hh" +#include "check_uptime.hh" +#include "drive_size.hh" + #include #include #include @@ -106,6 +111,20 @@ static std::string read_file(const std::string& file_path) { return ""; } +void show_help() { + std::cout << "usage: centagent.exe [options]" << std::endl; + std::cout << "Options:" << std::endl; + std::cout << " --standalone: run the agent in standalone mode not from " + "service manager (mandatory for start it from command line)" + << std::endl; + std::cout << " --help: show this help" << std::endl; + std::cout << std::endl << "native checks options:" << std::endl; + check_cpu::help(std::cout); + check_memory::help(std::cout); + check_uptime::help(std::cout); + check_drive_size::help(std::cout); +} + /** * @brief this program can be started in two ways * from command line: main function @@ -242,6 +261,11 @@ int main(int argc, char* argv[]) { return _main(false); } + if (argc > 1 && !lstrcmpi(argv[1], "--help")) { + show_help(); + return 0; + } + SPDLOG_INFO( "centagent.exe will start in service mode, if you launch it from command " "line, use --standalone flag"); diff --git a/agent/src/native_check_memory_base.cc b/agent/src/native_check_base.cc similarity index 70% rename from agent/src/native_check_memory_base.cc rename to agent/src/native_check_base.cc index 3d8a8716b8d..6f005c02f57 100644 --- a/agent/src/native_check_memory_base.cc +++ b/agent/src/native_check_base.cc @@ -16,14 +16,14 @@ * For more information : contact@centreon.com */ -#include "native_check_memory_base.hh" +#include "native_check_base.hh" #include "com/centreon/common/rapidjson_helper.hh" using namespace com::centreon::agent; -using namespace com::centreon::agent::check_memory_detail; +using namespace com::centreon::agent::native_check_detail; /** - * @brief construct a memory_info to status converter + * @brief construct a snapshot to status converter * * @tparam nb_metric * @param status e_warning or e_critical @@ -34,12 +34,12 @@ using namespace com::centreon::agent::check_memory_detail; * @param free_threshold if true, status is set if value < threshold */ template -mem_to_status::mem_to_status(e_status status, - unsigned data_index, - double threshold, - unsigned total_data_index, - bool percent, - bool free_threshold) +measure_to_status::measure_to_status(e_status status, + unsigned data_index, + double threshold, + unsigned total_data_index, + bool percent, + bool free_threshold) : _status(status), _data_index(data_index), _threshold(threshold), @@ -48,8 +48,8 @@ mem_to_status::mem_to_status(e_status status, _free_threshold(free_threshold) {} template -void mem_to_status::compute_status( - const memory_info& to_test, +void measure_to_status::compute_status( + const snapshot& to_test, e_status* status) const { if (_status <= *status) { return; @@ -69,7 +69,7 @@ void mem_to_status::compute_status( } /** - * @brief Construct a new check check_memory_base + * @brief Construct a new check native_check_base * * @param io_context * @param logger @@ -84,7 +84,7 @@ void mem_to_status::compute_status( * @param handler called at measure completion */ template -check_memory_base::check_memory_base( +native_check_base::native_check_base( const std::shared_ptr& io_context, const std::shared_ptr& logger, time_point first_start_expected, @@ -111,13 +111,13 @@ check_memory_base::check_memory_base( * @param timeout */ template -void check_memory_base::start_check(const duration& timeout) { +void native_check_base::start_check(const duration& timeout) { if (!check::_start_check(timeout)) { return; } try { - std::shared_ptr> mem_metrics = + std::shared_ptr> mem_metrics = measure(); _io_context->post([me = shared_from_this(), @@ -150,18 +150,20 @@ void check_memory_base::start_check(const duration& timeout) { * @return e_status plugins status output */ template -e_status check_memory_base::compute( - const check_memory_detail::memory_info& data, +e_status native_check_base::compute( + const native_check_detail::snapshot& data, std::string* output, std::list* perfs) const { e_status status = e_status::ok; - for (const auto& mem_status : _mem_to_status) { - mem_status.second.compute_status(data, &status); + for (const auto& mem_status : _measure_to_status) { + mem_status.second->compute_status(data, &status); } *output = status_label[status]; - data.dump_to_output(output, _output_flags); + data.dump_to_output(output); + + const auto& metric_definitions = get_metric_definitions(); for (const auto& metric : metric_definitions) { common::perfdata& to_add = perfs->emplace_back(); @@ -174,29 +176,33 @@ e_status check_memory_base::compute( metric.total_data_index) * 100); } else { - to_add.unit("B"); - to_add.min(0); - to_add.max(data.get_metric(metric.total_data_index)); + if (_no_percent_unit) { + to_add.unit(_no_percent_unit); + } + if (metric.total_data_index != nb_metric) { + to_add.min(0); + to_add.max(data.get_metric(metric.total_data_index)); + } to_add.value(data.get_metric(metric.data_index)); } - // we search mem_to_status to get warning and critical thresholds + // we search measure_to_status to get warning and critical thresholds // warning - auto mem_to_status_search = _mem_to_status.find(std::make_tuple( + auto mem_to_status_search = _measure_to_status.find(std::make_tuple( metric.data_index, metric.total_data_index, e_status::warning)); - if (mem_to_status_search != _mem_to_status.end()) { + if (mem_to_status_search != _measure_to_status.end()) { to_add.warning_low(0); to_add.warning(metric.percent - ? 100 * mem_to_status_search->second.get_threshold() - : mem_to_status_search->second.get_threshold()); + ? 100 * mem_to_status_search->second->get_threshold() + : mem_to_status_search->second->get_threshold()); } // critical - mem_to_status_search = _mem_to_status.find(std::make_tuple( + mem_to_status_search = _measure_to_status.find(std::make_tuple( metric.data_index, metric.total_data_index, e_status::critical)); - if (mem_to_status_search != _mem_to_status.end()) { + if (mem_to_status_search != _measure_to_status.end()) { to_add.critical_low(0); to_add.critical(metric.percent - ? 100 * mem_to_status_search->second.get_threshold() - : mem_to_status_search->second.get_threshold()); + ? 100 * mem_to_status_search->second->get_threshold() + : mem_to_status_search->second->get_threshold()); } } return status; diff --git a/agent/src/scheduler.cc b/agent/src/scheduler.cc index d35d24ab70d..bf89d3d480d 100644 --- a/agent/src/scheduler.cc +++ b/agent/src/scheduler.cc @@ -18,7 +18,7 @@ #include "scheduler.hh" #include "check_cpu.hh" -#ifdef _WINDOWS +#ifdef _WIN32 #include "check_memory.hh" #include "check_uptime.hh" #endif @@ -562,7 +562,7 @@ std::shared_ptr scheduler::default_check_builder( return std::make_shared( io_context, logger, first_start_expected, check_interval, service, cmd_name, cmd_line, *args, conf, std::move(handler)); -#ifdef _WINDOWS +#ifdef _WIN32 } else if (check_type == "uptime"sv) { return std::make_shared( io_context, logger, first_start_expected, check_interval, service, diff --git a/agent/test/check_exec_test.cc b/agent/test/check_exec_test.cc index b115d4db9c1..60f49bc77eb 100644 --- a/agent/test/check_exec_test.cc +++ b/agent/test/check_exec_test.cc @@ -22,7 +22,7 @@ using namespace com::centreon::agent; -#ifdef _WINDOWS +#ifdef _WIN32 #define ECHO_PATH "tests\\echo.bat" #define SLEEP_PATH "tests\\sleep.bat" #define END_OF_LINE "\r\n" @@ -97,7 +97,7 @@ TEST(check_exec_test, timeout) { ASSERT_GT(pid, 0); std::this_thread::sleep_for(std::chrono::seconds(1)); -#ifdef _WINDOWS +#ifdef _WIN32 auto process_handle = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, pid); ASSERT_NE(process_handle, nullptr); @@ -138,7 +138,7 @@ TEST(check_exec_test, bad_command) { cond.wait(l); ASSERT_EQ(status, 3); ASSERT_EQ(outputs.size(), 1); -#ifdef _WINDOWS +#ifdef _WIN32 // message is language dependant ASSERT_GE(outputs.begin()->size(), 20); #else diff --git a/agent/test/check_windows_memory_test.cc b/agent/test/check_windows_memory_test.cc index 3403432d71c..cb8adaa1c8a 100644 --- a/agent/test/check_windows_memory_test.cc +++ b/agent/test/check_windows_memory_test.cc @@ -19,9 +19,7 @@ #include #include -#include -#include "check.hh" #include "com/centreon/common/perfdata.hh" #include "com/centreon/common/rapidjson_helper.hh" @@ -30,7 +28,7 @@ extern std::shared_ptr g_io_context; using namespace com::centreon::agent; -using namespace com::centreon::agent::check_memory_detail; +using namespace com::centreon::agent::native_check_detail; using namespace std::string_literals; @@ -55,11 +53,11 @@ class test_check : public check_memory { const std::list& perfdata, const std::list& outputs) {}) {} - std::shared_ptr> - measure() const override { - return std::make_shared(mock, - perf_mock); + std::shared_ptr> + measure() override { + return std::make_shared(mock, perf_mock, + _output_flags); } }; diff --git a/agent/test/test_main.cc b/agent/test/test_main.cc index 21d63bb5a22..eea1558b443 100644 --- a/agent/test/test_main.cc +++ b/agent/test/test_main.cc @@ -23,13 +23,12 @@ std::shared_ptr g_io_context( class CentreonEngineEnvironment : public testing::Environment { public: -#ifndef _WINDOWS +#ifndef _WIN32 void SetUp() override { setenv("TZ", ":Europe/Paris", 1); return; } #endif - }; /**