Unverified Commit cb3b85d6 authored by NexAdn's avatar NexAdn
Browse files

Initial commit

parents
BasedOnStyle: LLVM # LLVM, Google, Chromium, Mozilla, WebKit
AccessModifierOffset: -4
AlignEscapedNewlinesLeft: true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: false # None(false), Inline, All(true)
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: false
AlwaysBreakBeforeMultilineStrings: true
AlwaysBreakTemplateDeclarations: true
BinPackParameters: true
BreakBeforeBinaryOperators: NonAssignment # None, NonAssignment, All
BreakBeforeBraces: Custom # Attach, Linux, Stroustrup, Allman, GNU
BraceWrapping:
AfterClass: true
AfterControlStatement: false
AfterEnum: true
AfterFunction: true
AfterNamespace: true
AfterStruct: true
AfterUnion: true
AfterExternBlock: false
BeforeCatch: true
BeforeElse: false
IndentBraces: false
SplitEmptyFunction: false
SplitEmptyNamespace: true
BreakBeforeTernaryOperators: false
BreakConstructorInitializersBeforeComma: true
ColumnLimit: 100
# CommentPragmas
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 2
Cpp11BracedListStyle: true
DerivePointerAlignment: false
DisableFormat: false
ExperimentalAutoDetectBinPacking: true
# ForEachMacros
IndentCaseLabels: false
IndentWidth: 4
IndentWrappedFunctionNames: false
KeepEmptyLinesAtTheStartOfBlocks: false
Language: Cpp # None, Cpp, JavaScript, Proto
MaxEmptyLinesToKeep: 1
NamespaceIndentation: Inner # None, Inner, All
# ObjCSpaceAfterProperty
# ObjCSpaceBeforeProtocolList
#// PenaltyBreakBeforeFirstCallParameter
#// PenaltyBreakComment
#// PenaltyBreakFirstLessLess
#// PenaltyBreakString
#// PenaltyExcessCharacter
#// PenaltyReturnTypeOnItsOwnLine:
PointerAlignment: Left # Left, Right, Middle
SpaceAfterCStyleCast: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements # Never, ControlStatements, Always
# SpaceBeforeRangeBasedForLoopColon: true
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInCStyleCastParentheses: false
SpacesInContainerLiterals: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: Cpp11 # Cpp03, Cpp11, Auto
TabWidth: 4
UseTab: ForIndentation # Never(false), ForIndentation, Always(true)
/.vscode
/build
[submodule "3rdparty/json"]
path = 3rdparty/json
url = https://github.com/nlohmann/json.git
#add_subdirectory(json)
Subproject commit a015b78e81c859b88840cb0cd4001ce1fe5e7865
cmake_minimum_required(VERSION 3.0)
project(icinga-ampel LANGUAGES C CXX VERSION 1.0)
find_package(CURL REQUIRED)
#find_package(cgreen REQUIRED)
#enable_testing(true)
#
#set(REQUIRED_TEST_TARGETS "")
#
#macro(make_test name file)
#add_executable(test_${name}_runner
# ${file}
#)
#target_include_directories(test_${name}_runner
# PUBLIC
# ${PROJECT_SOURCE_DIR}
# ${PROJECT_SOURCE_DIR}/src
# ${PROJECT_SOURCE_DIR}/test
#)
#target_link_libraries(test_${name}_runner
# # TODO
# cgreen
#)
#add_test(NAME test_${name}
# COMMAND ./test_${name}_runner
# WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
#)
#set(${REQUIRED_TEST_TARGETS} ${REQUIRED_TEST_TARGETS} test_${name}_runner)
#endmacro()
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED true)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Werror")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror")
add_subdirectory(3rdparty)
add_subdirectory(src)
#add_subdirectory(test)
#
#add_custom_target(check
# COMMAND ${CMAKE_CTEST_COMMAND}
# DEPENDS ${REQUIRED_TEST_TARGETS}
#)
[
{
"directory": "/home/adrian/nfs/Atom/icinga-ampel/build/src",
"command": "/usr/bin/c++ -I/home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include -Wall -Werror -std=gnu++14 -o CMakeFiles/icinga-ampel.dir/main.cpp.o -c /home/adrian/nfs/Atom/icinga-ampel/src/main.cpp",
"file": "/home/adrian/nfs/Atom/icinga-ampel/src/main.cpp"
},
{
"directory": "/home/adrian/nfs/Atom/icinga-ampel/build/src",
"command": "/usr/bin/c++ -I/home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include -Wall -Werror -std=gnu++14 -c /home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include/nlohmann/json.hpp",
"file": "/home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include/nlohmann/json.hpp"
},
{
"directory": "/home/adrian/nfs/Atom/icinga-ampel/build/src",
"command": "/usr/bin/c++ -I/home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include -Wall -Werror -std=gnu++14 -c /home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include/nlohmann/adl_serializer.hpp",
"file": "/home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include/nlohmann/adl_serializer.hpp"
},
{
"directory": "/home/adrian/nfs/Atom/icinga-ampel/build/src",
"command": "/usr/bin/c++ -I/home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include -Wall -Werror -std=gnu++14 -c /home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include/nlohmann/detail/conversions/from_json.hpp",
"file": "/home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include/nlohmann/detail/conversions/from_json.hpp"
},
{
"directory": "/home/adrian/nfs/Atom/icinga-ampel/build/src",
"command": "/usr/bin/c++ -I/home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include -Wall -Werror -std=gnu++14 -c /home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include/nlohmann/detail/exceptions.hpp",
"file": "/home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include/nlohmann/detail/exceptions.hpp"
},
{
"directory": "/home/adrian/nfs/Atom/icinga-ampel/build/src",
"command": "/usr/bin/c++ -I/home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include -Wall -Werror -std=gnu++14 -c /home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include/nlohmann/detail/input/position_t.hpp",
"file": "/home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include/nlohmann/detail/input/position_t.hpp"
},
{
"directory": "/home/adrian/nfs/Atom/icinga-ampel/build/src",
"command": "/usr/bin/c++ -I/home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include -Wall -Werror -std=gnu++14 -c /home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include/nlohmann/detail/macro_scope.hpp",
"file": "/home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include/nlohmann/detail/macro_scope.hpp"
},
{
"directory": "/home/adrian/nfs/Atom/icinga-ampel/build/src",
"command": "/usr/bin/c++ -I/home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include -Wall -Werror -std=gnu++14 -c /home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include/nlohmann/thirdparty/hedley/hedley.hpp",
"file": "/home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include/nlohmann/thirdparty/hedley/hedley.hpp"
},
{
"directory": "/home/adrian/nfs/Atom/icinga-ampel/build/src",
"command": "/usr/bin/c++ -I/home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include -Wall -Werror -std=gnu++14 -c /home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include/nlohmann/detail/meta/cpp_future.hpp",
"file": "/home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include/nlohmann/detail/meta/cpp_future.hpp"
},
{
"directory": "/home/adrian/nfs/Atom/icinga-ampel/build/src",
"command": "/usr/bin/c++ -I/home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include -Wall -Werror -std=gnu++14 -c /home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include/nlohmann/detail/meta/type_traits.hpp",
"file": "/home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include/nlohmann/detail/meta/type_traits.hpp"
},
{
"directory": "/home/adrian/nfs/Atom/icinga-ampel/build/src",
"command": "/usr/bin/c++ -I/home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include -Wall -Werror -std=gnu++14 -c /home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include/nlohmann/detail/iterators/iterator_traits.hpp",
"file": "/home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include/nlohmann/detail/iterators/iterator_traits.hpp"
},
{
"directory": "/home/adrian/nfs/Atom/icinga-ampel/build/src",
"command": "/usr/bin/c++ -I/home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include -Wall -Werror -std=gnu++14 -c /home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include/nlohmann/detail/meta/void_t.hpp",
"file": "/home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include/nlohmann/detail/meta/void_t.hpp"
},
{
"directory": "/home/adrian/nfs/Atom/icinga-ampel/build/src",
"command": "/usr/bin/c++ -I/home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include -Wall -Werror -std=gnu++14 -c /home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include/nlohmann/detail/meta/detected.hpp",
"file": "/home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include/nlohmann/detail/meta/detected.hpp"
},
{
"directory": "/home/adrian/nfs/Atom/icinga-ampel/build/src",
"command": "/usr/bin/c++ -I/home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include -Wall -Werror -std=gnu++14 -c /home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include/nlohmann/json_fwd.hpp",
"file": "/home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include/nlohmann/json_fwd.hpp"
},
{
"directory": "/home/adrian/nfs/Atom/icinga-ampel/build/src",
"command": "/usr/bin/c++ -I/home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include -Wall -Werror -std=gnu++14 -c /home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include/nlohmann/detail/value_t.hpp",
"file": "/home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include/nlohmann/detail/value_t.hpp"
},
{
"directory": "/home/adrian/nfs/Atom/icinga-ampel/build/src",
"command": "/usr/bin/c++ -I/home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include -Wall -Werror -std=gnu++14 -c /home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include/nlohmann/detail/conversions/to_json.hpp",
"file": "/home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include/nlohmann/detail/conversions/to_json.hpp"
},
{
"directory": "/home/adrian/nfs/Atom/icinga-ampel/build/src",
"command": "/usr/bin/c++ -I/home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include -Wall -Werror -std=gnu++14 -c /home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include/nlohmann/detail/iterators/iteration_proxy.hpp",
"file": "/home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include/nlohmann/detail/iterators/iteration_proxy.hpp"
},
{
"directory": "/home/adrian/nfs/Atom/icinga-ampel/build/src",
"command": "/usr/bin/c++ -I/home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include -Wall -Werror -std=gnu++14 -c /home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include/nlohmann/detail/input/binary_reader.hpp",
"file": "/home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include/nlohmann/detail/input/binary_reader.hpp"
},
{
"directory": "/home/adrian/nfs/Atom/icinga-ampel/build/src",
"command": "/usr/bin/c++ -I/home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include -Wall -Werror -std=gnu++14 -c /home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include/nlohmann/detail/input/input_adapters.hpp",
"file": "/home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include/nlohmann/detail/input/input_adapters.hpp"
},
{
"directory": "/home/adrian/nfs/Atom/icinga-ampel/build/src",
"command": "/usr/bin/c++ -I/home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include -Wall -Werror -std=gnu++14 -c /home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include/nlohmann/detail/input/json_sax.hpp",
"file": "/home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include/nlohmann/detail/input/json_sax.hpp"
},
{
"directory": "/home/adrian/nfs/Atom/icinga-ampel/build/src",
"command": "/usr/bin/c++ -I/home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include -Wall -Werror -std=gnu++14 -c /home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include/nlohmann/detail/meta/is_sax.hpp",
"file": "/home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include/nlohmann/detail/meta/is_sax.hpp"
},
{
"directory": "/home/adrian/nfs/Atom/icinga-ampel/build/src",
"command": "/usr/bin/c++ -I/home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include -Wall -Werror -std=gnu++14 -c /home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include/nlohmann/detail/input/lexer.hpp",
"file": "/home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include/nlohmann/detail/input/lexer.hpp"
},
{
"directory": "/home/adrian/nfs/Atom/icinga-ampel/build/src",
"command": "/usr/bin/c++ -I/home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include -Wall -Werror -std=gnu++14 -c /home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include/nlohmann/detail/input/parser.hpp",
"file": "/home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include/nlohmann/detail/input/parser.hpp"
},
{
"directory": "/home/adrian/nfs/Atom/icinga-ampel/build/src",
"command": "/usr/bin/c++ -I/home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include -Wall -Werror -std=gnu++14 -c /home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include/nlohmann/detail/iterators/internal_iterator.hpp",
"file": "/home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include/nlohmann/detail/iterators/internal_iterator.hpp"
},
{
"directory": "/home/adrian/nfs/Atom/icinga-ampel/build/src",
"command": "/usr/bin/c++ -I/home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include -Wall -Werror -std=gnu++14 -c /home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include/nlohmann/detail/iterators/primitive_iterator.hpp",
"file": "/home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include/nlohmann/detail/iterators/primitive_iterator.hpp"
},
{
"directory": "/home/adrian/nfs/Atom/icinga-ampel/build/src",
"command": "/usr/bin/c++ -I/home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include -Wall -Werror -std=gnu++14 -c /home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include/nlohmann/detail/iterators/iter_impl.hpp",
"file": "/home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include/nlohmann/detail/iterators/iter_impl.hpp"
},
{
"directory": "/home/adrian/nfs/Atom/icinga-ampel/build/src",
"command": "/usr/bin/c++ -I/home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include -Wall -Werror -std=gnu++14 -c /home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include/nlohmann/detail/iterators/json_reverse_iterator.hpp",
"file": "/home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include/nlohmann/detail/iterators/json_reverse_iterator.hpp"
},
{
"directory": "/home/adrian/nfs/Atom/icinga-ampel/build/src",
"command": "/usr/bin/c++ -I/home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include -Wall -Werror -std=gnu++14 -c /home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include/nlohmann/detail/json_pointer.hpp",
"file": "/home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include/nlohmann/detail/json_pointer.hpp"
},
{
"directory": "/home/adrian/nfs/Atom/icinga-ampel/build/src",
"command": "/usr/bin/c++ -I/home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include -Wall -Werror -std=gnu++14 -c /home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include/nlohmann/detail/json_ref.hpp",
"file": "/home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include/nlohmann/detail/json_ref.hpp"
},
{
"directory": "/home/adrian/nfs/Atom/icinga-ampel/build/src",
"command": "/usr/bin/c++ -I/home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include -Wall -Werror -std=gnu++14 -c /home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include/nlohmann/detail/output/binary_writer.hpp",
"file": "/home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include/nlohmann/detail/output/binary_writer.hpp"
},
{
"directory": "/home/adrian/nfs/Atom/icinga-ampel/build/src",
"command": "/usr/bin/c++ -I/home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include -Wall -Werror -std=gnu++14 -c /home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include/nlohmann/detail/output/output_adapters.hpp",
"file": "/home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include/nlohmann/detail/output/output_adapters.hpp"
},
{
"directory": "/home/adrian/nfs/Atom/icinga-ampel/build/src",
"command": "/usr/bin/c++ -I/home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include -Wall -Werror -std=gnu++14 -c /home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include/nlohmann/detail/output/serializer.hpp",
"file": "/home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include/nlohmann/detail/output/serializer.hpp"
},
{
"directory": "/home/adrian/nfs/Atom/icinga-ampel/build/src",
"command": "/usr/bin/c++ -I/home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include -Wall -Werror -std=gnu++14 -c /home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include/nlohmann/detail/conversions/to_chars.hpp",
"file": "/home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include/nlohmann/detail/conversions/to_chars.hpp"
},
{
"directory": "/home/adrian/nfs/Atom/icinga-ampel/build/src",
"command": "/usr/bin/c++ -I/home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include -Wall -Werror -std=gnu++14 -c /home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include/nlohmann/detail/macro_unscope.hpp",
"file": "/home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include/nlohmann/detail/macro_unscope.hpp"
},
{
"directory": "/home/adrian/nfs/Atom/icinga-ampel/build/src",
"command": "/usr/bin/c++ -I/home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include -Wall -Werror -std=gnu++14 -c /home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include/nlohmann/thirdparty/hedley/hedley_undef.hpp",
"file": "/home/adrian/nfs/Atom/icinga-ampel/3rdparty/json/include/nlohmann/thirdparty/hedley/hedley_undef.hpp"
}
]
{
title: 'icinga-ampel'
group: 'icinga'
paths: [
'/home/adrian/Atom/icinga-ampel'
'/home/adrian/nfs/Atom/icinga-ampel'
]
devMode: false
settings:
'editor.tabLength': 4
}
add_executable(icinga-ampel
main.cpp
)
target_include_directories(icinga-ampel
PUBLIC
${PROJECT_SOURCE_DIR}/3rdparty/json/include
)
target_link_libraries(icinga-ampel
PUBLIC
curl
)
/*
* Copyright (C) 2019 Adrian "nex" Schollmeyer <adrian.schollmeyer@fem.tu-ilmenau.de>
*/
#include <algorithm>
#include <iostream>
#include <memory>
#include <sstream>
#include <string>
#include <tuple>
#include <unordered_map>
#include <nlohmann/json.hpp>
#include <curl/curl.h>
namespace
{
constexpr const char* HOST_INFO_URL =
"https://localhost:5665/v1/objects/"
"hosts?attrs=acknowledgement&attrs=last_check_result&attrs=vars";
constexpr const char* SERVICE_INFO_URL =
"https://localhost:5665/v1/objects/"
"services?attrs=acknowledgement&attrs=last_check_result&attrs=vars";
constexpr bool SKIP_CERT_VERIFICATION = true;
constexpr float THRESHOLD_WARNING = 99.f;
constexpr float THRESHOLD_CRITICAL = 98.f;
constexpr int RETURN_OK = 0;
constexpr int RETURN_WARNING = 1;
constexpr int RETURN_CRITICAL = 2;
constexpr int RETURN_UNKNOWN = 3;
constexpr int MAX_RETURN_VALUE = 2;
constexpr const char* USER_PWD = "ampel:asdf";
std::ostringstream output_buf;
using json = nlohmann::json;
using health_data_t = std::tuple<int, int>;
using health_data_map_t = std::unordered_map<int, health_data_t>;
} // namespace
void print_plugin_output(int return_code, std::string status_message)
{
switch (return_code) {
case RETURN_OK:
std::cout << "Ampel GRUEN - " << status_message << std::endl;
break;
case RETURN_WARNING:
std::cout << "Ampel GELB - " << status_message << std::endl;
break;
case RETURN_CRITICAL:
std::cout << "Ampel ROT - " << status_message << std::endl;
break;
default:
std::cout << "Ampel UNKNOWN - " << status_message << std::endl;
break;
}
}
void print_health(int return_code, float health)
{
std::string health_as_str = std::to_string(health);
print_plugin_output(return_code, "Health " + health_as_str + " | health=" + health_as_str + "%;"
+ std::to_string(THRESHOLD_WARNING) + ":;"
+ std::to_string(THRESHOLD_CRITICAL) + ":;0;100");
}
#ifndef NDEBUG
void print_debug(std::string message)
{
std::clog << message;
}
#else
inline void print_debug(std::string message)
{}
#endif
void parse_result(health_data_map_t& output, const std::string& raw_data)
{
json data = json::parse(raw_data);
auto results = data["results"];
for (auto& object : results) {
if (object["attrs"]["acknowledgement"] > 0
|| (object["attrs"]["vars"]["ampel_disable"] != nullptr
&& object["attrs"]["vars"]["ampel_disable"])) {
continue;
}
int ampel_group{0};
if (object["attrs"]["vars"]["ampel_group"] > 0) {
ampel_group = static_cast<int>(object["attrs"]["vars"]["ampel_group"]);
}
int last_result = object["attrs"]["last_check_result"]["exit_status"];
auto target_it = output.find(ampel_group);
if (target_it == output.end()) {
output.insert(
{ampel_group,
{MAX_RETURN_VALUE - std::min(last_result, MAX_RETURN_VALUE), MAX_RETURN_VALUE}});
} else {
target_it->second = {std::get<0>(target_it->second) + MAX_RETURN_VALUE - last_result,
std::get<1>(target_it->second) + MAX_RETURN_VALUE};
}
}
}
size_t write_memory_cb(void* content, size_t size, size_t n_memb, void* userdata)
{
size_t real_size = size * n_memb;
std::string content_str{reinterpret_cast<char*>(content), real_size};
output_buf << content_str;
return real_size;
}
int main(int argc, char** argv)
{
print_debug("Initializing cURL\n");
CURL* curl;
CURLcode res;
curl = curl_easy_init();
if (!curl) {
print_plugin_output(RETURN_UNKNOWN, "Failed to initialize cURL");
return RETURN_UNKNOWN;
}
print_debug("Setting cURL opts\n");
curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_memory_cb);
curl_easy_setopt(curl, CURLOPT_USERPWD, USER_PWD);
curl_easy_setopt(curl, CURLOPT_URL, HOST_INFO_URL);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, (SKIP_CERT_VERIFICATION ? 0l : 1l));
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, (SKIP_CERT_VERIFICATION ? 0l : 1l));
health_data_map_t health_data;
print_debug("Performing cURL request\n");
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
print_plugin_output(RETURN_UNKNOWN, std::string("Failed to perform cURL request: ")
+ curl_easy_strerror(res));
curl_easy_cleanup(curl);
return RETURN_UNKNOWN;
}
parse_result(health_data, output_buf.str());
output_buf = std::ostringstream();
curl_easy_setopt(curl, CURLOPT_URL, SERVICE_INFO_URL);
print_debug("Performing cURL request\n");
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
print_plugin_output(RETURN_UNKNOWN, std::string("Failed to perfor cURL request: ")
+ curl_easy_strerror(res));
return RETURN_UNKNOWN;
}
parse_result(health_data, output_buf.str());
float min_health{1.f};
for (auto& health : health_data) {
float this_health = std::get<0>(health.second) * 1.f / std::get<1>(health.second);
if (this_health < min_health) {
min_health = this_health;
}
}
print_debug("Performing global cleanup\n");
curl_global_cleanup();
float health = min_health * 100.f;
if (health < THRESHOLD_CRITICAL) {
print_health(RETURN_CRITICAL, health);
return RETURN_CRITICAL;
} else if (health < THRESHOLD_WARNING) {
print_health(RETURN_WARNING, health);
return RETURN_WARNING;
} else {
print_health(RETURN_OK, health);
return RETURN_OK;
}
print_plugin_output(RETURN_UNKNOWN, "No data parsed.");
return RETURN_UNKNOWN;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment