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

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

 



Use virNetworkGetDHCPLeases and virNetworkGetDHCPLeasesForMAC 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          Protocol   IP address           Hostname     ClientId
     ------------------------------------------------------------------------------------------------
     13-09-2013 03:45:31  52:54:00:20:70:3d    ipv4       192.168.105.240/24    f18          *
     13-09-2013 03:32:31  52:54:00:b1:70:19    ipv4       192.168.105.201/24    LDAP         *

tools/virsh.pod
   * Document new command

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

diff --git a/tools/virsh-network.c b/tools/virsh-network.c
index 8ddd5ca..571bf2e 100644
--- a/tools/virsh-network.c
+++ b/tools/virsh-network.c
@@ -1129,6 +1129,101 @@ 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;
+    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 ? virNetworkGetDHCPLeasesForMAC(network, mac, &leases, flags)
+        :virNetworkGetDHCPLeases(network, &leases, flags);
+
+    if (nleases < 0) {
+        vshError(ctl, _("Failed to get leases info for %s"), name);
+        goto cleanup;
+    }
+
+    vshPrintExtra(ctl, " %-20s %-20s %-10s %-20s %-12s %s\n%s%s\n",
+                  _("Expiry Time"), _("MAC address"), _("Protocol"),
+                  _("IP address"), _("Hostname"), _("ClientId"),
+                  "------------------------------------------------",
+                  "------------------------------------------------");
+
+    for (i = 0; i < nleases; i++) {
+        const char *type = NULL;
+        virNetworkDHCPLeasesPtr lease = 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);
+
+        switch (lease->type) {
+        case VIR_IP_ADDR_TYPE_IPV4:
+            type = "ipv4";
+            break;
+        case VIR_IP_ADDR_TYPE_IPV6:
+            type = "ipv6";
+            break;
+        }
+
+        vshPrintExtra(ctl, " %-20s %-20s %-10s %s/%-5d %-12s %s\n",
+                      expirytime, lease->mac, type, lease->ipaddr,
+                      lease->prefix, 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 +1304,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

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]