20 Apr 2008

Public Webalizer stats for cPanel users

Posted by kilbot | Filed under: Programming

I like to give my hosting clients the ability to check their website statistics without having to log into cPanel. There is no native way to do this through WHM or cPanel so I have created a little Perl script to automatically make Webalizer stats public when a new account is created. If you would like to offer stats outside of cPanel, copy the following code into a file called postwwwacctuser and upload to your scripts folder, remembering to make the script executable. For more information on postwwwacctuser please see the cPanel/WHM Hooks page.

#!/usr/bin/perl
# You may need to change this path to /usr/local/bin/perl

print "\nRunning postwwwacct...";

# set up variables
my $user 	= $ARGV[0];
($name, $pass, $uid, $gid, $quota, $comment, $gcos, $dir, $shell, $expire) = getpwnam($user);
$statsdir 	= "/home/".$user."/public_html/stats";
$webalizer 	= "/home/".$user."/tmp/webalizer";
$tmpdir 	= "/home/".$user."/tmp";

if(eval 'symlink("","");', $@ eq '') {
	mkdir($webalizer, 0755) unless -d $webalizer;
	symlink($webalizer, $statsdir);
	chown($uid, $gid, $webalizer);
	chmod(0755, $tmpdir);
	chmod(0755, $webalizer);
	print "\nPublic stats added for ".$user;
}
else {
	print "\nPublic stats not added for ".$user;
}

exit;

You can then update existing accounts through the command line using:

/scripts/postwwwacctuser username

Where username is the account username you wish to update.

Please note: My knowledge of Perl is pretty basic, so use this script at your own risk. Please post any questions/improvements in the comments.