added the use of df_complex and changed fprintf to ERROR for error
[collectd.git] / src / volume.c
1 /**
2  * collectd - src/volume.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 volume_submit(const char *vol_name, const char *type, const char type_instance, gauge_t value)
29 {
30     value_t values[1];
31     value_list_t vl = VALUE_LIST_INIT;
32
33     values[0].gauge = value;
34
35     vl.values = values;
36     vl.values_len = STATIC_ARRAY_SIZE (values);
37     sstrncpy(vl.host, hostname_g, sizeof (vl.host));
38     sstrncpy(vl.type, type, sizeof (vl.type));
39     sstrncpy(vl.type_instance, type_instacne, sizeof (vl.type_instance));
40
41     plugin_dispatch_values (&vl);
42 }
43
44 static int volume_read(void)
45 {
46     lvm_t lvm;
47     vg_t vg;
48     int status = 0;
49     struct dm_list *vg_names;
50     struct lvm_str_list *name_list;
51
52     lvm = lvm_init(NULL);
53     if (!lvm) {
54         status = lvm_errno(lvm);
55         ERROR("volume plugin: lvm_init failed: %s", lvm_errmsg(lvm));
56     }
57
58     vg_names = lvm_list_vg_names(lvm);
59     if (!vg_names) {
60         status = lvm_errno(lvm);
61         ERROR("volume plugin lvm_list_vg_name failed %s", lvm_errmsg(lvm));
62     }
63
64     dm_list_iterate_items(name_list, vg_names) {
65         vg = lvm_vg_open(lvm, name_list->str, "r", 0);
66         volume_submit(name_list->str, "df_complex", "size", lvm_vg_get_size(vg));
67         volume_submit(name_list->str, "df_complex", "free", lvm_vg_get_free_size(vg));
68
69         lvm_vg_close(vg);
70     }
71
72     lvm_quit(lvm);
73     return (0);
74 }
75
76 void module_register(void)
77 {
78         plugin_register_read("volume", volume_read);
79 } /* void module_register */