Nagios plugin to verify a FTP site, by uploading a file and testing the time it takes.
check_ftp.php
#!/usr/bin/php
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_size = filesize($filea);
$file = fopen($filea, "r");
//echo ".Connecting to $url2 and looking for folder $dir sending file $file_name ... 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);
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_INFILE, $file);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($filea));
curl_setopt($ch, CURLOPT_FTPASCII, 1);
$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_upload']) ."n";
//echo "File_Sieze : " . ByteSize($file_size) ."n";
// If the upload size is differente the filesize, throws ERROR
if($file_size != $info['size_upload']) {
echo "CRITICAL - File .$file_name. was not uploaded, upload_size=" . ByteSize($info['size_upload']) . " ,file_size=" . ByteSize($file_size) . "!!n";
exit(2);
};
$time_taken = substr(round($info['total_time'],1),0,1);
$band = ByteSize($info['speed_upload']);
// 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_upload’]) .”) took ” . $time_taken . ” secs. and Bandwidth of ” . $band. ” |UploadTime=” . $time_taken . “;Bandwidth=” . $band . “n”;
exit(1);
} elseif ($time_taken > $warn) {
echo “WARNING – FTP to ” . $host . ” file ” . $file_name . “(” . ByteSize($info[‘size_upload’]) .”) took ” . $time_taken . ” secs. and Bandwidth of ” . $band. ” |UploadTime=” . $time_taken . “;Bandwidth=” . $band . “n”;
exit(2);
} else {
echo “OK – FTP to ” . $host . ” file ” . $file_name . “(” . ByteSize($info[‘size_upload’]) .”) took ” . $time_taken . ” secs. and Bandwidth of ” . $band. ” |UploadTime=” . $time_taken . “;Bandwidth=” . $band . “n”;
exit(0);
}
?>