Skip to content

Instantly share code, notes, and snippets.

@ryancbutler
Created February 14, 2019 19:44
Show Gist options
  • Save ryancbutler/8bd6ecbf12cee6e0398534fb197e1b6f to your computer and use it in GitHub Desktop.
Save ryancbutler/8bd6ecbf12cee6e0398534fb197e1b6f to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import json, requests, base64, sys, os, re, socket
slack_url = "https://hooks.slack.com/services/###MYKEYHERE#####"
def main():
consul_stdin = json.load(sys.stdin)
getSVCStatus(consul_stdin)
def post_to_slack(message, slack_url, emoji):
slack_data = {'text': message,"username": "ConsulWatcher",'icon_emoji': emoji}
response = requests.post(
slack_url, data=json.dumps(slack_data),
headers={'Content-Type': 'application/json'}
)
if response.status_code != 200:
raise ValueError(
'Request to slack returned an error %s, the response is:\n%s'
% (response.status_code, response.text)
)
def getSVCStatus(consul_stdin):
for svc in consul_stdin:
report = "Consul Service has changed!!\n*Service:* %s \n*Status:* %s \n *NODE:* %s\n *Comment:* %s\n *Alert Source:* %s" % (svc['ServiceName'],svc['Status'],svc['Node'],svc['Output'],socket.gethostname())
print(report)
post_to_slack(report, slack_url,":rotating_light:")
#print(status)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment