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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
|
/*
Copyright (C) 2021 The Falco Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
//
// mesos_state_t.h
//
// mesos state abstraction
//
#pragma once
#include "mesos_component.h"
#include "marathon_component.h"
#include "json/json.h"
#include "sinsp.h"
#include "sinsp_int.h"
#include "json_error_log.h"
#include <vector>
#include <map>
#include <unordered_map>
#include <algorithm>
//
// state
//
class mesos_state_t
{
public:
typedef std::shared_ptr<Json::Value> json_ptr_t;
#ifdef HAS_CAPTURE
struct capture
{
enum type_t
{
MESOS_STATE = 0,
MARATHON_GROUPS = 1,
MARATHON_APPS = 2
};
capture(type_t type, std::string&& data):
m_type(type),
m_data(std::move(data))
{
}
std::string to_string()
{
m_data.erase(std::remove_if(m_data.begin(), m_data.end(), [](char c) { return c == '\r' || c == '\n'; }));
std::ostringstream os;
switch(m_type)
{
case MESOS_STATE:
os << "{\"mesos_state\":" << m_data << '}' << std::flush;
break;
case MARATHON_GROUPS:
os << "{\"marathon_groups\":" << m_data << '}' << std::flush;
break;
case MARATHON_APPS:
os << "{\"marathon_apps\":" << m_data << '}' << std::flush;
break;
}
return os.str();
}
type_t m_type;
std::string m_data;
};
typedef std::deque<capture> capture_list;
const capture_list& get_capture_events() const
{
return m_capture;
}
std::string dequeue_capture_event()
{
std::string ret;
if(m_capture.size())
{
ret = m_capture.front().to_string();
m_capture.pop_front();
}
return ret;
}
void enqueue_capture_event(capture::type_t type, std::string&& data)
{
if(m_is_captured)
{
m_capture.emplace_back(capture(type, std::move(data)));
}
}
bool is_captured() const
{
return m_is_captured;
}
void capture_groups(const Json::Value& root, const std::string& framework_id, Json::Value& capt, bool capture_fw = false);
void capture_apps(const Json::Value& root, const std::string& framework_id);
#endif // HAS_CAPTURE
mesos_state_t(bool is_captured = false, bool verbose = false);
//
// frameworks
//
const mesos_frameworks& get_frameworks() const;
mesos_frameworks& get_frameworks();
const mesos_framework& get_framework(const std::string& framework_uid) const;
mesos_framework& get_framework(const std::string& framework_uid);
void push_framework(const mesos_framework& framework);
void emplace_framework(mesos_framework&& framework);
void remove_framework(const std::string& framework_uid);
void remove_framework(const Json::Value& framework);
const mesos_framework* get_framework_for_task(const std::string& task_id) const;
//
// tasks
//
std::unordered_set<std::string> get_all_task_ids() const;
const mesos_framework::task_map& get_tasks(const std::string& framework_uid) const;
mesos_framework::task_map& get_tasks(const std::string& framework_uid);
mesos_framework::task_ptr_t get_task(const std::string& uid) const;
void add_or_replace_task(mesos_framework& framework, mesos_task::ptr_t task);
void remove_task(mesos_framework& framework, const std::string& uid);
//
// slaves
//
const mesos_slaves& get_slaves() const;
mesos_slaves& get_slaves();
const mesos_slave& get_slave(const std::string& slave_uid) const;
mesos_slave& get_slave(const std::string& slave_uid);
void push_slave(const mesos_slave& slave);
void emplace_slave(mesos_slave&& slave);
//
// Marathon
//
void set_marathon_uri(const std::string& uri);
const std::string& get_marathon_uri() const;
//
// Marathon apps
//
void parse_apps(Json::Value&& root, const std::string& framework_id);
void parse_apps(json_ptr_t json, const std::string& framework_id);
marathon_app::ptr_t get_app(const std::string& app_id);
marathon_group::app_ptr_t add_or_replace_app(const std::string& id,
const std::string& group,
const std::string& task = "");
bool remove_app(const std::string& id);
void add_task_to_app(marathon_group::app_ptr_t app, const std::string& task_id);
marathon_app::ptr_t get_app(mesos_task::ptr_t task) const;
//
// Marathon groups
//
bool parse_groups(Json::Value&& root, const std::string& framework_id);
bool parse_groups(json_ptr_t json, const std::string& framework_id);
const marathon_groups& get_groups() const;
marathon_groups& get_groups();
marathon_group::ptr_t get_group(const std::string& group_id);
marathon_group::ptr_t get_group(mesos_task::ptr_t task) const;
marathon_group::ptr_t add_or_replace_group(marathon_group::ptr_t group, marathon_group::ptr_t to_group = 0);
marathon_group::ptr_t get_app_group(const std::string& app_id);
void erase_groups(const std::string& framework_id);
void print_groups() const;
//
// state
//
void clear_mesos();
void clear_marathon();
bool has_data() const;
private:
marathon_group::ptr_t add_group(const Json::Value& group, marathon_group::ptr_t to_group, const std::string& framework_id);
bool handle_groups(const Json::Value& groups, marathon_group::ptr_t p_groups, const std::string& framework_id);
marathon_app::ptr_t add_app(const Json::Value& app, const std::string& framework_id);
mesos_frameworks m_frameworks;
std::string m_marathon_uri;
mesos_slaves m_slaves;
marathon_groups m_groups;
bool m_verbose;
#ifdef HAS_CAPTURE
bool m_is_captured;
capture_list m_capture;
#endif // HAS_CAPTURE
typedef std::unordered_map<std::string, mesos_framework*> task_framework_map_t;
task_framework_map_t m_task_framework_cache;
};
//
// frameworks
//
inline const mesos_frameworks& mesos_state_t::get_frameworks() const
{
return m_frameworks;
}
inline mesos_frameworks& mesos_state_t::get_frameworks()
{
return m_frameworks;
}
inline const mesos_framework& mesos_state_t::get_framework(const std::string& framework_uid) const
{
for(const auto& framework : m_frameworks)
{
if(framework.get_uid() == framework_uid)
{
return framework;
}
}
throw sinsp_exception("Framework not found: " + framework_uid);
}
inline mesos_framework& mesos_state_t::get_framework(const std::string& framework_uid)
{
for(auto& framework : m_frameworks)
{
if(framework.get_uid() == framework_uid)
{
return framework;
}
}
throw sinsp_exception("Framework not found: " + framework_uid);
}
inline void mesos_state_t::push_framework(const mesos_framework& framework)
{
for(mesos_frameworks::iterator it = m_frameworks.begin(); it != m_frameworks.end();)
{
if(it->get_uid() == framework.get_uid())
{
it = m_frameworks.erase(it);
}
else { ++it; }
}
m_frameworks.push_back(framework);
}
inline void mesos_state_t::emplace_framework(mesos_framework&& framework)
{
for(mesos_frameworks::iterator it = m_frameworks.begin(); it != m_frameworks.end();)
{
if(it->get_uid() == framework.get_uid())
{
it = m_frameworks.erase(it);
}
else { ++it; }
}
m_frameworks.emplace_back(std::move(framework));
}
inline void mesos_state_t::remove_framework(const Json::Value& framework)
{
const Json::Value& id = framework["id"];
if(!id.isNull() && id.isString())
{
remove_framework(id.asString());
}
}
inline const mesos_framework* mesos_state_t::get_framework_for_task(const std::string& task_id) const
{
task_framework_map_t::const_iterator it = m_task_framework_cache.find(task_id);
if(it != m_task_framework_cache.end())
{
return it->second;
}
return 0;
}
inline void mesos_state_t::remove_framework(const std::string& framework_uid)
{
for(mesos_frameworks::iterator it = m_frameworks.begin(); it != m_frameworks.end(); ++it)
{
if(it->get_uid() == framework_uid)
{
for(auto& task : it->get_tasks())
{
m_task_framework_cache.erase(task.first);
}
m_frameworks.erase(it);
return;
}
}
}
//
// tasks
//
inline void mesos_state_t::add_or_replace_task(mesos_framework& framework, mesos_task::ptr_t task)
{
if(task)
{
framework.add_or_replace_task(task);
m_task_framework_cache[task->get_uid()] = &framework;
}
}
inline void mesos_state_t::remove_task(mesos_framework& framework, const std::string& uid)
{
mesos_task::ptr_t task = framework.get_task(uid);
if(task)
{
std::string app_id = task->get_marathon_app_id();
if(!app_id.empty())
{
marathon_group::ptr_t group = get_app_group(app_id);
if(group)
{
if(!group->remove_task(uid))
{
std::string errstr = "Task [" + uid + "] not found in Marathon app [" + app_id + ']';
g_logger.log(errstr,
sinsp_logger::SEV_ERROR);
g_json_error_log.log(uid, errstr, sinsp_utils::get_current_time_ns(), "remove-task");
}
}
else
{
std::string errstr = "Group not found for Marathon app [" + app_id + "] while trying to remove task [" + uid + ']';
g_logger.log(errstr,
sinsp_logger::SEV_ERROR);
g_json_error_log.log(app_id, errstr, sinsp_utils::get_current_time_ns(), "remove-task");
}
}
else
{
g_logger.log("Task [" + uid + "] has no Marathon app ID.", sinsp_logger::SEV_WARNING);
}
}
else
{
g_logger.log("Task [" + uid + "] not found in framework [" + framework.get_uid() + ']', sinsp_logger::SEV_WARNING);
}
framework.remove_task(uid);
m_task_framework_cache.erase(uid);
}
//
// slaves
//
inline const mesos_slaves& mesos_state_t::get_slaves() const
{
return m_slaves;
}
inline mesos_slaves& mesos_state_t::get_slaves()
{
return m_slaves;
}
inline const mesos_slave& mesos_state_t::get_slave(const std::string& slave_uid) const
{
for(const auto& slave : m_slaves)
{
if(slave.get_uid() == slave_uid)
{
return slave;
}
}
throw sinsp_exception("Slave not found: " + slave_uid);
}
inline mesos_slave& mesos_state_t::get_slave(const std::string& slave_uid)
{
for(auto& slave : m_slaves)
{
if(slave.get_uid() == slave_uid)
{
return slave;
}
}
throw sinsp_exception("Slave not found: " + slave_uid);
}
inline void mesos_state_t::push_slave(const mesos_slave& slave)
{
for(mesos_slaves::iterator it = m_slaves.begin(); it != m_slaves.end();)
{
if(it->get_uid() == slave.get_uid())
{
it = m_slaves.erase(it);
}
else { ++it; }
}
m_slaves.push_back(slave);
}
inline void mesos_state_t::emplace_slave(mesos_slave&& slave)
{
for(mesos_slaves::iterator it = m_slaves.begin(); it != m_slaves.end();)
{
if(it->get_uid() == slave.get_uid())
{
it = m_slaves.erase(it);
}
else { ++it; }
}
m_slaves.emplace_back(std::move(slave));
}
//
// Marathon
//
inline void mesos_state_t::set_marathon_uri(const std::string& uri)
{
m_marathon_uri = uri;
}
inline const std::string& mesos_state_t::get_marathon_uri() const
{
return m_marathon_uri;
}
//
// apps
//
//
// groups
//
inline const marathon_groups& mesos_state_t::get_groups() const
{
return m_groups;
}
inline marathon_groups& mesos_state_t::get_groups()
{
return m_groups;
}
//
// state
//
inline void mesos_state_t::clear_mesos()
{
m_frameworks.clear();
m_slaves.clear();
}
inline void mesos_state_t::clear_marathon()
{
m_groups.clear();
}
inline bool mesos_state_t::has_data() const
{
return m_frameworks.size() > 0 && m_slaves.size() > 0;
}
|