ipmi plugin: Send notifications upon added/removed sensors.
authorPeter Holik <peter@holik.at>
Tue, 19 Aug 2008 08:50:48 +0000 (10:50 +0200)
committerFlorian Forster <octo@huhu.verplant.org>
Tue, 19 Aug 2008 08:50:48 +0000 (10:50 +0200)
It's enabled using the two new configuration options:
 * NotifySensorAdd
   A notification is produced when a sensor is removed. During the first
   minute, i. e. while the plugin is scanning for sensors, this is disabled.
 * NotifySensorRemove
   If a sensor got removed a notification is produced.

Signed-off-by: Florian Forster <octo@huhu.verplant.org>
src/ipmi.c

index 7ae54a0..da1b0cd 100644 (file)
@@ -52,18 +52,24 @@ struct c_ipmi_sensor_list_s
 static pthread_mutex_t sensor_list_lock = PTHREAD_MUTEX_INITIALIZER;
 static c_ipmi_sensor_list_t *sensor_list = NULL;
 
+static int c_ipmi_init_in_progress = 0;
 static int c_ipmi_active = 0;
 static pthread_t thread_id = (pthread_t) 0;
 
 static const char *config_keys[] =
 {
        "Sensor",
-       "IgnoreSelected"
+       "IgnoreSelected",
+       "NotifySensorAdd",
+       "NotifySensorRemove"
 };
 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
 
 static ignorelist_t *ignorelist = NULL;
 
+static int c_ipmi_nofiy_add = 0;
+static int c_ipmi_nofiy_remove = 0;
+
 /*
  * Misc private functions
  */
@@ -258,6 +264,21 @@ static int sensor_list_add (ipmi_sensor_t *sensor)
 
   pthread_mutex_unlock (&sensor_list_lock);
 
+  if (c_ipmi_nofiy_add && (c_ipmi_init_in_progress == 0))
+  {
+    notification_t n = { NOTIF_OKAY, time(NULL), "", "", "ipmi",
+                         "", "", "", NULL };
+
+    sstrncpy (n.host, hostname_g, sizeof (n.host));
+    sstrncpy (n.type_instance, list_item->sensor_name,
+              sizeof (n.type_instance));
+    sstrncpy (n.type, list_item->sensor_type, sizeof (n.type));
+    ssnprintf (n.message, sizeof (n.message),
+              "sensor %s added", list_item->sensor_name);
+
+    plugin_dispatch_notification (&n);
+  }
+
   return (0);
 } /* int sensor_list_add */
 
@@ -297,6 +318,21 @@ static int sensor_list_remove (ipmi_sensor_t *sensor)
 
   pthread_mutex_unlock (&sensor_list_lock);
 
+  if (c_ipmi_nofiy_remove && c_ipmi_active)
+  {
+    notification_t n = { NOTIF_WARNING, time(NULL), "", "",
+                         "ipmi", "", "", "", NULL };
+
+    sstrncpy (n.host, hostname_g, sizeof (n.host));
+    sstrncpy (n.type_instance, list_item->sensor_name,
+              sizeof (n.type_instance));
+    sstrncpy (n.type, list_item->sensor_type, sizeof (n.type));
+    ssnprintf (n.message, sizeof (n.message),
+              "sensor %s removed", list_item->sensor_name);
+
+    plugin_dispatch_notification (&n);
+  }
+
   free (list_item);
   return (0);
 } /* int sensor_list_remove */
@@ -500,11 +536,25 @@ static int c_ipmi_config (const char *key, const char *value)
   {
     int invert = 1;
     if ((strcasecmp ("True", value) == 0)
-       || (strcasecmp ("Yes", value) == 0)
-       || (strcasecmp ("On", value) == 0))
+        || (strcasecmp ("Yes", value) == 0)
+        || (strcasecmp ("On", value) == 0))
       invert = 0;
     ignorelist_set_invert (ignorelist, invert);
   }
+  else if (strcasecmp ("NotifySensorAdd", key) == 0)
+  {
+    if ((strcasecmp ("True", value) == 0)
+        || (strcasecmp ("Yes", value) == 0)
+        || (strcasecmp ("On", value) == 0))
+      c_ipmi_nofiy_add = 1;
+  }
+  else if (strcasecmp ("NotifySensorRemove", key) == 0)
+  {
+    if ((strcasecmp ("True", value) == 0)
+        || (strcasecmp ("Yes", value) == 0)
+        || (strcasecmp ("On", value) == 0))
+      c_ipmi_nofiy_remove = 1;
+  }
   else
   {
     return (-1);
@@ -517,6 +567,9 @@ static int c_ipmi_init (void)
 {
   int status;
 
+  /* Don't send `ADD' notifications during startup (~ 1 minute) */
+  c_ipmi_init_in_progress = 1 + (60 / interval_g);
+
   c_ipmi_active = 1;
 
   status = pthread_create (&thread_id, /* attr = */ NULL, thread_main,
@@ -541,7 +594,12 @@ static int c_ipmi_read (void)
   }
 
   sensor_list_read_all ();
-  
+
+  if (c_ipmi_init_in_progress > 0)
+    c_ipmi_init_in_progress--;
+  else
+    c_ipmi_init_in_progress = 0;
+
   return (0);
 } /* int c_ipmi_read */