Linux technical support - [email protected]


prometheus webhook php script example

This simple script can text to you for example. You should install some webserver, in my case it is nginx + php.

And of course you should configure Alertmanager for sending webhook to your webserver.

Alertmanager send POST web request, that request can be parsed by this script:

<?php

$my_json = file_get_contents('php://input');

$myArray = json_decode($my_json, true);

$my_message = "Prometheus: " . $myArray['status'] . " " . $myArray['groupLabels']['alertname'] . "\n" . $myArray['commonLabels']['instance'];

system('YOUR COMMAND HARE');
?>
<?php
$my_json = file_get_contents('php://input');
 
$myArray = json_decode($my_json, true);
 
$my_message = $myArray['alerts'][0]['status'] . ": " . $myArray['alerts'][0]['annotations']['summary'] . ", node: " . $myArray['alerts'][0]['labels']['instance'];

if (empty($myArray['status'])) {
    $my_message = "empty message.";
}
 
system('YOUR COMMAND HARE');
?>

Alertmanager config part:

receivers:
- name: 'team-X-mails'
  email_configs:
  - to: '[email protected]'
    require_tls: false
    send_resolved: true
  webhook_configs:
  - url: http://1.1.1.1

tcpdump of POST request:

POST / HTTP/1.1
Host: alexlinux.com
User-Agent: Go-http-client/1.1
Content-Length: 728
Content-Type: application/json
Accept-Encoding: gzip

{"receiver":"team-X-mails","status":"resolved","alerts":[{"status":"resolved","labels":{"alertname":"sw_percona_cluster_rep_status","env":"lan","instance":"alexlinux.com:9100","job":"node"},"annotations":{},"startsAt":"2016-12-14T12:33:37.805+03:00","endsAt":"2016-12-14T12:35:37.805+03:00","generatorURL":"http://alexlinux.com:9090/graph?g0.expr=sw_percona_cluster_rep_status+%21%3D+2\u0026g0.tab=0"}],"groupLabels":{"alertname":"sw_percona_cluster_rep_status"},"commonLabels":{"alertname":"sw_percona_cluster_rep_status","env":"lan","instance":"alexlinux.com:9100","job":"node"},"commonAnnotations":{},"externalURL":"http://alexlinux.com:9093","version":"3","groupKey":7369734698630657363}

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>