Custom Hook Runs but Does Not Work
AnsweredI have this hook that I need it to check current disk utilization % and if disk utilization is has reached a certain threshold %, the account creation process should fail. The disk check happens well and the hook is processed but its not aborting the account creation process even after correctly identifying that the disk threshold has been reached.
What am I missing to make it abort the account creation process?
#!/usr/bin/perl
package ServerUtilizationHook;
use strict;
use warnings;
use Cpanel::Logger;
use JSON;
my $logger = Cpanel::Logger->new();
my $disk_threshold = 10;
# Embed hook attributes alongside the action code.
sub describe {
my $hooks = [
{
'blocking' => 1,
'category' => 'Whostmgr',
'event' => 'Accounts::Create',
'stage' => 'pre',
'hook' => 'ServerUtilizationHook::check_disk_usage',
'exectype' => 'module',
},
];
return $hooks;
}
sub check_disk_usage {
# Get disk usage information for the /home partition
my $df_output = `df -h /home`;
my ($usage_percent) = $df_output =~ /(\d+)%/m;
my $status_code = 0;
my $error_message ="Account creation failed: /home partition is $usage_percent% full. Maximum allowed is $disk_threshold%.";
$logger->info("Checking available disk space before account creation...");
# Check if usage percentage is $disk_threshold% or more
if ($usage_percent > $disk_threshold ) {
my $status_code = 1900;
die $error_message;
}
return $status_code;
};
1;
-
If I recall correctly, the error message you have needs to start with "BAILOUT" when dying on blocking events in order to halt execution of the relevant hooked action. See here: https://api.docs.cpanel.net/guides/guide-to-standardized-hooks/guide-to-standardized-hooks-exceptions/
0 -
hey Andy, this indeed works. needed to include BAILOUT in the phrase. thanks a lot for the assist.
0 -
One more thing, you also need to return a status code of 0 when you want the blocking to happen. so when the condition fails, make sure the my $status_code = 0; is set in the above code.
i got this from this repohttps://github.com/CpanelInc/standard_hooks_examples/blob/master/PasswordLimitations.pm
0
Please sign in to leave a comment.
Comments
3 comments