diff options
-rw-r--r-- | Makefile | 4 | ||||
-rw-r--r-- | TODO | 1 | ||||
-rw-r--r-- | examples/plugins/random | 36 | ||||
-rw-r--r-- | src/plugins/random.py | 59 |
4 files changed, 97 insertions, 3 deletions
@@ -32,13 +32,13 @@ PLUGINS = chase clock cpuinfo cpuonline cpu df discstats ipaddress \ kmsg leapsec linereader loadavg lunar mem moc network \ pacman snmp snmp6 softirqs solar uname uptime users \ vmstat weather xdisplay xkb alsa dentrystate inodestate \ - files hdparm tzclock ropty ping inotify + files hdparm tzclock ropty ping inotify random PLUGIN_EXAMPLES = chase clock cpu cpuinfo cpuonline df discstats \ ipaddress kmsg loadavg lunar mem moc network \ pacman uname uptime users xdisplay xkb alsa \ dentrystate inodestate files tzclock ropty ping \ - inotify + inotify random TRICK_EXAMPLES = localutcclock anytzclock @@ -45,7 +45,6 @@ List of plugins to implement: /proc/sysvipc /proc/locks /proc/swaps - /proc/sys/kernel/random/ Demo plugins: diff --git a/examples/plugins/random b/examples/plugins/random new file mode 100644 index 0000000..67b97b3 --- /dev/null +++ b/examples/plugins/random @@ -0,0 +1,36 @@ +# -*- python -*- + +# A xpybar configuration example testing the features of plugins.random + +import time +import threading + +from plugins.random import Random +from plugins.clock import Clock + + +OUTPUT, HEIGHT, YPOS, TOP = 0, 12, 24, True + + +clock = Clock(sync_to = Clock.SECONDS) + +start_ = start +def start(): + start_() + async(lambda : clock.continuous_sync(lambda : bar.invalidate())) + + +random_ = Random() +def redraw(): + data = [('Avail', random_.entropy_avail), + ('Size', lambda : random_.poolsize), + ('Read', lambda : random_.read_wakeup_threshold), + ('Write', lambda : random_.write_wakeup_threshold), + ('Reseed', lambda : random_.urandom_min_reseed_secs), + ('Boot', lambda : random_.boot_id)] + + text = ' │ '.join('%s: %s' % (text, str(value())) for (text, value) in data) + + bar.clear() + bar.draw_coloured_text(0, 10, 0, 2, text) + diff --git a/src/plugins/random.py b/src/plugins/random.py new file mode 100644 index 0000000..c942f35 --- /dev/null +++ b/src/plugins/random.py @@ -0,0 +1,59 @@ +# -*- python -*- +''' +xpybar – xmobar replacement written in python +Copyright © 2014, 2015 Mattias Andrée (maandree@member.fsf.org) + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see <http://www.gnu.org/licenses/>. +''' + + +class Random: + ''' + Entropy pool status + + @variable poolsize:int The size of the entropy pool, see `man 4 random` for more information + @variable read_wakeup_threshold:int The number of bits of entropy required for waking up processes + that sleep waiting for entropy from `/dev/random` + @variable write_wakeup_threshold:int The number of bits of entropy below which we wake up processes + that do a `select` or `poll` for write access to `/dev/random` + @variable urandom_min_reseed_secs:int The minimum number of seconds between urandom pool reseeding + @variable boot_id:int Random UUID generated when the system is booted + ''' + + + def __init__(self): + ''' + Constructor + ''' + def read(filename): + with open('/proc/sys/kernel/random/' + filename, 'rb') as file: + return file.read().decode('utf-8', 'replace').rstrip('\n') + + self.poolsize = int(read('poolsize')) + self.read_wakeup_threshold = int(read('read_wakeup_threshold')) + self.write_wakeup_threshold = int(read('write_wakeup_threshold')) + self.urandom_min_reseed_secs = int(read('urandom_min_reseed_secs')) + self.boot_id = read('boot_id') + + + def entropy_avail(self): + ''' + Get the number of bits available in the entropy pool. + Note that `poolsize` is in bytes on Linux<2.6. + + @return :int The number of bits available in the entropy pool + ''' + with open('/proc/sys/kernel/random/entropy_avail', 'rb') as file: + return int(file.read().decode('utf-8', 'replace').rstrip('\n')) + |