#!/usr/bin/python import argparse,sys,commands,urllib,re import httplib from urlparse import urlparse __author__ = 'phillipmoore' parser = argparse.ArgumentParser(description='This script will compare the current running helpspot version with the latest helpspot build version') parser.add_argument('-s','--helpspot',required=True,help='HelpSpot Server URL (ex: http://helpspot.domain.com or http://www.domain.com/helpspot)') parser.add_argument('-w','--warn',action='store_true',help='Send a warning signal to nagios if versions mismatch',required=False) parser.add_argument('-c','--crit',action='store_true',help='Send a critical signal to nagios if versions mismatch (default)',required=False) args = parser.parse_args() def checkUrl(url): try: p = urlparse(url) conn = httplib.HTTPConnection(p.netloc) conn.request('HEAD', p.path) resp = conn.getresponse() return resp.status < 400 except: return False def verComp(version1, version2): def normalize(v): return [int(x) for x in re.sub(r'(\.0+)*$', '', v).split(".")] return cmp(normalize(version1), normalize(version2)) ######################## # Nagios states # # 0 - OK # # 1 - WARNING # # 2 - CRITICAL # # 3 - STATE UNKNOWN # ######################## HELPSPOT_BUILD_VERSION_URL = 'https://store.helpspot.com/latest-release' HELPSPOT_VERSION_URL = args.helpspot + "/helpspot/version.txt" if checkUrl(HELPSPOT_BUILD_VERSION_URL) and checkUrl(HELPSPOT_VERSION_URL): helpspot_build_version = urllib.urlopen(HELPSPOT_BUILD_VERSION_URL).read() helpspot_version = urllib.urlopen(HELPSPOT_VERSION_URL).read() if verComp(helpspot_build_version, helpspot_version) <= 0: print "OK: HELPSPOT is up to date (version: " + helpspot_version + ")" sys.exit(0) else: if(args.warn): print "WARNING: HELPSPOT is not at the latest version. LATEST: " + helpspot_build_version + ", CURRENT: " + helpspot_version sys.exit(1) else: print "CRITICAL: HELPSPOT is not at the latest version. LATEST: " + helpspot_build_version + ", CURRENT: " + helpspot_version else: print "STATE UNKNOWN: Problem with the URL"