Eunomia  0.1.0
A lightweight eBPF-based CloudNative Monitor tool for Container Security and Observability
tracker.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2  *
3  * Copyright (c) 2022, 郑昱笙,濮雯旭,张典典(牛校牛子队)
4  * All rights reserved.
5  */
6 
7 #ifndef TRAKER_H
8 #define TRAKER_H
9 
10 #include <iostream>
11 #include <mutex>
12 #include <thread>
13 #include <memory>
14 #include <vector>
15 
16 #include "tracker_config.h"
17 #include "spdlog/spdlog.h"
18 
20 
23 {
25  std::jthread thread;
27  std::mutex mutex;
28  friend class tracker_manager;
29 
30  public:
32  volatile bool exiting;
34  virtual ~tracker_base()
35  {
36  stop_tracker();
37  spdlog::debug("tracker_base::~tracker_base()");
38  }
40  virtual void start_tracker(void) = 0;
42  void stop_tracker(void)
43  {
44  exiting = true;
45  if (thread.joinable())
46  {
47  thread.join();
48  }
49  }
50 };
51 
53 class prometheus_server;
54 
56 
58 template<typename ENV, typename EVENT>
60 {
61 public:
63  using event = EVENT;
67  using tracker_event_handler = std::shared_ptr<event_handler<EVENT>>;
68 
70  struct prometheus_event_handler final : public event_handler<EVENT>
71  {
73  {
74  }
76  {
77  }
78  };
80  struct plain_text_event_printer final : public event_handler<EVENT>
81  {
83  {
84  }
85  };
87  struct json_event_printer final : public event_handler<EVENT>
88  {
90  {
91  }
92  };
94  struct csv_event_printer final : public event_handler<EVENT>
95  {
97  {
98  }
99  };
103  {
104  }
105 
107  stop_tracker();
108  }
109 };
110 
112 
114 template<typename TRACKER>
115 concept tracker_concept = requires
116 {
118  typename TRACKER::event;
120  typename TRACKER::config_data;
122  typename TRACKER::tracker_event_handler;
124  typename TRACKER::prometheus_event_handler;
126  typename TRACKER::json_event_printer;
128  typename TRACKER::plain_text_event_printer;
130  typename TRACKER::csv_event_printer;
131 };
132 
134 
139 template<tracker_concept TRACKER, typename EVENT>
140 static int handle_tracker_event(void *ctx, void *data, size_t data_sz)
141 {
142  if (!data || !ctx)
143  {
144  std::cout << "warn: no data or no ctx" << std::endl;
145  return 0;
146  }
147  const EVENT &e = *(const EVENT *)data;
148  TRACKER &pt = *(TRACKER *)ctx;
149  auto event = tracker_event<EVENT>{ e };
150  if (pt.current_config.handler)
151  {
152  pt.current_config.handler->do_handle_event(event);
153  }
154  else
155  {
156  std::cout << "warn: no handler for tracker event" << std::endl;
157  }
158  return 0;
159 }
160 
161 #endif
eunomia_mode::server
@ server
tracker_base::start_tracker
virtual void start_tracker(void)=0
start the tracker thread
tracker_base::~tracker_base
virtual ~tracker_base()
constructor
Definition: tracker.h:34
tracker_with_config::plain_text_event_printer
print to plain text
Definition: tracker.h:80
event_handler
the event handler for single type
Definition: event_handler.h:52
tracker_with_config::plain_text_event_printer::handle
void handle(tracker_event< EVENT > &e)
implement this function to handle the event
Definition: tracker.h:82
tracker_config< ipc_env, ipc_event >
tracker_with_config::csv_event_printer
print to csv
Definition: tracker.h:94
tracker_with_config
tracker template with env and data
Definition: tracker.h:59
tracker_with_config::prometheus_event_handler::prometheus_event_handler
prometheus_event_handler(prometheus_server &server)
Definition: tracker.h:72
tracker_with_config::json_event_printer::handle
void handle(tracker_event< EVENT > &e)
implement this function to handle the event
Definition: tracker.h:89
tracker_with_config::csv_event_printer::handle
void handle(tracker_event< EVENT > &e)
implement this function to handle the event
Definition: tracker.h:96
tracker_base::exiting
volatile bool exiting
is the tracker exiting
Definition: tracker.h:32
tracker_manager
tracker manager for owning and managing tracker instances
Definition: tracker_manager.h:21
tracker_concept
concept tracker_concept
concept for a single thread tracker
Definition: tracker.h:115
tracker_with_config::prometheus_event_handler
default event handlers for prometheus
Definition: tracker.h:70
tracker_with_config::tracker_with_config
tracker_with_config(tracker_config< ENV, EVENT > config)
Definition: tracker.h:102
tracker_event
the basic event type
Definition: event_handler.h:31
tracker_base
the base type of a tracker
Definition: tracker.h:22
tracker_base::stop_tracker
void stop_tracker(void)
stop the tracker thread
Definition: tracker.h:42
tracker_config.h
tracker_with_config::json_event_printer
used for json exporter, inherits from json_event_handler
Definition: tracker.h:87
tracker_with_config::~tracker_with_config
virtual ~tracker_with_config()
Definition: tracker.h:106
prometheus_server
Definition: prometheus_server.h:22
tracker_with_config< ipc_env, ipc_event >::tracker_event_handler
std::shared_ptr< event_handler< ipc_event > > tracker_event_handler
type alias for event handler
Definition: tracker.h:67
config
seccomp_config config
Definition: seccomp_test.cpp:13
tracker_with_config::current_config
tracker_config< ENV, EVENT > current_config
config data
Definition: tracker.h:101
tracker_with_config::prometheus_event_handler::handle
void handle(tracker_event< EVENT > &e)
implement this function to handle the event
Definition: tracker.h:75