Python-SSH-Nagios-Icinga-Sc.../check_last_yum_upgrade_via_...

64 lines
2.2 KiB
Python

#!/usr/bin/env python3
# check_last_yum_upgrade_via_ssh.py
import sys,re
from optparse import OptionParser
from time import time
from datetime import date
from subprocess import run, PIPE
from calendar import month_abbr
parser = OptionParser()
parser.add_option("-H","--host-name",dest="host",help="host to send command to")
parser.add_option("-F","--configfile",dest="ssh_config_file",help="ssh config file to use")
parser.add_option("-i","--identitiy-file",dest="ssh_key_file",help="identity file to use")
# args is throwaway variable that captures left over arguments from sys.argv
options,args = parser.parse_args()
ssh_command = "ssh "
if options.ssh_config_file: ssh_command += "-F " + options.ssh_config_file + " "
if options.ssh_key_file: ssh_command += "-i " + options.ssh_key_file + " "
if options.host:
ssh_command += options.host
else:
print("Critical - Invalid Host")
sys.exit(2)
# Requires a remote script.
# $USER ALL=(root) NOPASSWD: /usr/local/sbin/yum_log_parse.bash in /etc/sudoers
# or maybe this will work
# %wheel ALL=(root) NOPASSWD:/usr/local/sbin/yum_log_parse.bash
ssh_command += " \'sudo /usr/local/sbin/yum_log_parse.bash\'"
start = time()
output = run(ssh_command, shell=True, stdout=PIPE).stdout.decode("utf-8").rstrip()
upgrade_month_string,upgrade_day_string = output.split(' ')
upgrade_month = list(month_abbr).index(upgrade_month_string)
upgrade_day = upgrade_day_string.lstrip('0')
current_date = date.today()
upgrade_date = date(current_date.year, int(upgrade_month), int(upgrade_day))
if upgrade_date > current_date:
upgrade_date = upgrade_date.replace(year=current_date.year - 1)
time_since_upgrade = current_date - upgrade_date
if time_since_upgrade.days > 60:
elapsed_time = (time() - start)
print(str(time_since_upgrade.days) + " days since last upgrade | elapsed_time=" + str(elapsed_time))
sys.exit(2)
elif time_since_upgrade.days > 30:
elapsed_time = (time() - start)
print(str(time_since_upgrade.days) + " days since last upgrade | elapsed_time=" + str(elapsed_time))
sys.exit(1)
else:
elapsed_time = (time() - start)
print(str(time_since_upgrade.days) + " days since last upgrade | elapsed_time=" + str(elapsed_time))
sys.exit(0)
'''
'''