[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt] [PATCH 4/4] net-dhcp-leases: Add virsh support



Use virNetworkGetDHCPLeases and virNetworkGetDHCPLeaseForMAC in virsh.

The new feature supports the follwing methods:

1. Retrieve leases info for a given virtual network

2. Retrieve leases info for given network interface

tools/virsh-domain-monitor.c
   * Introduce new command : net-dhcp-leases
     Example Usage: net-dhcp-leases <network> [mac]

     virsh # net-dhcp-leases default

     Expiry Time          MAC address          IP address        Hostname     ClientId
     ----------------------------------------------------------------------------------
     11-09-2013 03:53:11  52:54:00:89:4e:97    192.168.101.130   f18          *
     11-09-2013 03:45:20  52:54:00:fe:4c:4f    192.168.101.197   *            *

tools/virsh.pod
   * Document new command



---
 tools/virsh-network.c | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++
 tools/virsh.pod       |  6 ++++
 2 files changed, 98 insertions(+)

diff --git a/tools/virsh-network.c b/tools/virsh-network.c
index 8ddd5ca..6319d29 100644
--- a/tools/virsh-network.c
+++ b/tools/virsh-network.c
@@ -1129,6 +1129,92 @@ cmdNetworkEdit(vshControl *ctl, const vshCmd *cmd)
 
     return ret;
 }
+/*
+ * "net-dhcp-leases" command
+ */
+static const vshCmdInfo info_network_dhcp_leases[] = {
+    {.name = "help",
+     .data = N_("Print lease info for a given network")
+    },
+    {.name = "desc",
+     .data = N_("Print lease info for a given network")
+    },
+    {.name = NULL}
+};
+
+static const vshCmdOptDef opts_network_dhcp_leases[] = {
+    {.name = "network",
+     .type = VSH_OT_DATA,
+     .flags = VSH_OFLAG_REQ,
+     .help = N_("network name or uuid")
+    },
+    {.name = "mac",
+     .type = VSH_OT_DATA,
+     .flags = VSH_OFLAG_NONE,
+     .help = N_("MAC address")
+    },
+    {.name = NULL}
+};
+
+static bool
+cmdNetworkDHCPLeases(vshControl *ctl, const vshCmd *cmd)
+{
+    const char *name = NULL;
+    const char *mac = NULL;
+    virNetworkDHCPLeasesPtr *leases = NULL;
+    virNetworkDHCPLeases lease_mac;
+    int nleases = 0;
+    bool ret = false;
+    size_t i;
+    unsigned int flags = 0;
+    virNetworkPtr network = NULL;
+
+    if (vshCommandOptString(cmd, "mac", &mac) < 0)
+        goto cleanup;
+
+    if (!(network = vshCommandOptNetworkBy(ctl, cmd, &name,
+                                           VSH_BYNAME | VSH_BYUUID)))
+        goto cleanup;
+
+    nleases = mac ? virNetworkGetDHCPLeaseForMAC(network, mac, &lease_mac, flags)
+              :virNetworkGetDHCPLeases(network, &leases, flags);
+
+    if (nleases < 0) {
+        vshError(ctl, _("Failed to get leases info for %s"), name);
+        goto cleanup;
+    }
+
+    vshPrintExtra(ctl, " %-20s %-20s %-17s %-12s %s\n%s%s\n",
+                  _("Expiry Time"), _("MAC address"),
+                  _("IP address"), _("Hostname"), _("ClientId"),
+                  "--------------------------------------------",
+                  "--------------------------------------------");
+
+    for (i = 0; i < nleases; i++) {
+        virNetworkDHCPLeasesPtr lease = mac ? &lease_mac : leases[i];
+        time_t expirytime_tmp = lease->expirytime;
+        struct tm ts;
+        char expirytime[32];
+        ts = *localtime_r(&expirytime_tmp, &ts);
+        strftime(expirytime, sizeof(expirytime), "%d-%m-%Y %H:%M:%S", &ts);
+
+        vshPrintExtra(ctl, " %-20s %-20s %-17s %-12s %s\n",
+                      expirytime, lease->mac, lease->ipaddr,
+                      lease->hostname, lease->clientid);
+    }
+
+    ret = true;
+
+cleanup:
+    if (leases) {
+        for (i = 0; i < nleases; i++)
+            virNetworkDHCPLeaseFree(leases[i]);
+    }
+    VIR_FREE(leases);
+    if (network)
+        virNetworkFree(network);
+    return ret;
+}
 
 const vshCmdDef networkCmds[] = {
     {.name = "net-autostart",
@@ -1209,5 +1295,11 @@ const vshCmdDef networkCmds[] = {
      .info = info_network_uuid,
      .flags = 0
     },
+    {.name = "net-dhcp-leases",
+     .handler = cmdNetworkDHCPLeases,
+     .opts = opts_network_dhcp_leases,
+     .info = info_network_dhcp_leases,
+     .flags = 0
+    },
     {.name = NULL}
 };
diff --git a/tools/virsh.pod b/tools/virsh.pod
index 0ae5178..b87f646 100644
--- a/tools/virsh.pod
+++ b/tools/virsh.pod
@@ -2325,6 +2325,12 @@ If I<--current> is specified, affect the current network state.
 Both I<--live> and I<--config> flags may be given, but I<--current> is
 exclusive. Not specifying any flag is the same as specifying I<--current>.
 
+=item B<net-dhcp-leases> I<network> I<mac>
+
+Get a list of dhcp leases for all network interfaces connected to the given
+virtual I<network> or limited output just for one interface if I<mac> is
+specified.
+
 =back
 
 =head1 INTERFACE COMMANDS
-- 
1.7.11.7


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]