Python 3 compatibility and code cleaning

This commit is contained in:
Benjamin Renard 2021-05-21 13:01:34 +02:00
parent fadfa27591
commit 4a5fdff76a
3 changed files with 190 additions and 171 deletions

47
README
View file

@ -1,47 +0,0 @@
Nagios plugin to check Ceph cluster usage
=========================================
Usage
-----
Usage: check_ceph_usage [options]
Options:
-h, --help show this help message and exit
-d, --debug
-b BIN, --bin=BIN Ceph binary (default : /usr/bin/ceph)
--conf=CONF Ceph configuration file
-m MON, --mon=MON Ceph monitor address[:port]
-i ID, --id=ID Ceph client id
-k KEYRING, --keyring=KEYRING
Ceph client keyring file
-w WARNDATA, --warning-data=WARNDATA
Warning data threshold (default : 70%)
-c CRITDATA, --critical-data=CRITDATA
Critical data threshold (default : 85%)
-W WARNALLOC, --warning-allocated=WARNALLOC
Warning allocated threshold (default : 80%)
-C CRITALLOC, --critical-allocated=CRITALLOC
Critical allocated threshold (default : 90%)
Copyright
---------
Copyright (c) 2013 Benjamin Renard <brenard@zionetrix.net>
License
-------
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License version 2
as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

47
README.md Normal file
View file

@ -0,0 +1,47 @@
# Nagios plugin to check Ceph cluster usage
## Usage
```
usage: check_ceph_usage [-h] [-d] [-b BIN] [--conf CONF] [-m MON] [-i ID]
[-k KEYRING] [-w WARNDATA] [-c CRITDATA]
[-W WARNALLOC] [-C CRITALLOC]
optional arguments:
-h, --help show this help message and exit
-d, --debug
-b BIN, --bin BIN Ceph binary (default: /usr/bin/ceph)
--conf CONF Ceph configuration file
-m MON, --mon MON Ceph monitor address[:port]
-i ID, --id ID Ceph client id
-k KEYRING, --keyring KEYRING
Ceph client keyring file
-w WARNDATA, --warning-data WARNDATA
Warning data threshold (default: 70%)
-c CRITDATA, --critical-data CRITDATA
Critical data threshold (default: 85%)
-W WARNALLOC, --warning-allocated WARNALLOC
Warning allocated threshold (default: 80%)
-C CRITALLOC, --critical-allocated CRITALLOC
Critical allocated threshold (default: 90%)
```
## Copyright
Copyright (c) 2013-2021 Benjamin Renard <brenard@zionetrix.net>
## License
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License version 3
as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

View file

