Skip to content

Instantly share code, notes, and snippets.

@vwrs
Last active October 11, 2019 14:01
Show Gist options
  • Save vwrs/d1d43f9c9f0f1a9a0538aabdfd4f66de to your computer and use it in GitHub Desktop.
Save vwrs/d1d43f9c9f0f1a9a0538aabdfd4f66de to your computer and use it in GitHub Desktop.
continuously watching a specific host by ping and raising an alert (post to the slack channel by webhook URL) when the host is unreachable
import time
import requests
import json
import pings # pip install pings
HOSTNAME = '{{ HOSTNAME }}'
PING_TIMES = 10
SLEEP_TIME = 100
WEBHOOK_URL = '{{ WEBHOOK_URL }}'
def post_slack(text):
requests.post(WEBHOOK_URL, data=json.dumps({
'text': text,
'username': 'ping bot',
'icon_emoji': ':thinking_face:',
}))
if __name__ == '__main__':
p = pings.Ping(quiet=False)
while True:
response = p.ping(HOSTNAME, times=PING_TIMES)
if not response.is_reached():
post_slack(HOSTNAME + ': host might be down!')
time.sleep(SLEEP_TIME)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment