Missing virt-df/.depend.
[virt-top.git] / mlvirsh / mlvirsh.ml
index 5b63a77..8052506 100644 (file)
@@ -1,10 +1,22 @@
 (* virsh-like command line tool.
    (C) Copyright 2007 Richard W.M. Jones, Red Hat Inc.
    http://libvirt.org/
-   $Id: mlvirsh.ml,v 1.2 2007/08/21 13:24:09 rjones Exp $
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *)
 
-open ExtString
 open Printf
 
 module C = Libvirt.Connect
@@ -63,6 +75,48 @@ and input_all chan =
   done;
   Buffer.contents buf
 
+(* Split a string at a separator.
+ * Functions copied from extlib Copyright (C) 2003 Nicolas Cannasse et al.
+ * to avoid the explicit dependency on extlib.
+ *)
+let str_find str sub =
+  let sublen = String.length sub in
+  if sublen = 0 then
+    0
+  else
+    let found = ref 0 in
+    let len = String.length str in
+    try
+      for i = 0 to len - sublen do
+        let j = ref 0 in
+        while String.unsafe_get str (i + !j) = String.unsafe_get sub !j do
+          incr j;
+          if !j = sublen then begin found := i; raise Exit; end;
+        done;
+      done;
+      raise Not_found
+    with
+      Exit -> !found
+
+let str_split str sep =
+  let p = str_find str sep in
+  let len = String.length sep in
+  let slen = String.length str in
+  String.sub str 0 p, String.sub str (p + len) (slen - p - len)
+
+let str_nsplit str sep =
+  if str = "" then []
+  else (
+    let rec nsplit str sep =
+      try
+       let s1 , s2 = str_split str sep in
+       s1 :: nsplit s2 sep
+      with
+       Not_found -> [str]
+    in
+    nsplit str sep
+  )
+
 (* Hypervisor connection. *)
 type conn_t = No_connection | RO of Libvirt.ro C.t | RW of Libvirt.rw C.t
 let conn = ref No_connection
@@ -125,6 +179,12 @@ let do_command =
     | [str1; str2] -> print (fn (arg1 str1) (Some (arg2 str2)))
     | _ -> failwith "incorrect number of arguments for function"
   in
+  let cmd012 print fn arg1 arg2 = function (* Command with 0, 1 or 2 args. *)
+    | [] -> print (fn None None)
+    | [str1] -> print (fn (Some (arg1 str1)) None)
+    | [str1; str2] -> print (fn (Some (arg1 str1)) (Some (arg2 str2)))
+    | _ -> failwith "incorrect number of arguments for function"
+  in
   let cmdN print fn =          (* Command with any number of args. *)
     fun args -> print (fn args)
   in
@@ -205,7 +265,7 @@ let do_command =
     let cpumap =
       String.make (C.cpumaplen (C.maxcpus_of_node_info info)) '\000' in
     List.iter (C.use_cpu cpumap)
-      (List.map int_of_string (String.nsplit str ","));
+      (List.map int_of_string (str_nsplit str ","));
     cpumap
   in
 
@@ -213,6 +273,7 @@ let do_command =
   let no_return _ = () in
   let print_int i = print_endline (string_of_int i) in
   let print_int64 i = print_endline (Int64.to_string i) in
+  let print_int64_array a = Array.iter print_int64 a in
   let print_bool b = print_endline (string_of_bool b) in
   let print_version v =
     let major = v / 1000000 in
@@ -413,6 +474,19 @@ let do_command =
       cmd1 print_endline D.get_xml_desc
        (arg_full_connection domain_of_string),
       "Print the XML description of a domain.";
+    "freecell",
+      cmd012 print_int64_array (
+       fun start max ->
+         let conn = get_readonly_connection () in
+         match start, max with
+         | None, _ ->
+             [| C.node_get_free_memory conn |]
+         | Some start, None ->
+             C.node_get_cells_free_memory conn start 1
+         | Some start, Some max ->
+             C.node_get_cells_free_memory conn start max
+          ) int_of_string int_of_string,
+      "Display free memory for machine, NUMA cell or range of cells";
     "get-autostart",
       cmd1 print_bool D.get_autostart
        (arg_readonly_connection domain_of_string),
@@ -647,7 +721,7 @@ let rec interactive_mode () =
     | RW _ -> "mlvirsh# " in
   print_string prompt;
   let command = read_line () in
-  (match String.nsplit command " " with
+  (match str_nsplit command " " with
    | [] -> ()
    | command :: args ->
        do_command command args