add more config debug modes. remove duplicate code. refactor of start collector

This commit is contained in:
Meliox 2026-06-07 02:25:18 +02:00
parent d45407fc7e
commit 16fa01f9d4
3 changed files with 45 additions and 51 deletions

View File

@ -21,16 +21,6 @@ sub collector_for_temperature_sensors {
my ($device) = @_; my ($device) = @_;
$process_type = 'collector'; $process_type = 'collector';
$0 = "collector-temperature-sensors"; $0 = "collector-temperature-sensors";
debug(__LINE__, "Temperature sensor collector started");
unless (check_executable('/usr/bin/sensors', 'lm-sensors',
$config{debug}{sensors_mode},
$config{debug}{sensors_output_file})) {
debug(__LINE__, "sensors not available and not in debug mode, exiting");
exit(1);
}
my %cache; my %cache;
my $shutdown = 0; my $shutdown = 0;
setup_collector_signals('temperature-sensors', \$shutdown); setup_collector_signals('temperature-sensors', \$shutdown);
@ -65,39 +55,39 @@ sub _get_temperature_sensors {
my $sensors_output; my $sensors_output;
if ($config{debug}{sensors_mode} && -f $config{debug}{sensors_output_file}) { if ($config{debug}{lm_sensors_mode} && -f $config{debug}{lm_sensors_output_file}) {
debug(__LINE__, "Debug mode: reading sensors data from $config{debug}{sensors_output_file}"); debug(__LINE__, "Debug mode: reading lm-sensors data from $config{debug}{lm_sensors_output_file}");
if (open my $fh, '<', $config{debug}{sensors_output_file}) { if (open my $fh, '<', $config{debug}{lm_sensors_output_file}) {
local $/; local $/;
$sensors_output = <$fh>; $sensors_output = <$fh>;
close $fh; close $fh;
debug(__LINE__, "Read sensors data from debug file, length: " debug(__LINE__, "Read lm-sensors data from debug file, length: "
. length($sensors_output) . " bytes"); . length($sensors_output) . " bytes");
} else { } else {
debug(__LINE__, "Failed to open debug file $config{debug}{sensors_output_file}: $!"); debug(__LINE__, "Failed to open debug file $config{debug}{lm_sensors_output_file}: $!");
$sensors_output = '{}'; $sensors_output = '{}';
} }
} else { } else {
$sensors_output = `sensors -j 2>/dev/null | python3 -m json.tool`; $sensors_output = `sensors -j 2>/dev/null | python3 -m json.tool`;
debug(__LINE__, "Raw sensors output collected from command"); debug(__LINE__, "Raw lm-sensors output collected from command");
} }
debug(__LINE__, "Raw sensors output collected"); debug(__LINE__, "Raw lm-sensors output collected");
my $data = _sanitize_sensors($sensors_output); my $data = _sanitize_sensors($sensors_output);
debug(__LINE__, "Sanitized sensors output"); debug(__LINE__, "Sanitized lm-sensors output");
$data = _get_drive_names($data, $cache_ref); $data = _get_drive_names($data, $cache_ref);
debug(__LINE__, "Translated drive names in sensors output"); debug(__LINE__, "Translated drive names in lm-sensors output");
$data = _get_cpu_name($data, $cache_ref); $data = _get_cpu_name($data, $cache_ref);
debug(__LINE__, "Translated CPU names in sensors output"); debug(__LINE__, "Translated CPU names in lm-sensors output");
# Wrap in top-level key # Wrap in top-level key
my $sensors_json; my $sensors_json;
eval { $sensors_json = decode_json($data); }; eval { $sensors_json = decode_json($data); };
if ($@) { if ($@) {
debug(__LINE__, "Failed to parse final sensors JSON: $@"); debug(__LINE__, "Failed to parse final lm-sensors JSON: $@");
return $data; return $data;
} }
@ -126,7 +116,7 @@ sub _sanitize_sensors {
} }
# ============================================================================ # ============================================================================
# Enrich sensors data with drive device info # Enrich lm-sensors data with drive device info
# ============================================================================ # ============================================================================
sub _get_drive_names { sub _get_drive_names {
@ -144,7 +134,7 @@ sub _get_drive_names {
/^drivetemp-scsi-/ || /^drivetemp-nvme-/ || /^nvme-pci-/ /^drivetemp-scsi-/ || /^drivetemp-nvme-/ || /^nvme-pci-/
} keys %{$sensors_data}; } keys %{$sensors_data};
debug(__LINE__, "Found " . scalar(@entries) . " drive entries in sensors output"); debug(__LINE__, "Found " . scalar(@entries) . " drive entries in lm-sensors output");
my @drive_names; my @drive_names;
@ -282,7 +272,7 @@ sub _get_drive_names {
} }
# ============================================================================ # ============================================================================
# Enrich sensors data with CPU model info # Enrich lm-sensors data with CPU model info
# ============================================================================ # ============================================================================
sub _get_cpu_name { sub _get_cpu_name {

View File

@ -38,13 +38,22 @@ our %config = (
nvidia_mode => 1, nvidia_mode => 1,
nvidia_devices_file => '/tmp/nvidia-smi-devices.csv', nvidia_devices_file => '/tmp/nvidia-smi-devices.csv',
nvidia_output_file => '/tmp/nvidia-smi-output.csv', nvidia_output_file => '/tmp/nvidia-smi-output.csv',
sensors_mode => 0, intel_mode => 0,
sensors_output_file => '/tmp/sensors-output.json', intel_devices_file => '/tmp/intel-gpu-devices.json',
amd_mode => 0,
amd_devices_file => '/tmp/amd-gpu-devices.json',
ups_mode => 0,
ups_output_file => '/tmp/ups-output.json',
lm_sensors_mode => 0,
lm_sensors_output_file => '/tmp/sensors-output.json',
}, },
intervals => { intervals => {
data_pull => 1, # seconds between data pulls data_pull => 1, # seconds between data pulls
collector_timeout => 10, # stop collectors after N seconds of inactivity collector_timeout => 10, # stop collectors after N seconds of inactivity
}, },
lm_sensors => {
enabled => 1,
},
ups => { ups => {
enabled => 1, enabled => 1,
device_name => 'ups@192.168.3.2', device_name => 'ups@192.168.3.2',

View File

@ -234,38 +234,27 @@ sub _pve_mod_keep_alive {
# ============================================================================ # ============================================================================
sub _start_sensors_collector { sub _start_sensors_collector {
debug(__LINE__, "Starting temperature sensor collector"); return unless $config{lm_sensors}{enabled};
return unless check_executable('/usr/bin/sensors', 'lm-sensors',
unless (check_executable('/usr/bin/sensors', 'lm-sensors', $config{debug}{lm_sensors_mode},
$config{debug}{sensors_mode}, $config{debug}{lm_sensors_output_file});
$config{debug}{sensors_output_file})) {
debug(__LINE__, "sensors not available and not in debug mode, skipping");
return;
}
debug(__LINE__, "Starting lm-sensors collector");
_start_collector('sensors', 'sensors', _start_collector('sensors', 'sensors',
\&collector_for_temperature_sensors, \&collector_for_temperature_sensors,
{ name => 'sensors' }); { name => 'sensors' });
} }
sub _start_ups_collector { sub _start_ups_collector {
unless ($config{ups}{enabled}) { unless ($config{ups}{enabled} && $config{ups}{device_name}) {
debug(__LINE__, "UPS support not enabled, skipping collector startup"); debug(__LINE__, "UPS collection disabled/invalid in config, skipping");
return;
}
debug(__LINE__, "Starting UPS collector");
unless (check_executable('/usr/bin/upsc', 'UPS')) {
debug(__LINE__, "upsc not available, skipping UPS collector startup");
return;
}
unless ($config{ups}{device_name}) {
debug(__LINE__, "No UPS configured, skipping collector startup");
return; return;
} }
return unless check_executable('/usr/bin/upsc', 'UPS',
$config{debug}{ups_mode},
$config{debug}{ups_output_file});
debug(__LINE__, "Starting UPS collector: $config{ups}{device_name}");
_start_collector('ups', 'ups', \&collector_for_ups, _start_collector('ups', 'ups', \&collector_for_ups,
{ ups_name => $config{ups}{device_name} }); { ups_name => $config{ups}{device_name} });
} }
@ -284,7 +273,9 @@ sub _start_graphics_collectors {
my @nvidia_devices; my @nvidia_devices;
# Intel (each GPU has its own collector) # Intel (each GPU has its own collector)
if ($config{gpu}{intel_enabled} && check_executable('/usr/bin/intel_gpu_top', 'Intel')) { if ($config{gpu}{intel_enabled} && check_executable('/usr/bin/intel_gpu_top', 'Intel',
$config{debug}{intel_mode},
$config{debug}{intel_devices_file})) {
my @intel_devices = get_intel_gpu_devices(); my @intel_devices = get_intel_gpu_devices();
for my $device (@intel_devices) { for my $device (@intel_devices) {
push @all_devices, $device; push @all_devices, $device;
@ -294,7 +285,9 @@ sub _start_graphics_collectors {
} }
# AMD (each GPU has its own collector) # AMD (each GPU has its own collector)
if ($config{gpu}{amd_enabled} && check_executable('/usr/bin/rocm-smi', 'AMD')) { if ($config{gpu}{amd_enabled} && check_executable('/usr/bin/rocm-smi', 'AMD',
$config{debug}{amd_mode},
$config{debug}{amd_devices_file})) {
my @amd_devices = get_amd_gpu_devices(); my @amd_devices = get_amd_gpu_devices();
for my $device (@amd_devices) { for my $device (@amd_devices) {
push @all_devices, $device; push @all_devices, $device;
@ -304,7 +297,9 @@ sub _start_graphics_collectors {
} }
# NVIDIA (all GPUs collected together in one collector due to nvidia-smi design) # NVIDIA (all GPUs collected together in one collector due to nvidia-smi design)
if ($config{gpu}{nvidia_enabled} && check_executable('/usr/bin/nvidia-smi', 'NVIDIA')) { if ($config{gpu}{nvidia_enabled} && check_executable('/usr/bin/nvidia-smi', 'NVIDIA',
$config{debug}{nvidia_mode},
$config{debug}{nvidia_devices_file})) {
@nvidia_devices = get_nvidia_gpu_devices(); @nvidia_devices = get_nvidia_gpu_devices();
} }