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