Last active
October 7, 2021 07:44
-
-
Save OliPassey/1efd5f56daed13b4dea77df484e4ce1d to your computer and use it in GitHub Desktop.
add pushbullet notifications to grafana alerts via custom webhook (multi-user support)
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
# Copyright (c) 2018 Oli Passey & Steven Gould | |
# Author: Oli Passey & Steven Gould | |
# More information: https://olipassey.me.uk | |
# 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. | |
<?php | |
// Capture incoming data and try to detect JSON | |
if($json = json_decode(file_get_contents("php://input"), true)) { | |
print_r($json); | |
$data = $json; | |
} else { | |
print_r($_POST); | |
$data = $_POST; | |
} | |
// Write to a log file for debug and inspection | |
ob_start(); | |
var_dump($data); | |
$var = ob_get_clean(); | |
file_put_contents('hook.log', $var); | |
// Send the useful stuff onto Pushbullet | |
$pbtitle = $data["title"]; | |
$pbmessage = $data["message"]; | |
// Who gets the notifications? | |
// device_id (ID) from https://www.pushbullet.com/#devices (Click your device and ID is appended to the URL) | |
// Pushbullet API Key (password) from https://www.pushbullet.com/#settings/account (Click Create Access token) | |
$targets = [ | |
["id" => "**********************", "password" => "**********************************" . ":" . ""], | |
["id" => "**********************", "password" => "**********************************" . ":" . ""], | |
["id" => "**********************", "password" => "**********************************" . ":" . ""] | |
]; | |
// Use CURL to send | |
foreach ($targets as $target) { | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, "https://api.pushbullet.com/v2/pushes"); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, '{"device_iden": "'.$target["id"].'", "type": "note", "title":"'.$pbtitle.'", "body": "'.$pbmessage.'"}'); | |
curl_setopt($ch, CURLOPT_POST, 1); | |
curl_setopt($ch, CURLOPT_USERPWD, $target["password"]); | |
// Headers must set content-type as json | |
$headers = array(); | |
$headers[] = "Content-Type: application/json"; | |
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); | |
$result = curl_exec($ch); | |
if (curl_errno($ch)) { | |
echo 'Error:' . curl_error($ch); | |
} | |
curl_close ($ch); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment