Skip to content

Instantly share code, notes, and snippets.

@timmahoney
Last active October 7, 2016 12:43
Show Gist options
  • Save timmahoney/d2046be53060138e53b9ad900440f6ca to your computer and use it in GitHub Desktop.
Save timmahoney/d2046be53060138e53b9ad900440f6ca to your computer and use it in GitHub Desktop.
Infrastructure Exercises
#!/bin/bash
# Nagios Plugin Bash Script - exercise_1.sh
# This script checks a json endpoint url
#
# --url is the only required parameter.
#
# Examples:
# Will return error code:
# ./check_queue_status_json.sh --url http://mockbin.org/bin/9b11175a-da3e-4bb1-8d3f-510f97cf6dc5/
# Will return WARNING
# ./check_queue_status_json.sh --url http://mockbin.org/bin/38c1d5a6-411e-4139-a579-5581339675a5/
# Will return CRITICAL
# ./check_queue_status_json.sh --url http://mockbin.org/bin/38c1d5a6-411e-4139-a579-5581339675a5/ --crit-threshold 600
PROGNAME=`basename $0`
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3
CRITICAL_THRESHOLD=1000
WARNING_THRESHOLD=500
print_help() {
echo "Check Queue Status:"
echo " $PROGNAME --url <Queue Status Endpoint URL>"
echo " $PROGNAME --crit-threshold <CRIT_THRESHOLD>"
echo " $PROGNAME --warn-threshold <WARN_THRESHOLD>"
echo " $PROGNAME --help"
echo ""
echo "Default Warning Threshold is 500"
echo "Default Critical Threshold is 1000"
}
while test -n "$1"; do
case "$1" in
--help)
print_help
exit $STATE_OK
;;
-h)
print_help
exit $STATE_OK
;;
--url)
URL=$2
shift
;;
-u)
URL=$2
shift
;;
--crit-threshold)
CRITICAL_THRESHOLD=$2
shift
;;
-c)
CRITICAL_THRESHOLD=$2
shift
;;
--warn-threshold)
WARNING_THRESHOLD=$2
shift
;;
-w)
WARNING_THRESHOLD=$2
shift
;;
*)
echo "UNKNOWN: UNEXPECTED ARGUMENT $1"
print_help
exit $STATE_UNKNOWN
;;
esac
shift
done
# Check for missing parameters
if [[ -z "$URL" ]]
then
echo "UNKNOWN: MISSING PARAMETERS"
print_help
exit $STATE_UNKNOWN
fi
RESULT=$(curl -s -S "$URL")
if [[ -n $(echo $RESULT | jq 'select(.|keys[]=="messages")') ]]; then
MESSAGE_COUNT=$(echo $RESULT | jq '.messages')
if [[ "$MESSAGE_COUNT" -ge "$CRITICAL_THRESHOLD" ]]; then
echo "CRITICAL: $MESSAGE_COUNT messages in queue"
exit $STATE_CRITICAL
elif [[ "$MESSAGE_COUNT" -ge "$WARNING_THRESHOLD" ]]; then
echo "WARNING: $MESSAGE_COUNT messages in queue"
exit $STATE_WARNING
else
echo "OK: $MESSAGE_COUNT messages in queue"
exit $STATE_OK
fi
else
if [[ -n $(echo $RESULT | jq 'select(.|keys[]=="error")') ]]; then
ERROR=$(echo $RESULT | jq '.error')
echo "CRITICAL: $ERROR"
exit $STATE_CRITICAL
else
echo "UNKNOWN: UNEXPECTED RESPONSE"
exit $STATE_UNKNOWN
fi
fi
#!/usr/bin/python
import os
import argparse
import getpass
import configparser
import github3
def mfa():
"""Get the MFA code
"""
try:
prompt = raw_input
except NameError:
prompt = input
code = ''
while not code:
code = prompt('Enter 2FA code : ')
return code
def setup_username(user, configfilename):
"""Set up the username configuration file
Use the configparser to structure the file
"""
config = configparser.ConfigParser()
config['DEFAULT']['username'] = user
with open(configfilename, 'w') as configfile:
config.write(configfile)
return user
def load_username(configfilename):
"""Load the username.
If the configuration file exists
"""
config = configparser.ConfigParser()
config.read(configfilename)
return config['DEFAULT']['username']
if __name__ == '__main__':
# Parse the arguments
parser = argparse.ArgumentParser(description='GitHub Release Information')
parser.add_argument('--repo', help='GitHub Repository', required=True)
parser.add_argument('--tag', help='GitHub Repo Release Tag', required=True)
parser.add_argument('--user', help='GitHub username')
args = parser.parse_args()
# Configuration filename
configfilename = "%s/.view-github-release" % os.path.expanduser('~')
# If the username is set in the cli, set up the config file
# If the username is NOT set, load it from the config file, IF it exists
username = None
if args.user:
username = setup_username(args.user, configfilename)
elif os.path.isfile(configfilename):
username = load_username(configfilename)
# If our username is set, get the password and login
if username:
password = getpass.getpass("GitHub Password : ")
gh = github3.login(username, password, two_factor_callback=mfa)
# Otherwise, exit and tell the username
else:
print("Please set a username!")
parser.print_help()
exit()
# Split out the repository path into owner and name
try:
repo_owner,repo_name = args.repo.split("/")
except ValueError:
print("Repository param is not well formed!")
parser.print_help()
exit()
# Get the Release
release = gh.repository(repo_owner, repo_name).release_from_tag(args.tag)
if not release.tag_name:
print("No Release loaded; Check Repo path and tag name!")
parser.print_help()
exit()
# Output the release info
print("Release %s" % args.tag)
print("URL | %s" % release.html_url)
print("Publish Date | %s" % release.published_at)
print("Author | %s" % release.author['login'])
print("Tag Name | %s" % release.tag_name)
print("Name | %s" % release.name)
print("#################")
print("Body")
print("#################")
print(release.body)

View Github Release

View Github releases with this handy dandy python script.

Setup

pip install --pre github3.py
pip install configparser

Some Python libraries I could have used to perform this task. Bullet points are marked + for PRO, - for CON

TL;DR github3.py is fully featured, well maintained, has support for everything, and the docs are great.

github3.py https://github.com/sigmavirus24/github3.py/

    • Recent Activity
    • Good documentation
    • Support for Releases
    • Support for Two Factor Auth

PyGithub

    • Active Repo
    • Most Stars
    • Support for our use case (releases) out of the box
    • no MFA?

Pygithub3

    • Latest commit 2 years ago

libsaas

    • Latest commit 8 months ago
    • isn't focused on github

sanction

    • OAuth2 client implementation only

agithub

    • Recent Activity
    • bottom 3 repos star-wise
    • isn't focused on github

githubpy

    • Not very robust

Github-Flask

    • Flask is easy to get started with
    • Nice docs
    • Auth Only

torngithub

    • Latest commit 2013
    • oauth only
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment