Last active
March 24, 2017 11:20
-
-
Save tizot/88b9ed001e78a3e0e196f414352368e3 to your computer and use it in GitHub Desktop.
Script to send a notification on Pushbullet when a command has finished. See details in comment.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/python3 | |
from argparse import ArgumentParser | |
from socket import gethostname | |
import subprocess | |
import arrow | |
import requests | |
# Usage: | |
# monitor ping -c 5 google.com | |
PUSH_KEY = 'PUSHBULLET_API_KEY' # change me! | |
def main(command, params): | |
begin_time = arrow.utcnow() | |
push_data = {'type': 'note'} | |
try: | |
ret = subprocess.check_call([command] + params) | |
push_data['title'] = 'La commande "%s" a terminé.' % command | |
except subprocess.CalledProcessError as err: | |
push_data['title'] = 'La commande "%s" a terminé avec une erreur...' % command | |
duration = arrow.utcnow().humanize(begin_time, locale='fr_fr', only_distance=True) | |
push_data['body'] = "Hostname : %s\nTemps d'exécution : %s" % (gethostname(), duration) | |
push_headers = {'Access-Token': PUSH_KEY} | |
r = requests.post('https://api.pushbullet.com/v2/pushes', data=push_data, headers=push_headers) | |
if __name__ == "__main__": | |
parser = ArgumentParser() | |
parser.add_argument('command') | |
args = parser.parse_known_args() | |
cmd = args[0].command | |
params = args[1] | |
main(cmd, params) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Place this file into your
/usr/local/bin
, make it executable (chmod +x monitor
). Ensure that you have python3 with the modulesarrow
andrequests
(installable withpip install arrow requests
).