#!/usr/bin/env perl
use JSON qw(decode_json);
use Sys::Hostname;
use Getopt::Long;
use strict;
use warnings;

my @services;
my $verbose;
my $num_pgs;

GetOptions (
 "service=s{1}"     	=> \@services,
 "verbose"          	=> \$verbose,
 "num_pgs=i"            => \$num_pgs,
);

sub help {
  print "Usage: $0 --service=\$SERVICE.\$ID --service=\$SERVICE.\$ID --num_pgs=XYZ --verbose\n";
  exit 1;
}

if ( (!@services) ) {
  help();
}

$num_pgs ||= 200;

my $service;
my @alarms;
my @checked;
foreach $service (@services) {
  if($verbose) { print "running loop service $service\n"; }

    push @checked, "$service";

    my $command = 'status';
    if ($service =~ m/^mon\./ ) {
      $command = 'mon_status';
    }

    if($verbose) { print "running command: /usr/bin/ceph daemon $service $command 2>&1\n"; }
    my $json = `/usr/bin/ceph daemon $service $command 2>&1`;
    my $json_out = eval { decode_json($json) };

    # If not valid JSON, then set alarm
    if ($@) {
      push @alarms, "NOT-RUNNING: $service";
      next;
    }

    if ( (defined $json_out->{num_pgs}) and ($json_out->{num_pgs} > $num_pgs)) {
      push @alarms, "PG-OVERBOOKED: $service";
    }

# Active only works on OSD, but MON, etc ...
#    if ( (defined $json_out->{state}) and ($json_out->{state} ne 'active') ) {
#      push @alarms, "INACTIVE: $service";
#    }

}

if (@alarms) {
  my $brokenServices = join(' ', @alarms);
  print "CRITICAL - $brokenServices\n";
  exit 2;
} 

my $checkedServices = join(' ', @checked);
print "OK - Checked: $checkedServices \n";
exit 0;
