Merge branch 'collectd-5.4'
[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 #include "common.h"
28 #include "plugin.h"
29
30 #define NO_VALUE UINT64_MAX
31 #define PERCENT_SCALE_FACTOR 1e-8
32
33 static uint64_t get_lv_property_int(lv_t lv, char const *property)
34 {
35     lvm_property_value_t v;
36
37     v = lvm_lv_get_property(lv, property);
38     if (!v.is_valid || !v.is_integer)
39         return NO_VALUE;
40     /* May be NO_VALUE if @property does not apply to this LV */
41     return v.value.integer;
42 }
43
44 static char const *get_lv_property_string(lv_t lv, char const *property)
45 {
46     lvm_property_value_t v;
47
48     v = lvm_lv_get_property(lv, property);
49     if (!v.is_valid || !v.is_string)
50         return NULL;
51     return v.value.string;
52 }
53
54 static void lvm_submit (char const *plugin_instance, char const *type_instance,
55         uint64_t ivalue)
56 {
57     value_t v;
58     value_list_t vl = VALUE_LIST_INIT;
59
60     v.gauge = (gauge_t) ivalue;
61
62     vl.values = &v;
63     vl.values_len = 1;
64
65     sstrncpy(vl.host, hostname_g, sizeof (vl.host));
66     sstrncpy(vl.plugin, "lvm", sizeof (vl.plugin));
67     sstrncpy(vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
68     sstrncpy(vl.type, "df_complex", sizeof (vl.type));
69     sstrncpy(vl.type_instance, type_instance, sizeof (vl.type_instance));
70
71     plugin_dispatch_values (&vl);
72 }
73
74 static void report_lv_utilization(lv_t lv, char const *vg_name,
75         char const *lv_name, uint64_t lv_size,
76         char const *used_percent_property)
77 {
78     uint64_t used_percent_unscaled;
79     uint64_t used_bytes;
80     char plugin_instance[DATA_MAX_NAME_LEN];
81
82     used_percent_unscaled = get_lv_property_int(lv, used_percent_property);
83     if (used_percent_unscaled == NO_VALUE)
84         return;
85     used_bytes = lv_size * (used_percent_unscaled * PERCENT_SCALE_FACTOR);
86
87     ssnprintf(plugin_instance, sizeof(plugin_instance), "%s-%s",
88             vg_name, lv_name);
89     lvm_submit(plugin_instance, "used", used_bytes);
90     lvm_submit(plugin_instance, "free", lv_size - used_bytes);
91 }
92
93 static void report_thin_pool_utilization(lv_t lv, char const *vg_name,
94         uint64_t lv_size)
95 {
96     char const *data_lv;
97     char const *metadata_lv;
98     uint64_t metadata_size;
99
100     data_lv = get_lv_property_string(lv, "data_lv");
101     metadata_lv = get_lv_property_string(lv, "metadata_lv");
102     metadata_size = get_lv_property_int(lv, "lv_metadata_size");
103     if (data_lv == NULL || metadata_lv == NULL || metadata_size == NO_VALUE)
104         return;
105
106     report_lv_utilization(lv, vg_name, data_lv, lv_size, "data_percent");
107     report_lv_utilization(lv, vg_name, metadata_lv, metadata_size,
108             "metadata_percent");
109 }
110
111 static void vg_read(vg_t vg, char const *vg_name)
112 {
113     struct dm_list *lvs;
114     struct lvm_lv_list *lvl;
115     char const *name;
116     char const *attrs;
117     uint64_t size;
118
119     lvm_submit (vg_name, "free", lvm_vg_get_free_size(vg));
120
121     lvs = lvm_vg_list_lvs(vg);
122     if (!lvs) {
123         /* no VGs are defined, which is not an error per se */
124         return;
125     }
126
127     dm_list_iterate_items(lvl, lvs) {
128          lvm_submit(vg_name, lvm_lv_get_name(lvl->lv), lvm_lv_get_size(lvl->lv));
129     }
130
131     dm_list_iterate_items(lvl, lvs) {
132         name = lvm_lv_get_name(lvl->lv);
133         attrs = get_lv_property_string(lvl->lv, "lv_attr");
134         size = lvm_lv_get_size(lvl->lv);
135         if (name == NULL || attrs == NULL || size == NO_VALUE)
136             continue;
137
138         /* Condition on volume type.  We want the reported sizes in the
139            volume group to sum to the size of the volume group, so we ignore
140            virtual volumes.  */
141         switch (attrs[0]) {
142             case 's':
143             case 'S':
144                 /* Snapshot.  Also report used/free space. */
145                 report_lv_utilization(lvl->lv, vg_name, name, size,
146                         "data_percent");
147                 break;
148             case 't':
149                 /* Thin pool virtual volume.  We report the underlying data
150                    and metadata volumes, not this one.  Report used/free
151                    space, then ignore. */
152                 report_thin_pool_utilization(lvl->lv, vg_name, size);
153                 continue;
154             case 'v':
155                 /* Virtual volume.  Ignore. */
156                 continue;
157             case 'V':
158                 /* Thin volume or thin snapshot.  Ignore. */
159                 continue;
160         }
161         lvm_submit(vg_name, name, size);
162     }
163 }
164
165 static int lvm_read(void)
166 {
167     lvm_t lvm;
168     struct dm_list *vg_names;
169     struct lvm_str_list *name_list;
170
171     lvm = lvm_init(NULL);
172     if (!lvm) {
173         ERROR("lvm plugin: lvm_init failed.");
174         return (-1);
175     }
176
177     vg_names = lvm_list_vg_names(lvm);
178     if (!vg_names) {
179         ERROR("lvm plugin lvm_list_vg_name failed %s", lvm_errmsg(lvm));
180         lvm_quit(lvm);
181         return (-1);
182     }
183
184     dm_list_iterate_items(name_list, vg_names) {
185         vg_t vg;
186
187         vg = lvm_vg_open(lvm, name_list->str, "r", 0);
188         if (!vg) {
189             ERROR ("lvm plugin: lvm_vg_open (%s) failed: %s",
190                     name_list->str, lvm_errmsg(lvm));
191             continue;
192         }
193
194         vg_read(vg, name_list->str);
195         lvm_vg_close(vg);
196     }
197
198     lvm_quit(lvm);
199     return (0);
200 } /*lvm_read */
201
202 void module_register(void)
203 {
204     plugin_register_read("lvm", lvm_read);
205 } /* void module_register */