Home > BashScripts, Linux > check_ftp_download script

check_ftp_download script

December 10th, 2012 Leave a comment Go to comments

This is a script that tests the FTP site by downloading a specific file and verifying how long it takes to do so.
It also measures the bandwidth.
It uses curl PHP library to do the FTP part.

More of mine FTP related scripts

More Nagios FTP addons

script:

#!/usr/bin/php
<?php
// Script to connect to a FTP site and Download a file, return the time it took and compare to WARN and CRIT values
// Formated for Nagios usage
// Use a test user for the FTP as it could be simple to get this password
// I use a 5mb file, created by using: dd bs=1024 count=5120 skip=5120 if=/dev/sda of=file.tst
// by Felipe Ferreira http://felipeferreira.net

if($argc < 7){
echo “nPlease provide args <hostname> <user> <password> <ftpfolder> <filename> <warn> <crit>”;
echo “nExample: n check_ftp.php ftp.check.com anonymous pass /folder1010/ /tmp/file.txt 4 6 n n”;
exit(1);
}
$host =  $argv[1];
$userpass = $argv[2] . “:” . $argv[3];
$dir = $argv[4];
$filea  = $argv[5];
$warn = $argv[6];
$crit = $argv[7];

// ##################- FUNCTIONS -############33
function ByteSize($bytes)
{
$size = $bytes / 1024;
if($size < 1024)
{
$size = number_format($size, 2);
$size .= ‘ KB’;
}
else
{
if($size / 1024 < 1024)
{
$size = number_format($size / 1024, 2);
$size .= ‘ MB’;
}
else if ($size / 1024 / 1024 < 1024)
{
$size = number_format($size / 1024 / 1024, 2);
$size .= ‘ GB’;
}
}
return $size;
}

// ##############- END FUNCTIONS

$files = explode(“/”, $filea);
$file_name=$files[(count($files) -1 )];
// Example “ftp://user:pwd@ftp.server.com/test:21”);
$url2=$host .”:21″. $dir . $file_name;
$url=”ftp://” . $argv[2] . “:” . $argv[3] . “@” .  $url2;
$file = fopen($filea, “r”);
$file_down = “/tmp/$file_name”;
$file_size = filesize($file_down);
//echo “.Connecting  to $url and downloading $file_name to $file_down… n”;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FTP_USE_EPSV, FALSE);
//curl_setopt($ch, CURLOPT_VERBOSE, 1);
$fh = fopen($file_down, ‘w’) or die($php_errormsg);
curl_setopt($ch, CURLOPT_FILE, $fh);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);

if ($output === FALSE) {
echo “UNKOWN – cURL Error: ” . curl_error($ch);
exit(3);
}

//echo “Upload_Size: ” . ByteSize($info[‘size_download’]) .”n”;
//echo “File_Sieze : ” . ByteSize($file_size) .”n”;
//print_r($info);
// If the upload size is differente the filesize, throws ERROR

if($file_size != $info[‘size_download’]) {
echo “CRITICAL – File .$file_name. was not downloaded, download_size=” . ByteSize($info[‘size_download’]) . ” ,file_size=” . ByteSize($file_size) . “!!n”;
exit(2);
};

$time_taken = substr(round($info[‘total_time’],1),0,1);
$band = ByteSize($info[‘speed_download’]);

// CONVERT STRING TO INTEGER
$time_taken = (int)$time_taken;
$warn = (int)$warn;
$crit = (int)$crit;

if ($time_taken > $crit) {
echo “CRITICAL – FTP to ” . $host . ” file ” .  $file_name . “(” . ByteSize($info[‘size_download’]) .”) took ” . $time_taken . ” secs. and Bandwidth of ” . $band. ” |DownloadTime=
” . $time_taken .”n”;
exit(1);
} elseif ($time_taken > $warn) {
echo “WARNING – FTP to ” . $host . ” file ” .  $file_name . “(” . ByteSize($info[‘size_download’]) .”) took ” . $time_taken . ” secs. and Bandwidth of ” . $band. ” |DownloadTime=”
. $time_taken .”n”;
exit(2);
} else {
echo “OK – FTP to ” . $host . ” file ” .  $file_name . “(” . ByteSize($info[‘size_download’]) .”) took ” . $time_taken . ” secs. and Bandwidth of ” . $band. ” |DownloadTime=” . $time_taken . “n”;
exit(0);
}

?>

Categories: BashScripts, Linux Tags:
  1. Akam
    January 9th, 2015 at 20:15 | #1

    Hello,

    Thanks a lot for the script.
    Would you mind pointing me to a place where I can gather installation instructions in my Nagios 3? I haven’t dealt much with it.

    Thanks a lot,
    Akma

  1. No trackbacks yet.

VAMOVE *

*