+(* Store the node_info and hostname for each connection, fetched
+ * once just after we connect since these don't normally change.
+ * Hash of connid -> (C.node_info, hostname option, uri)
+ *)
+let static_conn_info = Hashtbl.create 13
+
+let open_connection uri =
+ (* If this fails, let the exception escape and be printed
+ * in the global exception handler.
+ *)
+ let conn = C.connect ~name:uri () in
+
+ let node_info = C.get_node_info conn in
+ let hostname =
+ try Some (C.get_hostname conn)
+ with
+ | Libvirt.Not_supported "virConnectGetHostname"
+ | Libvirt.Virterror _ -> None in
+
+ (* Add it to our list of connections. *)
+ let conn_id = add_conn conn in
+ Hashtbl.add static_conn_info conn_id (node_info, hostname, uri)
+
+(* Stores the state and history for each domain.
+ * Hash of (connid, domid) -> mutable domhistory structure.
+ * We never delete entries in this hash table, which may be a problem
+ * for very very long-lived instances of virt-ctrl.
+ *)
+type domhistory = {
+ (* for %CPU calculation: *)
+ mutable last_cpu_time : int64; (* last virDomainInfo->cpuTime *)
+ mutable last_time : float; (* exact time we measured the above *)
+
+ (* historical data for graphs etc: *)
+ mutable hist : dhentry array; (* historical data *)
+ mutable hist_posn : int; (* position within array *)
+}
+and dhentry = {
+ hist_cpu : int; (* historical %CPU entry *)
+ hist_mem : int64; (* historical memory entry (KB) *)
+}
+
+let domhistory = Hashtbl.create 13
+
+let empty_dhentry = {
+ hist_cpu = 0; hist_mem = 0L;
+}
+let new_domhistory () = {
+ last_cpu_time = 0L; last_time = 0.;
+ hist = Array.make 0 empty_dhentry; hist_posn = 0;
+}
+
+(* These set limits on the amount of history we collect. *)
+let hist_max = 86400 (* max history stored, seconds *)
+let hist_rot = 3600 (* rotation of array when we hit max *)
+