| 1 |
#!/usr/bin/python3 |
| 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 |
from __future__ import print_function |
| 26 |
|
| 27 |
import sys |
| 28 |
import os |
| 29 |
import time |
| 30 |
import optparse |
| 31 |
|
| 32 |
AVAIL = '/proc/sys/kernel/random/entropy_avail' |
| 33 |
|
| 34 |
parser = optparse.OptionParser() |
| 35 |
parser.add_option("-r", "--retries", dest="retries", metavar="NUM", |
| 36 |
type="int", default=10, |
| 37 |
help="Max number of retries [10].") |
| 38 |
parser.add_option("-s", "--sleep", dest="sleep", metavar="MSECS", |
| 39 |
type="int", default=250, |
| 40 |
help="Amount of time to wait between reads [250msec].") |
| 41 |
parser.add_option("-w", "--watermark", dest="watermark", metavar="BYTES", |
| 42 |
type="int", default=800, |
| 43 |
help="Minimum number of expected bytes in the entropy pool [800].") |
| 44 |
(options, args) = parser.parse_args() |
| 45 |
|
| 46 |
if len(args) != 0: |
| 47 |
parser.print_help() |
| 48 |
sys.exit(4) |
| 49 |
|
| 50 |
if not os.path.exists(AVAIL): |
| 51 |
print("File %s does not exist." % AVAIL) |
| 52 |
sys.exit(4) |
| 53 |
|
| 54 |
tries = 0 |
| 55 |
values = [] |
| 56 |
while tries <= options.retries: |
| 57 |
f = open(AVAIL) |
| 58 |
avail = f.readline().rstrip() |
| 59 |
if len(avail) == 0: |
| 60 |
print("Could not read anything from %s" % AVAIL) |
| 61 |
sys.exit(4) |
| 62 |
try: |
| 63 |
avail = int(avail) |
| 64 |
except ValueError: |
| 65 |
print("Did not read a number from %s, got '%s' instead" % (AVAIL, avail)) |
| 66 |
sys.exit(4) |
| 67 |
|
| 68 |
if avail >= options.watermark: |
| 69 |
if tries > 0: |
| 70 |
print("OK: %d bytes in the pool after %d attempts.|entropy=%d bytes" % (avail, tries, avail)) |
| 71 |
else: |
| 72 |
print("OK: %d bytes in the pool.|entropy=%d bytes" % (avail, avail)) |
| 73 |
sys.exit(0) |
| 74 |
|
| 75 |
values.append(avail) |
| 76 |
time.sleep(1.0 * options.sleep / 1000) |
| 77 |
tries += 1 |
| 78 |
|
| 79 |
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)) |
| 80 |
sys.exit(1) |
| 81 |
|
| 82 |
# vim:set et: |
| 83 |
# vim:set ts=4: |
| 84 |
# vim:set shiftwidth=4: |