Skip to content

Instantly share code, notes, and snippets.

@hetrixtools
Last active January 5, 2021 20:29
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hetrixtools/3789e032af9224be2cdf49e557a7d484 to your computer and use it in GitHub Desktop.
Save hetrixtools/3789e032af9224be2cdf49e557a7d484 to your computer and use it in GitHub Desktop.
Proof of concept for the Uptime Monitor API v2 - Add/Edit/Delete Functions
<?php
// Your API Key
$api_key = 'APIKEY';
// Uncomment the API Call you wish to use below
///////////////////////////////////////////////////////////////////////////////
// Add Uptime Monitor
//$api_call = 'https://api.hetrixtools.com/v2/'.$api_key.'/uptime/add/';
// Edit Uptime Monitor
//$api_call = 'https://api.hetrixtools.com/v2/'.$api_key.'/uptime/edit/';
// Delete Uptime Monitor
//$api_call = 'https://api.hetrixtools.com/v2/'.$api_key.'/uptime/delete/';
///////////////////////////////////////////////////////////////////////////////
// Common Variables
$common = array(
'MID' => '', // Monitor ID to edit or delete. Needed when editing or deleting a monitor. Ignored when adding a monitor
'Type' => '', // Monitor Type. Accepted: 1 - Website, 2 - Ping/Service, 3 - SMTP
'Name' => '', // Monitor Name. Accepted: a-z, A-Z, 0-9, spaces, dots, dashes
'Target' => '', // Link for Website Uptime Monitors, or IP/Hostname for all other Uptime Monitor types
'Timeout' => 10, // Timeout. Accepted: 3, 5, 10, [15 is available just for the website type]. Suggested: 10
'Frequency' => 1, // Checkup frequency (minutes). Accepted: 1, 3, 5, 10. Suggested: 1
'FailsBeforeAlert' => 3, // Failed tries before issuing alert. Accepted: 1-3. Suggested: 3
'FailedLocations' => '', // [Optional] Failed locations before issuing alert. Accepted: 2-12. Suggested: 50%+1 of number of monitored locations. Leave empty for auto
'ContactList' => '', // [Optional] Contact List ID, leave empty for none
'Category' => '', // [Optional] Category for this monitor, leave empty for none
'AlertAfter' => '', // [Optional] Alert only after X minutes, leave empty to disable. Accepted: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 30, 40, 50, 60 [must be multiple of Frequency]
'RepeatTimes' => '', // [Optional] Repeat alert X times, leave empty to disable. Accepted: 0-30
'RepeatEvery' => '', // [Optional] Repeat alert every X minutes, leave empty to disable. Accepted: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 30, 40, 50, 60 [must be multiple of Frequency]
'Public' => false, // Privacy for this monitor's Uptime Report
'ShowTarget' => false, // Whether or not to show the monitored target in the uptime report
'VerSSLCert' => false, // Whether or not to verify the SSL certificate of the monitored target
'VerSSLHost' => false, // Whether or not to verify the SSL host of the monitored target
// Monitoring Locations
// Comment out (or set to false) the ones you do not wish to monitor from
// [min: 3, max: depends on your package(3,6,9,12)]
'Locations' => array(
'nyc' => true, // New York
'sfo' => true, // San Francisco
'dal' => true, // Dallas
'ams' => true, // Amsterdam
'lon' => true, // London
'fra' => true, // Frankfurt
'sgp' => true, // Singapore
'syd' => true, // Sydney
'sao' => true, // Sao Paulo
'tok' => true, // Tokyo
'mba' => true, // Mumbai
'msw' => true, // Moscow
)
);
// Specific Monitor Type Variables
$specific[1] = array( // Website Uptime Monitor
'Keyword' => '', // [Optional] Keyword to look for on the monitored website, leave empty for none. Max length: 128
'HTTPCodes' => '200', // [Optional] Specify the accepted HTTP codes (separated by comma). Your website will be considered online if any of these HTTP codes is returned.
'MaxRedirects' => '5', // Maximum number of redirects to follow. Accepted: 0-10
);
$specific[2] = array( // Ping/Service Uptime Monitor
'Port' => '', // [Optional] Port to be monitored, leave empty for PING. Accepted: 0-65535
);
$specific[3] = array( // SMTP Uptime Monitor
'Port' => '', // SMTP Port to be monitored. Accepted: 0-65535
'CheckAuth' => false, // Whether or not to check the SMTP authentication functionality
'SMTPUser' => '', // If CheckAuth is set to true, provide the SMTP Username
'SMTPPass' => '', // If CheckAuth is set to true, provide the SMTP Password
);
// Prepare JSON
$post = array_merge($common,$specific[$common['Type']]);
$post = json_encode($post);
// Make the API Call
$ch = curl_init();
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $api_call);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$data = curl_exec($ch);
curl_close($ch);
// Return the data
echo $data;
// Example returns
// {"status":"ERROR","error_message":"you are already monitoring this target"}
// {"status":"SUCCESS","monitor_id":"xyz","action":"added"}
// {"status":"SUCCESS","monitor_id":"xyz","action":"updated"}
// {"status":"SUCCESS","monitor_id":"xyz","action":"deleted"}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment