modbus plugin: Add documentation and rename to "datagroup".
[collectd.git] / src / modbus.c
index cad4b2c..661ab11 100644 (file)
@@ -1,6 +1,7 @@
 /**
  * collectd - src/modbus.c
  * Copyright (C) 2010  noris network AG
+ * Copyright (C) 2011  Universiteit Gent
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of the GNU Lesser General Public License as published by
@@ -18,6 +19,7 @@
  *
  * Authors:
  *   Florian Forster <octo at noris.net>
+ *   Ivo De Decker <ivo.dedecker at ugent.be>
  **/
 
 #include "collectd.h"
@@ -88,6 +90,16 @@ struct mb_data_s /* {{{ */
   mb_data_t *next;
 }; /* }}} */
 
+struct mb_datagroup_s;
+typedef struct mb_datagroup_s mb_datagroup_t;
+struct mb_datagroup_s /* {{{ */
+{
+  char *name;
+  mb_data_t *collect;
+
+  mb_datagroup_t *next;
+}; /* }}} */
+
 struct mb_slave_s /* {{{ */
 {
   int id;
@@ -131,6 +143,7 @@ struct mb_data_group_s /* {{{ */
  * Global variables
  */
 static mb_data_t *data_definitions = NULL;
+static mb_datagroup_t *data_groups = NULL;
 
 /*
  * Functions
@@ -224,6 +237,78 @@ static int data_copy_by_name (mb_data_t **dst, mb_data_t *src, /* {{{ */
   return (data_copy (dst, ptr));
 } /* }}} int data_copy_by_name */
 
+static mb_datagroup_t *datagroup_get_by_name (mb_datagroup_t *src, /* {{{ */
+    const char *name)
+{
+  mb_datagroup_t *ptr;
+
+  if (name == NULL)
+    return (NULL);
+
+  for (ptr = src; ptr != NULL; ptr = ptr->next)
+    if (strcasecmp (ptr->name, name) == 0)
+      return (ptr);
+
+  return (NULL);
+} /* }}} mb_datagroup_t *datagroup_get_by_name */
+
+static int datagroup_append (mb_datagroup_t **dst, mb_datagroup_t *src) /* {{{ */
+{
+  mb_datagroup_t *ptr;
+
+  if ((dst == NULL) || (src == NULL))
+    return (EINVAL);
+
+  ptr = *dst;
+
+  if (ptr == NULL)
+  {
+    *dst = src;
+    return (0);
+  }
+
+  while (ptr->next != NULL)
+    ptr = ptr->next;
+
+  ptr->next = src;
+
+  return (0);
+} /* }}} int datagroup_append */
+
+/* Copy a single mb_datagroup_t and append it to another list. */
+static int datagroup_copy (mb_datagroup_t **dst, const mb_datagroup_t *src) /* {{{ */
+{
+  mb_datagroup_t *tmp;
+  int status;
+
+  if ((dst == NULL) || (src == NULL))
+    return (EINVAL);
+
+  tmp = malloc (sizeof (*tmp));
+  if (tmp == NULL)
+    return (ENOMEM);
+  memcpy (tmp, src, sizeof (*tmp));
+  tmp->name = NULL;
+  tmp->next = NULL;
+
+  tmp->name = strdup (src->name);
+  if (tmp->name == NULL)
+  {
+    sfree (tmp);
+    return (ENOMEM);
+  }
+
+  status = datagroup_append (dst, tmp);
+  if (status != 0)
+  {
+    sfree (tmp->name);
+    sfree (tmp);
+    return (status);
+  }
+
+  return (0);
+} /* }}} int datagroup_copy */
+
 /* Read functions */
 
 static int mb_submit (mb_host_t *host, mb_slave_t *slave, /* {{{ */
@@ -795,6 +880,24 @@ static int mb_config_add_slave (mb_host_t *host, oconfig_item_t *ci) /* {{{ */
         data_copy_by_name (&slave->collect, data_definitions, buffer);
       status = 0; /* continue after failure. */
     }
+    else if (strcasecmp ("Datagroup", child->key) == 0)
+    {
+      char buffer[1024];
+      mb_datagroup_t *ds;
+
+      status = cf_util_get_string_buffer (child, buffer, sizeof (buffer));
+      if (status == 0) {
+        ds = datagroup_get_by_name (data_groups, buffer);
+        if (ds) {
+          mb_data_t *data;
+          for (data = ds->collect; data != NULL; data = data->next)
+          {
+            data_copy (&slave->collect, data);
+          }
+        }
+      }
+      status = 0; /* continue after failure. */
+       }
     else
     {
       ERROR ("Modbus plugin: Unknown configuration option: %s", child->key);
@@ -904,6 +1007,53 @@ static int mb_config_add_host (oconfig_item_t *ci) /* {{{ */
   return (status);
 } /* }}} int mb_config_add_host */
 
+
+static int mb_config_add_datagroup (oconfig_item_t *ci) /* {{{ */
+{
+  mb_datagroup_t datagroup;
+  int status;
+  int i;
+
+  memset (&datagroup, 0, sizeof (datagroup));
+  datagroup.name = NULL;
+  datagroup.collect = NULL;
+  datagroup.next = NULL;
+
+  status = cf_util_get_string (ci, &datagroup.name);
+  
+  for (i = 0; i < ci->children_num; i++)
+  {
+    oconfig_item_t *child = ci->children + i;
+    status = 0;
+
+    if (strcasecmp ("Collect", child->key) == 0)
+    {
+      char buffer[1024];
+      status = cf_util_get_string_buffer (child, buffer, sizeof (buffer));
+      if (status == 0) {
+        data_copy_by_name (&datagroup.collect, data_definitions, buffer);
+         }
+      status = 0; /* continue after failure. */
+    }
+    else
+    {
+      ERROR ("Modbus plugin: Unknown configuration option: %s", child->key);
+      status = -1;
+    }
+
+    if (status != 0)
+      break;
+  } /* for (i = 0; i < ci->children_num; i++) */
+
+  if ((status == 0) && (datagroup.collect == NULL))
+    status = EINVAL;
+
+  if (status == 0)
+    datagroup_copy (&data_groups, &datagroup);
+
+  return (status);
+} /* }}} int mb_config_add_datagroup */
+
 static int mb_config (oconfig_item_t *ci) /* {{{ */
 {
   int i;
@@ -919,6 +1069,8 @@ static int mb_config (oconfig_item_t *ci) /* {{{ */
       mb_config_add_data (child);
     else if (strcasecmp ("Host", child->key) == 0)
       mb_config_add_host (child);
+    else if (strcasecmp ("Datagroup", child->key) == 0)
+      mb_config_add_datagroup (child);
     else
       ERROR ("Modbus plugin: Unknown configuration option: %s", child->key);
   }