Last active
November 2, 2018 12:20
-
-
Save TrentSPalmer/d6c00bdb2aa87d4d598c1fb1e7bb928e to your computer and use it in GitHub Desktop.
Python SSH Nagios/Icinga Scripts for Checking System Updates.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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) | |
''' | |
''' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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) | |
''' | |
''' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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}' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment