2018-03-24 21:26:10 +00:00
|
|
|
(* (c) 2017, 2018 Hannes Mehnert, all rights reserved *)
|
2017-05-26 14:30:34 +00:00
|
|
|
|
|
|
|
(* the process responsible for gathering statistics (CPU + mem + network) *)
|
|
|
|
|
|
|
|
(* a shared unix domain socket between vmmd and vmm_stats is used as
|
|
|
|
communication channel, where the vmmd can issue commands:
|
|
|
|
|
|
|
|
- add pid taps
|
|
|
|
- remove pid
|
|
|
|
- statistics pid
|
|
|
|
|
2018-04-30 22:14:33 +00:00
|
|
|
every 10 seconds, statistics of all registered pids are recorded. `statistics`
|
2017-05-26 14:30:34 +00:00
|
|
|
reports last recorded stats *)
|
|
|
|
|
|
|
|
open Lwt.Infix
|
|
|
|
|
2019-03-27 23:11:43 +00:00
|
|
|
open Albatross_stats_pure
|
2018-10-25 14:55:54 +00:00
|
|
|
|
|
|
|
let t = ref (empty ())
|
2017-05-26 14:30:34 +00:00
|
|
|
|
|
|
|
let pp_sockaddr ppf = function
|
|
|
|
| Lwt_unix.ADDR_UNIX str -> Fmt.pf ppf "unix domain socket %s" str
|
|
|
|
| Lwt_unix.ADDR_INET (addr, port) -> Fmt.pf ppf "TCP %s:%d"
|
|
|
|
(Unix.string_of_inet_addr addr) port
|
|
|
|
|
2019-10-10 20:26:36 +00:00
|
|
|
let handle s addr =
|
2017-05-26 14:30:34 +00:00
|
|
|
Logs.info (fun m -> m "handling stats connection %a" pp_sockaddr addr) ;
|
2019-01-14 23:25:59 +00:00
|
|
|
let rec loop () =
|
2018-09-09 18:52:04 +00:00
|
|
|
Vmm_lwt.read_wire s >>= function
|
2018-10-25 14:02:04 +00:00
|
|
|
| Error _ ->
|
|
|
|
Logs.err (fun m -> m "exception while reading") ;
|
2019-01-14 23:25:59 +00:00
|
|
|
Lwt.return_unit
|
2018-10-22 22:02:05 +00:00
|
|
|
| Ok wire ->
|
2018-10-25 14:55:54 +00:00
|
|
|
match handle !t s wire with
|
2018-10-25 14:02:04 +00:00
|
|
|
| Error (`Msg msg) ->
|
|
|
|
Vmm_lwt.write_wire s (fst wire, `Failure msg) >>= fun _ ->
|
2019-01-14 23:25:59 +00:00
|
|
|
Lwt.return_unit
|
|
|
|
| Ok (t', close, out) ->
|
2018-10-25 14:02:04 +00:00
|
|
|
t := t' ;
|
|
|
|
Vmm_lwt.write_wire s (fst wire, `Success (`String out)) >>= function
|
|
|
|
| Ok () ->
|
2019-01-14 23:25:59 +00:00
|
|
|
(match close with
|
2019-11-11 20:49:51 +00:00
|
|
|
| Some (_, s') ->
|
2018-10-25 14:02:04 +00:00
|
|
|
Vmm_lwt.safe_close s' >>= fun () ->
|
|
|
|
(* read the next *)
|
2019-01-14 23:25:59 +00:00
|
|
|
loop ()
|
|
|
|
| None -> loop ())
|
2018-10-25 14:02:04 +00:00
|
|
|
| Error _ ->
|
|
|
|
Logs.err (fun m -> m "error while writing") ;
|
2019-01-14 23:25:59 +00:00
|
|
|
Lwt.return_unit
|
2017-05-26 14:30:34 +00:00
|
|
|
in
|
2019-01-14 23:25:59 +00:00
|
|
|
loop () >>= fun () ->
|
|
|
|
Vmm_lwt.safe_close s
|
2017-05-26 14:30:34 +00:00
|
|
|
|
2018-12-06 20:40:52 +00:00
|
|
|
let timer () =
|
2018-10-25 14:55:54 +00:00
|
|
|
let t', outs = tick !t in
|
2018-09-20 20:53:42 +00:00
|
|
|
t := t' ;
|
2019-01-06 02:00:16 +00:00
|
|
|
Lwt_list.iter_p (fun (s, id, stat) ->
|
2018-09-20 20:53:42 +00:00
|
|
|
Vmm_lwt.write_wire s stat >>= function
|
|
|
|
| Ok () -> Lwt.return_unit
|
|
|
|
| Error `Exception ->
|
2019-01-06 02:00:16 +00:00
|
|
|
Logs.debug (fun m -> m "removing entry %a" Vmm_core.Name.pp id) ;
|
|
|
|
t := remove_entry !t id ;
|
2018-09-20 20:53:42 +00:00
|
|
|
Vmm_lwt.safe_close s)
|
2018-12-06 20:40:52 +00:00
|
|
|
outs
|
2017-05-26 14:30:34 +00:00
|
|
|
|
2019-10-12 00:15:44 +00:00
|
|
|
let m = Vmm_core.conn_metrics "unix"
|
2019-10-10 20:26:36 +00:00
|
|
|
|
2019-10-29 22:37:42 +00:00
|
|
|
let jump _ interval influx tmpdir =
|
|
|
|
Sys.(set_signal sigpipe Signal_ignore);
|
|
|
|
Albatross_cli.set_tmpdir tmpdir;
|
2018-04-03 20:49:00 +00:00
|
|
|
let interval = Duration.(to_f (of_sec interval)) in
|
2017-05-26 14:30:34 +00:00
|
|
|
Lwt_main.run
|
2019-10-10 20:26:36 +00:00
|
|
|
(Albatross_cli.init_influx "albatross_stats" influx;
|
|
|
|
Vmm_lwt.server_socket `Stats >>= fun s ->
|
2018-12-06 20:40:52 +00:00
|
|
|
let _ev = Lwt_engine.on_timer interval true (fun _e -> Lwt.async timer) in
|
2017-05-26 14:30:34 +00:00
|
|
|
let rec loop () =
|
|
|
|
Lwt_unix.accept s >>= fun (cs, addr) ->
|
2019-10-10 20:26:36 +00:00
|
|
|
m `Open;
|
|
|
|
Lwt.async (fun () -> handle cs addr >|= fun () -> m `Close);
|
2017-05-26 14:30:34 +00:00
|
|
|
loop ()
|
|
|
|
in
|
|
|
|
loop ())
|
|
|
|
|
|
|
|
open Cmdliner
|
2019-03-27 23:11:43 +00:00
|
|
|
open Albatross_cli
|
2017-05-26 14:30:34 +00:00
|
|
|
|
2018-04-03 20:49:00 +00:00
|
|
|
let interval =
|
|
|
|
let doc = "Interval between statistics gatherings (in seconds)" in
|
2018-10-26 19:35:40 +00:00
|
|
|
Arg.(value & opt int 10 & info [ "interval" ] ~doc)
|
2018-04-03 20:49:00 +00:00
|
|
|
|
2017-05-26 14:30:34 +00:00
|
|
|
let cmd =
|
2019-10-29 22:37:42 +00:00
|
|
|
Term.(term_result (const jump $ setup_log $ interval $ influx $ tmpdir)),
|
2019-11-11 20:49:51 +00:00
|
|
|
Term.info "albatross_stats" ~version
|
2017-05-26 14:30:34 +00:00
|
|
|
|
|
|
|
let () = match Term.eval cmd with `Ok () -> exit 0 | _ -> exit 1
|