From c2f7fbcf2358cf83b103954ec0dc579aea39e3cb Mon Sep 17 00:00:00 2001 From: "Adrian C. (anrxc)" Date: Tue, 29 Mar 2011 02:21:53 +0200 Subject: dio: provide stats for all devices, rewritten by Joerg Old dio.lua was moved to contrib. New one is used like CPU widget is, request the device or parition in the format argument, {sda read_mb} as an example. New widget doesn't provide scheduler information, but I don't know anyone who used that. If you think this is wrong let me know. --- contrib/README | 8 +++++++ contrib/dio.lua | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ contrib/init.lua | 1 + 3 files changed, 81 insertions(+) create mode 100644 contrib/dio.lua (limited to 'contrib') diff --git a/contrib/README b/contrib/README index 20b000e..16716ac 100644 --- a/contrib/README +++ b/contrib/README @@ -27,6 +27,14 @@ vicious.contrib.batpmu vicious.contrib.batproc - +vicious.contrib.dio + - provides I/O statistics for requested storage devices + - takes the disk as an argument, i.e. "sda" (or a specific + partition, i.e. "sda/sda2") + - returns a table with string keys: {total_s}, {total_kb}, {total_mb}, + {read_s}, {read_kb}, {read_mb}, {write_s}, {write_kb}, {write_mb} + and {sched} + vicious.contrib.mpc - diff --git a/contrib/dio.lua b/contrib/dio.lua new file mode 100644 index 0000000..40c4cad --- /dev/null +++ b/contrib/dio.lua @@ -0,0 +1,72 @@ +--------------------------------------------------- +-- Licensed under the GNU General Public License v2 +-- * (c) 2010, Adrian C. +--------------------------------------------------- + +-- {{{ Grab environment +local ipairs = ipairs +local setmetatable = setmetatable +local table = { insert = table.insert } +local string = { gmatch = string.gmatch } +local helpers = require("vicious.helpers") +-- }}} + + +-- Disk I/O: provides I/O statistics for requested storage devices +module("vicious.contrib.dio") + + +-- Initialize function tables +local disk_usage = {} +local disk_total = {} +-- Variable definitions +local unit = { ["s"] = 1, ["kb"] = 2, ["mb"] = 2048 } + +-- {{{ Disk I/O widget type +local function worker(format, disk) + if not disk then return end + + local disk_lines = { [disk] = {} } + local disk_stats = helpers.pathtotable("/sys/block/" .. disk) + + if disk_stats.stat then + local match = string.gmatch(disk_stats.stat, "[%s]+([%d]+)") + for i = 1, 11 do -- Store disk stats + table.insert(disk_lines[disk], match()) + end + end + + -- Ensure tables are initialized correctly + local diff_total = { [disk] = {} } + if not disk_total[disk] then + disk_usage[disk] = {} + disk_total[disk] = {} + + while #disk_total[disk] < #disk_lines[disk] do + table.insert(disk_total[disk], 0) + end + end + + for i, v in ipairs(disk_lines[disk]) do + -- Diskstats are absolute, substract our last reading + diff_total[disk][i] = v - disk_total[disk][i] + + -- Store totals + disk_total[disk][i] = v + end + + -- Calculate and store I/O + helpers.uformat(disk_usage[disk], "read", diff_total[disk][3], unit) + helpers.uformat(disk_usage[disk], "write", diff_total[disk][7], unit) + helpers.uformat(disk_usage[disk], "total", diff_total[disk][7] + diff_total[disk][3], unit) + + -- Store I/O scheduler + if disk_stats.queue and disk_stats.queue.scheduler then + disk_usage[disk]["{sched}"] = string.gmatch(disk_stats.queue.scheduler, "%[([%a]+)%]") + end + + return disk_usage[disk] +end +-- }}} + +setmetatable(_M, { __call = function(_, ...) return worker(...) end }) diff --git a/contrib/init.lua b/contrib/init.lua index e0f681a..a8c5ee6 100644 --- a/contrib/init.lua +++ b/contrib/init.lua @@ -9,6 +9,7 @@ require("vicious.contrib.batacpi") require("vicious.contrib.batpmu") require("vicious.contrib.batproc") +require("vicious.contrib.dio") require("vicious.contrib.mpc") require("vicious.contrib.netcfg") require("vicious.contrib.net") -- cgit v1.2.3