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 **/
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
26 #if KERNEL_LINUX
27 # define ENTROPY_HAVE_READ 1
28 #else
29 # define ENTROPY_HAVE_READ 0
30 #endif
32 #define ENTROPY_FILE "/proc/sys/kernel/random/entropy_avail"
34 #if ENTROPY_HAVE_READ
35 static void entropy_submit (double entropy)
36 {
37 value_t values[1];
38 value_list_t vl = VALUE_LIST_INIT;
40 values[0].gauge = entropy;
42 vl.values = values;
43 vl.values_len = 1;
44 vl.time = time (NULL);
45 strcpy (vl.host, hostname_g);
46 strcpy (vl.plugin, "entropy");
47 strcpy (vl.plugin_instance, "");
48 strcpy (vl.type_instance, "");
50 plugin_dispatch_values ("entropy", &vl);
51 }
53 static int entropy_read (void)
54 {
55 #if KERNEL_LINUX
56 double entropy;
57 FILE *fh;
58 char buffer[64];
60 fh = fopen (ENTROPY_FILE, "r");
61 if (fh == NULL)
62 return (-1);
64 if (fgets (buffer, sizeof (buffer), fh) == NULL)
65 {
66 fclose (fh);
67 return (-1);
68 }
69 fclose (fh);
71 entropy = atof (buffer);
73 if (entropy > 0.0)
74 entropy_submit (entropy);
75 #endif /* KERNEL_LINUX */
77 return (0);
78 }
79 #endif /* ENTROPY_HAVE_READ */
81 void module_register (void)
82 {
83 #if ENTROPY_HAVE_READ
84 plugin_register_read ("entropy", entropy_read);
85 #endif
86 } /* void module_register */