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 **/
22 #include <lvm2app.h>
24 #include "collectd.h"
25 #include "common.h"
26 #include "plugin.h"
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;
34 v.gauge = (gauge_t) ivalue;
36 vl.values = &v;
37 vl.values_len = 1;
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));
45 plugin_dispatch_values (&vl);
46 }
48 static int vg_read(vg_t vg, char const *vg_name)
49 {
50 struct dm_list *lvs;
51 struct lvm_lv_list *lvl;
53 lvm_submit (vg_name, "free", lvm_vg_get_free_size(vg));
55 lvs = lvm_vg_list_lvs(vg);
56 dm_list_iterate_items(lvl, lvs) {
57 lvm_submit(vg_name, lvm_lv_get_name(lvl->lv), lvm_lv_get_size(lvl->lv));
58 }
60 return (0);
61 }
63 static int lvm_read(void)
64 {
65 lvm_t lvm;
66 struct dm_list *vg_names;
67 struct lvm_str_list *name_list;
69 lvm = lvm_init(NULL);
70 if (!lvm) {
71 ERROR("lvm plugin: lvm_init failed.");
72 return (-1);
73 }
75 vg_names = lvm_list_vg_names(lvm);
76 if (!vg_names) {
77 ERROR("lvm plugin lvm_list_vg_name failed %s", lvm_errmsg(lvm));
78 lvm_quit(lvm);
79 return (-1);
80 }
82 dm_list_iterate_items(name_list, vg_names) {
83 vg_t vg;
85 vg = lvm_vg_open(lvm, name_list->str, "r", 0);
86 if (!vg) {
87 ERROR ("lvm plugin: lvm_vg_open (%s) failed: %s",
88 name_list->str, lvm_errmsg(lvm));
89 continue;
90 }
92 vg_read(vg, name_list->str);
93 lvm_vg_close(vg);
94 }
96 lvm_quit(lvm);
97 return (0);
98 } /*lvm_read */
100 void module_register(void)
101 {
102 plugin_register_read("lvm", lvm_read);
103 } /* void module_register */