Skip to content

Instantly share code, notes, and snippets.

@adrianlzt
Last active September 14, 2022 16:04
Show Gist options
  • Save adrianlzt/1a0a7c92fb0cb17455307908f5632fe3 to your computer and use it in GitHub Desktop.
Save adrianlzt/1a0a7c92fb0cb17455307908f5632fe3 to your computer and use it in GitHub Desktop.
Pushbullet notifications for m/monit
#!/usr/bin/env python3
# Copyright (c) 2022 Adrian Lopez
#
# Based on https://github.com/PinGwynn/mmonit-mattermost
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
import argparse
import json
import os
import urllib.request as urllib2
import logging
logging.basicConfig(filename='/var/log/pushbullet.log', level=logging.DEBUG)
logging.info('Starting Pushbullet script')
VERSION = "0.0.1"
def parse():
parser = argparse.ArgumentParser(description='Sends alerts to Pushbullet')
parser.add_argument('--url', help='Incoming Webhook URL', default="https://api.pushbullet.com/v2/pushes")
parser.add_argument('--token', help='Pushbullet token', required=True)
parser.add_argument('--notificationtype', help='Notification Type',
default='none')
args = parser.parse_args()
return args
def make_data():
body = "{action}\n" \
"{event}\n\n" \
"Host affected: {host}\n" \
"Service affected: {service}\n" \
"Details: {details}" \
.format(action=os.environ.get('MONIT_ACTION', 'none'),
event=os.environ['MONIT_EVENT'],
host=os.environ['MONIT_HOST'],
service=os.environ['MONIT_SERVICE'],
details=os.environ['MONIT_DESCRIPTION'])
title = "{}: {}".format(os.environ['MONIT_SERVICE'],os.environ['MONIT_EVENT'])
payload = {
"type": "note",
"title": title,
"body": body
}
return payload
def request(url, token, data):
req = urllib2.Request(url, json.dumps(data).encode('utf-8'))
req.add_header('Access-Token', token)
req.add_header('Content-Type', 'application/json')
response = urllib2.urlopen(req)
return response.read()
def main():
args = parse()
data = make_data()
response = request(args.url, args.token, data)
logging.info('Pushbullet response: {}'.format(response))
if __name__ == "__main__":
try:
main()
except Exception as e:
logging.error('Pushbullet script failed: {}'.format(e))
@adrianlzt
Copy link
Author

Config example:

...
  then exec "/usr/local/bin/pushbullet.py --token FOOBAR"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment