2019-02-25 08:48:22 -05:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2013-07-23 03:12:38 -04:00
|
|
|
|
2014-05-25 10:23:35 -04:00
|
|
|
#include "icinga/compatutility.hpp"
|
|
|
|
|
#include "icinga/checkcommand.hpp"
|
|
|
|
|
#include "icinga/eventcommand.hpp"
|
2016-08-15 08:39:33 -04:00
|
|
|
#include "icinga/notificationcommand.hpp"
|
2014-05-25 10:23:35 -04:00
|
|
|
#include "icinga/pluginutility.hpp"
|
|
|
|
|
#include "icinga/service.hpp"
|
|
|
|
|
#include "base/utility.hpp"
|
2015-08-15 14:28:05 -04:00
|
|
|
#include "base/configtype.hpp"
|
2014-05-25 10:23:35 -04:00
|
|
|
#include "base/objectlock.hpp"
|
|
|
|
|
#include "base/convert.hpp"
|
2013-07-30 16:38:33 -04:00
|
|
|
#include <boost/algorithm/string/replace.hpp>
|
2013-07-31 12:06:54 -04:00
|
|
|
#include <boost/algorithm/string/join.hpp>
|
2013-07-23 03:12:38 -04:00
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
2023-03-01 11:16:28 -05:00
|
|
|
/* Used in DB IDO and Livestatus. */
|
2013-12-05 08:54:16 -05:00
|
|
|
String CompatUtility::GetCommandLine(const Command::Ptr& command)
|
2013-08-02 08:12:07 -04:00
|
|
|
{
|
2013-12-05 08:54:16 -05:00
|
|
|
Value commandLine = command->GetCommandLine();
|
|
|
|
|
|
2013-12-17 04:20:28 -05:00
|
|
|
String result;
|
2013-12-05 08:54:16 -05:00
|
|
|
if (commandLine.IsObjectType<Array>()) {
|
|
|
|
|
Array::Ptr args = commandLine;
|
|
|
|
|
|
|
|
|
|
ObjectLock olock(args);
|
2023-03-31 06:36:37 -04:00
|
|
|
for (String arg : args) {
|
2013-12-05 08:54:16 -05:00
|
|
|
// This is obviously incorrect for non-trivial cases.
|
2013-12-17 04:20:28 -05:00
|
|
|
result += " \"" + EscapeString(arg) + "\"";
|
2013-12-05 08:54:16 -05:00
|
|
|
}
|
|
|
|
|
} else if (!commandLine.IsEmpty()) {
|
2013-12-17 04:20:28 -05:00
|
|
|
result = EscapeString(Convert::ToString(commandLine));
|
2013-12-05 08:54:16 -05:00
|
|
|
} else {
|
2013-12-17 04:20:28 -05:00
|
|
|
result = "<internal>";
|
2013-12-05 08:54:16 -05:00
|
|
|
}
|
|
|
|
|
|
2013-12-17 04:20:28 -05:00
|
|
|
return result;
|
2013-12-05 08:54:16 -05:00
|
|
|
}
|
2013-08-02 08:12:07 -04:00
|
|
|
|
2018-01-04 02:54:18 -05:00
|
|
|
String CompatUtility::GetCommandNamePrefix(const Command::Ptr& command)
|
2017-12-07 05:29:39 -05:00
|
|
|
/* Helper. */
|
2014-05-12 08:30:15 -04:00
|
|
|
{
|
|
|
|
|
if (!command)
|
|
|
|
|
return Empty;
|
|
|
|
|
|
|
|
|
|
String prefix;
|
2016-08-15 08:39:33 -04:00
|
|
|
if (command->GetReflectionType() == CheckCommand::TypeInstance)
|
2014-05-12 08:30:15 -04:00
|
|
|
prefix = "check_";
|
2016-08-15 08:39:33 -04:00
|
|
|
else if (command->GetReflectionType() == NotificationCommand::TypeInstance)
|
2014-05-12 08:30:15 -04:00
|
|
|
prefix = "notification_";
|
2016-08-15 08:39:33 -04:00
|
|
|
else if (command->GetReflectionType() == EventCommand::TypeInstance)
|
2014-05-12 08:30:15 -04:00
|
|
|
prefix = "event_";
|
|
|
|
|
|
|
|
|
|
return prefix;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-04 02:54:18 -05:00
|
|
|
String CompatUtility::GetCommandName(const Command::Ptr& command)
|
2023-03-01 11:16:28 -05:00
|
|
|
/* Used in DB IDO and Livestatus. */
|
2014-05-12 08:30:15 -04:00
|
|
|
{
|
|
|
|
|
if (!command)
|
|
|
|
|
return Empty;
|
|
|
|
|
|
|
|
|
|
return GetCommandNamePrefix(command) + command->GetName();
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-01 11:16:28 -05:00
|
|
|
/* Used in DB IDO and Livestatus. */
|
2014-05-02 21:18:37 -04:00
|
|
|
String CompatUtility::GetCheckableCommandArgs(const Checkable::Ptr& checkable)
|
|
|
|
|
{
|
|
|
|
|
CheckCommand::Ptr command = checkable->GetCheckCommand();
|
|
|
|
|
|
|
|
|
|
if (command) {
|
|
|
|
|
Host::Ptr host;
|
|
|
|
|
Service::Ptr service;
|
|
|
|
|
tie(host, service) = GetHostService(checkable);
|
|
|
|
|
String command_line = GetCommandLine(command);
|
|
|
|
|
|
2022-08-26 10:42:43 -04:00
|
|
|
Dictionary::Ptr args = new Dictionary();
|
2014-05-02 21:18:37 -04:00
|
|
|
Dictionary::Ptr command_vars = command->GetVars();
|
|
|
|
|
|
|
|
|
|
if (command_vars) {
|
2014-11-07 03:26:44 -05:00
|
|
|
ObjectLock olock(command_vars);
|
2016-08-25 00:19:44 -04:00
|
|
|
for (const Dictionary::Pair& kv : command_vars) {
|
2014-05-02 21:18:37 -04:00
|
|
|
String macro = "$" + kv.first + "$"; // this is too simple
|
|
|
|
|
if (command_line.Contains(macro))
|
|
|
|
|
args->Set(kv.first, kv.second);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Dictionary::Ptr host_vars = host->GetVars();
|
|
|
|
|
|
|
|
|
|
if (host_vars) {
|
2014-11-07 03:26:44 -05:00
|
|
|
ObjectLock olock(host_vars);
|
2016-08-25 00:19:44 -04:00
|
|
|
for (const Dictionary::Pair& kv : host_vars) {
|
2014-05-02 21:18:37 -04:00
|
|
|
String macro = "$" + kv.first + "$"; // this is too simple
|
|
|
|
|
if (command_line.Contains(macro))
|
|
|
|
|
args->Set(kv.first, kv.second);
|
|
|
|
|
macro = "$host.vars." + kv.first + "$";
|
|
|
|
|
if (command_line.Contains(macro))
|
|
|
|
|
args->Set(kv.first, kv.second);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (service) {
|
|
|
|
|
Dictionary::Ptr service_vars = service->GetVars();
|
|
|
|
|
|
|
|
|
|
if (service_vars) {
|
2014-11-07 03:26:44 -05:00
|
|
|
ObjectLock olock(service_vars);
|
2016-08-25 00:19:44 -04:00
|
|
|
for (const Dictionary::Pair& kv : service_vars) {
|
2014-05-02 21:18:37 -04:00
|
|
|
String macro = "$" + kv.first + "$"; // this is too simple
|
|
|
|
|
if (command_line.Contains(macro))
|
|
|
|
|
args->Set(kv.first, kv.second);
|
|
|
|
|
macro = "$service.vars." + kv.first + "$";
|
|
|
|
|
if (command_line.Contains(macro))
|
|
|
|
|
args->Set(kv.first, kv.second);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String arg_string;
|
2014-11-07 03:26:44 -05:00
|
|
|
ObjectLock olock(args);
|
2016-08-25 00:19:44 -04:00
|
|
|
for (const Dictionary::Pair& kv : args) {
|
2014-05-04 10:49:24 -04:00
|
|
|
arg_string += Convert::ToString(kv.first) + "=" + Convert::ToString(kv.second) + "!";
|
2014-05-02 21:18:37 -04:00
|
|
|
}
|
|
|
|
|
return arg_string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Empty;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-01 11:16:28 -05:00
|
|
|
/* Used in DB IDO and Livestatus. */
|
2014-04-03 09:36:13 -04:00
|
|
|
int CompatUtility::GetCheckableNotificationLastNotification(const Checkable::Ptr& checkable)
|
2013-12-05 08:54:16 -05:00
|
|
|
{
|
|
|
|
|
double last_notification = 0.0;
|
2016-08-25 00:19:44 -04:00
|
|
|
for (const Notification::Ptr& notification : checkable->GetNotifications()) {
|
2013-07-23 03:12:38 -04:00
|
|
|
if (notification->GetLastNotification() > last_notification)
|
|
|
|
|
last_notification = notification->GetLastNotification();
|
2013-12-05 08:54:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return static_cast<int>(last_notification);
|
|
|
|
|
}
|
2013-07-23 03:12:38 -04:00
|
|
|
|
2023-03-01 11:16:28 -05:00
|
|
|
/* Used in DB IDO and Livestatus. */
|
2014-04-03 09:36:13 -04:00
|
|
|
int CompatUtility::GetCheckableNotificationNextNotification(const Checkable::Ptr& checkable)
|
2013-12-05 08:54:16 -05:00
|
|
|
{
|
|
|
|
|
double next_notification = 0.0;
|
2016-08-25 00:19:44 -04:00
|
|
|
for (const Notification::Ptr& notification : checkable->GetNotifications()) {
|
2014-04-03 09:36:13 -04:00
|
|
|
if (next_notification == 0 || notification->GetNextNotification() < next_notification)
|
2013-07-23 03:12:38 -04:00
|
|
|
next_notification = notification->GetNextNotification();
|
2013-12-05 08:54:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return static_cast<int>(next_notification);
|
|
|
|
|
}
|
2013-07-23 03:12:38 -04:00
|
|
|
|
2023-03-01 11:16:28 -05:00
|
|
|
/* Used in DB IDO and Livestatus. */
|
2014-04-03 09:36:13 -04:00
|
|
|
int CompatUtility::GetCheckableNotificationNotificationNumber(const Checkable::Ptr& checkable)
|
2013-12-05 08:54:16 -05:00
|
|
|
{
|
|
|
|
|
int notification_number = 0;
|
2016-08-25 00:19:44 -04:00
|
|
|
for (const Notification::Ptr& notification : checkable->GetNotifications()) {
|
2013-07-23 03:12:38 -04:00
|
|
|
if (notification->GetNotificationNumber() > notification_number)
|
|
|
|
|
notification_number = notification->GetNotificationNumber();
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-05 08:54:16 -05:00
|
|
|
return notification_number;
|
|
|
|
|
}
|
2013-07-23 03:12:38 -04:00
|
|
|
|
2023-03-01 11:16:28 -05:00
|
|
|
/* Used in DB IDO and Livestatus. */
|
2014-04-03 09:36:13 -04:00
|
|
|
double CompatUtility::GetCheckableNotificationNotificationInterval(const Checkable::Ptr& checkable)
|
2013-12-05 08:54:16 -05:00
|
|
|
{
|
2013-07-30 12:17:13 -04:00
|
|
|
double notification_interval = -1;
|
2013-07-31 12:06:54 -04:00
|
|
|
|
2016-08-25 00:19:44 -04:00
|
|
|
for (const Notification::Ptr& notification : checkable->GetNotifications()) {
|
2014-04-09 04:25:23 -04:00
|
|
|
if (notification_interval == -1 || notification->GetInterval() < notification_interval)
|
|
|
|
|
notification_interval = notification->GetInterval();
|
2013-12-05 08:54:16 -05:00
|
|
|
}
|
2013-07-31 12:06:54 -04:00
|
|
|
|
2013-12-05 08:54:16 -05:00
|
|
|
if (notification_interval == -1)
|
|
|
|
|
notification_interval = 60;
|
2013-07-31 12:06:54 -04:00
|
|
|
|
2013-12-05 08:54:16 -05:00
|
|
|
return notification_interval / 60.0;
|
|
|
|
|
}
|
2013-07-31 12:06:54 -04:00
|
|
|
|
2017-12-07 05:29:39 -05:00
|
|
|
/* Helper. */
|
2014-04-03 09:36:13 -04:00
|
|
|
int CompatUtility::GetCheckableNotificationTypeFilter(const Checkable::Ptr& checkable)
|
2013-12-05 08:54:16 -05:00
|
|
|
{
|
|
|
|
|
unsigned long notification_type_filter = 0;
|
2013-07-30 12:17:13 -04:00
|
|
|
|
2016-08-25 00:19:44 -04:00
|
|
|
for (const Notification::Ptr& notification : checkable->GetNotifications()) {
|
2013-12-05 08:54:16 -05:00
|
|
|
ObjectLock olock(notification);
|
|
|
|
|
|
2016-08-14 10:58:46 -04:00
|
|
|
notification_type_filter |= notification->GetTypeFilter();
|
2013-12-05 08:54:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return notification_type_filter;
|
2013-07-30 12:17:13 -04:00
|
|
|
}
|
|
|
|
|
|
2017-12-07 05:29:39 -05:00
|
|
|
/* Helper. */
|
2014-04-03 09:36:13 -04:00
|
|
|
int CompatUtility::GetCheckableNotificationStateFilter(const Checkable::Ptr& checkable)
|
2013-08-01 07:20:30 -04:00
|
|
|
{
|
2013-12-05 08:54:16 -05:00
|
|
|
unsigned long notification_state_filter = 0;
|
2013-08-01 07:20:30 -04:00
|
|
|
|
2016-08-25 00:19:44 -04:00
|
|
|
for (const Notification::Ptr& notification : checkable->GetNotifications()) {
|
2013-12-05 08:54:16 -05:00
|
|
|
ObjectLock olock(notification);
|
2013-08-01 07:20:30 -04:00
|
|
|
|
2016-08-14 10:58:46 -04:00
|
|
|
notification_state_filter |= notification->GetStateFilter();
|
2013-08-01 07:20:30 -04:00
|
|
|
}
|
|
|
|
|
|
2013-12-05 08:54:16 -05:00
|
|
|
return notification_state_filter;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-01 11:16:28 -05:00
|
|
|
/* Used in DB IDO and Livestatus. */
|
2014-04-03 09:36:13 -04:00
|
|
|
std::set<User::Ptr> CompatUtility::GetCheckableNotificationUsers(const Checkable::Ptr& checkable)
|
2013-10-01 09:37:50 -04:00
|
|
|
{
|
|
|
|
|
/* Service -> Notifications -> (Users + UserGroups -> Users) */
|
|
|
|
|
std::set<User::Ptr> allUsers;
|
|
|
|
|
std::set<User::Ptr> users;
|
|
|
|
|
|
2016-08-25 00:19:44 -04:00
|
|
|
for (const Notification::Ptr& notification : checkable->GetNotifications()) {
|
2013-10-01 09:37:50 -04:00
|
|
|
ObjectLock olock(notification);
|
|
|
|
|
|
|
|
|
|
users = notification->GetUsers();
|
|
|
|
|
|
|
|
|
|
std::copy(users.begin(), users.end(), std::inserter(allUsers, allUsers.begin()));
|
|
|
|
|
|
2016-08-25 00:19:44 -04:00
|
|
|
for (const UserGroup::Ptr& ug : notification->GetUserGroups()) {
|
2013-10-01 09:37:50 -04:00
|
|
|
std::set<User::Ptr> members = ug->GetMembers();
|
|
|
|
|
std::copy(members.begin(), members.end(), std::inserter(allUsers, allUsers.begin()));
|
2013-11-29 06:28:17 -05:00
|
|
|
}
|
|
|
|
|
}
|
2013-10-01 09:37:50 -04:00
|
|
|
|
2013-10-02 15:09:13 -04:00
|
|
|
return allUsers;
|
2013-10-01 09:37:50 -04:00
|
|
|
}
|
|
|
|
|
|
2023-03-01 11:16:28 -05:00
|
|
|
/* Used in DB IDO and Livestatus. */
|
2014-04-03 09:36:13 -04:00
|
|
|
std::set<UserGroup::Ptr> CompatUtility::GetCheckableNotificationUserGroups(const Checkable::Ptr& checkable)
|
2013-10-01 09:37:50 -04:00
|
|
|
{
|
2013-10-02 15:09:13 -04:00
|
|
|
std::set<UserGroup::Ptr> usergroups;
|
2013-10-01 09:37:50 -04:00
|
|
|
/* Service -> Notifications -> UserGroups */
|
2016-08-25 00:19:44 -04:00
|
|
|
for (const Notification::Ptr& notification : checkable->GetNotifications()) {
|
2013-10-01 09:37:50 -04:00
|
|
|
ObjectLock olock(notification);
|
|
|
|
|
|
2016-08-25 00:19:44 -04:00
|
|
|
for (const UserGroup::Ptr& ug : notification->GetUserGroups()) {
|
2013-10-02 15:09:13 -04:00
|
|
|
usergroups.insert(ug);
|
2013-11-29 06:28:17 -05:00
|
|
|
}
|
|
|
|
|
}
|
2013-10-01 09:37:50 -04:00
|
|
|
|
2013-10-02 15:09:13 -04:00
|
|
|
return usergroups;
|
2013-10-01 09:37:50 -04:00
|
|
|
}
|
|
|
|
|
|
2023-03-01 11:16:28 -05:00
|
|
|
/* Used in DB IDO, Livestatus, CompatLogger, GelfWriter, IcingaDB. */
|
2013-12-05 08:54:16 -05:00
|
|
|
String CompatUtility::GetCheckResultOutput(const CheckResult::Ptr& cr)
|
2013-09-24 12:48:24 -04:00
|
|
|
{
|
|
|
|
|
if (!cr)
|
2013-12-05 08:54:16 -05:00
|
|
|
return Empty;
|
2013-09-24 12:48:24 -04:00
|
|
|
|
|
|
|
|
String output;
|
|
|
|
|
|
2013-11-09 08:22:38 -05:00
|
|
|
String raw_output = cr->GetOutput();
|
2013-10-31 09:18:20 -04:00
|
|
|
|
2013-09-24 12:48:24 -04:00
|
|
|
size_t line_end = raw_output.Find("\n");
|
|
|
|
|
|
2013-12-05 08:54:16 -05:00
|
|
|
return raw_output.SubStr(0, line_end);
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-01 11:16:28 -05:00
|
|
|
/* Used in DB IDO, Livestatus and IcingaDB. */
|
2013-12-05 08:54:16 -05:00
|
|
|
String CompatUtility::GetCheckResultLongOutput(const CheckResult::Ptr& cr)
|
|
|
|
|
{
|
|
|
|
|
if (!cr)
|
|
|
|
|
return Empty;
|
|
|
|
|
|
|
|
|
|
String long_output;
|
|
|
|
|
String output;
|
|
|
|
|
|
|
|
|
|
String raw_output = cr->GetOutput();
|
|
|
|
|
|
|
|
|
|
size_t line_end = raw_output.Find("\n");
|
2013-09-24 12:48:24 -04:00
|
|
|
|
|
|
|
|
if (line_end > 0 && line_end != String::NPos) {
|
|
|
|
|
long_output = raw_output.SubStr(line_end+1, raw_output.GetLength());
|
2013-12-05 08:54:16 -05:00
|
|
|
return EscapeString(long_output);
|
2013-09-24 12:48:24 -04:00
|
|
|
}
|
|
|
|
|
|
2013-12-05 08:54:16 -05:00
|
|
|
return Empty;
|
2013-09-24 12:48:24 -04:00
|
|
|
}
|
|
|
|
|
|
2023-03-01 11:16:28 -05:00
|
|
|
/* Helper for DB IDO and Livestatus. */
|
2013-09-30 14:25:53 -04:00
|
|
|
String CompatUtility::EscapeString(const String& str)
|
|
|
|
|
{
|
|
|
|
|
String result = str;
|
|
|
|
|
boost::algorithm::replace_all(result, "\n", "\\n");
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-01 11:04:11 -05:00
|
|
|
/* Used in ExternalCommandListener. */
|
2015-05-27 10:05:10 -04:00
|
|
|
String CompatUtility::UnEscapeString(const String& str)
|
|
|
|
|
{
|
|
|
|
|
String result = str;
|
|
|
|
|
boost::algorithm::replace_all(result, "\\n", "\n");
|
|
|
|
|
return result;
|
|
|
|
|
}
|