forked from draios/sysdig
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmarathon_http.cpp
93 lines (80 loc) · 2.33 KB
/
marathon_http.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//
// marathon_http.cpp
//
#ifdef HAS_CAPTURE
#include "marathon_http.h"
#include "curl/curl.h"
#include "curl/easy.h"
#define BUFFERSIZE 512 // b64 needs this macro
#include "b64/encode.h"
#include "sinsp.h"
#include "sinsp_int.h"
#include "mesos.h"
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <unistd.h>
marathon_http::marathon_http(mesos& m, const uri& url, bool discover_marathon, int timeout_ms, const string& token):
mesos_http(m, url, false, discover_marathon, timeout_ms, token)
{
g_logger.log("Creating Marathon HTTP object for [" + url.to_string(false) + "] ...", sinsp_logger::SEV_DEBUG);
if(refresh_data())
{
g_logger.log("Created Marathon HTTP connection (" + url.to_string(false) + ") for framework [" +
get_framework_name() + "] (" + get_framework_id() + "), version: " + get_framework_version(),
sinsp_logger::SEV_INFO);
}
else
{
throw sinsp_exception("Could not obtain Mesos Marathon framework information.");
}
g_logger.log("Marathon request [" + get_request() + ']', sinsp_logger::SEV_DEBUG);
}
marathon_http::~marathon_http()
{
}
bool marathon_http::refresh_data()
{
std::ostringstream os;
CURLcode res = get_data(make_uri("/v2/info"), os);
if(res != CURLE_OK)
{
g_logger.log(curl_easy_strerror(res), sinsp_logger::SEV_ERROR);
return false;
}
try
{
Json::Value root;
Json::Reader reader;
if(reader.parse(os.str(), root, false))
{
set_framework_id(get_json_string(root, "frameworkId"));
set_framework_name(get_json_string(root, "name"));
set_framework_version(get_json_string(root, "version"));
g_logger.log("Found Marathon framework: " + get_framework_name() + " (" + get_framework_id() + "), version: " + get_framework_version(), sinsp_logger::SEV_DEBUG);
}
else
{
g_logger.log("Error parsing framework info.\nJSON:\n---\n" + os.str() + "\n---", sinsp_logger::SEV_ERROR);
return false;
}
}
catch(std::exception& ex)
{
g_logger.log(std::string("Error parsing framework info:") + ex.what(), sinsp_logger::SEV_ERROR);
return false;
}
return true;
}
std::string marathon_http::get_groups(const std::string& group_id)
{
std::ostringstream os;
CURLcode res = get_data(make_uri("/v2/groups" + group_id), os);
if(res != CURLE_OK)
{
g_logger.log(curl_easy_strerror(res), sinsp_logger::SEV_ERROR);
return "";
}
return os.str();
}
#endif // HAS_CAPTURE