Merge branch 'collectd-5.3' into collectd-5.4
[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 int vg_read(vg_t vg, char const *vg_name)
49 {
50     struct dm_list *lvs;
51     struct lvm_lv_list *lvl;
52
53     lvm_submit (vg_name, "free", lvm_vg_get_free_size(vg));
54
55     lvs = lvm_vg_list_lvs(vg);
56     if (!lvs) {
57         /* no VGs are defined, which is not an error per se */
58         return (0);
59     }
60
61     dm_list_iterate_items(lvl, lvs) {
62          lvm_submit(vg_name, lvm_lv_get_name(lvl->lv), lvm_lv_get_size(lvl->lv));
63     }
64
65     return (0);
66 }
67
68 static int lvm_read(void)
69 {
70     lvm_t lvm;
71     struct dm_list *vg_names;
72     struct lvm_str_list *name_list;
73
74     lvm = lvm_init(NULL);
75     if (!lvm) {
76         ERROR("lvm plugin: lvm_init failed.");
77         return (-1);
78     }
79
80     vg_names = lvm_list_vg_names(lvm);
81     if (!vg_names) {
82         ERROR("lvm plugin lvm_list_vg_name failed %s", lvm_errmsg(lvm));
83         lvm_quit(lvm);
84         return (-1);
85     }
86
87     dm_list_iterate_items(name_list, vg_names) {
88         vg_t vg;
89
90         vg = lvm_vg_open(lvm, name_list->str, "r", 0);
91         if (!vg) {
92             ERROR ("lvm plugin: lvm_vg_open (%s) failed: %s",
93                     name_list->str, lvm_errmsg(lvm));
94             continue;
95         }
96
97         vg_read(vg, name_list->str);
98         lvm_vg_close(vg);
99     }
100
101     lvm_quit(lvm);
102     return (0);
103 } /*lvm_read */
104
105 void module_register(void)
106 {
107     plugin_register_read("lvm", lvm_read);
108 } /* void module_register */