@ -1,4 +1,4 @@
#!/usr/bin/python #!/usr/bin/env python
# #
# Nagios plugin to check Ceph cluster usage # Nagios plugin to check Ceph cluster usage
# #
@ -7,20 +7,20 @@
# Options: # Options:
# -h, --help show this help message and exit # -h, --help show this help message and exit
# -d, --debug # -d, --debug
# -b BIN, --bin=BIN Ceph binary (default : /usr/bin/ceph) # -b BIN, --bin=BIN Ceph binary (default: /usr/bin/ceph)
# --conf=CONF Ceph configuration file # --conf=CONF Ceph configuration file
# -m MON, --mon=MON Ceph monitor address[:port] # -m MON, --mon=MON Ceph monitor address[:port]
# -i ID, --id=ID Ceph client id # -i ID, --id=ID Ceph client id
# -k KEYRING, --keyring=KEYRING # -k KEYRING, --keyring=KEYRING
# Ceph client keyring file # Ceph client keyring file
# -w WARNDATA, --warning-data=WARNDATA # -w WARNDATA, --warning-data=WARNDATA
# Warning data threshold (default : 70%) # Warning data threshold (default: 70%)
# -c CRITDATA, --critical-data=CRITDATA # -c CRITDATA, --critical-data=CRITDATA
# Critical data threshold (default : 85%) # Critical data threshold (default: 85%)
# -W WARNALLOC, --warning-allocated=WARNALLOC # -W WARNALLOC, --warning-allocated=WARNALLOC
# Warning allocated threshold (default : 80%) # Warning allocated threshold (default: 80%)
# -C CRITALLOC, --critical-allocated=CRITALLOC # -C CRITALLOC, --critical-allocated=CRITALLOC
# Critical allocated threshold (default : 90%) # Critical allocated threshold (default: 90%)
# #
# Copyright (c) 2013 Benjamin Renard <brenard@zionetrix.net> # Copyright (c) 2013 Benjamin Renard <brenard@zionetrix.net>
# #
@ -38,8 +38,11 @@
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# #
import sys,os,json,subprocess import sys
from optparse import OptionParser import os
import json
import subprocess
import argparse
# default ceph values # default ceph values
CEPH_COMMAND = '/usr/bin/ceph' CEPH_COMMAND = '/usr/bin/ceph'
@ -49,103 +52,116 @@ WARN_ALLOC = 80
CRIT_ALLOC = 90 CRIT_ALLOC = 90
# nagios exit code # nagios exit code
STATUS_OK = 0 STATUS = {
STATUS_WARNING = 1 'OK': 0,
STATUS_ERROR = 2 'WARNING': 1,
STATUS_UNKNOWN = 3 'CRITICAL': 2,
'UNKNOWN': 3
}
parser = OptionParser() parser = argparse.ArgumentParser()
parser.add_option('-d', parser.add_argument(
'--debug', '-d', '--debug',
action="store_true", action="store_true",
dest="debug", dest="debug",
default=False) default=False
)
parser.add_option('-b', parser.add_argument(
'--bin', '-b', '--bin',
action="store", action="store",
dest="bin", dest="bin",
help="Ceph binary (default : %s)" % CEPH_COMMAND, help="Ceph binary (default: %s)" % CEPH_COMMAND,
type='string', type=str,
default=CEPH_COMMAND) default=CEPH_COMMAND
)
parser.add_option('--conf', parser.add_argument(
action="store", '--conf',
dest="conf", action="store",
help="Ceph configuration file", dest="conf",
type='string', help="Ceph configuration file",
default=None) type=str,
default=None
)
parser.add_option('-m', parser.add_argument(
'--mon', '-m', '--mon',
action="store", action="store",
dest="mon", dest="mon",
help="Ceph monitor address[:port]", help="Ceph monitor address[:port]",
type='string', type=str,
default=None) default=None
)
parser.add_option('-i', parser.add_argument(
'--id', '-i', '--id',
action="store", action="store",
dest="id", dest="id",
help="Ceph client id", help="Ceph client id",
type='string', type=str,
default=None) default=None
)
parser.add_option('-k', parser.add_argument(
'--keyring', '-k', '--keyring',
action="store", action="store",
dest="keyring", dest="keyring",
help="Ceph client keyring file", help="Ceph client keyring file",
type='string', type=str,
default=None) default=None
)
parser.add_option('-w', parser.add_argument(
'--warning-data', '-w', '--warning-data',
action="store", action="store",
dest="warndata", dest="warndata",
help="Warning data threshold (default : %s%%)" % WARN_DATA, help="Warning data threshold (default: %s%%%%)" % WARN_DATA,
type='int', type=int,
default=WARN_DATA) default=WARN_DATA
)
parser.add_option('-c', parser.add_argument(
'--critical-data', '-c', '--critical-data',
action="store", action="store",
dest="critdata", dest="critdata",
help="Critical data threshold (default : %s%%)" % CRIT_DATA, help="Critical data threshold (default: %s%%%%)" % CRIT_DATA,
type='int', type=int,
default=CRIT_DATA) default=CRIT_DATA
)
parser.add_option('-W', parser.add_argument(
'--warning-allocated', '-W', '--warning-allocated',
action="store", action="store",
dest="warnalloc", dest="warnalloc",
help="Warning allocated threshold (default : %s%%)" % WARN_ALLOC, help="Warning allocated threshold (default: %s%%%%)" % WARN_ALLOC,
type='int', type=int,
default=WARN_ALLOC) default=WARN_ALLOC
)
parser.add_option('-C', parser.add_argument(
'--critical-allocated', '-C', '--critical-allocated',
action="store", action="store",
dest="critalloc", dest="critalloc",
help="Critical allocated threshold (default : %s%%)" % CRIT_ALLOC, help="Critical allocated threshold (default: %s%%%%)" % CRIT_ALLOC,
type='int', type=int,
default=CRIT_ALLOC) default=CRIT_ALLOC
)
(options, args) = parser.parse_args() options = parser.parse_args()
# validate args # validate args
if not os.path.exists(options.bin): if not os.path.exists(options.bin):
print "ERROR: ceph executable '%s' doesn't exist" % options.bin print("ERROR: ceph executable '%s' doesn't exist" % options.bin)
sys.exit(STATUS_UNKNOWN) sys.exit(STATUS['UNKNOWN'])
if options.conf and not os.path.exists(options.conf): if options.conf and not os.path.exists(options.conf):
print "ERROR: ceph conf file '%s' doesn't exist" % options.conf print("ERROR: ceph conf file '%s' doesn't exist" % options.conf)
sys.exit(STATUS_UNKNOWN) sys.exit(STATUS['UNKNOWN'])
if options.keyring and not os.path.exists(options.keyring): if options.keyring and not os.path.exists(options.keyring):
print "ERROR: keyring file '%s' doesn't exist" % options.keyring print("ERROR: keyring file '%s' doesn't exist" % options.keyring)
sys.exit(STATUS_UNKNOWN) sys.exit(STATUS['UNKNOWN'])
# build command # build command
ceph_cmd = [options.bin] ceph_cmd = [options.bin]
@ -165,43 +181,46 @@ ceph_cmd.append('status')
ceph_cmd.append('--format=json') ceph_cmd.append('--format=json')
# exec command # exec command
p = subprocess.Popen(ceph_cmd,stdout=subprocess.PIPE,stderr=subprocess.PIPE) p = subprocess.Popen(ceph_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, err = p.communicate() output, err = p.communicate()
if output: if not output:
data=json.loads(output) print("UNKNOWN: fail to execute ceph status command")
if 'pgmap' not in data: sys.exit(STATUS['UNKNOWN'])
print "UNKNOWN : fail to read pgmap status"
sys.exit(STATUS_UNKNOWN)
if options.debug: data = json.loads(output.decode(sys.getdefaultencoding()))
print "data : %s" % data['pgmap']['data_bytes'] if 'pgmap' not in data:
print "allocated : %s" % data['pgmap']['bytes_used'] print("UNKNOWN: fail to read pgmap status")
print "total : %s" % data['pgmap']['bytes_total'] sys.exit(STATUS['UNKNOWN'])
PER_DATA=round(int(data['pgmap']['data_bytes'])*100/int(data['pgmap']['bytes_total']),1) if options.debug:
DATA_WARN_T=int(int(data['pgmap']['bytes_total'])*options.warndata/100) print("data: %s" % data['pgmap']['data_bytes'])
DATA_CRIT_T=int(int(data['pgmap']['bytes_total'])*options.critdata/100) print("allocated: %s" % data['pgmap']['bytes_used'])
PER_ALLOC=round(int(data['pgmap']['bytes_used'])*100/int(data['pgmap']['bytes_total']),1) print("total: %s" % data['pgmap']['bytes_total'])
ALLOC_WARN_T=int(int(data['pgmap']['bytes_total'])*options.warnalloc/100)
ALLOC_CRIT_T=int(int(data['pgmap']['bytes_total'])*options.critalloc/100)
PER_DATA = round(int(data['pgmap']['data_bytes']) * 100 / int(data['pgmap']['bytes_total']), 1)
DATA_WARN_T = int(int(data['pgmap']['bytes_total']) * options.warndata / 100)
DATA_CRIT_T = int(int(data['pgmap']['bytes_total']) * options.critdata / 100)
PER_ALLOC = round(int(data['pgmap']['bytes_used']) * 100 / int(data['pgmap']['bytes_total']), 1)
ALLOC_WARN_T = int(int(data['pgmap']['bytes_total']) * options.warnalloc / 100)
ALLOC_CRIT_T = int(int(data['pgmap']['bytes_total']) * options.critalloc / 100)
if options.debug: if options.debug:
print "%% data : %s" % PER_DATA print("%% data: %s" % PER_DATA)
print "%% allocated : %s" % PER_ALLOC print("%% allocated: %s" % PER_ALLOC)
STATUS=STATUS_OK if PER_DATA > options.critdata or PER_ALLOC > options.critalloc:
STATUS_TXT="OK" STATUS_TXT = 'CRITICAL'
if PER_DATA > options.critdata or PER_ALLOC > options.critalloc: elif PER_DATA > options.warndata or PER_ALLOC > options.warnalloc:
STATUS=STATUS_CRITICAL STATUS_TXT = 'WARNING'
STATUS_TXT="CRITICAL"
elif PER_DATA > options.warndata or PER_ALLOC > options.warnalloc:
STATUS=STATUS_WARNING
STATUS_TXT="WARNING"
print "%s : %s%% allocated / %s%% really used|allocated=%sB;%s;%s;0;%s,used=%sB;%s;%s;0;%s" % (STATUS_TXT,PER_ALLOC,PER_DATA,data['pgmap']['bytes_used'],ALLOC_WARN_T,ALLOC_CRIT_T,data['pgmap']['bytes_total'],data['pgmap']['data_bytes'],DATA_WARN_T,DATA_CRIT_T,data['pgmap']['bytes_total'])
sys.exit(STATUS)
else: else:
print "UNKNOWN : fail to execute ceph status command" STATUS_TXT = 'OK'
sys.exit(STATUS_UNKNOWN)
print(
"%s - %s%% allocated / %s%% really used|allocated=%sB;%s;%s;0;%s,used=%sB;%s;%s;0;%s" % (
STATUS_TXT, PER_ALLOC, PER_DATA,
data['pgmap']['bytes_used'], ALLOC_WARN_T, ALLOC_CRIT_T, data['pgmap']['bytes_total'],
data['pgmap']['data_bytes'], DATA_WARN_T, DATA_CRIT_T, data['pgmap']['bytes_total']
)
)
sys.exit(STATUS[STATUS_TXT])