aboutsummaryrefslogtreecommitdiff
path: root/uptime.lua
blob: fdf45ea5a9a9b7f71da162345f216ba977a13659 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
---------------------------------------------------
-- Licensed under the GNU General Public License v2
--  * (c) 2010, Adrian C. <anrxc@sysphere.org>
--  * (c) 2009, Lucas de Vries <lucas@glacicle.com>
---------------------------------------------------

-- {{{ Grab environment
local setmetatable = setmetatable
local math = { floor = math.floor }
local string = { match = string.match }
local helpers = require("vicious.helpers")
-- }}}


-- Uptime: provides system uptime and load information
module("vicious.uptime")


-- {{{ Uptime widget type
local function worker(format)
    local proc = helpers.pathtotable("/proc")

    -- Get system uptime
    local up_t = math.floor(string.match(proc.uptime, "[%d]+"))
    local up_d = math.floor(up_t   / (3600 * 24))
    local up_h = math.floor((up_t  % (3600 * 24)) / 3600)
    local up_m = math.floor(((up_t % (3600 * 24)) % 3600) / 60)

    -- Get load averages
    local l1, l5, l15 =  -- Get load averages for past 1, 5 and 15 minutes
      string.match(proc.loadavg, "([%d]*%.[%d]*)%s([%d]*%.[%d]*)%s([%d]*%.[%d]*)")

    return {up_d, up_h, up_m, l1, l5, l15}
end
-- }}}

setmetatable(_M, { __call = function(_, ...) return worker(...) end })