2015-05-28 08:21:05 -04:00
|
|
|
<?php
|
2016-02-08 09:41:00 -05:00
|
|
|
/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */
|
2015-05-28 08:21:05 -04:00
|
|
|
|
|
|
|
|
namespace Icinga\Module\Monitoring\Object;
|
|
|
|
|
|
2015-10-05 07:18:10 -04:00
|
|
|
use Exception;
|
|
|
|
|
use Icinga\Application\Logger;
|
2023-08-14 09:02:26 -04:00
|
|
|
use stdClass;
|
2015-10-05 07:18:10 -04:00
|
|
|
|
2015-05-28 08:21:05 -04:00
|
|
|
/**
|
|
|
|
|
* Expand macros in string in the context of MonitoredObjects
|
|
|
|
|
*/
|
|
|
|
|
class Macro
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Known icinga macros
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
private static $icingaMacros = array(
|
2015-07-16 04:35:28 -04:00
|
|
|
'HOSTNAME' => 'host_name',
|
|
|
|
|
'HOSTADDRESS' => 'host_address',
|
|
|
|
|
'HOSTADDRESS6' => 'host_address6',
|
|
|
|
|
'SERVICEDESC' => 'service_description',
|
|
|
|
|
'host.name' => 'host_name',
|
|
|
|
|
'host.address' => 'host_address',
|
|
|
|
|
'host.address6' => 'host_address6',
|
2020-05-13 09:32:44 -04:00
|
|
|
'service.description' => 'service_description',
|
|
|
|
|
'service.name' => 'service_description'
|
2015-05-28 08:21:05 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return the given string with macros being resolved
|
|
|
|
|
*
|
|
|
|
|
* @param string $input The string in which to look for macros
|
|
|
|
|
* @param MonitoredObject|stdClass $object The host or service used to resolve macros
|
|
|
|
|
*
|
|
|
|
|
* @return string The substituted or unchanged string
|
|
|
|
|
*/
|
|
|
|
|
public static function resolveMacros($input, $object)
|
|
|
|
|
{
|
|
|
|
|
$matches = array();
|
|
|
|
|
if (preg_match_all('@\$([^\$\s]+)\$@', $input, $matches)) {
|
|
|
|
|
foreach ($matches[1] as $key => $value) {
|
|
|
|
|
$newValue = self::resolveMacro($value, $object);
|
|
|
|
|
if ($newValue !== $value) {
|
|
|
|
|
$input = str_replace($matches[0][$key], $newValue, $input);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $input;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Resolve a macro based on the given object
|
|
|
|
|
*
|
|
|
|
|
* @param string $macro The macro to resolve
|
|
|
|
|
* @param MonitoredObject|stdClass $object The object used to resolve the macro
|
|
|
|
|
*
|
|
|
|
|
* @return string The new value or the macro if it cannot be resolved
|
|
|
|
|
*/
|
|
|
|
|
public static function resolveMacro($macro, $object)
|
|
|
|
|
{
|
2024-03-25 09:27:46 -04:00
|
|
|
if (isset(self::$icingaMacros[$macro], $object->{self::$icingaMacros[$macro]})) {
|
2015-05-28 08:21:05 -04:00
|
|
|
return $object->{self::$icingaMacros[$macro]};
|
|
|
|
|
}
|
2015-10-05 07:18:10 -04:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$value = $object->$macro;
|
|
|
|
|
} catch (Exception $e) {
|
2024-03-25 09:20:04 -04:00
|
|
|
if ($object instanceof MonitoredObject) {
|
|
|
|
|
$objectName = $object instanceof Service
|
|
|
|
|
? $object->getHost()->getName() . '!' . $object->getName()
|
|
|
|
|
: $object->getName();
|
|
|
|
|
} else {
|
|
|
|
|
$host = 'host_name';
|
|
|
|
|
$service = 'service_description';
|
|
|
|
|
|
|
|
|
|
$objectName = isset($object->$service)
|
|
|
|
|
? $object->$service . '!' . $object->$host
|
|
|
|
|
: $object->$host;
|
2022-09-28 08:49:04 -04:00
|
|
|
}
|
|
|
|
|
|
2015-10-05 07:18:10 -04:00
|
|
|
$value = null;
|
2024-03-25 09:27:46 -04:00
|
|
|
Logger::debug(
|
|
|
|
|
'Unable to resolve macro "%s" on object "%s". An error occurred: %s',
|
|
|
|
|
$macro,
|
|
|
|
|
$objectName,
|
|
|
|
|
$e
|
|
|
|
|
);
|
2015-05-28 08:21:05 -04:00
|
|
|
}
|
|
|
|
|
|
2024-03-25 09:27:46 -04:00
|
|
|
return $value ?? $macro;
|
2015-05-28 08:21:05 -04:00
|
|
|
}
|
|
|
|
|
}
|