Skip to content
Snippets Groups Projects
cephUsage.pl 6.04 KiB
#!/usr/bin/perl
################################################################################
# cephDiskUsage.pl - Return disk space occupied, global or by a pool (group)
################################################################################
#
# This script executes 'ceph df detail' and returns one in:
#    *) global/pool/pool_group number of objects
#    *) global/pool/pool_group raw disk size in TB
#    *) global/pool/pool_group fraction of available raw disk size
#
# For the pool/pool_group part, it is intended to be used with the Zabbix Agent
# to enable low-level discovery for Ceph pools in Zabbix.
#
################################################################################
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;