Merge branch 'collectd-5.5' into collectd-5.6
[collectd.git] / src / lvm.c
1 /**
2  * collectd - src/lvm.c
3  * Copyright (C) 2013       Chad Malfait
4  * Copyright (C) 2014       Carnegie Mellon University
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the
8  * Free Software Foundation; only version 2 of the License is applicable.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18  *
19  * Authors:
20  *   Chad Malfait <malfaitc at yahoo.com>
21  *   Benjamin Gilbert <bgilbert at cs.cmu.edu>
22  **/
23
24 #include <lvm2app.h>
25
26 #include "collectd.h"
27
28 #include "common.h"
29 #include "plugin.h"
30
31 #define NO_VALUE UINT64_MAX
32 #define PERCENT_SCALE_FACTOR 1e-8
33
34 static uint64_t get_lv_property_int(lv_t lv, char const *property)
35 {
36     lvm_property_value_t v;
37
38     v = lvm_lv_get_property(lv, property);
39     if (!v.is_valid || !v.is_integer)
40         return NO_VALUE;
41     /* May be NO_VALUE if @property does not apply to this LV */
42     return v.value.integer;
43 }
44
45 static char const *get_lv_property_string(lv_t lv, char const *property)
46 {
47     lvm_property_value_t v;
48
49     v = lvm_lv_get_property(lv, property);
50     if (!v.is_valid || !v.is_string)
51         return NULL;
52     return v.value.string;
53 }
54
55 static void lvm_submit (char const *plugin_instance, char const *type_instance,
56         uint64_t ivalue)
57 {
58     value_t v;
59     value_list_t vl = VALUE_LIST_INIT;
60
61     v.gauge = (gauge_t) ivalue;
62
63     vl.values = &v;
64     vl.values_len = 1;
65
66     sstrncpy(vl.host, hostname_g, sizeof (vl.host));
67     sstrncpy(vl.plugin, "lvm", sizeof (vl.plugin));
68     sstrncpy(vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
69     sstrncpy(vl.type, "df_complex", sizeof (vl.type));
70     sstrncpy(vl.type_instance, type_instance, sizeof (vl.type_instance));
71
72     plugin_dispatch_values (&vl);
73 }
74
75 static void report_lv_utilization(lv_t lv, char const *vg_name,
76         char const *lv_name, uint64_t lv_size,
77         char const *used_percent_property)
78 {
79     uint64_t used_percent_unscaled;
80     uint64_t used_bytes;
81     char plugin_instance[DATA_MAX_NAME_LEN];
82
83     used_percent_unscaled = get_lv_property_int(lv, used_percent_property);
84     if (used_percent_unscaled == NO_VALUE)
85         return;
86     used_bytes = lv_size * (used_percent_unscaled * PERCENT_SCALE_FACTOR);
87
88     ssnprintf(plugin_instance, sizeof(plugin_instance), "%s-%s",
89             vg_name, lv_name);
90     lvm_submit(plugin_instance, "used", used_bytes);
91     lvm_submit(plugin_instance, "free", lv_size - used_bytes);
92 }
93
94 static void report_thin_pool_utilization(lv_t lv, char const *vg_name,
95         uint64_t lv_size)
96 {
97     char const *data_lv;
98     char const *metadata_lv;
99     uint64_t metadata_size;
100
101     data_lv = get_lv_property_string(lv, "data_lv");
102     metadata_lv = get_lv_property_string(lv, "metadata_lv");
103     metadata_size = get_lv_property_int(lv, "lv_metadata_size");
104     if (data_lv == NULL || metadata_lv == NULL || metadata_size == NO_VALUE)
105         return;
106
107     report_lv_utilization(lv, vg_name, data_lv, lv_size, "data_percent");
108     report_lv_utilization(lv, vg_name, metadata_lv, metadata_size,
109             "metadata_percent");
110 }
111
112 static void vg_read(vg_t vg, char const *vg_name)
113 {
114     struct dm_list *lvs;
115     struct lvm_lv_list *lvl;
116     char const *name;
117     char const *attrs;
118     uint64_t size;
119
120     lvm_submit (vg_name, "free", lvm_vg_get_free_size(vg));
121
122     lvs = lvm_vg_list_lvs(vg);
123     if (!lvs) {
124         /* no VGs are defined, which is not an error per se */
125         return;
126     }
127
128     dm_list_iterate_items(lvl, lvs) {
129         name = lvm_lv_get_name(lvl->lv);
130         attrs = get_lv_property_string(lvl->lv, "lv_attr");
131         size = lvm_lv_get_size(lvl->lv);
132         if (name == NULL || attrs == NULL || size == NO_VALUE)
133             continue;
134
135         /* Condition on volume type.  We want the reported sizes in the
136            volume group to sum to the size of the volume group, so we ignore
137            virtual volumes.  */
138         switch (attrs[0]) {
139             case 's':
140             case 'S':
141                 /* Snapshot.  Also report used/free space. */
142                 report_lv_utilization(lvl->lv, vg_name, name, size,
143                         "data_percent");
144                 break;
145             case 't':
146                 /* Thin pool virtual volume.  We report the underlying data
147                    and metadata volumes, not this one.  Report used/free
148                    space, then ignore. */
149                 report_thin_pool_utilization(lvl->lv, vg_name, size);
150                 continue;
151             case 'v':
152                 /* Virtual volume.  Ignore. */
153                 continue;
154             case 'V':
155                 /* Thin volume or thin snapshot.  Ignore. */
156                 continue;
157         }
158         lvm_submit(vg_name, name, size);
159     }
160 }
161
162 static int lvm_read(void)
163 {
164     lvm_t lvm;
165     struct dm_list *vg_names;
166     struct lvm_str_list *name_list;
167
168     lvm = lvm_init(NULL);
169     if (!lvm) {
170         ERROR("lvm plugin: lvm_init failed.");
171         return (-1);
172     }
173
174     vg_names = lvm_list_vg_names(lvm);
175     if (!vg_names) {
176         ERROR("lvm plugin lvm_list_vg_name failed %s", lvm_errmsg(lvm));
177         lvm_quit(lvm);
178         return (-1);
179     }
180
181     dm_list_iterate_items(name_list, vg_names) {
182         vg_t vg;
183
184         vg = lvm_vg_open(lvm, name_list->str, "r", 0);
185         if (!vg) {
186             ERROR ("lvm plugin: lvm_vg_open (%s) failed: %s",
187                     name_list->str, lvm_errmsg(lvm));
188             continue;
189         }
190
191         vg_read(vg, name_list->str);
192         lvm_vg_close(vg);
193     }
194
195     lvm_quit(lvm);
196     return (0);
197 } /*lvm_read */
198
199 void module_register(void)
200 {
201     plugin_register_read("lvm", lvm_read);
202 } /* void module_register */