From 596f7d98df722e885d0c89b73868a912650f0461 Mon Sep 17 00:00:00 2001
From: Fulvio Galeazzi <fulvio.galeazzi@garr.it>
Date: Fri, 3 Nov 2017 12:33:25 +0100
Subject: [PATCH] 2017-11-03:  FG;  Added degraded and misplaced objects.

---
 Ceph/Config/userparameter_ceph.conf |   4 +
 Ceph/Script/cephHealth.pl           | 203 ++++++++++++++++++++++++++++
 2 files changed, 207 insertions(+)
 create mode 100755 Ceph/Script/cephHealth.pl

diff --git a/Ceph/Config/userparameter_ceph.conf b/Ceph/Config/userparameter_ceph.conf
index 0bab014..655378e 100644
--- a/Ceph/Config/userparameter_ceph.conf
+++ b/Ceph/Config/userparameter_ceph.conf
@@ -1,6 +1,10 @@
 UserParameter=ceph.health,/etc/zabbix/scripts/ceph_health.sh
 # Global
 UserParameter=ceph.global.objs[*],/etc/zabbix/scripts/cephUsage.pl $1 -o
+UserParameter=ceph.global.objsdeg[*],/etc/zabbix/scripts/cephHealth.pl $1 --od
+UserParameter=ceph.global.objsdegfrac[*],/etc/zabbix/scripts/cephHealth.pl $1 --od -f
+UserParameter=ceph.global.objsmis[*],/etc/zabbix/scripts/cephHealth.pl $1 --om
+UserParameter=ceph.global.objsmisfrac[*],/etc/zabbix/scripts/cephHealth.pl $1 --om -f
 UserParameter=ceph.global.rawsize[*],/etc/zabbix/scripts/cephUsage.pl $1
 UserParameter=ceph.global.rawfrac[*],/etc/zabbix/scripts/cephUsage.pl $1 -f
 # Pools
