aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMattias Andrée <maandree@operamail.com>2014-03-01 21:07:57 +0100
committerMattias Andrée <maandree@operamail.com>2014-03-01 21:07:57 +0100
commit8049a8733933ef586d6d49279a291ba367b38625 (patch)
tree06170b4746d808979b9335df07aa67763a82391f
parentadd mem (diff)
downloadxpybar-8049a8733933ef586d6d49279a291ba367b38625.tar.gz
xpybar-8049a8733933ef586d6d49279a291ba367b38625.tar.bz2
xpybar-8049a8733933ef586d6d49279a291ba367b38625.tar.xz
add discstats
Signed-off-by: Mattias Andrée <maandree@operamail.com>
-rw-r--r--TODO2
-rw-r--r--examples/plugin-test21
-rw-r--r--src/plugins/discstats.py83
3 files changed, 103 insertions, 3 deletions
diff --git a/TODO b/TODO
index 142f0f8..2b2bb10 100644
--- a/TODO
+++ b/TODO
@@ -22,7 +22,7 @@ List of plugins to implement:
Backlight control
/proc/stat
/sys/devices/system/cpu/cpuX
- /proc/diskstats
+ hdparm
Thermal monitoring
ESSID and link quality for wireless interfaces
News feed syndication
diff --git a/examples/plugin-test b/examples/plugin-test
index d29b5a7..3fce9bc 100644
--- a/examples/plugin-test
+++ b/examples/plugin-test
@@ -11,6 +11,7 @@ from plugins.pacman import Pacman
from plugins.uname import Uname
from plugins.df import Discs
from plugins.mem import Memory
+from plugins.discstats import DiscStats
OUTPUT, HEIGHT, YPOS, TOP = 0, 24, 24, True
@@ -128,7 +129,23 @@ def redraw():
if use > 99: colour = '41;33'
discs = 'df: %s : \033[%sm%.2f\033[0m%%' % (' '.join(discs), colour, use)
- text = '%s │ %s │ %s │ %s │ %s │ %s │ %s │ %s │ %s\n%s'
- text %= (time, uptime, idle, loadavg, users, uname, mem, swp, shm, discs)
+ discstats_ = DiscStats()
+ discstats = []
+ for disc in discstats_.devices.keys():
+ if disc.startswith('sd') or disc.startswith('hd'):
+ disc = discstats_.devices[disc]
+ discstats.append((disc.device, disc.io_current))
+ def colourise(value):
+ colour = '39'
+ if value >= 1: colour = '32'
+ if value >= 3: colour = '33'
+ if value >= 5: colour = '31'
+ return '\033[%sm%i\033[0m' % (colour, value)
+ discstats.sort(key = lambda x : x[0])
+ discstats = ['%s:%s' % (d[2:], colourise(i)) for d, i in discstats]
+ discstats = 'Disc stats: %s' % (' '.join(discstats))
+
+ text = '%s │ %s │ %s │ %s │ %s │ %s │ %s │ %s │ %s\n%s │ %s'
+ text %= (time, uptime, idle, loadavg, users, uname, mem, swp, shm, discs, discstats)
bar.draw_coloured_text(0, 10, 0, 2, text)
diff --git a/src/plugins/discstats.py b/src/plugins/discstats.py
new file mode 100644
index 0000000..57b2844
--- /dev/null
+++ b/src/plugins/discstats.py
@@ -0,0 +1,83 @@
+# -*- python -*-
+'''
+xpybar – xmobar replacement written in python
+Copyright © 2014 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 DiscStats:
+ '''
+ Retrieve disc statistics
+
+ @variable devices:dict<str, Disc> Map from device name to disc
+ @variable majors:dict<int, dict<int, Disc>> Map from major to minor and then to disc
+ '''
+
+
+ def __init__(self):
+ '''
+ Constructor
+ '''
+ discstats = None
+ with open('/proc/diskstats', 'rb') as file:
+ discstats = file.read()
+ discstats = discstats.decode('utf-8', 'replace')
+ discstats = filter(lambda x : not x == '', discstats.split('\n'))
+
+ self.devices, self.majors = {}, {}
+
+ for line in discstats:
+ line = list(filter(lambda x : not x == '', line.split(' ')))
+ line = [line[i] if i == 2 else int(line[i]) for i in range(len(line))]
+ disc = DiscStat(line)
+ self.devices[disc.device] = disc
+ if disc.major not in self.majors:
+ self.majors[disc.major] = {}
+ self.majors[disc.major][disc.minor] = disc
+
+
+class DiscStat:
+ '''
+ Statistics about a single disc or partition
+
+ @variable major:int Device major number
+ @variable minor:int Device minor mumber
+ @variable device:str Device name
+ @variable r_complete:int Reads completed successfully
+ @variable r_merge:int Reads merged
+ @variable r_sectors:int Sectors read
+ @variable r_time:int Time spent reading, in ms
+ @variable w_complete:int Writes completed
+ @variable w_merge:int Writes merged
+ @variable w_sectors:int Sectors written
+ @variable w_time:int Time spent writing, in ms
+ @variable io_current:int I/O:s currently in progress
+ @variable io_time:int Time spent doing I/Os:, in ms
+ @variable io_weighted_time:int Weighted time spent doing I/O:s, in ms
+ '''
+
+
+ def __init__(self, fields):
+ '''
+ Constructor
+
+ @param fields:list<str|int> Fields from /proc/diskstats converted to proper data type
+ '''
+ (self.major, self.minor, self.device) = fields[0 : 3]
+ (self.r_complete, self.r_merge, self.r_sectors, self.r_time) = fields[3 : 7]
+ (self.w_complete, self.w_merge, self.w_sectors, self.w_time) = fields[7 : 11]
+ (self.io_current, self.io_time, self.io_weighted_time) = fields[11 : 14]
+