606acc70befb95b120e2155cfb5ace5758a387e0
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 void 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 }
59 }
61 static int lvm_read(void)
62 {
63 lvm_t lvm;
64 struct dm_list *vg_names;
65 struct lvm_str_list *name_list;
67 lvm = lvm_init(NULL);
68 if (!lvm) {
69 ERROR("lvm plugin: lvm_init failed.");
70 return (-1);
71 }
73 vg_names = lvm_list_vg_names(lvm);
74 if (!vg_names) {
75 ERROR("lvm plugin lvm_list_vg_name failed %s", lvm_errmsg(lvm));
76 lvm_quit(lvm);
77 return (-1);
78 }
80 dm_list_iterate_items(name_list, vg_names) {
81 vg_t vg;
83 vg = lvm_vg_open(lvm, name_list->str, "r", 0);
84 if (!vg) {
85 ERROR ("lvm plugin: lvm_vg_open (%s) failed: %s",
86 name_list->str, lvm_errmsg(lvm));
87 continue;
88 }
90 vg_read(vg, name_list->str);
91 lvm_vg_close(vg);
92 }
94 lvm_quit(lvm);
95 return (0);
96 } /*lvm_read */
98 void module_register(void)
99 {
100 plugin_register_read("lvm", lvm_read);
101 } /* void module_register */