* Prints an Icinga plugin API compliant output, including error handling. * * @param result * * @return Status code for exit() */
| 42 | * @return Status code for exit() |
| 43 | */ |
| 44 | static int FormatOutput(const Dictionary::Ptr& result) |
| 45 | { |
| 46 | if (!result) { |
| 47 | std::cerr << "UNKNOWN: No data received.\n"; |
| 48 | return 3; |
| 49 | } |
| 50 | |
| 51 | if (l_Debug) |
| 52 | std::cout << "\tJSON Body:\n" << result->ToString() << '\n'; |
| 53 | |
| 54 | Array::Ptr payloads = result->Get("payload"); |
| 55 | if (!payloads) { |
| 56 | std::cerr << "UNKNOWN: Answer format error: Answer is missing 'payload'.\n"; |
| 57 | return 3; |
| 58 | } |
| 59 | |
| 60 | if (payloads->GetLength() == 0) { |
| 61 | std::cerr << "UNKNOWN: Answer format error: 'payload' was empty.\n"; |
| 62 | return 3; |
| 63 | } |
| 64 | |
| 65 | if (payloads->GetLength() > 1) { |
| 66 | std::cerr << "UNKNOWN: Answer format error: Multiple payloads are not supported."; |
| 67 | return 3; |
| 68 | } |
| 69 | |
| 70 | Dictionary::Ptr payload; |
| 71 | |
| 72 | try { |
| 73 | payload = payloads->Get(0); |
| 74 | } catch (const std::exception&) { |
| 75 | std::cerr << "UNKNOWN: Answer format error: 'payload' was not a Dictionary.\n"; |
| 76 | return 3; |
| 77 | } |
| 78 | |
| 79 | Array::Ptr lines; |
| 80 | |
| 81 | try { |
| 82 | lines = payload->Get("lines"); |
| 83 | } catch (const std::exception&) { |
| 84 | std::cerr << "UNKNOWN: Answer format error: 'payload' is missing 'lines'.\n"; |
| 85 | return 3; |
| 86 | } |
| 87 | |
| 88 | if (!lines) { |
| 89 | std::cerr << "UNKNOWN: Answer format error: 'lines' is Null.\n"; |
| 90 | return 3; |
| 91 | } |
| 92 | |
| 93 | std::stringstream ssout; |
| 94 | |
| 95 | ObjectLock olock(lines); |
| 96 | |
| 97 | for (const Value& vline : lines) { |
| 98 | Dictionary::Ptr line; |
| 99 | |
| 100 | try { |
| 101 | line = vline; |