mirror of
https://gist.github.com/d6c00bdb2aa87d4d598c1fb1e7bb928e.git
synced 2025-01-18 03:18:24 -08:00
61 lines
2.3 KiB
Python
Executable File
61 lines
2.3 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
# check_last_dist_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 += " \'cat /var/log/apt/history.log\'"
|
|
|
|
start = time()
|
|
output = run(ssh_command, shell=True, stdout=PIPE).stdout.decode("utf-8").rstrip()
|
|
output_paragraphs = re.split('\n\s*\n',output)
|
|
block_with_last_dist_upgrade = ""
|
|
|
|
for block in output_paragraphs:
|
|
if "dist-upgrade" in block:
|
|
block_with_last_dist_upgrade = output_paragraphs.index(block)
|
|
|
|
if block_with_last_dist_upgrade == "":
|
|
elapsed_time = (time() - start)
|
|
print("no \"dist-upgrade\" in apt history log | elapsed_time=" + str(elapsed_time))
|
|
sys.exit(3)
|
|
|
|
last_dist_upgrade_details = output_paragraphs[block_with_last_dist_upgrade].split()
|
|
upgrade_year,upgrade_month,upgrade_day = last_dist_upgrade_details[1].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)
|