#!/usr/local/bin/python

import commands
import ipaddr
import argparse
import sys

# argparse statements.
parser = argparse.ArgumentParser(description='Nagios plugin which uses ssh to collect remote Synology server disk capacity') 
parser.add_argument('-H', '--server', dest='host',
	metavar='ip',
	default='192.168.1.10',
	help='Synology server IPv4 address.  Default: 192.168.1.10.'
	) 
parser.add_argument('-i', '--keyfile', dest='keyfile',
	metavar='file',
	help='Full path to SSH keyfile.  Ex: /usr/local/etc/nagios/keyfiles/key.ppk.',
	required=True
	) 
parser.add_argument('-d', '--disks', dest='disks',
	metavar='mdX,mdY',
	default='md0',
	help='Comma-delimited list of Synology disks to query.  Default: md0.  Ex: md0,md1.'
	)
parser.add_argument('-w', '--warning', dest='warning',
	metavar='percent',
	default=60,
	help='Warning disk capacity consumed.  Default: 60.',
	type=int
	)
parser.add_argument('-c', '--critical', dest='critical',
	metavar='percent',
	default=80,
	help='Critical disk capacity consumed.  Default: 80.',
	type=int
	)
parser.add_argument('-q', '--quiet', dest='quiet',
	action='store_true',
	default=False,
	help='Quiet mode.  Omit for testing purposes only.'
	)
parser.add_argument('-v', '--version',
        action='version',
        version='%(prog)s 1.0',
        help='Show program\'s version number and exit.'
        )

# fill variables from argparse.
args = parser.parse_args()
host = args.host
diskinput = args.disks
warning = int(args.warning)
critical = int(args.critical)
quiet = bool(args.quiet)
keyfile = args.keyfile

# Nagios expects these returned values.
UNKNOWN = -1
OK = 0
WARNING = 1
CRITICAL = 2

# fill the rest of the variables.
disks = diskinput.split(',')
results = []
remaining = []
i = 0
titlebuffer = 'OK'
buffer = ''
resultcode = UNKNOWN

# test input variables.
if warning >= critical:
	print "Please input a warning capacity (" + str(warning) + ") below the critical capacity (" + str(critical) + ")."
	raise SystemExit, resultcode

if warning < 0 or warning > 100:
	print "Please input a warning capacity percentage between 0-100."
	raise SystemExit, resultcode

if critical < 0 or critical > 100:
	print "Please input a critical capacity percentage between 0-100."
	raise SystemExit, resultcode

if not isinstance(disks, list):
	disks = []
	try:
		disks.append(diskinput)
	except:
		print 'Invalid comma-delimited list of Synology disks.  Ex: md0,md1,md2.'
		raise SystemExit, resultcode

try: 
	host = str(ipaddr.IPv4Address(host)) 
except: 
	print 'Invalid Synology server IPv4 address', host
	raise SystemExit, resultcode

# query the host for each disk device specified.
for disk in disks:
	if not quiet: print "Querying " + host + " about " + disk + "..."
	command = '/usr/bin/ssh ' + host + ' -i ' + keyfile + ' -l root -C "df -h /dev/' + disk + ' | grep dev"'
	try:
		x = commands.getoutput(command)
	except:
		print 'ERROR: Bad result from SSH (is your keyfile correct?).'
		raise SystemExit, resultcode
	y = x.split()
	try:
		size = y[4]
		left = y[3]
	except:
		print 'ERROR: Bad result from Synology server for disk "' + disk + '".' 
		raise SystemExit, resultcode
	if not quiet: print "Got " + size + "!"
	results.append(size)
	remaining.append(left)

# loop one final time to aggregate and print the buffer
resultcode = OK
for disk in disks:
	size = results[i]
	left = remaining[i]
	if int(size[:2]) >= warning:
		titlebuffer = 'WARNING'
		resultcode = WARNING
		if int(size[:2]) >= critical:
			titlebuffer = 'CRITICAL'
			resultcode = CRITICAL
	buffer = buffer + ' (' + disk + ': ' + size + " full, " + left + " left)"
	i = i + 1

print 'DISK ' + titlebuffer + ': ' + buffer
raise SystemExit, resultcode
