entropy plugin: Added a plugin to collect available entropy.
[collectd.git] / src / entropy.c
1 /**
2  * collectd - src/entropy.c
3  * Copyright (C) 2005,2006  Florian octo Forster
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  *   Florian octo Forster <octo at verplant.org>
20  **/
21
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25
26 #if KERNEL_LINUX
27 # define ENTROPY_HAVE_READ 1
28 #else
29 # define ENTROPY_HAVE_READ 0
30 #endif
31
32 #define ENTROPY_FILE "/proc/sys/kernel/random/entropy_avail"
33
34 static data_source_t dsrc[1] =
35 {
36         {"entropy",  DS_TYPE_GAUGE, 0.0, 4294967295.0}
37 };
38
39 static data_set_t ds =
40 {
41         "entropy", 1, dsrc
42 };
43
44 #if ENTROPY_HAVE_READ
45 static void entropy_submit (double entropy)
46 {
47         value_t values[1];
48         value_list_t vl = VALUE_LIST_INIT;
49
50         values[0].gauge = entropy;
51
52         vl.values = values;
53         vl.values_len = 1;
54         vl.time = time (NULL);
55         strcpy (vl.host, hostname);
56         strcpy (vl.plugin, "entropy");
57         strcpy (vl.plugin_instance, "");
58         strcpy (vl.type_instance, "");
59
60         plugin_dispatch_values ("entropy", &vl);
61 }
62
63 static int entropy_read (void)
64 {
65 #if KERNEL_LINUX
66         double entropy;
67         FILE *fh;
68         char buffer[64];
69
70         fh = fopen (ENTROPY_FILE, "r");
71         if (fh == NULL)
72                 return (-1);
73
74         if (fgets (buffer, sizeof (buffer), fh) == NULL)
75         {
76                 fclose (fh);
77                 return (-1);
78         }
79         fclose (fh);
80
81         entropy = atof (buffer);
82         
83         if (entropy > 0.0)
84                 entropy_submit (entropy);
85 #endif /* KERNEL_LINUX */
86
87         return (0);
88 }
89 #endif /* ENTROPY_HAVE_READ */
90
91 void module_register (void)
92 {
93         plugin_register_data_set (&ds);
94 #if ENTROPY_HAVE_READ
95         plugin_register_read ("entropy", entropy_read);
96 #endif
97 }