Apache Mesos
system.hpp
Go to the documentation of this file.
1 // Licensed under the Apache License, Version 2.0 (the "License");
2 // you may not use this file except in compliance with the License.
3 // You may obtain a copy of the License at
4 //
5 // http://www.apache.org/licenses/LICENSE-2.0
6 //
7 // Unless required by applicable law or agreed to in writing, software
8 // distributed under the License is distributed on an "AS IS" BASIS,
9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 // See the License for the specific language governing permissions and
11 // limitations under the License
12 
13 #ifndef __PROCESS_SYSTEM_HPP__
14 #define __PROCESS_SYSTEM_HPP__
15 
16 #include <string>
17 
18 #include <process/future.hpp>
19 #include <process/help.hpp>
20 #include <process/http.hpp>
21 #include <process/process.hpp>
22 
25 
26 #include <stout/os.hpp>
27 
28 namespace process {
29 
30 // The System process provides HTTP endpoints for retrieving system metrics,
31 // such as CPU load and memory usage. This is started by default during the
32 // initialization of libprocess.
33 class System : public Process<System>
34 {
35 public:
37  : ProcessBase("system"),
38  load_1min(
39  self().id + "/load_1min",
40  defer(self(), &System::_load_1min)),
41  load_5min(
42  self().id + "/load_5min",
43  defer(self(), &System::_load_5min)),
44  load_15min(
45  self().id + "/load_15min",
46  defer(self(), &System::_load_15min)),
47  cpus_total(
48  self().id + "/cpus_total",
49  defer(self(), &System::_cpus_total)),
50  mem_total_bytes(
51  self().id + "/mem_total_bytes",
52  defer(self(), &System::_mem_total_bytes)),
53  mem_free_bytes(
54  self().id + "/mem_free_bytes",
55  defer(self(), &System::_mem_free_bytes)) {}
56 
57  ~System() override {}
58 
59 protected:
60  void initialize() override
61  {
62  // TODO(dhamon): Check return values.
63  metrics::add(load_1min);
64  metrics::add(load_5min);
65  metrics::add(load_15min);
66  metrics::add(cpus_total);
67  metrics::add(mem_total_bytes);
68  metrics::add(mem_free_bytes);
69 
70  route("/stats.json", statsHelp(), &System::stats);
71  }
72 
73  void finalize() override
74  {
75  metrics::remove(load_1min);
76  metrics::remove(load_5min);
77  metrics::remove(load_15min);
78  metrics::remove(cpus_total);
79  metrics::remove(mem_total_bytes);
80  metrics::remove(mem_free_bytes);
81  }
82 
83 private:
84  static std::string statsHelp()
85  {
86  return HELP(
87  TLDR(
88  "Shows local system metrics."),
90  "> cpus_total Total number of available CPUs",
91  "> load_1min Average system load for last"
92  " minute in uptime(1) style",
93  "> load_5min Average system load for last"
94  " 5 minutes in uptime(1) style",
95  "> load_15min Average system load for last"
96  " 15 minutes in uptime(1) style",
97  "> memory_total_bytes Total system memory in bytes",
98  "> memory_free_bytes Free system memory in bytes"));
99  }
100 
101  // Gauge handlers.
102  Future<double> _load_1min()
103  {
104  Try<os::Load> load = os::loadavg();
105  if (load.isSome()) {
106  return load->one;
107  }
108  return Failure("Failed to get loadavg: " + load.error());
109  }
110 
111 
112  Future<double> _load_5min()
113  {
114  Try<os::Load> load = os::loadavg();
115  if (load.isSome()) {
116  return load->five;
117  }
118  return Failure("Failed to get loadavg: " + load.error());
119  }
120 
121 
122  Future<double> _load_15min()
123  {
124  Try<os::Load> load = os::loadavg();
125  if (load.isSome()) {
126  return load->fifteen;
127  }
128  return Failure("Failed to get loadavg: " + load.error());
129  }
130 
131 
132  Future<double> _cpus_total()
133  {
134  Try<long> cpus = os::cpus();
135  if (cpus.isSome()) {
136  return cpus.get();
137  }
138  return Failure("Failed to get cpus: " + cpus.error());
139  }
140 
141 
142  Future<double> _mem_total_bytes()
143  {
145  if (memory.isSome()) {
146  return static_cast<double>(memory->total.bytes());
147  }
148  return Failure("Failed to get memory: " + memory.error());
149  }
150 
151 
152  Future<double> _mem_free_bytes()
153  {
155  if (memory.isSome()) {
156  return static_cast<double>(memory->free.bytes());
157  }
158  return Failure("Failed to get memory: " + memory.error());
159  }
160 
161  // HTTP endpoints.
163  {
164  JSON::Object object;
165  Try<os::Load> load = os::loadavg();
166  if (load.isSome()) {
167  object.values["avg_load_1min"] = load->one;
168  object.values["avg_load_5min"] = load->five;
169  object.values["avg_load_15min"] = load->fifteen;
170  }
171 
172  Try<long> cpus = os::cpus();
173  if (cpus.isSome()) {
174  object.values["cpus_total"] = cpus.get();
175  }
176 
178  if (memory.isSome()) {
179  object.values["mem_total_bytes"] = memory->total.bytes();
180  object.values["mem_free_bytes"] = memory->free.bytes();
181  }
182 
183  return http::OK(object, request.url.query.get("jsonp"));
184  }
185 
186  metrics::PullGauge load_1min;
187  metrics::PullGauge load_5min;
188  metrics::PullGauge load_15min;
189 
190  metrics::PullGauge cpus_total;
191 
192  metrics::PullGauge mem_total_bytes;
193  metrics::PullGauge mem_free_bytes;
194 };
195 
196 } // namespace process {
197 
198 #endif // __PROCESS_SYSTEM_HPP__
Definition: system.hpp:33
Future< Response > request(const Request &request, bool streamedResponse=false)
Asynchronously sends an HTTP request to the process and returns the HTTP response once the entire res...
T & get()&
Definition: try.hpp:80
URL url
Definition: http.hpp:544
Future< Nothing > remove(const Metric &metric)
Definition: metrics.hpp:109
Definition: check.hpp:33
Definition: future.hpp:668
void initialize() override
Invoked when a process gets spawned.
Definition: system.hpp:60
~System() override
Definition: system.hpp:57
Definition: uuid.hpp:33
Definition: process.hpp:72
void route(const std::string &name, const Option< std::string > &help, const HttpRequestHandler &handler, const RouteOptions &options=RouteOptions())
Sets up a handler for HTTP requests with the specified name.
Future< Nothing > add(const T &metric)
Definition: metrics.hpp:95
Definition: http.hpp:533
Definition: json.hpp:158
hashmap< std::string, std::string > query
Definition: http.hpp:189
Try< Load > loadavg()
Definition: os.hpp:280
Definition: pull_gauge.hpp:46
std::string TLDR(const std::string &tldr)
Definition: help.hpp:69
Try< long > cpus()
Definition: os.hpp:265
bool isSome() const
Definition: try.hpp:77
static Try error(const E &e)
Definition: try.hpp:43
void finalize() override
Invoked when a process is terminated.
Definition: system.hpp:73
std::string HELP(const std::string &tldr, const Option< std::string > &description=None(), const Option< std::string > &authentication=None(), const Option< std::string > &authorization=None(), const Option< std::string > &references=None())
Bytes total
Definition: os.hpp:36
System()
Definition: system.hpp:36
Definition: executor.hpp:48
uint64_t bytes() const
Definition: bytes.hpp:79
PID< System > self() const
Returns the PID of the process.
Definition: process.hpp:514
Definition: http.hpp:678
double fifteen
Definition: os.hpp:28
Try< Memory > memory()
Definition: freebsd.hpp:78
Definition: process.hpp:505
Deferred< void()> defer(const PID< T > &pid, void(T::*method)())
Definition: defer.hpp:35
Bytes free
Definition: os.hpp:37
double five
Definition: os.hpp:27
Option< Value > get(const Key &key) const
Definition: hashmap.hpp:121
double one
Definition: os.hpp:26
std::string DESCRIPTION(T &&...args)
Definition: help.hpp:76
Definition: future.hpp:58