Code

Changed data type from vg to vol_group and lv to logical_vol to be more descriptive
[collectd.git] / src / volume.c
index 99a1434f3906e950a3e39130c045f36db5ce3571..009c9be123d06a72416d7fbd031c7ec1d0acba62 100644 (file)
  *   Chad Malfait <malfaitc at yahoo.com>
  **/
 
+#include <lvm2app.h>
+
 #include "collectd.h"
 #include "common.h"
 #include "plugin.h"
-#include <lvm2app.h>
-
-#define GB (1024*1024*1024)
-#define MB (1024*1024)
 
-static void volume_submit(const char *vol_name, gauge_t size, gauge_t free)
+static void vg_submit(const char *vg_name, gauge_t used, gauge_t free, gauge_t size)
 {
-    value_t values[2];
+    value_t values[3];
     value_list_t vl = VALUE_LIST_INIT;
 
-    values[0].gauge = size;
+    values[0].gauge = used;
     values[1].gauge = free;
+    values[2].gauge = size;
 
     vl.values = values;
     vl.values_len = STATIC_ARRAY_SIZE (values);
+
     sstrncpy(vl.host, hostname_g, sizeof (vl.host));
-    sstrncpy(vl.plugin, "volume", sizeof (vl.plugin));
-    sstrncpy(vl.type, vol_name, sizeof (vl.type));
+    sstrncpy(vl.plugin, "vol_group", sizeof (vl.plugin));
+    sstrncpy(vl.plugin_instance, vg_name, sizeof (vl.plugin_instance));
+    sstrncpy(vl.type, "vol_group", sizeof (vl.type));
+    sstrncpy(vl.type_instance, vg_name, sizeof (vl.type_instance));
 
     plugin_dispatch_values (&vl);
 }
 
-static int volume_read(void)
+static void lv_submit(const char *vg_name, const char *lv_name, gauge_t value)
+{
+
+    value_t values[1];
+    value_list_t vl = VALUE_LIST_INIT;
+
+    values[0].gauge = value;
+
+    vl.values = values;
+    vl.values_len = STATIC_ARRAY_SIZE (values);
+
+    sstrncpy(vl.host, hostname_g, sizeof (vl.host));
+    sstrncpy(vl.plugin, "vol_group", sizeof (vl.plugin));
+    sstrncpy(vl.plugin_instance, vg_name, sizeof (vl.plugin_instance));
+    sstrncpy(vl.type, "logical_vol", sizeof (vl.type));
+    sstrncpy(vl.type_instance, lv_name, sizeof (vl.type_instance));
+
+    plugin_dispatch_values (&vl); 
+}
+
+static int lv_read(vg_t vg, const char *vg_name, unsigned long vg_free, unsigned long vg_size)
+{
+    struct dm_list *lvs;
+    struct lvm_lv_list *lvl;
+    unsigned long vg_used = 0;
+
+    vg_used = vg_size - vg_free;
+    lvs = lvm_vg_list_lvs(vg);
+
+    vg_submit(vg_name, vg_used, vg_free, vg_size);
+
+    dm_list_iterate_items(lvl, lvs) {
+         lv_submit(vg_name, lvm_lv_get_name(lvl->lv), lvm_lv_get_size(lvl->lv));
+    }
+    return (0);
+}
+
+static int vg_read(void)
 {
     lvm_t lvm;
     vg_t vg;
-    struct dm_list *vgnames;
-    struct lvm_str_list *strl;
+    struct dm_list *vg_names;
+    struct lvm_str_list *name_list;
 
     lvm = lvm_init(NULL);
     if (!lvm) {
-        fprintf(stderr, "lvm_init() failed\n");
+       ERROR("volume plugin: lvm_init failed: %s", lvm_errmsg(lvm));
+        lvm_quit(lvm);
+        exit(-1);
     }
 
-    vgnames = lvm_list_vg_names(lvm);
-    if (!vgnames) {
-        fprintf(stderr, "lvm_list_vg_names() failed\n");
+    vg_names = lvm_list_vg_names(lvm);
+    if (!vg_names) {
+       ERROR("volume plugin lvm_list_vg_name failed %s", lvm_errmsg(lvm));
+        lvm_quit(lvm);
+        exit(-1);
     }
 
-    dm_list_iterate_items(strl, vgnames) {
-        vg = lvm_vg_open(lvm, strl->str, "r", 0);
-        volume_submit(strl->str, (float)lvm_vg_get_size(vg)/GB, (float)lvm_vg_get_free_size(vg)/GB);
+    dm_list_iterate_items(name_list, vg_names) {
+        vg = lvm_vg_open(lvm, name_list->str, "r", 0);
+        lv_read(vg, name_list->str, lvm_vg_get_free_size(vg), lvm_vg_get_size(vg));
+
         lvm_vg_close(vg);
     }
 
     lvm_quit(lvm);
     return (0);
-}
+} /*vg_read */
 
 void module_register(void)
 {
-       plugin_register_read("volume", volume_read);
+       plugin_register_read("volume", vg_read);
 } /* void module_register */