#!/usr/bin/env python # Slack Status API Check Script - v1.1 (Python 3 Edition) # Tristan Self # The script uses the Slack Status API to be able to report the Slack service status, as presented from their API. It returns if there are any # known issues present and the date the last update was posted. It is recommended to review the Slack API documentation, which can be found: # https://api.slack.com/docs/slack-status for further information. # Usage Normal # ./check_slack_status.py -e https://api.slack.com/docs/slack-status # Usage Debug Mode # ./check_slack_status.py -e https://api.slack.com/docs/slack-status -d # For troubleshooting purposes, provides some additional debug information and output. # Version History # 1.0 - 01/12/2023 - First Release # 1.1 - 20/12/2023 - Fix to behaviour when issues are reported. # Python 3 Edition - Because Python 2.7 does not support zones in strptime correctly. import requests import sys import json from requests.packages.urllib3.exceptions import InsecureRequestWarning import argparse from datetime import datetime requests.packages.urllib3.disable_warnings(InsecureRequestWarning) def main(): ############################################################################################# # Initalise Variables ############################################################################################# strCheckOutputStatusText = "OK" strCheckOutputStatus = 0 intErrorCount = 0 strCheckOutputText = "" intDebugMode = 0 def F_prettyDateTime(strlongdate): date_format = '%Y-%m-%dT%H:%M:%S%z' date_obj = datetime.strptime(strlongdate,date_format) return date_obj.strftime('%d/%m/%Y %H:%M:%S %z') ############################################################################################# # Argument Collection ############################################################################################# # Parse the arguments passed from the command line. parser = argparse.ArgumentParser() parser.add_argument('-e','--endpointurl',help='Endpoint URL (e.g. https://status.slack.com/api/current)',required=True) parser.add_argument('-d','--debugmode',help='Enable debug mode',action='store_true') # Assign each arguments to the relevant variables. arguments = vars(parser.parse_args()) strEndpointURL = arguments['endpointurl'] intDebugMode = arguments['debugmode'] ############################################################################################# # Collect and Report the Status ############################################################################################# # Connect to the Slack Status API to collect the correct status information. try: objArrayInfo = requests.get(strEndpointURL, verify=False) except requests.exceptions.RequestException as e: # Build and print to the screen the check result. print ("CRITICAL - Failed to connect to API!") strCheckOutputStatus = 2 # Return the status to the calling program. sys.exit(strCheckOutputStatus) if intDebugMode: print("Status Code:",objArrayInfo.status_code) if objArrayInfo.status_code == 200: # Take the JSON Output turn it into a dictionary. objArrayInfoJSON = json.dumps(objArrayInfo.json(),indent=2) objArrayInfoDict = json.loads(objArrayInfoJSON) # Check the Slack reported status and report. if (objArrayInfoDict["status"] == "ok"): # No issues reported. print("OK - Slack is reporting 0 issues. Last update:",F_prettyDateTime(objArrayInfoDict["date_updated"])) strCheckOutputStatus = 0 sys.exit(strCheckOutputStatus) else: # Issues have been reported. print("CRITICAL - Slack is reporting issues, please ! Last update:",F_prettyDateTime(objArrayInfoDict["date_updated"])) strCheckOutputStatus = 2 sys.exit(strCheckOutputStatus) else: # Build and print to the screen the check result. print ("CRITICAL - Failed to connect to API! Status Code:",objArrayInfo.status_code) strCheckOutputStatus = 2 # Return the status to the calling program. sys.exit(strCheckOutputStatus) # Collect and report the status if __name__ == "__main__": main()