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

57 lines
2.0 KiB
Python
Executable File

#!/usr/bin/env python3
# check_last_pacman_upgrade_via_ssh.py
import sys,re
from optparse import OptionParser
from time import time
from datetime import date
from subprocess import run, PIPE
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)
ssh_command += " \'grep -e \'-Syu\' /var/log/pacman.log | tail -1 | cut -d \" \" -f 1 | cut -d \"[\" -f 2\'"
start = time()
output = run(ssh_command, shell=True, stdout=PIPE).stdout.decode("utf-8").rstrip()
if output == "":
elapsed_time = (time() - start)
print("no \"-Syu\" in pacman log | elapsed_time=" + str(elapsed_time))
sys.exit(3)
upgrade_year,upgrade_month,upgrade_day = output.split('-')
upgrade_month = upgrade_month.lstrip('0')
upgrade_day = upgrade_day.lstrip('0')
time_since_upgrade = date.today() - date(int(upgrade_year), int(upgrade_month), int(upgrade_day))
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)
'''
'''