From f426903cc8ff9b7188dd108fe39f175697c4152e Mon Sep 17 00:00:00 2001 From: Daniel Berteaud Date: Mon, 20 Jul 2015 12:18:28 +0200 Subject: [PATCH] Move UPS detection in a module --- lib/Zabbix/Agent/Addons/UPS.pm | 18 ++++++++++++++++++ zabbix_scripts/disco_nut_ups | 8 +++----- 2 files changed, 21 insertions(+), 5 deletions(-) create mode 100644 lib/Zabbix/Agent/Addons/UPS.pm diff --git a/lib/Zabbix/Agent/Addons/UPS.pm b/lib/Zabbix/Agent/Addons/UPS.pm new file mode 100644 index 0000000..96b6632 --- /dev/null +++ b/lib/Zabbix/Agent/Addons/UPS.pm @@ -0,0 +1,18 @@ +package Zabbix::Agent::Addons::UPS; + +use warnings; +use strict; +use File::Which; + +# List configured UPS (only nut is supported) +sub list_ups { + my @ups = (); + my $upsc = which('upsc'); + if ($upsc && -x $upsc){ + my @out = qx($upsc -l); + if ($? == 0){ + @ups = @out; + } + } + return @ups; +}; diff --git a/zabbix_scripts/disco_nut_ups b/zabbix_scripts/disco_nut_ups index e5374ea..ad0ed51 100644 --- a/zabbix_scripts/disco_nut_ups +++ b/zabbix_scripts/disco_nut_ups @@ -1,15 +1,13 @@ #!/usr/bin/perl -w use JSON; +use Zabbix::Agent::Addons::UPS; my $json; @{$json->{data}} = (); -if (system("upsc -l >/dev/null 2>&1") == 0){ - foreach my $ups (`upsc -l`){ - chomp($ups); - push @{$json->{data}}, {"{#UPSNAME}" => $ups}; - } +foreach my $ups (Zabbix::Agent::Addons::UPS::list_ups()){ + push @{$json->{data}}, {"{#UPSNAME}" => $ups}; } print to_json($json); exit(0);