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

[libvirt] [libvirt-snmp][PATCH v4 3/4] Create functions to fill in and send notification packets.



Before sending snmp notification we need to fill in useful data,
like domain status, UUID, etc.
---
 src/Makefile.am            |    2 +
 src/README.txt             |    7 ++-
 src/libvirtNotifications.c |  110 ++++++++++++++++++++++++++++++++++++++++++++
 src/libvirtNotifications.h |   21 ++++++++
 4 files changed, 138 insertions(+), 2 deletions(-)
 create mode 100644 src/libvirtNotifications.c
 create mode 100644 src/libvirtNotifications.h

diff --git a/src/Makefile.am b/src/Makefile.am
index 57b74c8..dcd463a 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -16,6 +16,7 @@ USER_SRCS = 	\
 		libvirtGuestTable_data_set.c \
 		libvirtGuestTable_data_access.c \
 		libvirtSnmpError.c \
+		libvirtNotifications.c \
 		libvirtSnmp.c
 
 USER_HDRS = 	\
@@ -23,6 +24,7 @@ USER_HDRS = 	\
 		libvirtGuestTable_data_set.h \
 		libvirtGuestTable_data_access.h \
 		libvirtSnmpError.h \
+		libvirtNotifications.h \
 		libvirtSnmp.h
 
 SRCS = 		\
diff --git a/src/README.txt b/src/README.txt
index 22dd2af..6d010f6 100644
--- a/src/README.txt
+++ b/src/README.txt
@@ -31,8 +31,11 @@ libvirtGuestTable*
 - Generated by this command: 'MIBDIRS="+." MIBS="+LIBVIRT-MIB" mib2c libvirtMIB'.
   - It asks few questions and produces the mentioned files.
 - libvirtGuestTable.h, libvirtGuestTable.c, libvirtGuestTable_data_access.c, libvirtGuestTable_data_get.c and libvirtGuestTable_data_set.c are manually edited to set correct data structures and their handling. It's nicely documented, see libvirtGuestTable-README-FIRST.txt and libvirtGuestTable-README-libvirtGuestTable.txt + TODOs in the code. It took me ~2 hours to update these files (but I've done this several times for different MIBs, so I know what to do :). Do not touch the rest of the files!
-- I've done everything necessary for simple read-write access.
-- This code must be regenerated when the definition of libvirtGuestTable in the MIB file is changed! There is some automatic merging tool, which merges my changes with newly generated code, but I wouldn't rely on it.
+
+libvirtNotifications.[c|h]
+- The SNMP trap (notification) stuff
+- Generated by this command: 'MIBDIRS="+." MIBS="+LIBVIRT-MIB" mib2c -c mib2c.notify.conf libvirtNotifications'
+- and slightly modified (especially filling up trap variables which are going to be send).
 
 
 Usage (tested on Fedora 14 and RHEL6)
