Last active
April 20, 2020 07:49
-
-
Save cutzenfriend/0be0144724f757f60260091feda7ff02 to your computer and use it in GitHub Desktop.
Mailcow unlimited mailbox compared to domain space & vmail E-Mail alerts php script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/****************************************************************************************************************************/ | |
// USECASE: | |
// All my mailboxes are set up as unlimited because I want all mailboxes to share the available domian space. | |
// Problem -> Used space from unlimited mailboxes will not be calculated into domain usage so I need to monitor (with alert email) this myself and inform my customers if they hit their limit. | |
// With unlimited mailboxes it could be that the vmail storage runs full so I will monitor this too. | |
// Run this script as often as you want (e.g. once a day). | |
// Add --nomail parameter if you do not want to send E-Mail and just get the console output. (Just shows domain above treshold) | |
// CONFIG: | |
// Set Mailcow Host | |
$MAILCOW_HOST = "https://mailcow.host"; | |
// Set Mailcow API_KEY (read-only) | |
$API_KEY = "XXXX-XXXXX-XXXX-XXXXX"; | |
// Which mail to alert on threshold reach? | |
$ALERT_MAIL = "notify@e.mail"; | |
// Which mail should php sendmail use to send mail? | |
$SENDER_MAIL = "sender@e.mail"; | |
// At how much percentage of a Domain usage you want to be notified? | |
$ALERT_THRESHOLD_DOMAIN = 90; | |
// At how much percentage of VMAIL usage you want to be notified? | |
$ALERT_THRESHOLD_VMAIL = 90; | |
/****************************************************************************************************************************/ | |
$SEND_MAIL = true; | |
// Change Mail header if needed | |
$header[] = 'MIME-Version: 1.0'; | |
$header[] = 'Content-type: text/html; charset=utf-8'; | |
$header[] = 'From: Mailcow Monitor <'.$SENDER_MAIL.'>'; | |
if (isset($argv[1]) == "--nomail") { | |
$SEND_MAIL = false; | |
} | |
function isa_convert_bytes_to_specified($bytes, $to, $decimal_places = 1) { | |
$formulas = array( | |
'K' => number_format($bytes / 1024, $decimal_places), | |
'M' => number_format($bytes / 1048576, $decimal_places), | |
'G' => number_format($bytes / 1073741824, $decimal_places) | |
); | |
return isset($formulas[$to]) ? $formulas[$to] : 0; | |
} | |
$cURLConnection = curl_init(); | |
curl_setopt($cURLConnection, CURLOPT_URL, $MAILCOW_HOST.'/api/v1/get/domain/all'); | |
curl_setopt($cURLConnection, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($cURLConnection, CURLOPT_HTTPHEADER, array( | |
'Content-Type: application/json', | |
'X-API-Key: '.$API_KEY | |
)); | |
$domainList = curl_exec($cURLConnection); | |
curl_setopt($cURLConnection, CURLOPT_URL, $MAILCOW_HOST.'/api/v1/get/status/vmail'); | |
curl_setopt($cURLConnection, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($cURLConnection, CURLOPT_HTTPHEADER, array( | |
'Content-Type: application/json', | |
'X-API-Key: '.$API_KEY | |
)); | |
$vmailResponse = curl_exec($cURLConnection); | |
curl_close($cURLConnection); | |
$jsonArrayResponse = json_decode($domainList); | |
$jsonArrayResponseVmail = json_decode($vmailResponse); | |
foreach($jsonArrayResponse as $obj){ | |
if (round($obj->bytes_total/($obj->max_quota_for_domain/100), 2) > ($ALERT_THRESHOLD_DOMAIN - 1)) { | |
echo "Domain: ".$obj->domain_name."\n"; | |
echo "Allowed Size: ".isa_convert_bytes_to_specified($obj->max_quota_for_domain, 'G')." GB\n"; | |
echo "Used Size: ".isa_convert_bytes_to_specified($obj->bytes_total, 'G')." GB\n"; | |
echo "Percentage used: ".round($obj->bytes_total/($obj->max_quota_for_domain/100), 2)."% used\n\n"; | |
if ($SEND_MAIL) { | |
$subject = 'Mailcow Usage '.round($obj->bytes_total/($obj->max_quota_for_domain/100), 2).'% alert for: '.$obj->domain_name; | |
$message = ' | |
<html> | |
<body> | |
<p><strong>Mailcow Usage alert for '.$obj->domain_name.'</strong></p> | |
<p>'.round($obj->bytes_total/($obj->max_quota_for_domain/100), 2).'% ('.isa_convert_bytes_to_specified($obj->bytes_total, 'G').'GB) of '.isa_convert_bytes_to_specified($obj->max_quota_for_domain, 'G').'GB used.</p> | |
</body> | |
</html> | |
'; | |
mail($ALERT_MAIL, $subject, $message, implode("\r\n", $header)); | |
} | |
} | |
} | |
echo "Disk Usage: ".$jsonArrayResponseVmail->used."\n"; | |
echo "Used: ".$jsonArrayResponseVmail->used_percent." used of ".$jsonArrayResponseVmail->total."\n\n"; | |
if (intval($jsonArrayResponseVmail->used_percent) > $ALERT_THRESHOLD_VMAIL - 1 and $SEND_MAIL) { | |
$subject = 'Mailcow VMAIL Usage '.$jsonArrayResponseVmail->used_percent.' alert for: '.$MAILCOW_HOST; | |
$message = ' | |
<html> | |
<body> | |
<p><strong>Mailcow VMAIL Usage alert for '.$MAILCOW_HOST.'</strong></p> | |
<p>'.$jsonArrayResponseVmail->used_percent.' ('.$jsonArrayResponseVmail->used.') of '.$jsonArrayResponseVmail->total.' used.</p> | |
</body> | |
</html> | |
'; | |
mail($ALERT_MAIL, $subject, $message, implode("\r\n", $header)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment