-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentropy_monitor.c
318 lines (305 loc) · 11.5 KB
/
entropy_monitor.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/******
*
* avail_entropy_mon.c
*
*
* collect available entropy for a number of iterations
* with an sleep interval in between each iteration.
*
* after the number of iterations
* print maximum, minimum and average
* to stdout in a zabbix-sender friendly format
* send information to syslog local1
*
* the math may be a bit sloppy
* ... what if entropy is over 32768? Maybe cap it at maximum
* ... what if one is using alot of iterations
* just keeping it simple for now.
*
***********/
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <linux/random.h>
#include <fcntl.h>
#include <syslog.h>
#include <sys/stat.h>
#define TRUE 1
#define FALSE 0
// eventually, these will be default values and set by options
// measure once a second, log results about once a minute
#define ITERATIONS_DEFAULT 60 // iterations to between outputting data
#define ITERATIONS_TESTING 10 // for testing
// iterations to between outputting data
#define SLEEP_DEFAULT 1 // sleep interval between iterations in seconds
// should get these from proc filesytem
#define READ_WAKEUP_THRESHOLD 64
#define WRITE_WAKEUP_THRESHOLD 128
void show_help()
{
fprintf ( stderr,
"Usage: entropy_monitor -dhltvz\n"
"Collect and output information about available entropy in /dev/random\n\n"
" -d debugging output to stderr\n"
" -h show this help\n"
" -l log to syslog\n"
" -v verbose output to stderr\n"
" -z output for suitable for zabbix_sender to stdout\n"
);
}
int main (int argc, char **argv)
{
// constants
const char dev_random_file[] = "/dev/random";
// flags to set with getopt.
int debug_flag = FALSE;
int zabbix_flag = FALSE;
int syslog_flag = FALSE;
int verbose_flag = FALSE;
int test_flag = FALSE; // test values
// other variables with a defaults
int samples_of_avail_ent = ITERATIONS_DEFAULT;
int error_found = FALSE;
int read_wakeup_threshold = 0;
int write_wakeup_threshold = 0;
//
int avail_ent;
int result;
int dev_random_filehandle;
struct stat dev_random_stat;
// getopt
while (( result = getopt (argc, argv, "dhltvz")) != -1)
{
switch (result)
{
case 'd':
debug_flag = TRUE;
break;
case 'h':
show_help();
return 0;
break;
case 'l':
syslog_flag = TRUE;
break;
case 't': //testing values
test_flag = TRUE;
read_wakeup_threshold = 3400;
write_wakeup_threshold = 4000;
samples_of_avail_ent = ITERATIONS_TESTING;
break;
case 'v':
verbose_flag = TRUE;
break;
case 'z':
zabbix_flag = TRUE;
break;
default:
show_help();
abort ();
}
}
if ( debug_flag )
{
fprintf (stderr, "DEBUG: "
"debug_flag: %d syslog_flag: %d test_flag: %d verbose_flag: %d zabbix_flag: %d\n",
debug_flag, syslog_flag, test_flag, verbose_flag, zabbix_flag);
}
// open files
dev_random_filehandle = open ( dev_random_file , O_RDONLY, O_NONBLOCK);
// get inode number
stat( dev_random_file, &dev_random_stat );
if ( debug_flag )
fprintf(stderr, "DEBUG: %s I-node number: %ld\n",dev_random_file , (long) dev_random_stat.st_ino);
if ( syslog_flag )
{
openlog ("entropy_monitor", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1);
}
if ( verbose_flag )
fprintf (stderr, "Collecting available entropy from %s for %d iterations every %d seconds\n", dev_random_file,
samples_of_avail_ent, SLEEP_DEFAULT);
// TODO log start
if (!test_flag)
{
// get read_wakeup_threshold
read_wakeup_threshold = READ_WAKEUP_THRESHOLD;
// get write_wakeup_threshold
write_wakeup_threshold = WRITE_WAKEUP_THRESHOLD;
}
// Log threshold values
// could send threshold values to zabbix to set the triggers.
// loop ... until error, maybe check for sigint or something else later
while (!error_found)
{
// loop 60 iterations (about 60 s)
long avail_entropy_sum = 0;
int avail_entropy_avg = 0;
int avail_entropy_high = 0;
int avail_entropy_low = 0;
int i;
int below_read_wakeup_threshold_count = 0;
int below_write_wakeup_threshold_count = 0;
int consecutive_below_read_wakeup_threshold_count = 0;
int consecutive_below_write_wakeup_threshold_count = 0;
int max_consecutive_below_read_wakeup_threshold_count = 0;
int max_consecutive_below_write_wakeup_threshold_count = 0;
int avail_ent_previous = -1;
for ( i = 1; i <= samples_of_avail_ent; i++)
{
// test dev random that dev random is random and get avail entropy
result = ioctl (dev_random_filehandle, RNDGETENTCNT, &avail_ent);
if (result)
{
fprintf (stderr, "/dev/random result = %d\n", result);
fprintf (stderr, "/dev/random is not a random device\n");
// the available entropy is not valid set to -1
avail_ent = -1;
error_found = TRUE;
break;
}
// on first iteration set high and low available entropy to the current value
if ( i == 1 )
{
avail_entropy_high = avail_ent;
avail_entropy_low = avail_ent;
}
// check for and set highs and lows
if ( avail_ent > avail_entropy_high )
avail_entropy_high = avail_ent;
if ( avail_ent < avail_entropy_low )
avail_entropy_low = avail_ent;
// collect the sum to calculate the average later on
avail_entropy_sum = avail_entropy_sum + avail_ent;
if ( debug_flag || verbose_flag )
fprintf(stderr, "/dev/random avail_ent = %d iters %d sum %ld\n",
avail_ent, i, avail_entropy_sum);
// check if entropy is lower than the thresholds and increment counters
// and if it is not the first iteration check for consecutive low entropy and increment counters
if ( avail_ent < below_write_wakeup_threshold_count )
{
below_write_wakeup_threshold_count++;
// read is always lower than write
if ( avail_ent < below_read_wakeup_threshold_count )
{
below_read_wakeup_threshold_count++;
}
// if we are below a threshold check which processes and users are using /dev/random
// log them (maybe log every sample as in theory these shouldn't last long)
fprintf (stdout, "- kernel.random.entropy_avail.log \"%s\"\n",
"low avalable entropy log text here" );
}
// calculate consecutive low entropy streaks
// and check if it is the biggest
if ( avail_ent_previous != -1 ) // don't do this if it is the first iteration
{
// check read wakeup threshold
if ( ( avail_ent_previous < read_wakeup_threshold ) && ( avail_ent < read_wakeup_threshold ))
{
// in a streak ... if not zero add one before printing, as a streak means at least 2 consecutive measurements
consecutive_below_read_wakeup_threshold_count++ ;
if (consecutive_below_read_wakeup_threshold_count > max_consecutive_below_read_wakeup_threshold_count)
{
// this is the longest streak
max_consecutive_below_read_wakeup_threshold_count = consecutive_below_read_wakeup_threshold_count;
}
}
else
{
// we are no longer in a streak zero the counter
consecutive_below_read_wakeup_threshold_count = 0;
}
// check write wakeup threshold
if ( ( avail_ent_previous < write_wakeup_threshold ) && ( avail_ent < write_wakeup_threshold ))
{
// in a streak ... if not zero add one before printing, as a streak means at least 2 consecutive measurements
consecutive_below_write_wakeup_threshold_count++ ;
if (consecutive_below_write_wakeup_threshold_count > max_consecutive_below_write_wakeup_threshold_count)
{
// this is the longest streak
max_consecutive_below_write_wakeup_threshold_count = consecutive_below_write_wakeup_threshold_count;
}
}
else
{
// we are no longer in a streak zero the counter
consecutive_below_write_wakeup_threshold_count = 0;
}
}
// remember entropy previous for the next time around
avail_ent_previous = avail_ent;
sleep ( SLEEP_DEFAULT );
} // end iteration loop
// account for first measurement ( if there is a streak)
if ( max_consecutive_below_read_wakeup_threshold_count > 0 )
max_consecutive_below_read_wakeup_threshold_count++;
if ( max_consecutive_below_write_wakeup_threshold_count > 0 )
max_consecutive_below_write_wakeup_threshold_count++;
avail_entropy_avg = (long) avail_entropy_sum / (long) samples_of_avail_ent ;
// notify times below read or write wakeup threshold
// send to zabbix always if enabled...even if value is 0, can alert of the zast value is > 0
if ( zabbix_flag )
{
fprintf (stdout, "- kernel.random.entropy_avail.maxsamplesbelow_read_wu_threshhold %d\n",
max_consecutive_below_read_wakeup_threshold_count );
fprintf (stdout, "- kernel.random.entropy_avail.maxsamplesbelow_write_wu_threshhold %d\n",
max_consecutive_below_write_wakeup_threshold_count );
}
// other only send if below threshold
if ( max_consecutive_below_read_wakeup_threshold_count > 0 )
{
if ( syslog_flag )
{
syslog ( LOG_WARNING, "entropy below read wakeup value %d for a maximum of %d consecutive measurements",
read_wakeup_threshold, max_consecutive_below_read_wakeup_threshold_count );
}
if ( debug_flag || verbose_flag )
{
fprintf (stderr, "entropy below read wakeup value %d for a maximum of %d consecutive measurements\n",
write_wakeup_threshold, max_consecutive_below_read_wakeup_threshold_count);
}
}
if ( max_consecutive_below_write_wakeup_threshold_count > 0 )
{
if ( syslog_flag )
{
syslog ( LOG_WARNING, "entropy below write wakeup value %d for a maximum of %d consecutive measurements",
write_wakeup_threshold, max_consecutive_below_write_wakeup_threshold_count );
}
if ( debug_flag || verbose_flag )
{
fprintf (stderr, "entropy below write wakeup value %d for a maximum of %d consecutive measurements\n",
write_wakeup_threshold, max_consecutive_below_write_wakeup_threshold_count);
}
}
// info ... avg, log, high
if ( syslog_flag )
{
syslog ( LOG_INFO , "avail.low %d high %d mean %d for last period of (%d) iterations", avail_entropy_low, avail_entropy_high, avail_entropy_avg, samples_of_avail_ent );
}
if ( debug_flag || verbose_flag )
{
fprintf ( stderr, "avail.low %d high %d mean %d for last period of (%d) iterations\n", avail_entropy_low, avail_entropy_high, avail_entropy_avg, samples_of_avail_ent );
}
if ( zabbix_flag )
{
// data to stdout for piping to be piped to zabbix_sender
fprintf (stdout, "- kernel.random.entropy_avail.mean %d\n", avail_entropy_avg );
fprintf (stdout, "- kernel.random.entropy_avail.high %d\n", avail_entropy_high );
fprintf (stdout, "- kernel.random.entropy_avail.low %d\n", avail_entropy_low );
}
// console output mode?
// this is probably not necessary.
fflush_unlocked(stdout);
}
// print this even if no NO_STDERR set
fprintf (stderr, "exiting due to error\n");
// send a log message too!
if ( syslog_flag )
{
syslog ( LOG_ERR , "exiting due to error");
closelog ();
}
close (dev_random_filehandle);
return 1;
}