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