I needed to check if operation was happening.
The doubt was, Is the FTP files being copied over every day to the server?
To be sure I wrote this PHP script to check.
It goes into the FTP server and check for a folder with the date of today.
check_ftp_dir.php
#!/usr/bin/php

$folder = date(“Y”) . date(“m”) . date(“d”);
// Handle arguments passed: “;
if($argc <= 4){ echo "nPlease provide args , example: n check_ftp_dir.php ftp.check.com anonymous pass folder1010 n n”;
exit(1);
} else {
$host = “ftp://” . $argv[1];
$userpass = $argv[2] . “:” . $argv[3];
$dir = $argv[4];
// overwrite with my preset custom values, In my case I want today’s date
$dir=$folder;
}
// Example “ftp://user:pwd@ftp.server.com:21”);
$ftp_url=”ftp://” . $argv[2] . “:” . $argv[3] . “@” . $argv[1] . “:21”;
//echo “n Connecting to $ftp_url and looking for folder $dir … n”;
$curl = curl_init();
curl_setopt ($curl, CURLOPT_URL, “$ftp_url”);
curl_setopt($curl, CURLOPT_PROXY, “proxy”); // CASE WE NEED PROXY
curl_setopt($curl, CURLOPT_PROXYPORT, “8080”);
// curl_setopt ($curl, CURLOPT_USERPWD, “$userpass”);
curl_setopt ($curl, CURLOPT_TIMEOUT, 15);
curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FTPLISTONLY, 1);
$return = trim(curl_exec($curl));
$files = explode(“n”, $return);
if(curl_error($curl)) {
echo “UNKOWN – Error to connect to ftp on $host – Error: ” . curl_error($curl) . “n”;
exit(3);
}
curl_close ($curl);
// Go thru all lines and search for the folder
foreach ($files as $line)
{
// echo gettype($line) . “n ->> ” . $line . “n”;
if (strpos($line,$dir)) {
echo “OK – Folder $dir found on $host. n”;
exit(0);
}
}
//Case it dir not found throw critical and exit
echo “CRITICAL – Folder $dir not found on $host. n”;
exit(2);
?>

Tags: , , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *