9496ca81a67bd6d330afa9cfbfbec24dc33b598d
[virt-top.git] / src / csv_output.ml
1 (* 'top'-like tool for libvirt domains.
2    (C) Copyright 2007-2017 Richard W.M. Jones, Red Hat Inc.
3    http://libvirt.org/
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 *)
19
20 (* CSV output functions. *)
21
22 open Printf
23 open ExtList
24
25 open Collect
26
27 module C = Libvirt.Connect
28
29 (* Hook for CSV support (see [opt_csv.ml]). *)
30 let csv_write : (string list -> unit) ref =
31   ref (
32     fun _ -> ()
33   )
34
35 (* Write CSV header row. *)
36 let write_csv_header (csv_cpu, csv_mem, csv_block, csv_net) block_in_bytes =
37   (!csv_write) (
38     [ "Hostname"; "Time"; "Arch"; "Physical CPUs";
39       "Count"; "Running"; "Blocked"; "Paused"; "Shutdown";
40       "Shutoff"; "Crashed"; "Active"; "Inactive";
41       "%CPU";
42       "Total hardware memory (KB)";
43       "Total memory (KB)"; "Total guest memory (KB)";
44       "Total CPU time (ns)" ] @
45       (* These fields are repeated for each domain: *)
46     [ "Domain ID"; "Domain name"; ] @
47     (if csv_cpu then [ "CPU (ns)"; "%CPU"; ] else []) @
48     (if csv_mem then [ "Mem (bytes)"; "%Mem";] else []) @
49     (if csv_block && not block_in_bytes
50        then [ "Block RDRQ"; "Block WRRQ"; ] else []) @
51     (if csv_block && block_in_bytes
52        then [ "Block RDBY"; "Block WRBY"; ] else []) @
53     (if csv_net then [ "Net RXBY"; "Net TXBY" ] else [])
54   )
55
56 (* Write summary data to CSV file. *)
57 let append_csv (_, _, _, _, _, node_info, hostname, _) (* setup *)
58                (csv_cpu, csv_mem, csv_block, csv_net)
59                { rd_doms = doms;
60                  rd_printable_time = printable_time;
61                  rd_nr_pcpus = nr_pcpus; rd_total_cpu = total_cpu;
62                  rd_totals = totals } (* state *) =
63   (* The totals / summary fields. *)
64   let (count, running, blocked, paused, shutdown, shutoff,
65        crashed, active, inactive,
66        total_cpu_time, total_memory, total_domU_memory) = totals in
67
68   let percent_cpu = 100. *. total_cpu_time /. total_cpu in
69
70   let summary_fields = [
71     hostname; printable_time; node_info.C.model; string_of_int nr_pcpus;
72     string_of_int count; string_of_int running; string_of_int blocked;
73     string_of_int paused; string_of_int shutdown; string_of_int shutoff;
74     string_of_int crashed; string_of_int active; string_of_int inactive;
75     sprintf "%2.1f" percent_cpu;
76     Int64.to_string node_info.C.memory;
77     Int64.to_string total_memory; Int64.to_string total_domU_memory;
78     Int64.to_string (Int64.of_float total_cpu_time)
79   ] in
80
81   (* The domains.
82    *
83    * Sort them by ID so that the list of relatively stable.  Ignore
84    * inactive domains.
85    *)
86   let doms = List.filter_map (
87     function
88     | _, Inactive -> None               (* Ignore inactive domains. *)
89     | name, Active rd -> Some (name, rd)
90   ) doms in
91   let cmp (_, { rd_domid = rd_domid1 }) (_, { rd_domid = rd_domid2 }) =
92     compare rd_domid1 rd_domid2
93   in
94   let doms = List.sort ~cmp doms in
95
96   let string_of_int64_option = Option.map_default Int64.to_string "" in
97
98   let domain_fields = List.map (
99     fun (domname, rd) ->
100       [ string_of_int rd.rd_domid; domname ] @
101         (if csv_cpu then [
102            string_of_float rd.rd_cpu_time; string_of_float rd.rd_percent_cpu
103          ] else []) @
104         (if csv_mem then [
105             Int64.to_string rd.rd_mem_bytes; Int64.to_string rd.rd_mem_percent
106          ] else []) @
107         (if csv_block then [
108            string_of_int64_option rd.rd_block_rd_info;
109            string_of_int64_option rd.rd_block_wr_info;
110          ] else []) @
111         (if csv_net then [
112            string_of_int64_option rd.rd_net_rx_bytes;
113            string_of_int64_option rd.rd_net_tx_bytes;
114          ] else [])
115   ) doms in
116   let domain_fields = List.flatten domain_fields in
117
118   (!csv_write) (summary_fields @ domain_fields)