From bf190465bf61cfdb9f19afb1c420a884a2214ba5 Mon Sep 17 00:00:00 2001 From: Daniel Berteaud Date: Wed, 13 Jan 2021 17:48:01 +0100 Subject: [PATCH] Modernize lvm monitoring scripts support getting all the value at once in a JSON structure, support VG monitoring, and various other cleanups GLPI #47654 --- zabbix_conf/lvm.conf | 3 +- zabbix_scripts/check_lvm_sudo | 77 ++++++++++++++++++++++++++++--------------- 2 files changed, 52 insertions(+), 28 deletions(-) diff --git a/zabbix_conf/lvm.conf b/zabbix_conf/lvm.conf index 981bdff..e1342c3 100644 --- a/zabbix_conf/lvm.conf +++ b/zabbix_conf/lvm.conf @@ -5,4 +5,5 @@ UserParameter=vfs.lvm.discovery[*],/usr/bin/sudo /var/lib/zabbix/bin/disco_lvm_s # Type: Agent or Agent (active) # Key: vfs.lvm.lv[volume,key] where volume is the path to a logical volume and # key can be size, allocation (for snapshots only) or status -UserParameter=vfs.lvm.lv[*],/usr/bin/sudo /var/lib/zabbix/bin/check_lvm_sudo $1 $2 +UserParameter=vfs.lvm.lv[*],/usr/bin/sudo /var/lib/zabbix/bin/check_lvm_sudo --lv='$1' --what='$2' +UserParameter=vfs.lvm.vg[*],/usr/bin/sudo /var/lib/zabbix/bin/check_lvm_sudo --vg='$1' --what='$2' diff --git a/zabbix_scripts/check_lvm_sudo b/zabbix_scripts/check_lvm_sudo index 83af2f4..6f64f2e 100644 --- a/zabbix_scripts/check_lvm_sudo +++ b/zabbix_scripts/check_lvm_sudo @@ -1,49 +1,72 @@ #!/usr/bin/perl -w use Zabbix::Agent::Addons::LVM; +use Getopt::Long; +use JSON; Zabbix::Agent::Addons::LVM->units(B); -if (@ARGV < 2){ - usage(); - exit(1); +my $vg = undef; +my $lv = undef; +my $what = undef; +my $pretty = 0; + +GetOptions( + 'vg=s' => \$vg, + 'lv=s' => \$lv, + 'what:s' => \$what, + "pretty" => \$pretty +); + +if (not defined $lv and not defined $vg){ + $lv ||= $ARGV[0]; + $what ||= $ARGV[1]; } -my $vol = $ARGV[0]; -my $what = $ARGV[1]; +if (not defined $lv and not defined $vg){ + usage(); + exit 1; +} sub usage { print<<"EOF"; Usage: $0 [size|allocation|allocation_pool_data|allocation_metadata|status] + $0 --lv= + $0 --lv= --what= + $0 --vg= + $0 --vg= --what= EOF } -my %info = get_lv_info($vol); - -if ($what eq 'size'){ - print $info{size}; -} -elsif ($what eq 'allocation'){ - my $ret = (defined $info{allocated_to_snapshot}) ? $info{allocated_to_snapshot} : "ZBX_NOTSUPPORTED"; - $ret =~ s/,/\./; - print $ret; +my $json; +if (defined $vg){ + %{$json} = get_volume_group_information($vg); +} elsif (defined $lv) { + %{$json} = get_lv_info($lv); +} else{ + usage(); } -elsif ($what eq 'allocation_pool_data'){ - my $ret = (defined $info{allocated_pool_data}) ? $info{allocated_pool_data} : "ZBX_NOTSUPPORTED"; - $ret =~ s/,/\./; - print $ret; -} -elsif ($what eq 'allocation_metadata'){ - my $ret = (defined $info{allocated_meta_data}) ? $info{allocated_meta_data} : "ZBX_NOTSUPPORTED"; - $ret =~ s/,/\./; - print $ret; + +# Normalize float values +foreach (qw(allocated_to_snapshot allocated_pool_data allocated_meta_data)){ + $json->{$_} =~ s/,/./g if (defined $json->{$_}); } -elsif ($what eq 'status'){ - print $info{status}; +# Compat with older versions +my $old_keys = { + allocation => 'allocated_to_snapshot', + allocation_pool_data => 'allocated_pool_data', + allocation_metadata => 'allocated_meta_data' +}; +if (defined $what && defined $old_keys->{$what}){ + $what = $old_keys->{$what}; } -else{ - usage(); + +if (defined $what and $what ne ''){ + print ((defined $json->{$what}) ? $json->{$what} : 'ZBX_NOTSUPPOTED'); +} else { + print to_json($json, { pretty => $pretty }); } + exit(0);