hosted by liquidweb


Go Back   Web Hosting Talk : Web Hosting Main Forums : Hosting Security and Technology : Hosting Security and Technology Tutorials : Howto: Determine mail statistics easily
Reply

Hosting Security and Technology Tutorials Tutorials related to server security or the like.
Forum Jump

Howto: Determine mail statistics easily

Reply Post New Thread In Hosting Security and Technology Tutorials Subscription
 
Send news tip View All Posts Thread Tools Search this Thread Display Modes
  #1  
Old 12-21-2006, 10:10 AM
linux-tech linux-tech is offline
<?require_once("life")?>
 
Join Date: Sep 2002
Location: inside your network
Posts: 9,548

Howto: Determine mail statistics easily


This is a very simple script that will take your mail statistics, and display them. Optionally, you can have them thrown into a db for viewing.

For the db (if you want it), create a db. Inside of that, create a table called mailstats, using the following (in phpmyadmin, or whatever you're comfortable with)
Code:
 CREATE TABLE `mailstats` (
`date` VARCHAR( 255 ) NOT NULL ,
`total` VARCHAR( 255 ) NOT NULL ,
`rejected` VARCHAR( 255 ) NOT NULL ,
`rbl` VARCHAR( 255 ) NOT NULL ,
`relay` VARCHAR( 255 ) NOT NULL ,
`SA` VARCHAR( 255 ) NOT NULL ,
`verify` VARCHAR( 255 ) NOT NULL ,
`lookup` VARCHAR( 255 ) NOT NULL ,
`dictionary` VARCHAR( 255 ) NOT NULL ,
PRIMARY KEY ( `date` )
) ENGINE = MYISAM CHARACTER SET latin1 COLLATE latin1_swedish_ci
This will create the necessary db, and is necessary ONLY if you're going to use the logging functions.

Now, for the php script: Save the following as whatever file you like (I use /bin/mailstats), make sure to chmod the file inside the shell:

Code:
chmod a+x /bin/mailstats
PHP Code:
#!/usr/local/bin/php
<?
//Uncomment the below line if you want to use the db portion of this
//connect_db();
print "Gathering mail stats\n";
//Make sure to change the logfiles if necessary. You should only change these two instances
$logfile="/var/log/exim_mainlog";
$rejectfile="/var/log/exim_rejectlog";
$mainacc=shell_exec("grep \"SMTP connection from\" $logfile -c");
$reject1 shell_exec("grep RBL $rejectfile -c");
$reject2 shell_exec("grep \"relay\" $rejectfile -c");
$reject3 shell_exec("grep \"score too high\" $rejectfile -c");
$reject4 shell_exec("grep \"Sender verify failed\" $rejectfile -c");
$reject5 shell_exec("grep \"host lookup did not complete\" $rejectfile -c");
$rejectotal shell_exec("grep rejected $rejectfile -c");
$dictionary shell_exec("grep \"dictionary attack\" $rejectfile -c");

print 
"Connections: $mainacc";
print 
"Rejected: $rejectotal";
print 
"RBL: $reject1";
print 
"Relay: $reject2";
print 
"SA Score: $reject3";
print 
"Sender Verify: $reject4";
print 
"Host Lookup: $reject5";
//Uncomment the below line if you want to use the db portion of this
//insert_stats($mainacc, $rejectotal,$reject1,$reject2,$reject3,$reject4,$reject5, $dictionary);
function insert_stats($mainacc,$rejected,$reject1,$reject2,$reject3,$reject4,$reject5,$dictionary)
$date=date('m-d-Y');
$query="SELECT date FROM mailstats WHERE date='$date'";
$result=mysql_query($query) or die(mysql_error());
$numrows=mysql_num_rows($result);
if (
$numrows 1)
{
$query="INSERT into mailstats VALUES('$date','$mainacc','$rejected','$reject1','$reject2','$reject3','$reject4','$reject5','$dictionary')";
$result=mysql_query($query) or die(mysql_error());
}
if (
$numrows 0)
{
$query="UPDATE mailstats SET
total='
$mainacc',
rejected='
$rejected',
rbl='
$reject1',
relay='
$reject2',
SA='
$reject3',
verify='
$reject4',
lookup='
$reject5',
dictionary='
$dictionary'
WHERE date='
$date'";
$result=mysql_query($query) or die(mysql_error());
}
}
function 
connect_db()
{
//fill out the db_name, $db_username, $db_pass fields if you want to use this
$db_name="";
$dbtype="MySQL";
$hostname="localhost";
$db_username="";
$db_password="";
$db_failure=mysql_error();
$dbh=mysql_connect ("$hostname""$db_username""$db_password") or die("DB Connection Problems");
mysql_select_db ("$db_name");
}
?>
Now, for the cron:
Code:
*/15 * * * * /bin/mailstats > /dev/null
Add that to your root crontab. This will update the db every 15 minutes. Only necessary (of course ) if you're using the db, otherwise, it's somewhat worthless.

This has been tested pretty extensively on 2 cpanel servers, no problems had. I'm in the process of developing something like this that will report on ALL domains, but that's going to take a bit more time.

Reply With Quote


Sponsored Links
Reply

Related posts from TheWhir.com
Title Type Date Posted
Yahoo Integrates Email Client with Dropbox Cloud Storage Web Hosting News 2013-04-04 14:31:40
SoftLayer - Right on Schedule - My Estimate $2.5 billion Blog 2013-03-19 05:45:58
Host1Plus Offers SmarterTools Email and Collaboration Services Web Hosting News 2012-06-08 15:06:38
cPanel Launches Version 11.32 of Control Panel and Web Host Manager Web Hosting News 2012-02-15 12:46:33
Email Provider OpenXchange Partners with Openwave Web Hosting News 2011-10-12 17:23:08


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes
Postbit Selector

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump
Login:
Log in with your username and password
Username:
Password:



Forgot Password?
Advertisement:
Web Hosting News:



 

X

Welcome to WebHostingTalk.com

Create your username to jump into the discussion!

WebHostingTalk.com is the largest, most influentual web hosting community on the Internet. Join us by filling in the form below.


(4 digit year)

Already a member?