From 441c6ac96692a15906e7b6b1f276ba73b688664b Mon Sep 17 00:00:00 2001 From: Trent Palmer Date: Wed, 27 Dec 2017 20:56:41 -0800 Subject: [PATCH] add four scripts --- check_last_dist_upgrade_via_ssh.py | 60 ++++++++++++++++++++++++++ check_last_pacman_upgrade_via_ssh.py | 56 ++++++++++++++++++++++++ check_last_yum_upgrade_via_ssh.py | 64 +++++++++++++++++++++++++++- yum_log_parse.bash | 5 +++ 4 files changed, 184 insertions(+), 1 deletion(-) create mode 100755 check_last_dist_upgrade_via_ssh.py create mode 100755 check_last_pacman_upgrade_via_ssh.py create mode 100755 yum_log_parse.bash diff --git a/check_last_dist_upgrade_via_ssh.py b/check_last_dist_upgrade_via_ssh.py new file mode 100755 index 0000000..cf26ee5 --- /dev/null +++ b/check_last_dist_upgrade_via_ssh.py @@ -0,0 +1,60 @@ +#!/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) diff --git a/check_last_pacman_upgrade_via_ssh.py b/check_last_pacman_upgrade_via_ssh.py new file mode 100755 index 0000000..47211e3 --- /dev/null +++ b/check_last_pacman_upgrade_via_ssh.py @@ -0,0 +1,56 @@ +#!/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) + +''' +''' diff --git a/check_last_yum_upgrade_via_ssh.py b/check_last_yum_upgrade_via_ssh.py index 5f7ce86..c9c2614 100644 --- a/check_last_yum_upgrade_via_ssh.py +++ b/check_last_yum_upgrade_via_ssh.py @@ -1 +1,63 @@ -#!/usr/bin/env python3 \ No newline at end of file +#!/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) + +''' +''' diff --git a/yum_log_parse.bash b/yum_log_parse.bash new file mode 100755 index 0000000..4d89131 --- /dev/null +++ b/yum_log_parse.bash @@ -0,0 +1,5 @@ +#!/bin/bash +# yum_log_parse.bash +# invoked from check_last_yum_upgrade_via_ssh.py + +grep Updated /var/log/yum.log | tail -1 | awk '{print $1" "$2}'