Code

initial commit of volume.c
[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  **/
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25 #include <lvm2app.h>
27 #define GB (1024*1024*1024)
28 #define MB (1024*1024)
30 static void volume_submit(const char *vol_name, gauge_t size, gauge_t free)
31 {
32     value_t values[2];
33     value_list_t vl = VALUE_LIST_INIT;
35     values[0].gauge = size;
36     values[1].gauge = free;
38     vl.values = values;
39     vl.values_len = STATIC_ARRAY_SIZE (values);
40     sstrncpy(vl.host, hostname_g, sizeof (vl.host));
41     sstrncpy(vl.plugin, "volume", sizeof (vl.plugin));
42     sstrncpy(vl.type, vol_name, sizeof (vl.type));
44     plugin_dispatch_values (&vl);
45 }
47 static int volume_read(void)
48 {
49     lvm_t lvm;
50     vg_t vg;
51     struct dm_list *vgnames;
52     struct lvm_str_list *strl;
54     lvm = lvm_init(NULL);
55     if (!lvm) {
56         fprintf(stderr, "lvm_init() failed\n");
57     }
59     vgnames = lvm_list_vg_names(lvm);
60     if (!vgnames) {
61         fprintf(stderr, "lvm_list_vg_names() failed\n");
62     }
64     dm_list_iterate_items(strl, vgnames) {
65         vg = lvm_vg_open(lvm, strl->str, "r", 0);
66         volume_submit(strl->str, (float)lvm_vg_get_size(vg)/GB, (float)lvm_vg_get_free_size(vg)/GB);
67         lvm_vg_close(vg);
68     }
70     lvm_quit(lvm);
71     return (0);
72 }
74 void module_register(void)
75 {
76         plugin_register_read("volume", volume_read);
77 } /* void module_register */