On Fri, Aug 23, 2013 at 8:16 PM, Doug Goldstein <cardoe@gentoo.org> wrote:
>
> On Thu, Aug 22, 2013 at 5:18 PM, nehaljwani <nehaljw.kkd1@gmail.com> wrote:
>>
>> Use virDomainInterfacesAddresses in virsh
>>
>> tools/virsh-domain-monitor.c
>>    * Introduce new command : domifaddr
>>
>>    virsh # domifaddr f18
>>    Name       MAC address          IP address
>>    ---------------------------------------------------
>>    lo         00:00:00:00:00:00    127.0.0.1/8 ::1/128
>>    eth0       52:54:00:89:ad:35    192.168.102.142/24 fe80::5054:ff:fe89:ad35/64
>>    eth1       52:54:00:d3:39:ee    192.168.103.183/24 fe80::5054:ff:fed3:39ee/64
>>    eth2       52:54:00:fe:4c:4f    192.168.101.197/24 fe80::5054:ff:fefe:4c4f/64
>>    eth3       52:54:00:89:4e:97    192.168.101.130/24 fe80::5054:ff:fe89:4e97/64
>>
>
> Not a review, but a question. How does this work with IP aliases? Will it show all the aliases 1 per line or just show the primary address?
>  
>>
>> tools/virsh.pod
>>    * Document new command
>>
>> ---
>>  tools/virsh-domain-monitor.c | 99 ++++++++++++++++++++++++++++++++++++++++++++
>>  tools/virsh.pod              | 10 +++++
>>  2 files changed, 109 insertions(+)
>>
>> diff --git a/tools/virsh-domain-monitor.c b/tools/virsh-domain-monitor.c
>> index b29b82a..91efa71 100644
>> --- a/tools/virsh-domain-monitor.c
>> +++ b/tools/virsh-domain-monitor.c
>> @@ -1871,6 +1871,99 @@ cleanup:
>>  }
>>  #undef FILTER
>>
>> +/* "domifaddr" command
>> + */
>> +static const vshCmdInfo info_domifaddr[] = {
>> +    {"help", N_("Get network interfaces' addresses for a running domain")},
>> +    {"desc", N_("Get network interfaces' addresses for a running domain")},
>> +    {NULL, NULL}
>> +};
>> +
>> +static const vshCmdOptDef opts_domifaddr[] = {
>> +    {"domain", VSH_OT_DATA, VSH_OFLAG_REQ, N_("domain name, id or uuid")},
>> +    {"interface", VSH_OT_DATA, VSH_OFLAG_NONE, N_("network interface name")},
>> +    {NULL, 0, 0, NULL}
>> +};
>> +
>> +static bool
>> +cmdDomIfAddr(vshControl *ctl, const vshCmd *cmd)
>> +{
>> +    virDomainPtr dom = NULL;
>> +    const char *interface = NULL;
>> +    virDomainInterfacePtr *ifaces = NULL;
>> +    size_t i, j;
>> +    int ifaces_count = 0;
>> +    unsigned int flags = 0;
>> +    bool ret = false;
>> +
>> +    if (!(dom = vshCommandOptDomain(ctl, cmd, NULL)))
>> +        return false;
>> +
>> +    if (vshCommandOptString(cmd, "interface", &interface) < 0) {
>> +        goto cleanup;
>> +    }
>> +
>> +    if ((ifaces_count = virDomainInterfacesAddresses(dom, &ifaces, flags)) < 0) {
>> +        vshError(ctl, _("Failed to query for interfaces addresses"));
>> +        goto cleanup;
>> +    }
>> +
>> +    vshPrintExtra(ctl, " %-10s %-17s    %s\n%s\n",
>> +                  _("Name"), _("MAC address"), _("IP address"),
>> +                  "---------------------------------------------------");
>> +
>> +    for (i = 0; i < ifaces_count; i++) {
>> +        virDomainInterfacePtr iface = ifaces[i];
>> +        virBuffer buf = VIR_BUFFER_INITIALIZER;
>> +        const char *hwaddr = "";
>> +        const char *ip_addr_str = NULL;
>> +
>> +        if (interface && STRNEQ(interface, iface->name)) {
>> +            virBufferFreeAndReset(&buf);
>> +            continue;
>> +        }
>> +
>> +        if (iface->hwaddr)
>> +            hwaddr = iface->hwaddr;
>> +
>> +        for (j = 0; j < iface->naddrs; j++) {
>> +            if (j)
>> +                virBufferAddChar(&buf, ' ');
>> +            virBufferAsprintf(&buf, "%s/%d",
>> +                              iface->addrs[j].addr,
>> +                              iface->addrs[j].prefix);
>> +        }
>> +
>> +        if (virBufferError(&buf)) {
>> +            virBufferFreeAndReset(&buf);
>> +            virReportOOMError();
>> +            return ret;
>> +        }
>> +
>> +        ip_addr_str = virBufferContentAndReset(&buf);
>> +
>> +        if (!ip_addr_str)
>> +            ip_addr_str = "";
>> +
>> +        vshPrintExtra(ctl, " %-10s %-17s    %s\n",
>> +                     iface->name, hwaddr, ip_addr_str);
>> +
>> +        virBufferFreeAndReset(&buf);
>> +    }
>> +
>> +    ret = true;
>> +
>> +cleanup:
>> +    for (i = 0; i < ifaces_count; i++) {
>> +        if (ifaces[i])
>> +            virDomainInterfaceFree(ifaces[i]);
>> +    }
>> +    VIR_FREE(ifaces);
>> +
>> +    virDomainFree(dom);
>> +    return ret;
>> +}
>> +
>>  const vshCmdDef domMonitoringCmds[] = {
>>      {.name = "domblkerror",
>>       .handler = cmdDomBlkError,
>> @@ -1944,5 +2037,11 @@ const vshCmdDef domMonitoringCmds[] = {
>>       .info = info_list,
>>       .flags = 0
>>      },
>> +    {.name = "domifaddr",
>> +     .handler = cmdDomIfAddr,
>> +     .opts = opts_domifaddr,
>> +     .info = info_domifaddr,
>> +     .flags = 0
>> +    },
>>      {.name = NULL}
>>  };
>> diff --git a/tools/virsh.pod b/tools/virsh.pod
>> index 0ae5178..008ffea 100644
>> --- a/tools/virsh.pod
>> +++ b/tools/virsh.pod
>> @@ -636,6 +636,16 @@ B<Explanation of fields> (fields appear in the following order):
>>    flush_total_times - total time flush operations took (ns)
>>      <-- other fields provided by hypervisor -->
>>
>> +
>> +=item B<domifaddr> I<domain> [I<interface>]
>> +
>> +Get a list of interfaces of a running domain along with their IP and MAC
>> +addresses, or limited output just for one interface if I<interface> is
>> +specified. Note, that I<interface>  can be driver dependent, it can be
>> +the name within guest OS or the name you would see in domain XML.
>> +Moreover, the whole command may require a guest agent to be configured
>> +for the queried domain under some drivers, notably qemu.
>> +
>>  =item B<domifstat> I<domain> I<interface-device>
>>
>>  Get network interface stats for a running domain.
>> --
>> 1.7.11.7
>>
>> --
>> libvir-list mailing list
>> libvir-list@redhat.com
>> https://www.redhat.com/mailman/listinfo/libvir-list
>
>
>
>
> --
> Doug Goldstein


The leases method will be applied to this API when virNetworkGetDHCPLeases() API is accepted, which is the next step after this. 
Refer: http://www.redhat.com/archives/libvir-list/2013-July/msg01603.html and its followups.

The output from the virNetworkGetDHCPLeases will be something of this kind:

     virsh # net-dhcp-leases default

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


--
Nehal J Wani
UG3, BTech CS+MS(CL)
IIIT-Hyderabad
http://commandlinewani.blogspot.com