[collectd] [PATCH] contrib/examples/: Added an example for a notification callback.

Sebastian Harl sh at tokkee.org
Tue Feb 5 22:26:23 CET 2008


Signed-off-by: Sebastian Harl <sh at tokkee.org>
---
 contrib/examples/MyPlugin.pm |   39 +++++++++++++++++++++++++++++++++-
 contrib/examples/myplugin.c  |   46 +++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 82 insertions(+), 3 deletions(-)

diff --git a/contrib/examples/MyPlugin.pm b/contrib/examples/MyPlugin.pm
index b852272..1a0247f 100644
--- a/contrib/examples/MyPlugin.pm
+++ b/contrib/examples/MyPlugin.pm
@@ -34,6 +34,7 @@ my $dataset =
 
 # This code is executed after loading the plugin to register it with collectd.
 plugin_register (TYPE_LOG, 'myplugin', 'my_log');
+plugin_register (TYPE_NOTIF, 'myplugin', 'my_notify');
 plugin_register (TYPE_DATASET, 'myplugin', $dataset);
 plugin_register (TYPE_INIT, 'myplugin', 'my_init');
 plugin_register (TYPE_READ, 'myplugin', 'my_read');
@@ -85,8 +86,7 @@ sub my_write
 	my $vl   = shift;
 
 	if (scalar (@$ds) != scalar (@{$vl->{'values'}})) {
-		plugin_log (LOG_WARNING,
-			"DS number does not match values length");
+		plugin_log (LOG_WARNING, "DS number does not match values length");
 		return;
 	}
 
@@ -126,3 +126,38 @@ sub my_log
 	return 1;
 } # my_log ()
 
+# This function is called when plugin_dispatch_notification () has been used
+sub my_notify
+{
+	my $notif = shift;
+
+	my ($sec, $min, $hour, $mday, $mon, $year) = localtime ($notif->{'time'});
+
+	printf "NOTIF (%04d-%02d-%02d %02d:%02d:%02d): %d - ",
+			$year + 1900, $mon + 1, $mday, $hour, $min, $sec,
+			$notif->{'severity'};
+
+	if (defined $notif->{'host'}) {
+		print "$notif->{'host'}: ";
+	}
+
+	if (defined $notif->{'plugin'}) {
+		print "$notif->{'plugin'}: ";
+	}
+
+	if (defined $notif->{'plugin_instance'}) {
+		print "$notif->{'plugin_instance'}: ";
+	}
+
+	if (defined $notif->{'type'}) {
+		print "$notif->{'type'}: ";
+	}
+
+	if (defined $notif->{'type_instance'}) {
+		print "$notif->{'type_instance'}: ";
+	}
+
+	print "$notif->{'message'}\n";
+	return 1;
+} # my_notify ()
+
diff --git a/contrib/examples/myplugin.c b/contrib/examples/myplugin.c
index fc7856b..cdd537a 100644
--- a/contrib/examples/myplugin.c
+++ b/contrib/examples/myplugin.c
@@ -18,7 +18,8 @@
  *   is optional
  */
 
-#include <stdio.h>
+#if ! HAVE_CONFIG_H
+
 #include <stdlib.h>
 
 #include <string.h>
@@ -33,6 +34,10 @@
 # undef __USE_ISOC99
 #endif /* DISABLE_ISOC99 */
 
+#include <time.h>
+
+#endif /* ! HAVE_CONFIG */
+
 #include <collectd/collectd.h>
 #include <collectd/common.h>
 #include <collectd/plugin.h>
@@ -146,6 +151,44 @@ static void my_log (int severity, const char *msg)
 } /* static void my_log (int, const char *) */
 
 /*
+ * This function is called when plugin_dispatch_notification () has been used.
+ */
+static int my_notify (const notification_t *notif)
+{
+	char time_str[32] = "";
+	struct tm *tm = NULL;
+
+	int n = 0;
+
+	if (NULL == (tm = localtime (&notif->time)))
+		time_str[0] = '\0';
+
+	n = strftime (time_str, 32, "%F %T", tm);
+	if (n >= 32) n = 31;
+	time_str[n] = '\0';
+
+	printf ("NOTIF (%s): %i - ", time_str, notif->severity);
+
+	if ('\0' != *notif->host)
+		printf ("%s: ", notif->host);
+
+	if ('\0' != *notif->plugin)
+		printf ("%s: ", notif->plugin);
+
+	if ('\0' != *notif->plugin_instance)
+		printf ("%s: ", notif->plugin_instance);
+
+	if ('\0' != *notif->type)
+		printf ("%s: ", notif->type);
+
+	if ('\0' != *notif->type_instance)
+		printf ("%s: ", notif->type_instance);
+
+	printf ("%s\n", notif->message);
+	return 0;
+} /* static int my_notify (notification_t *) */
+
+/*
  * This function is called before shutting down collectd.
  */
 static int my_shutdown (void)
@@ -161,6 +204,7 @@ static int my_shutdown (void)
 void module_register (void)
 {
 	plugin_register_log ("myplugin", my_log);
+	plugin_register_notification ("myplugin", my_notify);
 	plugin_register_data_set (&ds);
 	plugin_register_read ("myplugin", my_read);
 	plugin_register_init ("myplugin", my_init);
-- 
1.5.4.rc4.23.gcab31

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://mailman.verplant.org/pipermail/collectd/attachments/20080205/e5458c23/attachment.pgp 


More information about the collectd mailing list