diff --git a/src/libvirtNotifications.c b/src/libvirtNotifications.c
new file mode 100644
index 0000000..60fc44b
--- /dev/null
+++ b/src/libvirtNotifications.c
@@ -0,0 +1,110 @@
+/*
+ * libvirtNotifications.c: Fill in trap packets
+ *
+ * Copyright (C) 2011 Red Hat, Inc.
+ *
+ * See COPYING for the license of this software
+ *
+ * Michal Privoznik <mprivozn redhat com>
+ */
+
+#include <net-snmp/net-snmp-config.h>
+#include <net-snmp/net-snmp-includes.h>
+#include <net-snmp/agent/net-snmp-agent-includes.h>
+#include <string.h>
+#include "libvirtNotifications.h"
+#include "libvirtGuestTable_enums.h"
+#include "libvirtSnmpError.h"
+
+static const oid snmptrap_oid[] = { 1, 3, 6, 1, 6, 3, 1, 1, 4, 1, 0 };
+
+int
+send_libvirtGuestNotif_trap(virDomainPtr dom)
+{
+    netsnmp_variable_list *var_list = NULL;
+    const oid libvirtGuestNotif_oid[] = { 1, 3, 6, 1, 4, 1, 12345, 0, 1 };
+    const oid libvirtGuestName_oid[] =
+        { 1, 3, 6, 1, 4, 1, 12345, 1, 1, 1, 2, 0 };
+    const oid libvirtGuestUUID_oid[] =
+        { 1, 3, 6, 1, 4, 1, 12345, 1, 1, 1, 1, 1 };
+    const oid libvirtGuestState_oid[] =
+        { 1, 3, 6, 1, 4, 1, 12345, 1, 1, 1, 3, 2 };
+    const oid libvirtGuestRowStatus_oid[] =
+        { 1, 3, 6, 1, 4, 1, 12345, 1, 1, 1, 9, 3 };
+
+
+    const char *domName = virDomainGetName(dom);
+    unsigned char domUUID[VIR_UUID_BUFLEN];
+    virDomainInfo info;
+    int rowstatus = ROWSTATUS_ACTIVE;
+
+    if (virDomainGetUUID(dom, domUUID) < 0) {
+        printLibvirtError("Failed to get domain UUID");
+        return SNMP_ERR_GENERR;
+    }
+
+    if (virDomainGetInfo(dom, &info) < 0) {
+        printLibvirtError("Failed to get domain info");
+        return SNMP_ERR_GENERR;
+    }
+
+    /*
+     * If domain is shutting down, row in libvirtGuestTable will
+     * not be accessible anymore.
+     */
+    switch (info.state) {
+        case VIR_DOMAIN_SHUTDOWN:
+        case VIR_DOMAIN_SHUTOFF:
+        case VIR_DOMAIN_CRASHED:
+            rowstatus = ROWSTATUS_NOTINSERVICE;
+            break;
+
+        default:
+            rowstatus = ROWSTATUS_ACTIVE;
+            break;
+    };
+
+    /*
+     * Set the snmpTrapOid.0 value
+     */
+    snmp_varlist_add_variable(&var_list,
+                              snmptrap_oid, OID_LENGTH(snmptrap_oid),
+                              ASN_OBJECT_ID,
+                              libvirtGuestNotif_oid,
+                              sizeof(libvirtGuestNotif_oid));
+
+    /*
+     * Add any objects from the trap definition
+     */
+    snmp_varlist_add_variable(&var_list,
+                              libvirtGuestName_oid,
+                              OID_LENGTH(libvirtGuestName_oid),
+                              ASN_OCTET_STR, domName, strlen(domName));
+    snmp_varlist_add_variable(&var_list,
+                              libvirtGuestUUID_oid,
+                              OID_LENGTH(libvirtGuestUUID_oid),
+                              ASN_OCTET_STR, domUUID, sizeof(domUUID));
+    snmp_varlist_add_variable(&var_list,
+                              libvirtGuestState_oid,
+                              OID_LENGTH(libvirtGuestState_oid),
+                              ASN_INTEGER,
+                              (u_char *) & info.state, sizeof(info.state));
+    snmp_varlist_add_variable(&var_list,
+                              libvirtGuestRowStatus_oid,
+                              OID_LENGTH(libvirtGuestRowStatus_oid),
+                              ASN_INTEGER,
+                              (u_char *) & rowstatus, sizeof(rowstatus));
+
+    /*
+     * Add any extra (optional) objects here
+     */
+
+    /*
+     * Send the trap to the list of configured destinations
+     * and clean up
+     */
+    send_v2trap(var_list);
+    snmp_free_varbind(var_list);
+
+    return SNMP_ERR_NOERROR;
+}
diff --git a/src/libvirtNotifications.h b/src/libvirtNotifications.h
new file mode 100644
index 0000000..84eefc3
--- /dev/null
+++ b/src/libvirtNotifications.h
@@ -0,0 +1,21 @@
+/*
+ * libvirtNotifications.h: Fill in trap packets
+ *
+ * Copyright (C) 2011 Red Hat, Inc.
+ *
+ * See COPYING for the license of this software
+ *
+ * Michal Privoznik <mprivozn redhat com>
+ */
+
+#ifndef LIBVIRTNOTIFICATIONS_H
+#define LIBVIRTNOTIFICATIONS_H
+
+#include "libvirtSnmp.h"
+
+/*
+ * function declarations
+ */
+int send_libvirtGuestNotif_trap(virDomainPtr dom);
+
+#endif /* LIBVIRTNOTIFICATIONS_H */
-- 
1.7.4


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