Showing posts with label shell script. Show all posts
Showing posts with label shell script. Show all posts

Tuesday, July 10, 2018

Create your own OVH Server availability checker

Recently I came to know that OVH has a line of storage specific "So You Start"  (SyS) ARM based servers. I have been using a Kimsufi server, but some of these SyS servers are cheaper than the Kimsufi server I have been using. Also the storage available in SyS servers is larger than what I get from the Kimsufi Server for a slightly higher price. Therefore I decided to try a SyS ARM server (ARM-2T/1801armada01)

Unfortunately SyS ARM servers are not always available to order, specially the cheapest servers. I was refreshing their page regularly to order a server, but soon I understood that's not practical. I didn't see they become available in the web page even though I tried many times for several days.

Then I decided to automate the check so that I can order a server whenever they are available.

Fortunately OVH offers a REST API to communicate with their servers and services. It's easier to check the availability of a server sending a http request to their API. Using a bunch of commands, the availability can be checked using a one line Linux command.

Just checking the availability is not enough. There should be a way to notify the availability. I could setup a script to send the availability to an email easily, but I don't read emails often. The best way of doing that is to push a message to my mobile. I found a free service called pushme (pushme.jagcesar.se) which can be used to push a message to my mobile. It also use a REST API, so I can send a message as a http POST request to pushme servers. Then pushme forwards the message to the app installed in the mobile via iCloud.

I created a simple shell script to read the server code as an argument and used it to check the availability of that server type using the OVH REST API. If a server is available, it sends a message to pushme using pushme REST API.

Then the script was scheduled to run for every 5 minutes using a cron job so that I'll get a notification whenever a server is available.

I also created another script to send a test message in every day morning to verify that the availability checker mechanism works.

After a day of implementing this, I was able to purchase the server I wanted. :)
Hope this might help you to find a server as well.
 
Avaiability Checker. It worked!

Screenshot of the notifications I received about 1801armada01 availability

But note that pushme works only with Apple. you will need another message sender if you use Android. Also this is a very simple and lazy way of doing this. No errors are checked in the script and only happy path is considered.

Here are the shell scripts and crons I used,

server_checker.sh

#!/bin/bash

server_code=$1
pushmeapi_token='replace_this_with_your_pushmeapi_token'

availability_count=`curl -s "https://www.ovh.com/engine/api/dedicated/server/availabilities?country=eu&hardware=$server_code" | json_pp | grep availability | grep -v unavailable | wc -l`

if [ $availability_count -gt 0 ]; then
 curl -d "title=found a $server_code&token=$pushmeapi_token" -X POST  https://pushmeapi.jagcesar.se
fi



ping_sender.sh

#!/bin/bash

pushmeapi_token='replace_this_with_your_pushmeapi_token'

curl -d "title=test message&token=$pushmeapi_token" -X POST  https://pushmeapi.jagcesar.se



Cron Jobs

*/5 * * * * /home/seedbox/server_checker.sh 1801sk13
*/5 * * * * /home/seedbox/server_checker.sh 1801armada01
30 5 * * * /home/seedbox/ping_sender.sh


You can find out the server code from either their web page (look at the source of the web page) or using their API.

Examples:

In https://www.soyoustart.com/us/server-storage/ page, when you click ARM-2T, the server code is there at the end of the URL as 1801armada01

below command will provide the list of ARM server types,

curl -s "https://www.ovh.com/engine/api/dedicated/server/availabilities?country=eu" | json_pp | grep hardware | grep -v , | sort | uniq | grep armada

below command will provide the list of kimsufi server types,

curl -s "https://www.ovh.com/engine/api/dedicated/server/availabilities?country=eu" | json_pp | grep hardware | grep -v , | sort | uniq | grep sk



Anyway, here is a list of machine types of kimsufi servers and SyS ARM servers,

Kimsufi Servers

France
KS-1    1801sk12
KS-2    1801sk13
KS-3    1801sk14
KS-4    1801sk15
KS-5    1801sk16
KS-6    1801sk17
KS-7    1801sk18
KS-8    1801sk19
KS-9    1801sk20
KS-10    1801sk21
KS-11    1801sk22
KS-12    1801sk23

Canada
KS-1    1804sk12
KS-2    1804sk13
KS-3    1804sk14
KS-4    1804sk15
KS-5    1804sk16
KS-6    1804sk17
KS-7    1804sk18
KS-8    1804sk19
KS-9    1804sk20
KS-10    1804sk21
KS-11    1804sk22
KS-12    1804sk23

Sys ARM servers

France
ARM-2T    1801armada01
ARM-4T    1801armada02
ARM-6T    1801armada03

Canada
ARM-2T    1804armada01
ARM-4T    1804armada02
ARM-6T    1804armada03

Saturday, February 28, 2015

Fix /opt/etc/init.d scripts not executing issue in LS VL Buffalo linkstation

I noticed that the transmission-daemon start up script (S95transmission.sh) added to /opt/etc/init.d/ directory does not get executed when the linkstation is started up.

Let's look at how startup scripts put in to /opt/etc/init.d/ directory are invoked.

1. /etc/init.d/rcS invokes /etc/init.d/rc.optware script. These are the last few lines in the rcS script.

/usr/local/bin/hdd_raid_syncspeed.sh
            
# Optware setup
[ -x /etc/init.d/rc.optware ] && /etc/init.d/rc.optware start
                                                     
exit 0 

2. Then /etc/init.d/rc.optware script mounts /opt partition and invokes /opt/etc/rc.optware script.

case "$1" in
    start)
        echo "Starting Optware."
        if test -n "${REAL_OPT_DIR}"; then
            if ! grep ' /opt ' /proc/mounts >/dev/null 2>&1 ; then
                mkdir -p /opt
                mount -o bind ${REAL_OPT_DIR} /opt
            fi
        fi
[ -x /opt/etc/rc.optware ] && /opt/etc/rc.optware
    ;;
    reconfig)


3. This /opt/etc/rc.optware script simply executes scripts matches with S??*  located in /opt/etc/init.d/ directory.

#!/bin/sh

# Start all init scripts in /opt/etc/init.d
# executing them in numerical order.
#
for i in /opt/etc/init.d/S??* ;do

# Ignore dangling symlinks (if any).
[ ! -f "$i" ] && continue

case "$i" in
  *.sh)
# Source shell script for speed.
(
trap - INT QUIT TSTP
set start
. $i
)


So after some investigations I have found out that rcS script didn't execute it's last few lines. It seemed it exited just after executing /usr/local/bin/hdd_raid_syncspeed.sh line. The simple solution was to move this line to the bottom of the script so that the optware setup is executed first. Now the startup script added to the /opt/etc/rc.optware is started whenever the linkstation is rebooted. :)

The last few lines of rcS after the modification,

#/usr/local/bin/hdd_raid_syncspeed.sh
            
# Optware setup
[ -x /etc/init.d/rc.optware ] && /etc/init.d/rc.optware start
                                         
/usr/local/bin/hdd_raid_syncspeed.sh
                        

exit 0