Code

Added logical volume size metrics
[collectd.git] / src / volume.c
1 /**
2  * collectd - src/volume.c
3  * Copyright (C) 2013       Chad Malfait
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; only version 2 of the License is applicable.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17  *
18  * Authors:
19  *   Chad Malfait <malfaitc at yahoo.com>
20  **/
22 #include <lvm2app.h>
24 #include "collectd.h"
25 #include "common.h"
26 #include "plugin.h"
28 static void vg_submit(const char *vg_name, gauge_t used, gauge_t free, gauge_t size)
29 {
30     value_t values[3];
31     value_list_t vl = VALUE_LIST_INIT;
33     values[0].gauge = used;
34     values[1].gauge = free;
35     values[2].gauge = size;
37     vl.values = values;
38     vl.values_len = STATIC_ARRAY_SIZE (values);
40     sstrncpy(vl.host, hostname_g, sizeof (vl.host));
41     sstrncpy(vl.plugin, "vg", sizeof (vl.plugin));
42     sstrncpy(vl.plugin_instance, vg_name, sizeof (vl.plugin_instance));
43     sstrncpy(vl.type, "vg", sizeof (vl.type));
44     sstrncpy(vl.type_instance, vg_name, sizeof (vl.type_instance));
46     plugin_dispatch_values (&vl);
47 }
49 static void lv_submit(const char *vg_name, const char *lv_name, gauge_t value)
50 {
52     value_t values[1];
53     value_list_t vl = VALUE_LIST_INIT;
55     values[0].gauge = value;
57     vl.values = values;
58     vl.values_len = STATIC_ARRAY_SIZE (values);
60     sstrncpy(vl.host, hostname_g, sizeof (vl.host));
61     sstrncpy(vl.plugin, "vg", sizeof (vl.plugin));
62     sstrncpy(vl.plugin_instance, vg_name, sizeof (vl.plugin_instance));
63     sstrncpy(vl.type, "lv", sizeof (vl.type));
64     sstrncpy(vl.type_instance, lv_name, sizeof (vl.type_instance));
66     plugin_dispatch_values (&vl); 
67 }
69 static int lv_read(vg_t vg, const char *vg_name, unsigned long vg_free, unsigned long vg_size)
70 {
71     struct dm_list *lvs;
72     struct lvm_lv_list *lvl;
73     unsigned long vg_used = 0;
75     vg_used = vg_size - vg_free;
76     lvs = lvm_vg_list_lvs(vg);
78     vg_submit(vg_name, vg_used, vg_free, vg_size);
80     dm_list_iterate_items(lvl, lvs) {
81          lv_submit(vg_name, lvm_lv_get_name(lvl->lv), lvm_lv_get_size(lvl->lv));
82     }
83     return (0);
84 }
86 static int vg_read(void)
87 {
88     lvm_t lvm;
89     vg_t vg;
90     struct dm_list *vg_names;
91     struct lvm_str_list *name_list;
93     lvm = lvm_init(NULL);
94     if (!lvm) {
95         ERROR("volume plugin: lvm_init failed: %s", lvm_errmsg(lvm));
96         lvm_quit(lvm);
97         exit(-1);
98     }
100     vg_names = lvm_list_vg_names(lvm);
101     if (!vg_names) {
102         ERROR("volume plugin lvm_list_vg_name failed %s", lvm_errmsg(lvm));
103         lvm_quit(lvm);
104         exit(-1);
105     }
107     dm_list_iterate_items(name_list, vg_names) {
108         vg = lvm_vg_open(lvm, name_list->str, "r", 0);
109         lv_read(vg, name_list->str, lvm_vg_get_free_size(vg), lvm_vg_get_size(vg));
111         lvm_vg_close(vg);
112     }
114     lvm_quit(lvm);
115     return (0);
116 } /*vg_read */
118 void module_register(void)
120         plugin_register_read("volume", vg_read);
121 } /* void module_register */