This is the very simple script I currently use to check SoYouStart availability. The script also use MandrillApp for notification email sending. You will need to setup the cron to run every minutes for script running. This script is adapted from the one at LowEndTalk community.
Setting up Environment
- Install PHP: Google it 😛
- Install Composer:
[bash]curl -s http://getcomposer.org/installer | php
mv composer.phar /usr/sbin/composer[/bash] - Install Mandrill apps dependency
- Create composer.json file with the following content:
[bash]{
"require": {
"mandrill/mandrill": "1.0.*"
}
}[/bash] - Install dependencies:
[bash]composer install[/bash]
- Create composer.json file with the following content:
The script
Create a script called check-availability.php as follows (I check 3 SYS packages in BHS location. You can replace the data center, soyoustart package in the script as well):
[php]
<?php
define(‘CHECK_URL’, ‘http://ws.ovh.com/dedicated/r2/ws.dispatcher/getAvailability2’);
define(‘NOTIFICATION_EMAILS’, ‘[email protected]’);
define(‘MANDRILL_API’, ‘YOUR_MANDRILL_APP_API’);
define(‘TEMP_PREVIOUS_MSG_FILE’, dirname(__FILE__). ‘/tmp/sys-avail-cache.txt’);
$f = file_get_contents(CHECK_URL);
$a = json_decode($f);
$avail = $a->answer->availability;
$str_avai = ”;
$zone_to_check = array(‘142sys4′,’143sys4′,’143sys1′,’142sys5′); //Replace whatever you want
foreach($avail as $s) {
if( in_array($s->reference, $zone_to_check)) {
$z = $s->zones;
foreach($z as $zone) {
if($zone->availability!==’unavailable’ && strtolower($zone->zone) == ‘bhs’) {
$str_avai .= $s->reference . " is " . $zone->availability . " in " . $zone->zone . "\n";
}
}
}
}
//– write to cache file to avoid repeated notifications
$previous_message = @file_get_contents(TEMP_PREVIOUS_MSG_FILE);
if ($str_avai != ”){
if ($str_avai != $previous_message){
if (defined(NOTIFICATION_EMAILS) && NOTIFICATION_EMAILS != ”){
$emails = explode(‘,’,NOTIFICATION_EMAILS);
foreach ($emails as $email)
sendNotificationEmail($email, $str_avai);
}
$ff = fopen(TEMP_PREVIOUS_MSG_FILE, "w");
fwrite($ff, $str_avai);
fclose($ff);
}
}else {
$ff = fopen(TEMP_PREVIOUS_MSG_FILE, "w");
fwrite($ff, ‘-‘);
fclose($ff);
}
/**
* Send email via MANDRILL APP
*/
function sendNotificationEmail($to, $msg){
require dirname(__FILE__) . ‘/vendor/autoload.php’;
$mandrill = new Mandrill(MANDRILL_API);
$date = new DateTime(‘now’, new DateTimeZone(‘Asia/Ho_Chi_Minh’)); // replace your timezone here
$message = new stdClass();
$message->text = $msg;
$message->subject = $date->format(‘Y-m-d H:i:s’) . "SoYouStart Availability";
$message->from_email = "notification@YOUR_DOMAIN.COM";
$message->from_name = "ANYONE";
$message->to = array(array("email" => $to));
$message->track_opens = true;
$response = $mandrill->messages->send($message);
return ;
}
[/php]
Setup cron to run it minutely and you will be done.
2 thoughts on “Script to check SoYouStart availability”
The script didn’t work for me.
You need to change the line 42: “if (!empty(NOTIFICATION_EMAILS))” => ” if (defined(NOTIFICATION_EMAILS))
Function “empty” is for variables, not for constants.
Also you’ll need to create the tmp directory to storage the cache file.
Ups…thank you for the script !! 😉