#!/bin/sh

: << =cut

=head1 NAME

ceph_capacity - Shows ceph total storage capacity, used raw space and used data space

=head1 CONFIGURATION

 [ceph_capacity]
 env.warning_level Used raw space percentage above warning alert should be issued
 env.critical_level Used raw space percentage above critical alert should be issued

=head1 AUTHOR

Mate Gabri <mate@gabri.hu>

=head1 LICENSE

GPLv2

=head1 MAGICK MARKERS

 #%# family=auto
 #%# capabilities=autoconf

=cut

if [ "$1" = "autoconf" ]; then
	echo yes
	exit 0
fi

WARNING_LEVEL=${warning_level:-"80"}
CRITICAL_LEVEL=${critical_level:-"90"}

CEPH_STATUS=$(ceph -s --format=json)

if [ "$1" = "config" ]; then

	echo 'graph_title CEPH capacity'
	echo 'graph_category fs'
	echo 'graph_vlabel GB'
	echo 'graph_info CEPH cluster capacity'
	echo 'graph_args --base 1000 -l 0'

	CAPACITY=$(echo "$CEPH_STATUS" | jq '.pgmap.bytes_total')
	WARNING=$(echo "scale=2;$CAPACITY * ($WARNING_LEVEL/100)" | bc -l | cut -d '.' -f 1)
	CRITICAL=$(echo "scale=2;$CAPACITY * ($CRITICAL_LEVEL/100)" | bc -l | cut -d '.' -f 1)
	echo "capacity.label Capacity"
	echo "used.label Raw used"
	echo "used.draw AREA"
	echo "used.warning $WARNING"
	echo "used.critical $CRITICAL"
	echo "data.label Data"
	echo "data.draw AREA"

	exit 0
fi

echo "capacity.value $(echo "$CEPH_STATUS" | jq '.pgmap.bytes_total')"
echo "used.value     $(echo "$CEPH_STATUS" | jq '.pgmap.bytes_used')"
echo "data.value     $(echo "$CEPH_STATUS" | jq '.pgmap.data_bytes')"  # no idea of the intention of this metric; could be bytes_avail
