Commit version 24.12.13800

This commit is contained in:
2025-01-06 17:35:06 -05:00
parent b7f6a79c2c
commit 55d9218816
6133 changed files with 4239740 additions and 1374287 deletions

View File

@ -5,9 +5,9 @@
*
* This file is part of Observium.
*
* @package observium
* @subpackage alerting
* @copyright (C) 2006-2013 Adam Armstrong, (C) 2013-2015 Observium Limited
* @package observium
* @subpackage alerting
* @copyright (C) Adam Armstrong
*
*/

View File

@ -6,87 +6,83 @@
*
* @package observium
* @subpackage alerting
* @copyright (C) 2006-2013 Adam Armstrong, (C) 2013-2020 Observium Limited
* @copyright (C) Adam Armstrong
*
*/
// FIXME: This is fairly messy and crude. Feel free to improve it!
// Slack only params
switch($message_tags['ALERT_STATE'])
{
case "RECOVER":
$color = "2850816";
break;
case "SYSLOG":
if ($message_tags['ALERT_STATE'] === "SYSLOG") {
$color = "8410368";
break;
default:
} elseif ($message_tags['ALERT_STATE'] === "RECOVER") {
$color = "2850816";
} elseif (str_contains($message_tags['ALERT_STATE'], "REMINDER")) {
$color = "8388651";
} else {
$color = "8388651";
}
$emoji = ':' . $message_tags['ALERT_EMOJI_NAME'] . ':';
// JSON data
$data = [
// "username" => $endpoint['username'],
// 'icon_emoji' => $emoji,
//"text" => $title,
//"text" => $title,
];
if (isset($endpoint['short']) && $endpoint['short'] === 'true')
{
// Short format
$data['embeds'][] = [
'title' => $message_tags['TITLE'],
'url' => $message_tags['ALERT_URL'],
'color' => $color
];
if (isset($endpoint['short']) && $endpoint['short'] === 'true') {
// Short format
$data['embeds'][] = [
'title' => $message_tags['TITLE'],
'url' => $message_tags['ALERT_URL'],
'color' => $color
];
} else {
$data['embeds'][] = [
'title' => $emoji.' '.$message_tags['TITLE'],
'url' => $message_tags['ALERT_URL'],
'color' => $color,
//'text' => simple_template('slack_text.tpl', $message_tags, array('is_file' => TRUE)),
'fields' => [
[
'name' => 'Device/Location',
'value' => $message_tags['DEVICE_HOSTNAME'] . " (" . $message_tags['DEVICE_OS'] . ")" . PHP_EOL . $message_tags['DEVICE_LOCATION'],
'inline' => TRUE,
],
[
'name' => 'Entity',
'url' => $message_tags['ENTITY_URL'],
'value' => $message_tags['ENTITY_TYPE'] . " / " . $message_tags['ENTITY_NAME'] .
(isset($message_tags['ENTITY_DESCRIPTION']) ? PHP_EOL . $message_tags['ENTITY_DESCRIPTION'] : ''),
$data['embeds'][] = [
'title' => $emoji . ' ' . $message_tags['TITLE'],
'url' => $message_tags['ALERT_URL'],
'color' => $color,
//'text' => simple_template('slack_text.tpl', $message_tags, array('is_file' => TRUE)),
'fields' => [
[
'name' => 'Device/Location',
'value' => $message_tags['DEVICE_HOSTNAME'] . " (" . $message_tags['DEVICE_OS'] . ")" . PHP_EOL . $message_tags['DEVICE_LOCATION'],
'inline' => TRUE,
],
[
'name' => 'Alert Message/Duration',
'value' => $message_tags['ALERT_MESSAGE'] . PHP_EOL . $message_tags['DURATION'],
],
[
'name' => 'Entity',
'url' => $message_tags['ENTITY_URL'],
'value' => $message_tags['ENTITY_TYPE'] . " / " . $message_tags['ENTITY_NAME'] .
(isset($message_tags['ENTITY_DESCRIPTION']) ? PHP_EOL . $message_tags['ENTITY_DESCRIPTION'] : ''),
'inline' => TRUE,
],
[
'name' => 'Alert Message/Duration',
'value' => $message_tags['ALERT_MESSAGE'] . PHP_EOL . $message_tags['DURATION'],
//'inline' => TRUE,
],
[
'name' => 'Metrics',
'value' => str_replace(" ", "", $message_tags['METRICS']),
],
[
'name' => 'Metrics',
'value' => str_replace(" ", "", $message_tags['METRICS']),
//'inline' => TRUE,
],
],
],
];
];
/*
foreach ($graphs as $graph)
{
$data['attachments'][] = array('fallback' => "Graph Image",
'title' => $graph['label'],
'image_url' => $graph['url'],
'color' => 'danger');
/*
foreach ($graphs as $graph)
{
$data['attachments'][] = array('fallback' => "Graph Image",
'title' => $graph['label'],
'image_url' => $graph['url'],
'color' => 'danger');
}
*/
}
*/
}

View File

@ -6,97 +6,120 @@
*
* @package observium
* @subpackage alerting
* @copyright (C) 2006-2013 Adam Armstrong, (C) 2013-2021 Observium Limited
* @copyright (C) Adam Armstrong
*
*/
// Find local hostname
$localhost = get_localhost();
$emails = [];
$cfg = $GLOBALS['config']['email'];
$emails = [];
$mail_params = [];
$emails[$endpoint['email']] = $endpoint['contact_descr'];
// Mail backend params
$backend = strtolower(trim($config['email']['backend']));
switch ($backend)
{
case 'sendmail':
$mail_params['sendmail_path'] = $config['email']['sendmail_path'];
break;
case 'smtp':
$mail_params['host'] = $config['email']['smtp_host'];
$mail_params['port'] = $config['email']['smtp_port'];
if ($config['email']['smtp_secure'] === 'ssl')
{
$mail_params['host'] = 'ssl://'.$config['email']['smtp_host'];
if ($config['email']['smtp_port'] == 25)
{
$mail_params['port'] = 465; // Default port for SSL
}
}
$mail_params['timeout'] = $config['email']['smtp_timeout'];
$mail_params['auth'] = $config['email']['smtp_auth'];
$mail_params['username'] = $config['email']['smtp_username'];
$mail_params['password'] = $config['email']['smtp_password'];
$mail_params['localhost'] = $localhost;
if (OBS_DEBUG) { $mail_params['debug'] = TRUE; }
break;
case 'smtpmx':
case 'mx':
$mail_params['mailname'] = $localhost;
$mail_params['netdns'] = FALSE;
$backend = strtolower(trim($cfg['backend']));
switch ($backend) {
case 'sendmail':
$sendmail_exec = explode(' ', $cfg['sendmail_path'])[0];
if (empty($cfg['sendmail_path']) || !is_executable($sendmail_exec)) {
$notify_status['success'] = FALSE;
$notify_status['error'] = "Sendmail exec ($sendmail_exec) is not found or not executable";
return;
}
$mail_params['sendmail_path'] = $cfg['sendmail_path'];
break;
$mail_params['timeout'] = $config['email']['smtp_timeout'];
if (OBS_DEBUG) { $mail_params['debug'] = TRUE; }
case 'smtp':
$mail_params['host'] = $cfg['smtp_host'];
$mail_params['port'] = $cfg['smtp_port'];
if ($cfg['smtp_secure'] === 'ssl') {
$mail_params['host'] = 'ssl://' . $cfg['smtp_host'];
if ($cfg['smtp_port'] == 25) {
$mail_params['port'] = 465; // Default port for SSL
}
}
$mail_params['timeout'] = $cfg['smtp_timeout'];
$mail_params['auth'] = $cfg['smtp_auth'];
$mail_params['username'] = $cfg['smtp_username'];
$mail_params['password'] = $cfg['smtp_password'];
$mail_params['localhost'] = $localhost;
if (OBS_DEBUG) {
$mail_params['debug'] = TRUE;
}
break;
$backend = 'smtpmx';
break;
case 'mail':
default:
$backend = 'mail'; // Default mailer backend
case 'smtpmx':
case 'mx':
$mail_params['mailname'] = $localhost;
$mail_params['netdns'] = FALSE;
$mail_params['timeout'] = $cfg['smtp_timeout'];
if (OBS_DEBUG) {
$mail_params['debug'] = TRUE;
}
$backend = 'smtpmx';
break;
case 'mail':
default:
$sendmail_path = ini_get('sendmail_path');
$sendmail_exec = explode(' ', $sendmail_path)[0];
if (empty($sendmail_path) || !is_executable($sendmail_exec)) {
if (!empty($cfg['sendmail_path']) && is_executable(explode(' ', $cfg['sendmail_path'])[0])) {
// Try set from our config
//$mail_params['sendmail_path'] = $cfg['sendmail_path'];
ini_set('sendmail_path', $cfg['sendmail_path']);
} else {
$notify_status['success'] = FALSE;
$notify_status['error'] = "mail() exec ($sendmail_exec) is not found or not executable";
return;
}
}
$backend = 'mail'; // Default mailer backend
}
// Time sent RFC 2822
$time_rfc = date('r', time());
// Mail headers
$headers = array();
if (empty($config['email']['from']))
{
// Default "From:"
$headers['From'] = 'Observium <observium@'.$localhost.'>';
$headers['Return-Path'] = 'observium@'.$localhost;
$headers = [];
if (empty($cfg['from'])) {
// Default "From:"
$headers['From'] = 'Observium <observium@' . $localhost . '>';
$headers['Return-Path'] = 'observium@' . $localhost;
} else {
// Validate configured mail from
foreach (parse_email($config['email']['from']) as $from => $from_name)
{
$headers['From'] = (empty($from_name) ? $from : '"' . $from_name . '" <' . $from . '>'); // From:
$headers['Return-Path'] = $from;
break; // use only first entry
}
// Validate configured mail from
foreach(parse_email($cfg['from']) as $from => $from_name) {
$headers['From'] = (empty($from_name) ? $from : '"' . $from_name . '" <' . $from . '>'); // From:
$headers['Return-Path'] = $from;
break; // use only first entry
}
}
$rcpts = array();
$rcpts_full = array();
foreach ($emails as $to => $to_name)
{
$rcpts_full[] = (empty($to_name) ? $to : '"'.trim($to_name).'" <'.$to.'>');
$rcpts[] = $to;
$rcpts = [];
$rcpts_full = [];
foreach ($emails as $to => $to_name) {
$rcpts_full[] = (empty($to_name) ? $to : '"' . trim($to_name) . '" <' . $to . '>');
$rcpts[] = $to;
}
$rcpts_full = implode(', ', $rcpts_full);
$rcpts = implode(', ', $rcpts);
$headers['To'] = $rcpts_full; // To:
$headers['Subject'] = $message_tags['TITLE']; // Subject:
$headers['To'] = $rcpts_full; // To:
$headers['Subject'] = $message_tags['TITLE']; // Subject:
// ID and Date, leave it before X- headers
$headers['Message-ID'] = '<' . md5(uniqid(time())) . '@' . $localhost . '>';
$headers['Date'] = $time_rfc;
$headers['Message-ID'] = '<' . md5(uniqid(time(), TRUE)) . '@' . $localhost . '>';
$headers['Date'] = $time_rfc;
$headers['X-Priority'] = 3; // Mail priority
$headers['X-Mailer'] = OBSERVIUM_PRODUCT.' '.OBSERVIUM_VERSION; // X-Mailer:
$headers['X-Priority'] = 3; // Mail priority
$headers['X-Mailer'] = OBSERVIUM_PRODUCT . ' ' . OBSERVIUM_VERSION; // X-Mailer:
// Mail autogenerated, suppress autorespond by any issue system
$headers['Precedence'] = 'bulk';
@ -106,15 +129,20 @@ $headers['X-Auto-Response-Suppress'] = 'All';
$time_sent = $time_rfc;
// Creating the Mime message
$mime = new Mail_mime(array('head_charset' => 'utf-8',
'text_charset' => 'utf-8',
'html_charset' => 'utf-8',
'eol' => PHP_EOL));
$mime = new Mail_mime(['head_charset' => 'utf-8',
'text_charset' => 'utf-8',
'html_charset' => 'utf-8',
'eol' => PHP_EOL]);
// Mail text footer
$message_tags['FOOTER'] = "\n\nE-mail sent to: $rcpts\n" .
"E-mail sent at: $time_sent\n\n" .
"-- \n" . OBSERVIUM_PRODUCT_LONG . ' ' . OBSERVIUM_VERSION . "\n" . OBSERVIUM_URL . "\n";
// Generate Mail text in both plain text and html
$message['text'] = simple_template('email_text', $message_tags, array('is_file' => TRUE));
$message['text'] = simple_template('email_text', $message_tags, [ 'is_file' => TRUE ]);
$message_tags_html = $message_tags;
$message_tags_html = $message_tags;
$message_tags_html['CONDITIONS'] = nl2br(escape_html($message_tags['CONDITIONS']));
$message_tags_html['METRICS'] = nl2br(escape_html($message_tags['METRICS']));
@ -122,67 +150,55 @@ $message_tags_html['METRICS'] = nl2br(escape_html($message_tags['METRICS']));
//$graphs = json_decode($message_tags_html['ENTITY_GRAPHS_ARRAY'], TRUE);
$graphs = $message_tags_html['ENTITY_GRAPHS_ARRAY'];
if (is_array($graphs) && count($graphs))
{
$message_tags_html['ENTITY_GRAPHS'] = ''; // Reinit graphs html
foreach ($graphs as $key => $graph)
{
$cid = generate_random_string(16);
// Unencode data uri tag to file content
list($gmime, $base64) = explode(';', $graph['data'], 2);
$gmime = substr($gmime, 5);
$base64 = substr($base64, 7);
//print_vars(substr($graph['data'], 0, 20));
//print_vars($gmime);
//print_vars(substr($base64, 0, 20));
$mime->addHTMLImage(base64_decode($base64), $gmime, $cid.'.png', FALSE, $cid);
if (is_array($graphs) && count($graphs)) {
$message_tags_html['ENTITY_GRAPHS'] = ''; // Reinit graphs html
foreach ($graphs as $key => $graph) {
$cid = random_string(16);
// Unencode data uri tag to file content
[ $gmime, $base64 ] = explode(';', $graph['data'], 2);
$gmime = substr($gmime, 5);
$base64 = substr($base64, 7);
//print_vars(substr($graph['data'], 0, 20));
//print_vars($gmime);
//print_vars(substr($base64, 0, 20));
$mime->addHTMLImage(base64_decode($base64), $gmime, $cid . '.png', FALSE, $cid);
$message_tags_html['ENTITY_GRAPHS'] .= '<h4>'.$graph['type'].'</h4>';
$message_tags_html['ENTITY_GRAPHS'] .= '<a href="'.$graph['url'].'"><img src="cid:'.$cid.'"></a><br />';
}
$message_tags_html['ENTITY_GRAPHS'] .= '<h4>' . $graph['type'] . '</h4>';
$message_tags_html['ENTITY_GRAPHS'] .= '<a href="' . $graph['url'] . '"><img src="cid:' . $cid . '"></a><br />';
}
}
//print_vars($message_tags_html);
// Mail html footer
$message_tags_html['FOOTER'] = "\n<br /><p style=\"font-size: 11px;\">E-mail sent to: $rcpts<br />\n" .
"E-mail sent at: $time_sent</p>\n" .
'<div style="font-size: 11px; color: #999;">-- <br /><a href="' .
OBSERVIUM_URL . '">' . OBSERVIUM_PRODUCT_LONG . ' ' . OBSERVIUM_VERSION . "</a></div>\n";
$message['html'] = simple_template('email_html', $message_tags_html, array('is_file' => TRUE));
$message['html'] = simple_template('email_html', $message_tags_html, [ 'is_file' => TRUE ]);
unset($message_tags_html);
foreach ($message as $part => $part_body)
{
switch ($part)
{
case 'text':
case 'txt':
case 'plain':
$part_body .= "\n\nE-mail sent to: $rcpts\n";
$part_body .= "E-mail sent at: $time_sent\n\n";
$part_body .= "-- \n" . OBSERVIUM_PRODUCT_LONG . ' ' . OBSERVIUM_VERSION . "\n" . OBSERVIUM_URL . "\n";
$mime->setTXTBody($part_body);
break;
case 'html':
$part_footer = "\n<br /><p style=\"font-size: 11px;\">E-mail sent to: $rcpts<br />\n";
$part_footer .= "E-mail sent at: $time_sent</p>\n";
$part_footer .= '<div style="font-size: 11px; color: #999;">-- <br /><a href="'.OBSERVIUM_URL.'">'.OBSERVIUM_PRODUCT_LONG.' '.OBSERVIUM_VERSION."</a></div>\n";
if (stripos($part_body, '</body>'))
{
$part_body = str_ireplace('</body>', $part_footer.'</body>', $part_body);
} else {
$part_body .= $part_footer;
}
$mime->setHTMLBody($part_body);
break;
//case 'image':
// break;
//case 'attachment':
// break;
}
foreach ($message as $part => $part_body) {
switch ($part) {
case 'text':
case 'txt':
case 'plain':
$mime->setTXTBody($part_body);
break;
case 'html':
$mime->setHTMLBody($part_body);
break;
//case 'image':
// break;
//case 'attachment':
// break;
}
}
$body = $mime->get();
// Prepare headers
foreach ($headers as $name => $value) {
$headers[$name] = $mime->encodeHeader($name, $value, 'utf-8', 'quoted-printable');
$headers[$name] = $mime->encodeHeader($name, $value, 'utf-8', 'quoted-printable');
}
$headers = $mime->headers($headers);
//var_dump($headers);
@ -193,11 +209,11 @@ $mail = Mail::factory($backend, $mail_params);
$status = $mail->send($rcpts, $headers, $body);
if (PEAR::isError($status)) {
//print_message('%rMailer Error%n: ' . $status->getMessage(), 'color');
$notify_status['success'] = FALSE;
$notify_status['error'] = $status->getMessage();
//print_message('%rMailer Error%n: ' . $status->getMessage(), 'color');
$notify_status['success'] = FALSE;
$notify_status['error'] = $status->getMessage();
} else {
$notify_status['success'] = TRUE;
$notify_status['success'] = TRUE;
}
unset($message);

View File

@ -1,5 +1,4 @@
<?php
/**
* Observium
*
@ -7,33 +6,40 @@
*
* @package observium
* @subpackage alerting
* @copyright (C) 2006-2013 Adam Armstrong, (C) 2013-2019 Observium Limited
* @copyright (C) Adam Armstrong
*
*/
$message_keys = array_keys($message_tags);
// Export all tags for external program usage
foreach (array_keys($message_tags) as $key)
{
putenv("OBSERVIUM_$key=" . $message_tags[$key]);
$unescape = [];
foreach ($message_keys as $key) {
putenv("OBSERVIUM_$key=" . $message_tags[$key]);
$unescape['from'][] = '\$OBSERVIUM_' . $key;
$unescape['to'][] = '$OBSERVIUM_' . $key;
}
// Clean script from injections and
// Revert back OBSERVIUM variables from escaping
// I.e.: "\$OBSERVIUM_TITLE \$OBSERVIUM_TIMESTAMP \$OBSERVIUM_DURATION"
$script_cmd = str_replace($unescape['from'], $unescape['to'], escapeshellcmd($endpoint['script']));
// Execute given script
external_exec($endpoint['script']);
external_exec($script_cmd, $exec_status);
// If script's exit code is 0, success. Otherwise we mark it as failed.
if ($GLOBALS['exec_status']['exitcode'] == 0)
{
$notify_status['success'] = TRUE;
// If script's exit code is 0, success. Otherwise, we mark it as failed.
if ($exec_status['exitcode'] === 0) {
$notify_status['success'] = TRUE;
} else {
$notify_status['success'] = FALSE;
$notify_status['success'] = FALSE;
}
// Clean out all set environment variable we set before execution
foreach (array_keys($message_tags) as $key)
{
putenv("OBSERVIUM_$key");
// Clean out all set environment variables we set before execution
foreach ($message_keys as $key) {
putenv("OBSERVIUM_$key");
}
unset($message, $output, $exitcode);
unset($exec_status, $unescape, $script_cmd);
// EOF

View File

@ -1,16 +1,17 @@
<?php
/**
* Observium
*
* This file is part of Observium.
*
* @package observium
* @subpackage alerting
* @copyright (C) 2006-2013 Adam Armstrong, (C) 2013-2019 Observium Limited
* @package observium
* @subpackage alerting
* @copyright (C) Adam Armstrong
*
*/
// CLEANME. Who still use it?
$message = $message_tags['TITLE'] . ' [' . $message_tags['ALERT_URL'] . ']' . PHP_EOL;
$message .= str_replace(" ", "", $message_tags['METRICS']);
@ -18,74 +19,70 @@ use Fabiang\Xmpp\Options;
use Fabiang\Xmpp\Client;
use Fabiang\Xmpp\Protocol\Message;
if (isset($endpoint['server']))
{
// Set server hostname if specified by user
$hostname = $endpoint['server'];
} else {
// Find server by SRV record, if we have an @ in our login username
if (strstr($endpoint['username'], '@') !== FALSE)
{
list(,$xmppdomain) = explode('@', $endpoint['username'], 2);
if (isset($endpoint['server'])) {
// Set server hostname if specified by user
$hostname = $endpoint['server'];
} elseif (str_contains($endpoint['username'], '@')) {
[, $xmppdomain] = explode('@', $endpoint['username'], 2);
$resolver = new Net_DNS2_Resolver();
$maxprio = -1;
// Find and use highest priority server only. Could be improved to cycle if there are multiple?
try
{
$response = $resolver->query("_xmpp-client._tcp.$xmppdomain", 'SRV', 'IN');
if ($response)
{
foreach ($response->answer as $answer)
{
if ($answer->priority > $maxprio)
{
$hostname = $answer->target;
}
try {
$response = $resolver->query("_xmpp-client._tcp.$xmppdomain", 'SRV');
if ($response) {
foreach ($response->answer as $answer) {
if ($answer->priority > $maxprio) {
$hostname = $answer->target;
}
}
}
}
} catch (Exception $e) { print_debug("Error while resolving: " . $e->getMessage()); } // Continue when error resolving
}
} catch (Exception $e) {
print_debug("Error while resolving: " . $e->getMessage());
} // Continue when error resolving
}
if ($hostname != '')
{
// Default to port to 5222 unless specified by endpoint data
$port = ($endpoint['port'] ? $endpoint['port'] : 5222);
if (empty($hostname)) {
// reason: Could not determine server hostname!
$notify_status['success'] = FALSE;
unset($message);
list($username,$xmppdomain) = explode('@',$endpoint['username']); // Username is only the part before @
$password = $endpoint['password'];
return;
}
$options = new Options("tcp://$hostname:$port");
$options->setUsername($username);
$options->setPassword($password);
// Default to port to 5222 unless specified by endpoint data
$port = $endpoint['port'] ?: 5222;
list($rusername,$rxmppdomain ) = explode('@',$endpoint['recipient']);
if ($rxmppdomain != '') { $options->setTo($rxmppdomain); } // Set destination domain to the recipient's part after the @
[ $username, $xmppdomain ] = explode('@', $endpoint['username']); // Username is only the part before @
$password = $endpoint['password'];
$client = new Client($options);
$options = new Options("tcp://$hostname:$port");
$options->setUsername($username);
$options->setPassword($password);
try
{
[ $rusername, $rxmppdomain ] = explode('@', $endpoint['recipient']);
if ($rxmppdomain != '') {
$options->setTo($rxmppdomain);
} // Set destination domain to the recipient's part after the @
$client = new Client($options);
try {
$client->connect();
$xmessage = new Message;
$xmessage->setMessage($message);
$xmessage->setTo($endpoint['recipient']);
$client->send($xmessage);
$client->send($xmessage);
$client->disconnect();
$notify_status['success'] = TRUE;
} catch (Exception $e) {
} catch (Exception $e) {
// reason: $e->getMessage()
$notify_status['success'] = FALSE;
}
} else {
// reason: Could not determine server hostname!
$notify_status['success'] = FALSE;
}
unset($message);