Website Monitoring Webhook

What is a Webhook?

Webhooks are "user-defined HTTP callbacks". Webhook is a way for ServiceUptime to trigger a script on your site when an event occurs, such as server down.

Enter the URL for your script and if your website fails ServiceUptime system will send JSON data to the URL you provide.

Sample Alert JSON Data:

{
  DateTime: "30/10/2014 01:29:55",
  WebHookUrl: "http://requestb.in/10dr4kk1",
  MonitorId: "8",
  CheckResult: "Failed",
  MonitorName: "SVR/1",
  Service: "web page",
  CheckInterval: "5",
  HostName: "",
  Port: "0",
  PageUrl: "http://www.myhost.org/servercheck.php",
  SearchString: "Server OK",
  ErrorMsg: "(String not found)",
  TimeZone: "-06:00",
  FirstLocation: "San Jose, CA United States (64.85.167.65)",
  SecondLocation: "Scranton, PA United States (192.187.116.50)",
}

What do I do with the data?

Once you have the data from the monitor, you can do just about anything:

  • Log the data
  • Send an email to your webhost with your account info
  • Display messages or send alerts to your users
  • Switch to a backup server
  • Trigger events in 3rd party notification systems, CRM's, etc.

Using the Webhook:

Creating Your Script

First, you need a file on your site that will process the results. Once you create the file, upload it to your site. The URL for this file is where our system will send the data.

Below is a sample PHP script that writes to a log and sends an email for DOWN alerts.

$AlertJSON = @file_get_contents('php://input');

if(strlen($AlertJSON)) {
    $AlertData = @json_decode($AlertJSON, true);
    if(is_array($AlertData)) { //Process alert
        if($AlertData['CheckResult']=='Failed') {
            //Process DOWN alert
            @mail('admin@yourdomain.com', $AlertData['MonitorName'].' is DOWN', $AlertJSON]);
            @file_put_contents('logs/ServiceUptime-alerts.log', $AlertData['DateTime'].':'.$AlertData['MonitorName']." is DOWN\n",FILE_APPEND);
        }
        else {
            //Process UP alert
            @file_put_contents('logs/ServiceUptime-alerts.log', $AlertData['DateTime'].':'.$AlertData['MonitorName']." is OK\n",FILE_APPEND);
        }
    }
}

Entering Your Webhook URL

Next, you have to tell the ServiceUptime system where to send the data.

1) Upgrade to Premium plan if you haven't already.


2) Click on "Monitors" in the top menu, click the button to add a new monitor or the link to edit an exiting one. Enter your Webhook URL in the 'Send Webhook Alert' field and save changes.


Troubleshooting:

Testing Your Webhook

If you'd like to verify that your webhooks are being sent and works correctly we recommend using a service like RequestBin: http://requestb.in

Proceed to that website and press the "Create a RequestBin" button. Copy the URL from the big text box at the top of the page.

Then, in your ServiceUptime control panel, specify the URL you copied as your webhook URL and send a test alert for one of your monitors.

Once you refresh the RequestBin page (the one where you copied the URL from the big text box), you'll see the raw JSON data as well as details about the webhook. These details should give you an idea of how you'll need to prepare your endpoint to listen for the ServiceUptime webhook.

After you've finished your testing, just remove the RequestBin URL from your ServiceUptime control panel, and replace it with your application's webhook receiver.

Sign Up Now!


I myself have just signed up to your service as a friend of mine had passed me onto this service and since being with this service I have found it to be a great asset as it will help determine if my website is down as I host others website also. So thank you ServiceUptime for bringing us a great service.

Brian L.