3 * Copyright (C) 2013 Chad Malfait
4 * Copyright (C) 2014 Carnegie Mellon University
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.
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.
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
20 * Chad Malfait <malfaitc at yahoo.com>
21 * Benjamin Gilbert <bgilbert at backtick.net>
31 #ifdef HAVE_SYS_CAPABILITY_H
32 #include <sys/capability.h>
33 #endif /* HAVE_SYS_CAPABILITY_H */
35 #define NO_VALUE UINT64_MAX
36 #define PERCENT_SCALE_FACTOR 1e-8
38 static uint64_t get_lv_property_int(lv_t lv, char const *property) {
39 lvm_property_value_t v;
41 v = lvm_lv_get_property(lv, property);
42 if (!v.is_valid || !v.is_integer)
44 /* May be NO_VALUE if @property does not apply to this LV */
45 return v.value.integer;
48 static char const *get_lv_property_string(lv_t lv, char const *property) {
49 lvm_property_value_t v;
51 v = lvm_lv_get_property(lv, property);
52 if (!v.is_valid || !v.is_string)
54 return v.value.string;
57 static void lvm_submit(char const *plugin_instance, char const *type_instance,
59 value_list_t vl = VALUE_LIST_INIT;
61 vl.values = &(value_t){.gauge = (gauge_t)ivalue};
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));
69 plugin_dispatch_values(&vl);
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;
77 char plugin_instance[DATA_MAX_NAME_LEN];
79 used_percent_unscaled = get_lv_property_int(lv, used_percent_property);
80 if (used_percent_unscaled == NO_VALUE)
82 used_bytes = lv_size * (used_percent_unscaled * PERCENT_SCALE_FACTOR);
84 snprintf(plugin_instance, sizeof(plugin_instance), "%s-%s", vg_name, lv_name);
85 lvm_submit(plugin_instance, "used", used_bytes);
86 lvm_submit(plugin_instance, "free", lv_size - used_bytes);
89 static void report_thin_pool_utilization(lv_t lv, char const *vg_name,
92 char const *metadata_lv;
93 uint64_t metadata_size;
95 data_lv = get_lv_property_string(lv, "data_lv");
96 metadata_lv = get_lv_property_string(lv, "metadata_lv");
97 metadata_size = get_lv_property_int(lv, "lv_metadata_size");
98 if (data_lv == NULL || metadata_lv == NULL || metadata_size == NO_VALUE)
101 report_lv_utilization(lv, vg_name, data_lv, lv_size, "data_percent");
102 report_lv_utilization(lv, vg_name, metadata_lv, metadata_size,
106 static void vg_read(vg_t vg, char const *vg_name) {
108 struct lvm_lv_list *lvl;
113 lvm_submit(vg_name, "free", lvm_vg_get_free_size(vg));
115 lvs = lvm_vg_list_lvs(vg);
117 /* no VGs are defined, which is not an error per se */
121 dm_list_iterate_items(lvl, lvs) {
122 name = lvm_lv_get_name(lvl->lv);
123 attrs = get_lv_property_string(lvl->lv, "lv_attr");
124 size = lvm_lv_get_size(lvl->lv);
125 if (name == NULL || attrs == NULL || size == NO_VALUE)
128 /* Condition on volume type. We want the reported sizes in the
129 volume group to sum to the size of the volume group, so we ignore
134 /* Snapshot. Also report used/free space. */
135 report_lv_utilization(lvl->lv, vg_name, name, size, "data_percent");
138 /* Thin pool virtual volume. We report the underlying data
139 and metadata volumes, not this one. Report used/free
140 space, then ignore. */
141 report_thin_pool_utilization(lvl->lv, vg_name, size);
144 /* Virtual volume. Ignore. */
147 /* Thin volume or thin snapshot. Ignore. */
150 lvm_submit(vg_name, name, size);
154 static int lvm_read(void) {
156 struct dm_list *vg_names;
157 struct lvm_str_list *name_list;
159 lvm = lvm_init(NULL);
161 ERROR("lvm plugin: lvm_init failed.");
165 vg_names = lvm_list_vg_names(lvm);
167 ERROR("lvm plugin lvm_list_vg_name failed %s", lvm_errmsg(lvm));
172 dm_list_iterate_items(name_list, vg_names) {
175 vg = lvm_vg_open(lvm, name_list->str, "r", 0);
177 ERROR("lvm plugin: lvm_vg_open (%s) failed: %s", name_list->str,
182 vg_read(vg, name_list->str);
190 static int c_lvm_init(void) {
191 #if defined(HAVE_SYS_CAPABILITY_H) && defined(CAP_SYS_ADMIN)
192 if (check_capability(CAP_SYS_ADMIN) != 0) {
194 WARNING("lvm plugin: Running collectd as root, but the "
195 "CAP_SYS_ADMIN capability is missing. The plugin's read "
196 "function will probably fail. Is your init system dropping "
199 WARNING("lvm plugin: collectd doesn't have the CAP_SYS_ADMIN "
200 "capability. If you don't want to run collectd as root, try "
201 "running \"setcap cap_sys_admin=ep\" on the collectd binary.");
207 void module_register(void) {
208 plugin_register_init("lvm", c_lvm_init);
209 plugin_register_read("lvm", lvm_read);
210 } /* void module_register */