2012-07-09 20:32:02 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2014-01-09 00:32:11 +01:00
|
|
|
* Copyright (C) 2012-present Icinga Development Team (http://www.icinga.org) *
|
2012-07-09 20:32:02 +02:00
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License *
|
|
|
|
* as published by the Free Software Foundation; either version 2 *
|
|
|
|
* of the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software Foundation *
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2013-11-04 14:41:24 +01:00
|
|
|
#include "icinga/pluginutility.h"
|
2013-06-13 11:33:00 +02:00
|
|
|
#include "icinga/checkcommand.h"
|
2013-03-17 20:19:29 +01:00
|
|
|
#include "icinga/macroprocessor.h"
|
2013-03-22 14:40:55 +01:00
|
|
|
#include "icinga/icingaapplication.h"
|
2013-11-07 13:37:58 +01:00
|
|
|
#include "icinga/perfdatavalue.h"
|
2013-03-17 20:19:29 +01:00
|
|
|
#include "base/dynamictype.h"
|
2013-03-22 14:40:55 +01:00
|
|
|
#include "base/logger_fwd.h"
|
2013-03-25 18:36:15 +01:00
|
|
|
#include "base/scriptfunction.h"
|
|
|
|
#include "base/utility.h"
|
2013-03-25 20:47:02 +01:00
|
|
|
#include "base/process.h"
|
2013-11-13 09:08:17 +01:00
|
|
|
#include "base/objectlock.h"
|
2013-03-15 18:21:29 +01:00
|
|
|
#include <boost/algorithm/string/classification.hpp>
|
2013-03-18 11:02:18 +01:00
|
|
|
#include <boost/algorithm/string/split.hpp>
|
2013-11-17 01:00:03 +01:00
|
|
|
#include <boost/algorithm/string/trim.hpp>
|
2013-03-16 21:18:53 +01:00
|
|
|
#include <boost/foreach.hpp>
|
2012-06-13 13:42:55 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2013-11-04 14:41:24 +01:00
|
|
|
ServiceState PluginUtility::ExitStatusToState(int exitStatus)
|
2013-01-22 12:05:36 +01:00
|
|
|
{
|
|
|
|
switch (exitStatus) {
|
2012-07-13 21:00:54 +02:00
|
|
|
case 0:
|
2013-01-22 12:05:36 +01:00
|
|
|
return StateOK;
|
2012-07-13 21:00:54 +02:00
|
|
|
case 1:
|
2013-01-22 12:05:36 +01:00
|
|
|
return StateWarning;
|
2012-07-13 21:00:54 +02:00
|
|
|
case 2:
|
2013-01-22 12:05:36 +01:00
|
|
|
return StateCritical;
|
2012-07-13 21:00:54 +02:00
|
|
|
default:
|
2013-01-22 12:05:36 +01:00
|
|
|
return StateUnknown;
|
2012-06-19 09:38:20 +02:00
|
|
|
}
|
2012-06-24 16:30:16 +02:00
|
|
|
}
|
2012-06-13 13:42:55 +02:00
|
|
|
|
2013-11-09 14:22:38 +01:00
|
|
|
CheckResult::Ptr PluginUtility::ParseCheckOutput(const String& output)
|
2012-06-29 14:14:51 +02:00
|
|
|
{
|
2013-11-09 14:22:38 +01:00
|
|
|
CheckResult::Ptr result = make_shared<CheckResult>();
|
2013-01-22 12:05:36 +01:00
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
String text;
|
|
|
|
String perfdata;
|
2012-06-29 14:14:51 +02:00
|
|
|
|
2013-03-18 11:02:18 +01:00
|
|
|
std::vector<String> lines;
|
|
|
|
boost::algorithm::split(lines, output, boost::is_any_of("\r\n"));
|
2012-06-29 14:14:51 +02:00
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
BOOST_FOREACH (const String& line, lines) {
|
|
|
|
size_t delim = line.FindFirstOf("|");
|
2012-06-29 14:14:51 +02:00
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
if (!text.IsEmpty())
|
|
|
|
text += "\n";
|
2012-06-29 14:14:51 +02:00
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
if (delim != String::NPos) {
|
|
|
|
text += line.SubStr(0, delim);
|
2012-06-29 14:14:51 +02:00
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
if (!perfdata.IsEmpty())
|
|
|
|
perfdata += " ";
|
2012-06-29 14:14:51 +02:00
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
perfdata += line.SubStr(delim + 1, line.GetLength());
|
2012-06-29 14:14:51 +02:00
|
|
|
} else {
|
2012-08-02 09:38:08 +02:00
|
|
|
text += line;
|
2012-06-29 14:14:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-17 01:00:03 +01:00
|
|
|
boost::algorithm::trim(perfdata);
|
|
|
|
|
2013-11-09 14:22:38 +01:00
|
|
|
result->SetOutput(text);
|
|
|
|
result->SetPerformanceData(ParsePerfdata(perfdata));
|
2013-01-22 12:05:36 +01:00
|
|
|
|
|
|
|
return result;
|
2012-06-13 13:42:55 +02:00
|
|
|
}
|
2013-11-07 13:37:58 +01:00
|
|
|
|
|
|
|
Value PluginUtility::ParsePerfdata(const String& perfdata)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
Dictionary::Ptr result = make_shared<Dictionary>();
|
|
|
|
|
|
|
|
size_t begin = 0;
|
2013-11-17 03:29:43 +01:00
|
|
|
String multi_prefix;
|
2013-11-07 13:37:58 +01:00
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
size_t eqp = perfdata.FindFirstOf('=', begin);
|
|
|
|
|
|
|
|
if (eqp == String::NPos)
|
|
|
|
break;
|
|
|
|
|
|
|
|
String key = perfdata.SubStr(begin, eqp - begin);
|
|
|
|
|
2013-11-13 15:42:58 +01:00
|
|
|
if (key.GetLength() > 2 && key[0] == '\'' && key[key.GetLength() - 1] == '\'')
|
|
|
|
key = key.SubStr(1, key.GetLength() - 2);
|
|
|
|
|
2013-11-17 03:29:43 +01:00
|
|
|
size_t multi_index = key.RFind("::");
|
|
|
|
|
|
|
|
if (multi_index != String::NPos)
|
|
|
|
multi_prefix = "";
|
|
|
|
|
2013-11-07 13:37:58 +01:00
|
|
|
size_t spq = perfdata.FindFirstOf(' ', eqp);
|
|
|
|
|
|
|
|
if (spq == String::NPos)
|
|
|
|
spq = perfdata.GetLength();
|
|
|
|
|
|
|
|
String value = perfdata.SubStr(eqp + 1, spq - eqp - 1);
|
|
|
|
|
2013-11-17 03:29:43 +01:00
|
|
|
if (!multi_prefix.IsEmpty())
|
|
|
|
key = multi_prefix + "::" + key;
|
|
|
|
|
2013-11-07 13:37:58 +01:00
|
|
|
result->Set(key, PerfdataValue::Parse(value));
|
|
|
|
|
2013-11-17 03:29:43 +01:00
|
|
|
if (multi_index != String::NPos)
|
|
|
|
multi_prefix = key.SubStr(0, multi_index);
|
|
|
|
|
2013-11-07 13:37:58 +01:00
|
|
|
begin = spq + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
} catch (const std::exception&) {
|
|
|
|
return perfdata;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
String PluginUtility::FormatPerfdata(const Value& perfdata)
|
|
|
|
{
|
2013-11-30 17:42:50 +01:00
|
|
|
std::ostringstream result;
|
2013-11-07 13:37:58 +01:00
|
|
|
|
|
|
|
if (!perfdata.IsObjectType<Dictionary>())
|
|
|
|
return perfdata;
|
|
|
|
|
|
|
|
Dictionary::Ptr dict = perfdata;
|
|
|
|
|
2013-11-13 09:08:17 +01:00
|
|
|
ObjectLock olock(dict);
|
|
|
|
|
2013-11-30 17:42:50 +01:00
|
|
|
bool first = true;
|
|
|
|
BOOST_FOREACH(const Dictionary::Pair& kv, dict) {
|
|
|
|
if (!first)
|
|
|
|
result << " ";
|
|
|
|
else
|
|
|
|
first = false;
|
2013-11-07 13:37:58 +01:00
|
|
|
|
2013-11-30 17:42:50 +01:00
|
|
|
result << kv.first << "=" << PerfdataValue::Format(kv.second);
|
2013-11-07 13:37:58 +01:00
|
|
|
}
|
|
|
|
|
2013-11-30 17:42:50 +01:00
|
|
|
return result.str();
|
2013-11-07 13:37:58 +01:00
|
|
|
}
|