[libvirt] [libvirt-snmp][PATCH v4 4/4] Add SNMP trap/notification support.

Daniel P. Berrange berrange at redhat.com
Fri Apr 1 11:17:07 UTC 2011


On Wed, Mar 30, 2011 at 01:20:30PM +0200, Michal Privoznik wrote:
> This patch adds support for domain lifecycle notification support
> over SNMP traps. SNMP subagent monitors any domain events and when
> something interesting happens, it sends a trap.
> 
> Monitoring is done in a joinable thread using polling (used
> domain-events example from libvirt) so we won't block the agent itself.
> 
> Some debug info can be printed out by setting LIBVIRT_SNMP_VERBOSE
> environment variable.
> ---

>  src/event.c                |  193 ++++++++++++
>  src/event.h                |  101 ++++++

You don't want to be copying those files. They are libvirt's
internal event driver setup code. You just want to be providing
an implementation fir that, which is what the next two files
do:

>  src/event_poll.c           |  724 ++++++++++++++++++++++++++++++++++++++++++++
>  src/event_poll.h           |  132 ++++++++


> diff --git a/src/libvirtSnmp.c b/src/libvirtSnmp.c
> index dd1bd33..48eeb5c 100644
> --- a/src/libvirtSnmp.c
> +++ b/src/libvirtSnmp.c
> @@ -20,14 +20,63 @@
>   * Author: Michal Privoznik <mprivozn at redhat.com>
>   */
>  
> +#include <config.h>
> +
>  #include <stdio.h>
>  #include <stdlib.h>
> +#include <sys/types.h>
> +#include <sys/poll.h>
> +#include <pthread.h>
> +#include <signal.h>
>  
>  #include "libvirtSnmp.h"
> -/* include our MIB structures*/
> -#include "libvirtGuestTable.h"
> -
> +#include "libvirtGuestTable.h"      /* include our MIB structures*/
> +#include "libvirtNotifications.h"
> +#ifdef LIBVIRT_OLD
> +# include "event.h"
> +#endif
> +
> +#define DEBUG0(fmt) if (verbose) printf("%s:%d :: " fmt "\n", \
> +        __func__, __LINE__)
> +#define DEBUG(fmt, ...) if (verbose) printf("%s:%d: " fmt "\n", \
> +        __func__, __LINE__, __VA_ARGS__)
> +#define STREQ(a,b) (strcmp(a,b) == 0)
> +
> +#ifndef ATTRIBUTE_UNUSED
> +#define ATTRIBUTE_UNUSED __attribute__((__unused__))
> +#endif
> +
> +int verbose = 0;
>  virConnectPtr conn;
> +int callbackRet = -1;
> +int run = 1;
> +pthread_t poll_thread;
> +
> +static int
> +domainEventCallback(virConnectPtr conn ATTRIBUTE_UNUSED,
> +                    virDomainPtr dom, int event, int detail,
> +                    void *opaque ATTRIBUTE_UNUSED)
> +{
> +    DEBUG("%s EVENT: Domain %s(%d) %d %d\n", __func__,
> +          virDomainGetName(dom), virDomainGetID(dom), event, detail);
> +
> +    send_libvirtGuestNotif_trap(dom);
> +    return 0;
> +}
> +
> +static void
> +myFreeFunc(void *opaque)
> +{
> +    if (opaque)
> +        free(opaque);
> +}
> +
> +/* Signal trap function */
> +static void
> +stop(int sig)
> +{
> +    run = 0;
> +}
>  
>  static void
>  showError(virConnectPtr conn)
> @@ -188,10 +237,77 @@ out:
>      return ret;
>  }
>  
> +/* Polling thread function */
> +void *
> +pollingThreadFunc(void *foo) {
> +    while (run) {
> +        if (virEventRunDefaultImpl() < 0) {

This wants to be   'virEventPollRunOnce() < 0'

> +            showError(conn);
> +            pthread_exit(NULL);
> +        }
> +    }
> +    return NULL;
> +}
> +

>  int libvirtSnmpInit(void)
>  {
> -    /* virConnectOpenAuth is called here with all default parameters,
> -     * except, possibly, the URI of the hypervisor. */
> +    char *verbose_env = getenv("LIBVIRT_SNMP_VERBOSE");
> +
> +    verbose = verbose_env != NULL;
> +
> +    /* if we don't already have registered callback */
> +    if (callbackRet == -1)
> +        virEventRegisterDefaultImpl();
> +

And this would be

    if (virEventPollInit() < 0)
        return -1;

    virEventRegisterImpl(
        virEventPollAddHandle,
        virEventPollUpdateHandle,
        virEventPollRemoveHandle,
        virEventPollAddTimeout,
        virEventPollUpdateTimeout,
        virEventPollRemoveTimeout);


Regards,
Daniel
-- 
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org       -o-       http://live.gnome.org/gtk-vnc :|




More information about the libvir-list mailing list