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!


Website uptime is of critical importance to my business. I run several websites, all of which have some form of ecommerce running on them 24/7. If my site goes offline, I NEED to know about it as soon as possible. With ServiceUptime monthly service my company has the peace of mind, knowing that we will be alerted to any downtime on any of our sites and services.

John Westrbook - www.LifeEmpowered.com