MCPcopy Create free account
hub / github.com/Icinga/icinga2 / FetchData

Function FetchData

plugins/check_nscp_api.cpp:336–418  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

334 */
335
336static Dictionary::Ptr FetchData(const String& host, const String& port, const String& password,
337 const String& endpoint)
338{
339 namespace beast = boost::beast;
340 namespace http = beast::http;
341
342 Shared<AsioTlsStream>::Ptr tlsStream;
343
344 try {
345 tlsStream = Connect(host, port);
346 } catch (const std::exception& ex) {
347 std::cerr << "Connection error: " << ex.what();
348 throw ex;
349 }
350
351 Url::Ptr url;
352
353 try {
354 url = new Url(endpoint);
355 } catch (const std::exception& ex) {
356 std::cerr << "URL error: " << ex.what();
357 throw ex;
358 }
359
360 url->SetScheme("https");
361 url->SetHost(host);
362 url->SetPort(port);
363
364 // NSClient++ uses `time=1m&time=5m` instead of `time[]=1m&time[]=5m`
365 url->SetArrayFormatUseBrackets(false);
366
367 http::request<http::string_body> request (http::verb::get, std::string(url->Format(true)), 10);
368
369 request.set(http::field::user_agent, "Icinga/check_nscp_api/" + String(VERSION));
370 request.set(http::field::host, host + ":" + port);
371
372 request.set(http::field::accept, "application/json");
373 request.set("password", password);
374
375 if (l_Debug) {
376 std::cout << "Sending request to " << url->Format(false, false) << "'.\n";
377 }
378
379 try {
380 http::write(*tlsStream, request);
381 tlsStream->flush();
382 } catch (const std::exception& ex) {
383 std::cerr << "Cannot write HTTP request to REST API at URL '" << url->Format(false, false) << "': " << ex.what();
384 throw ex;
385 }
386
387 beast::flat_buffer buffer;
388 http::parser<false, http::string_body> p;
389
390 try {
391 HttpResponseReasonInjector<decltype(*tlsStream)> reasonInjector (*tlsStream);
392 http::read(reasonInjector, buffer, p);
393 } catch (const std::exception &ex) {

Callers 1

mainFunction · 0.85

Calls 11

ScriptErrorClass · 0.85
SetSchemeMethod · 0.80
SetHostMethod · 0.80
SetPortMethod · 0.80
ConnectFunction · 0.70
StringClass · 0.50
whatMethod · 0.45
FormatMethod · 0.45
getMethod · 0.45
IsEmptyMethod · 0.45

Tested by

no test coverage detected