View Full Version : Check file size and email if different
DWZ
Fri 12th Dec '03, 11:01am
Hey,
I wanted to know if it would be possible to create some sort of script that will check the file size of two files on the server, and if the file size has changed since the last check, it would send an email to me.
So maybe write the current file sizes into some text file which it refers to each time it's run. Then it could be run on a cron every hour.
I was thinking the Database error file and admin cp failed logins file. This way I could set vBulletin to not send an email to me when there is a database error, as when a database error tends to happen, a lot of people see it, which results in a lot of emails. This way I would receive a maximum of one email a hour saying there is a problem and that I should checkout the error log(s).
What do you think?
Symen_4ab
Sun 21st Dec '03, 8:53pm
Ok, here's how I would do it :
// Put the filesize stored in the file in a variable
ob_start(); // Start output buffering
include ("filesize.txt"); // All output goes to buffer
$filesize = ob_get_contents(); // Assign buffer to a variable
ob_end_clean(); // Clear buffer and turn off output buffering
// Compare the filesize of the file to the one stored in the file
if ($filesize <> filesize(errorlogfilename)) {
// send email
mail("superadmin@domain,com", "This is the subject", "Hi there, how are you ?\n blablabla");
}
// Store the new filesize in the file
$file = fopen("filesize.txt", "w");
$txt = filesize(errorlogfilename);
fputs($file, $txt);
Warning : I'm not a php guru (far from that, actually).. This seems to work (at least on my intranet), but I think this could be optimized easily (output buffering is probably not needed, for an exemple).
edit : The file filesize.txt must exist prior the execution of this script (I do not want to put a if file exist ;) ) and must (probably) be chmodded to 777 (php-writable at least)
DWZ
Mon 22nd Dec '03, 1:50am
Thanks for that :)
I've been testing that code but can't get it to work. From what I can tell, the script looks at the file size of the error log, places the error log size into our .txt file, but will not send any email if the size has changed.
I had been playing around with the code and I'm currently using:<?php
# Email address email is sent to
$email = "dwz@ausforum.com";
# Email subject
$subject = "vBulletin Admin Login error";
# What's actually said in the email
$message = "Alan,\n\nThe vBulletin admin error log file size for AusForum has been increased since the last check.\n\nYou should probably go check on it (/home/ausforum/vBulletin/errors/admin-login-errors.log)\n\nRegards,\nAusForum Server (heh)";
# What is the name of the person sending this email email?
$fromname = "AusForum Server";
# What is the email address of the person sending this email?
$fromemail = "server@ausforum.com";
# File that the file size is kept in
$filesizelocation = "admin-login-errors-filesize.txt";
# Logfile where all the errors go location
$logfile = "admin-login-errors.log";
##########################################
// Put the filesize stored in the file in a variable
ob_start(); // Start output buffering
include ("$filesizelocation"); // All output goes to buffer
$filesize = ob_get_contents(); // Assign buffer to a variable
ob_end_clean(); // Clear buffer and turn off output buffering
// Compare the filesize of the file to the one stored in the file
if ($filesize <> filesize("$logfile")) {
// send email
mail($email,"$subject",
$message, "From:$fromname<$fromemail>");
}
// Store the new filesize in the file
$file = fopen("$filesizelocation", "w");
$txt = filesize("$logfile");
fputs($file, $txt);
?>Am I missing something? Thanks :)
Symen_4ab
Mon 22nd Dec '03, 11:50am
Hmm, the mail feature is actually the only thing I was not able to test :)
I tried to echo a text just before the mail should be sent, it worked (echoed the text..)
So there is a problem with the mail function we are using :(
mail("nobody@example.com", "the subject", $message,
"From: webmaster@{$_SERVER['SERVER_NAME']}\r\n"
."Reply-To: webmaster@{$_SERVER['SERVER_NAME']}\r\n"
."X-Mailer: PHP/" . phpversion());
That's example 2 (Sending mail with extra headers.) on php.net's mail function page (http://ch.php.net/manual/en/function.mail.php), give it a try...
Symen_4ab
Mon 22nd Dec '03, 2:19pm
A trivial fix, so the script doesn't write the size in the txt file if the filesize is the same then before..
Just edited the code to reflect the changes suggered by php.net (headers)
# What's actually said in the email
$message = "Alan,\n\nThe vBulletin admin error log file size for AusForum has been increased since the last check.\n\nYou should probably go check on it (/home/ausforum/vBulletin/errors/admin-login-errors.log)\n\nRegards,\nAusForum Server (heh)";
# File that the file size is kept in
$filesizelocation = "admin-login-errors-filesize.txt";
# Logfile where all the errors go location
$logfile = "admin-login-errors.log";
##########################################
// Put the filesize stored in the file in a variable
ob_start(); // Start output buffering
include ("$filesizelocation"); // All output goes to buffer
$filesize = ob_get_contents(); // Assign buffer to a variable
ob_end_clean(); // Clear buffer and turn off output buffering
// Compare the filesize of the file to the one stored in the file
if ($filesize <> filesize("$logfile")) {
// send email
mail("dwz@ausforum.com", "vBulletin Admin Login error", $message,
"From: server@{$_SERVER['SERVER_NAME']}\r\n"
."Reply-To: server@{$_SERVER['SERVER_NAME']}\r\n"
."X-Mailer: PHP/" . phpversion());
// Store the new filesize in the file
$file = fopen("$filesizelocation", "w");
$txt = filesize("$logfile");
fputs($file, $txt);
}
DWZ
Mon 22nd Dec '03, 9:09pm
Still nothing :\
But I just noticed that it is now not updating the file size in the .txt file - The only thing I noticed different between the two examples of code is that in the first one we had "}" after the send email command, but now it's after the update file size command. I moved it back but it still didn't work :\
Symen_4ab
Mon 22nd Dec '03, 9:33pm
Still nothing :\
But I just noticed that it is now not updating the file size in the .txt file - The only thing I noticed different between the two examples of code is that in the first one we had "}" after the send email command, but now it's after the update file size command. I moved it back but it still didn't work :\
True, I just moved the } ending the if function, so it only updates the txt file if the filesize is different.
It doesn't update the size anymore ? I don't see where the problem is, sorry...
Are you sure the txt file is chmodded to 777 ?
For the mail thing, I know there are many problems when sending through php, as it was often used for generating spams. At least, in the "from:blabla@domain.dom" domain.com must be the domain where php is running on.
I'm sorry, I can't help further, my php knowledge base is going just this far :(
Hope you'll be able to do something with this, I also hope a gentle php-guru will come here ;)
edit : For debugging purposes, just put the following line :
echo "<br />Trying to mail !<br />";
After the "// send email" line, so it should echo the message if it detects that the filesize has changed.
DWZ
Mon 22nd Dec '03, 9:45pm
Sorry, my mistake. It looks like it *is* updating the file size. I think my FTP client is stuffed...
I just tried the echo code both after it is supposed to send the email and after it is supposed to be updating the .txt file. When the file size is different, it is reporting both events happening - and then showing nothing if the file size is the same (the way it should :)). But yes, it still does not want to send any emails :\
I have a small PHP script that sends me an email every time it is run, it uses:
mail($email,"Unauthorized access attempt to AusForum",
$message, "From:AusForum Server<server@ausforum.com>");t works without any problems (this is the same server) - so I don't really know what's happening...
Thanks for all your help Symen :)
Symen_4ab
Mon 22nd Dec '03, 11:34pm
You're welcome ;)
I cannot find an explaination for the mail function not working. If it's working in another script, it should work on this one...
Well, I can only wish you good luck with this one :/
sidhighwind
Tue 23rd Dec '03, 5:54pm
i would check your error_log to see if anything is going on with it. It will spit somthing out and let you know. Just do a cat error_log | grep "PHP" and it will pull up any php error's or warning's. Just a thought.
DWZ
Wed 24th Dec '03, 8:39am
Wait, hang on a moment - My inbox just got like 50 of the emails sent out by the script when I have not run it for ages, and no one else knows of the script location - maybe it's a problem with my mail server?
I'll contact my host.
Symen_4ab
Wed 24th Dec '03, 10:25am
Yes, seems there is a problem with the mail server...
Strange as the destination mailbox is on the same server as the script, it could not be realying problems...
As usual, good luck ;)
vBulletin® v3.8.0 Release Candidate 1, Copyright ©2000-2008, Jelsoft Enterprises Ltd.