Convert .cvsignore files to .cvsignore files, and remove some generated files.
[virt-top.git] / examples / list_domains.ml
1 (* Simple demo program showing how to list out domains.
2    Usage: list_domains [URI]
3    (C) Copyright 2007 Richard W.M. Jones, Red Hat Inc.
4    http://libvirt.org/
5    $Id: list_domains.ml,v 1.1 2007/08/06 10:16:53 rjones Exp $
6  *)
7
8 open Printf
9
10 module C = Libvirt.Connect
11 module D = Libvirt.Domain
12 module N = Libvirt.Network
13
14 let () =
15   try
16     let name =
17       if Array.length Sys.argv >= 2 then
18         Some (Sys.argv.(1))
19       else
20         None in
21     let conn = C.connect ?name () in
22
23     (* List running domains. *)
24     let n = C.num_of_domains conn in
25     let ids = C.list_domains conn n in
26     let domains = Array.map (D.lookup_by_id conn) ids in
27     Array.iter (
28       fun dom ->
29         printf "%8d %s\n%!" (D.get_id dom) (D.get_name dom)
30     ) domains;
31
32     (* List inactive domains. *)
33     let n = C.num_of_defined_domains conn in
34     let names = C.list_defined_domains conn n in
35     Array.iter (
36       fun name ->
37         printf "inactive %s\n%!" name
38     ) names;
39   with
40     Libvirt.Virterror err ->
41       eprintf "error: %s\n" (Libvirt.Virterror.to_string err)
42
43 let () =
44   (* Run the garbage collector which is a good way to check for
45    * memory corruption errors and reference counting issues in libvirt.
46    *)
47   Gc.compact ()