diff --git a/Ceph/Script/cephHealth.pl b/Ceph/Script/cephHealth.pl
new file mode 100755
index 0000000..01d581d
--- /dev/null
+++ b/Ceph/Script/cephHealth.pl
@@ -0,0 +1,203 @@
+#!/usr/bin/perl
+################################################################################
+# cephHealth.pl - Return health status, degraded and recovering objects
+################################################################################
+#
+# This script executes 'ceph health detail' and returns one in:
+#    *) <status_flag>, 0=HEALTH_OK 1=HEALTH_WARN 2=HEALTH_ERR
+#    *) number of objects degraded
+#    *) number of objects degraded, fraction of total
+#    *) number of objects misplaced
+#    *) number of objects misplaced, fraction of total
+#
+################################################################################
+use strict;
+use warnings;
+use Getopt::Long;
+
+sub fail_usage
+{
+    my ($msg)=@_;
+    print STDERR $msg."\n" if $msg;
+    print STDERR "Please use '-h' for usage.\n";
+    exit 1;
+}
+
+sub convertNumber
+{
+    my ($string,$toWhat) = @_;
+    my $value = 0.;
+    if ($toWhat eq 'size') {
+	# return unit: T
+	$value = $string;
+	my $lastChar = lc(chop($string));
+	if ($lastChar eq "p") {
+	    $value = $string * 1000.;
+	} elsif ($lastChar eq "t") {
+	    $value = $string + 0.;
+	} elsif ($lastChar eq "g") {
+	    $value = $string * 0.001;
+	} elsif ($lastChar eq "m") {
+	    $value = $string * 0.001 * 0.001;
+	} elsif ($lastChar eq "k") {
+	    $value = $string * 0.001 * 0.001;
+	} else {
+	    $value = $value * 0.001 * 0.001 * 0.001 * 0.001;
+	}
+    } elsif ($toWhat eq 'count') {
+	# return unit: M
+	$value = $string;
+	my $lastChar = lc(chop($string));
+	if ($lastChar eq "p") {
+	    $value = $string * 1000. * 1000. * 1000.;
+	} elsif ($lastChar eq "t") {
+	    $value = $string * 1000. * 1000.;
+	} elsif ($lastChar eq "g") {
+	    $value = $string * 1000.;
+	} elsif ($lastChar eq "m") {
+	    $value = $string + 0.;
+	} elsif ($lastChar eq "k") {
+	    $value = $string * 0.001;
+	} else {
+	    $value = $value * 0.001 * 0.001;
+	}
+    }
+    return $value;
+}
+
+my $_sudo = `which sudo`;
+chomp $_sudo;
+
+### Options
+our($opt_exec, $opt_become, $opt_cluster, $opt_user, $opt_keyring, $opt_monhost,
+    $opt_status, $opt_object_degraded, $opt_object_misplaced, $opt_fraction, $opt_filter, $opt_debug, $opt_h);
+if (@ARGV > 0) {
+    GetOptions("e=s"=>\$opt_exec,
+               "b"  =>\$opt_become,
+               "c=s"=>\$opt_cluster,
+               "u=s"=>\$opt_user,
+               "k=s"=>\$opt_keyring,
+               "m=s"=>\$opt_monhost,
+               "status" =>\$opt_status,
+               "od" =>\$opt_object_degraded,
+               "om" =>\$opt_object_misplaced,
+               "f"  =>\$opt_fraction,
+               "s=s"=>\$opt_filter,
+               "d"  =>\$opt_debug,
+               "h"  =>\$opt_h) || fail_usage;
+} fail_usage "Unknown parameter." if (@ARGV > 0);
+
+my $cephCmd = "";
+if (defined $opt_exec) {
+    $cephCmd = "$opt_exec";
+} else {
+    $cephCmd = "/usr/bin/ceph";
+}
+if ( ! -e $cephCmd) {
+    die "Executable $cephCmd not found!";
+}
+if (defined $opt_become) {
+    $cephCmd = "$_sudo $cephCmd";
+}
+#
+if (defined $opt_cluster) {
+    $cephCmd .= " --cluster $opt_cluster";
+}
+if (defined $opt_user) {
+    $cephCmd .= " --user $opt_user";
+}
+if (defined $opt_keyring) {
+    $cephCmd .= " --keyring $opt_keyring";
+}
+if (defined $opt_monhost) {
+    $cephCmd .= " -m $opt_monhost";
+}
+#
+if (defined $opt_status && (defined $opt_object_degraded || defined $opt_object_misplaced)) {
+    fail_usage "Either status or objects, not both.";
+}
+if (defined $opt_object_degraded && defined $opt_object_misplaced) {
+    fail_usage "Either degraded objects or misplaced, not both.";
+}
+#
+my $doStatus = 0;
+if (defined $opt_status) {
+    $doStatus = 1;
+}
+my $doObjectDeg = 0;
+if (defined $opt_object_degraded) {
+    $doObjectDeg = 1;
+}
+my $doObjectMis = 0;
+if (defined $opt_object_misplaced) {
+    $doObjectMis = 1;
+}
+my $doFraction = 0;
+if (defined $opt_fraction) {
+    $doFraction = 1;
+}
+my $matchStr = "";
+if (defined $opt_filter) {
+    $matchStr = $opt_filter;
+}
+
+# Fetch the data and put it in an array
+my @_data = `$cephCmd health detail`;
+chomp @_data;
+
+my %poolHash;
+my $firstLine = 1;
+my $skipNext = 0;
+my %statusHash = (
+    'HEALTH_OK' => 0,
+    'HEALTH_WARN' => 1,
+    'HEALTH_ERR' => 2,
+    );
+my $status = 0;
+my $objDegraded = 0;
+my $objDegradedFrac = 0;
+my $objMisplaced = 0;
+my $objMisplacedFrac = 0;
+# Read the array and print the wanted data
+foreach my $_line (@_data)
+{
+    if ($firstLine) {
+	my @fields = split(/\s+/, $_line);
+	if (exists $statusHash{$fields[0]}) {
+	    $status = $statusHash{$fields[0]};
+	} else {
+	    $status = 2;
+	}
+	if ($_line =~ m/ (\d+)\/(\d+) objects misplaced \(([-+]?[0-9]*\.?[0-9]+)\%\)/) {
+	    $objMisplaced = $1;
+	    $objMisplacedFrac = $3;
+	}
+	if ($_line =~ m/ (\d+)\/(\d+) objects degraded \(([-+]?[0-9]*\.?[0-9]+)\%\)/) {
+	    $objDegraded = $1;
+	    $objDegradedFrac = $3;
+	}
+	$firstLine = 0;
+	next;
+    }
+}
+
+if (defined $opt_status) {
+    print $status."\n";
+} elsif (defined $opt_object_degraded) {
+    if (defined $opt_fraction) {
+	print $objDegradedFrac."\n";
+    } else {
+	print $objDegraded."\n";
+    }
+} elsif (defined $opt_object_misplaced) {
+    if (defined $opt_fraction) {
+	print $objMisplacedFrac."\n";
+    } else {
+	print $objMisplaced."\n";
+    }
+} else {
+    print "-1\n";
+}
+exit 0;
+
+
-- 
GitLab