File: get_all_domain_stats.ml

package info (click to toggle)
ocaml-libvirt 0.6.1.5-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 796 kB
  • sloc: sh: 3,306; ml: 2,567; ansic: 1,776; perl: 804; makefile: 268
file content (73 lines) | stat: -rw-r--r-- 1,960 bytes parent folder | download | duplicates (2)
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
(* Example of using Domain.get_all_domain_stats (virConnectGetAllDomainStats).
 * Usage: get_all_domain_stats
 * https://libvirt.org/
 *)

open Printf

module C = Libvirt.Connect
module D = Libvirt.Domain

let print_stats conn stats =
  try
    Array.iter (
      fun { D.dom_uuid = uuid; D.params = params } ->
        let dom = D.lookup_by_uuid conn uuid in
        printf "domain %s:\n" (D.get_name dom);
        Array.iteri (
          fun i (field, value) ->
            printf "\t%-20s = " field;
            (match value with
             | D.TypedFieldInt32 i -> printf "%ld" i
             | D.TypedFieldUInt32 i -> printf "%ld" i
             | D.TypedFieldInt64 i -> printf "%Ld" i
             | D.TypedFieldUInt64 i -> printf "%Ld" i
             | D.TypedFieldFloat f -> printf "%g" f
             | D.TypedFieldBool b -> printf "%b" b
             | D.TypedFieldString s -> printf "%S" s);
            printf "\n";
        ) params;
        printf "\n"
    ) stats
  with
    Libvirt.Virterror err ->
      eprintf "error: %s\n" (Libvirt.Virterror.to_string err)

let () =
  if Array.length Sys.argv <> 1 then (
    eprintf "error: get_all_domain_stats\n";
    exit 1
  );

  let conn = C.connect_auth_readonly (C.get_auth_default ()) in

  let what = [
    D.StatsState;
    D.StatsCpuTotal;
    D.StatsBalloon;
    D.StatsVcpu;
    D.StatsInterface;
    D.StatsBlock;
  ] in
  let who = [] in (* empty list means returns all domains *)

  let quit = ref false in

  while not !quit do
    let stats = D.get_all_domain_stats conn what who in

    if stats <> [||] then print_stats conn stats
    else (
      printf "no guests found\n";
      quit := true
    );
    flush stdout;

    (* Run the garbage collector which is a good way to check for
     * memory corruption errors and reference counting issues in
     * libvirt.  You shouldn't do this in ordinary programs.
     *)
    Gc.compact ();

    if not !quit then Unix.sleep 3
  done