Run scripts before domlog rotates

JeffElkins

Member
Jan 20, 2018
5
0
1
US
cPanel Access Level
Root Administrator
I'm running a CentOS 6.9 VPS with cpanel/whm. Currently, my apache2 domlogs rotate daily. I need to run scripts that parse a domain log before it rotates.

I tried adding:

prerotate
/usr/local/bin/dstats > /dev/null 2>/dev/null || true
/usr/local/bin/dreport > /dev/null 2>/dev/null || true
endscript

to logrotate.d/httpd, but the scripts didn't appear to run. Investigating further, I see that logrotate.conf is set to rotate logs weekly.

What do I need to do to run these scripts before the domlog rotates?
 

JeffElkins

Member
Jan 20, 2018
5
0
1
US
cPanel Access Level
Root Administrator
Still no joy with this issue. As far as I can tell, the logrotate.conf does not control the daily rotation of the apache2/domlogs. Google has been zero help in solving this. Very frustrating, after hours of research.
 

cPanelMichael

Technical Support Community Manager
Staff member
Apr 11, 2011
47,903
2,226
363
cPanel Access Level
DataCenter Provider
Twitter
Hello,

The following option is available under the "Stats and Logs" tab in "WHM >> Tweak Settings":

"Delete each domain’s access logs after statistics are gathered"

Per it's description:

This setting will force the system to delete all access logs after each run.

Thus, you'd need to disable this option and rotate the Apache domlogs on your own, or setup a hook that runs your custom script "pre" log processing:

Guide to Standardized Hooks - Software Development Kit - cPanel Documentation
Guide to Standardized Hooks - Stats Functions - Software Development Kit - cPanel Documentation

Thank you.
 

JeffElkins

Member
Jan 20, 2018
5
0
1
US
cPanel Access Level
Root Administrator
I tried the code below, it appeared to register properly:

System:
Stats:
weight: 100
hook: MyStats::runscripts
exectype: module
stage: pre
id: KrW3DbP2pUlaz99VVnx7KWNe


However, calling the script (sub: runscripts) doesn't work. The script being called executes w/o issues manually.

#!/usr/bin/perl

# Package this module.
package MyStats;

# Return errors if Perl experiences problems.
use strict;
use warnings;

# Use cPanel's error logging module.
use Cpanel::Logger;

# Instantiate the cPanel logging object.
my $logger = Cpanel::Logger->new();

# Embed hook attributes alongside the action code.
sub describe {
my $hooks = [
{
'category' => 'System',
'event' => 'Stats',
'stage' => 'pre',
'hook' => 'MyStats::runscripts',
'exectype' => 'module',
}
];
return $hooks;
}

sub runscripts {
system("/bin/bash /usr/local/bin/dstats");
};
 

cPanelMichael

Technical Support Community Manager
Staff member
Apr 11, 2011
47,903
2,226
363
cPanel Access Level
DataCenter Provider
Twitter
Hello,

If you already have a script that works when you run it manually, you can simply add it to the hooks system using a command like this rather than setting up a separate custom script:

Code:
/usr/local/cpanel/bin/manage_hooks add script /var/cpanel/myapp/myscript.sh --manual --category System --event Stats --stage pre
Thank you.
 

cPanelMichael

Technical Support Community Manager
Staff member
Apr 11, 2011
47,903
2,226
363
cPanel Access Level
DataCenter Provider
Twitter
How do you delete a hook entered in this manner? I tried:

bin/manage_hooks del script /usr/local/bin/myscript, but no joy.
You'd use a command like this:

Code:
/usr/local/cpanel/bin/manage_hooks delete script /var/cpanel/myapp/myscript.sh --manual --category System --event Stats --stage pre
Or, simply use "WHM Home » Development » Manage Hooks".

We document this at:

Guide to Standardized Hooks - The manage_hooks Utility - Software Development Kit - cPanel Documentation

Thank you.