retire da-backup checks
[mirror/dsa-nagios.git] / dsa-nagios-checks / checks / dsa-check-entropy
1 #!/usr/bin/python
2
3 # Copyright 2011 Peter Palfrader
4 # Copyright 2014 Hendrik Koehler
5 #
6 # Permission is hereby granted, free of charge, to any person obtaining
7 # a copy of this software and associated documentation files (the
8 # "Software"), to deal in the Software without restriction, including
9 # without limitation the rights to use, copy, modify, merge, publish,
10 # distribute, sublicense, and/or sell copies of the Software, and to
11 # permit persons to whom the Software is furnished to do so, subject to
12 # the following conditions:
13 #
14 # The above copyright notice and this permission notice shall be
15 # included in all copies or substantial portions of the Software.
16 #
17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
25 import sys
26 import os
27 import time
28 import optparse
29
30 AVAIL = '/proc/sys/kernel/random/entropy_avail'
31
32 parser = optparse.OptionParser()
33 parser.add_option("-r", "--retries", dest="retries", metavar="NUM",
34   type="int", default=10,
35   help="Max number of retries [10].")
36 parser.add_option("-s", "--sleep", dest="sleep", metavar="MSECS",
37   type="int", default=250,
38   help="Amount of time to wait between reads [250msec].")
39 parser.add_option("-w", "--watermark", dest="watermark", metavar="BYTES",
40   type="int", default=800,
41   help="Minimum number of expected bytes in the entropy pool [800].")
42 (options, args) = parser.parse_args()
43
44 if len(args) != 0:
45     parser.print_help()
46     sys.exit(4)
47
48 if not os.path.exists(AVAIL):
49     print "File %s does not exist."%(AVAIL)
50     sys.exit(4)
51
52 tries = 0
53 values = []
54 while tries <= options.retries:
55     f = open('/proc/sys/kernel/random/entropy_avail')
56     avail = f.readline().rstrip()
57     if len(avail) == 0:
58         print "Could not read anything from %s"%(AVAIL)
59         sys.exit(4)
60     try:
61         avail = int(avail)
62     except ValueError:
63         print "Did not read a number from %s, got '%s' instead"%(AVAIL, avail)
64         sys.exit(4)
65
66     if avail >= options.watermark:
67         if tries > 0:
68             print "OK: %d bytes in the pool after %d attempts.|entropy=%d bytes"%(avail, tries, avail)
69         else:
70             print "OK: %d bytes in the pool.|entropy=%d bytes"%(avail, avail)
71         sys.exit(0)
72
73     values.append(avail)
74     time.sleep(1.0 * options.sleep / 1000)
75     tries += 1
76
77 print "WARNING: Too little entropy in the pool (min: %d, max: %d in %d reads)|entropy=%d bytes"%(min(values), max(values), tries-1, avail)
78 sys.exit(1)
79
80 # vim:set et:
81 # vim:set ts=4:
82 # vim:set shiftwidth=4: