lvm: Ignore virtual volumes
[collectd.git] / src / lvm.c
1 /**
2  * collectd - src/lvm.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  **/
21
22 #include <lvm2app.h>
23
24 #include "collectd.h"
25 #include "common.h"
26 #include "plugin.h"
27
28 static void lvm_submit (char const *plugin_instance, char const *type_instance,
29         uint64_t ivalue)
30 {
31     value_t v;
32     value_list_t vl = VALUE_LIST_INIT;
33
34     v.gauge = (gauge_t) ivalue;
35
36     vl.values = &v;
37     vl.values_len = 1;
38
39     sstrncpy(vl.host, hostname_g, sizeof (vl.host));
40     sstrncpy(vl.plugin, "lvm", sizeof (vl.plugin));
41     sstrncpy(vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
42     sstrncpy(vl.type, "df_complex", sizeof (vl.type));
43     sstrncpy(vl.type_instance, type_instance, sizeof (vl.type_instance));
44
45     plugin_dispatch_values (&vl);
46 }
47
48 static void vg_read(vg_t vg, char const *vg_name)
49 {
50     struct dm_list *lvs;
51     struct lvm_lv_list *lvl;
52     char const *attrs;
53
54     lvm_submit (vg_name, "free", lvm_vg_get_free_size(vg));
55
56     lvs = lvm_vg_list_lvs(vg);
57     dm_list_iterate_items(lvl, lvs) {
58         attrs = lvm_lv_get_attr(lvl->lv);
59         if (attrs == NULL)
60             continue;
61         /* Condition on volume type.  We want the reported sizes in the
62            volume group to sum to the size of the volume group, so we ignore
63            virtual volumes.  */
64         switch (attrs[0]) {
65             case 't':
66                 /* Thin pool virtual volume.  We report the underlying data
67                    and metadata volumes, not this one.  Ignore. */
68                 continue;
69             case 'v':
70                 /* Virtual volume.  Ignore. */
71                 continue;
72             case 'V':
73                 /* Thin volume or thin snapshot.  Ignore. */
74                 continue;
75         }
76         lvm_submit(vg_name, lvm_lv_get_name(lvl->lv), lvm_lv_get_size(lvl->lv));
77     }
78 }
79
80 static int lvm_read(void)
81 {
82     lvm_t lvm;
83     struct dm_list *vg_names;
84     struct lvm_str_list *name_list;
85
86     lvm = lvm_init(NULL);
87     if (!lvm) {
88         ERROR("lvm plugin: lvm_init failed.");
89         return (-1);
90     }
91
92     vg_names = lvm_list_vg_names(lvm);
93     if (!vg_names) {
94         ERROR("lvm plugin lvm_list_vg_name failed %s", lvm_errmsg(lvm));
95         lvm_quit(lvm);
96         return (-1);
97     }
98
99     dm_list_iterate_items(name_list, vg_names) {
100         vg_t vg;
101
102         vg = lvm_vg_open(lvm, name_list->str, "r", 0);
103         if (!vg) {
104             ERROR ("lvm plugin: lvm_vg_open (%s) failed: %s",
105                     name_list->str, lvm_errmsg(lvm));
106             continue;
107         }
108
109         vg_read(vg, name_list->str);
110         lvm_vg_close(vg);
111     }
112
113     lvm_quit(lvm);
114     return (0);
115 } /*lvm_read */
116
117 void module_register(void)
118 {
119     plugin_register_read("lvm", lvm_read);
120 } /* void module_register */