From ec110483dd31cf545bdf2d642050ed15401cdffa Mon Sep 17 00:00:00 2001 From: Daniel Berteaud Date: Fri, 13 Sep 2019 15:06:35 +0200 Subject: [PATCH] Add simple Zabbix service status scripts --- zabbix_conf/zimbra.conf | 11 ++++++++ zabbix_scripts/check_zimbra_sudo | 59 +++++++++++++++++++++++++++++++++++++++ zabbix_scripts/disco_zimbra_sudo | 60 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 130 insertions(+) create mode 100644 zabbix_conf/zimbra.conf create mode 100644 zabbix_scripts/check_zimbra_sudo create mode 100644 zabbix_scripts/disco_zimbra_sudo diff --git a/zabbix_conf/zimbra.conf b/zabbix_conf/zimbra.conf new file mode 100644 index 0000000..a58300b --- /dev/null +++ b/zabbix_conf/zimbra.conf @@ -0,0 +1,11 @@ +# Discovery of configured host +# Key: zimbra.discovery[services] or zimbra.discovery[servers] +# Macro: {#ZM_SERVICE} +# Other available macros: +UserParameter=zimbra.discovery[*],/usr/bin/sudo /var/lib/zabbix/bin/disco_zimbra_sudo --$1 + +# Item prototypes +# key: zimbra.check[*] +# or +# Returns a JSON object, use dependent item to split it +UserParameter=zimbra.status[*],/usr/bin/sudo /var/lib/zabbix/bin/check_zimbra_sudo --status=$2 diff --git a/zabbix_scripts/check_zimbra_sudo b/zabbix_scripts/check_zimbra_sudo new file mode 100644 index 0000000..97355f5 --- /dev/null +++ b/zabbix_scripts/check_zimbra_sudo @@ -0,0 +1,59 @@ +#!/usr/bin/perl -w + +use JSON; +use POSIX; +use Getopt::Long; +use Net::Domain qw(hostfqdn); +use Data::Dumper; + +my $pretty = 0; +my $status = 'all'; + +GetOptions( + "pretty" => \$pretty, + "status=s" => \$status +); + +if (defined $service and $service !~ m/^\w+$/){ + die "Invelid service name\n"; +} + +my $zmprov = '/opt/zimbra/bin/zmprov'; +my $zmcontrol = '/opt/zimbra/bin/zmcontrol'; +my $hostname = hostfqdn(); + +# We need to switch to zimbra +my $uid = getuid(); +my $gid = getgid(); +my (undef,undef,$zimuid,$zimgid) = getpwnam('zimbra'); + +if (not defined $zimuid or not defined $zimgid or not -e $zmprov){ + print 'ZBX_NOTSUPPOTED'; + exit; +} + +setuid($zimuid) if ($uid ne $zimuid); +setgid($zimgid) if ($gid ne $zimgid); + +# If there's no zimbra user or no zmcontrol, return unsupported item +if (not defined $zimuid or not defined $zimgid or not -e $zmprov){ + print 'ZBX_NOTSUPPOTED'; + exit 0; +} + +my $output = {}; + +if (defined $status){ + foreach my $line (qx($zmcontrol status)){ + if ($line =~ m/^\s+(\w+)\s+(Running|Stopped)/){ + $output->{$1} = ($2 eq 'Running') ? 1 : 0; + } + } + if ($service eq 'all'){ + print to_json($output, { pretty => $pretty }); + } elsif (defined $output->{$status}){ + print $output->{$status} + } else { + print 'ZBX_NOTSUPPORTED'; + } +} diff --git a/zabbix_scripts/disco_zimbra_sudo b/zabbix_scripts/disco_zimbra_sudo new file mode 100644 index 0000000..41540c7 --- /dev/null +++ b/zabbix_scripts/disco_zimbra_sudo @@ -0,0 +1,60 @@ +#!/usr/bin/perl -w + +use JSON; +use POSIX; +use Getopt::Long; +use Net::Domain qw(hostfqdn); +use Data::Dumper; + +my $json; +@{$json} = (); + +my $pretty = 0; +my $services = 1; +my $servers = 0; + +GetOptions( + "pretty" => \$pretty, + "services" => \$services, + "servers" => \$servers +); + +if ($servers) { + $services = 0; +} + +my $uid = getuid(); +my $gid = getgid(); +my (undef,undef,$zimuid,$zimgid) = getpwnam('zimbra'); + +my $zmprov = '/opt/zimbra/bin/zmprov'; +my $hostname = hostfqdn(); + +# If there's no zimbra user or no zmcontrol, just return an empty list +if (not defined $zimuid or not defined $zimgid or not -e $zmprov){ + print to_json({}); + exit; +} + +# Switch to Zimbra user +setuid($zimuid) if ($uid ne $zimuid); +setgid($zimgid) if ($gid ne $zimgid); + +if ($services){ + foreach my $service (qx($zmprov getServer $hostname zimbraServiceEnabled)){ + if ($service =~ m/^zimbraServiceEnabled:\s+(\w+)/){ + push @{$json}, { + '{#ZM_SERVICE}' => $1 + }; + } + } +} elsif ($servers){ + foreach my $server (qx($zmprov getAllServers)){ + chomp $server; + push @{$json}, { + '{#ZM_SERVER}' => $server + }; + } +} + +print to_json($json, { pretty => $pretty });