Skip to content
This repository has been archived by the owner on Dec 27, 2023. It is now read-only.

Commit

Permalink
feature(Tinebase/cli): monitoring mail server for/imap/smtp/sieve
Browse files Browse the repository at this point in the history
Change-Id: I99a005a7a4b82c5c080061929077584c23f8f827
  • Loading branch information
ccheng-dev committed Apr 23, 2021
1 parent 557e86d commit b771826
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
13 changes: 13 additions & 0 deletions tests/tine20/Tinebase/Frontend/CliTest.php
Expand Up @@ -382,6 +382,19 @@ public function testMonitoringCheckCache()
self::assertStringContainsString('CACHE ', $out);
self::assertLessThanOrEqual(1, $result);
}

/**
* testMonitoringMailServers
*/
public function testMonitoringMailServers()
{
ob_start();
$result = $this->_cli->monitoringMailServers();
$out = ob_get_clean();

self::assertStringContainsString('CONNECTION ', $out);
self::assertLessThanOrEqual(1, $result);
}

/**
* test cleanNotes
Expand Down
53 changes: 53 additions & 0 deletions tine20/Tinebase/Frontend/Cli.php
Expand Up @@ -1533,6 +1533,59 @@ public function monitoringCheckCache()
return $result;
}

/**
* nagios monitoring for mail servers
* imap/smtp/sieve
*
* @return integer
*
* @see http://nagiosplug.sourceforge.net/developer-guidelines.html#PLUGOUTPUT
*/
public function monitoringMailServers() {
$result = 0;
$servers = [
Tinebase_Config::SMTP,
Tinebase_Config::IMAP,
Tinebase_Config::SIEVE
];

$message = "\n";

foreach ($servers as $server) {
$serverConfig = Tinebase_Config::getInstance()->{$server};

$host = isset($serverConfig->{'hostname'}) ? $serverConfig->{'hostname'} : $serverConfig->{'host'};
$port = $serverConfig->{'port'};

$message .= $server . ' | host: '. $host . ' | port: ' . $port;

if (empty($host) || empty($port)) {
$message .= ' -> INVALID VALUE' . PHP_EOL;
$result = 2;
continue;
}

$output = shell_exec('nc -d -N -w3 ' . $host . ' ' . $port . PHP_EOL);

if ($output) {
if (strpos($output, 'OK') || strstr($output, '220')) {
$message .= ' -> CONNECTION OK' . PHP_EOL;
} else {
$message .= ' -> CONNECTION ERROR' . PHP_EOL;
$result = 1;
}

$message .= PHP_EOL . $output . PHP_EOL;
} else {
$message .= ' -> CONNECTION FAILED' . PHP_EOL;
$result = 2;
}
}

echo $message . "\n";
return $result;
}

/**
* undo changes to records defined by certain criteria (user, date, fields, ...)
*
Expand Down

0 comments on commit b771826

Please sign in to comment.