summaryrefslogtreecommitdiff
path: root/config/awesome/backup_status_widget.lua
blob: 577b08243e555f468ae084cb884dd90cf746cd3f (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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
-- from https://awesomewm.org/recipes/countdown/
-- on 2018-04-10
-- slightly modified

local gears = require("gears")
local awful = require("awful")
local wibox = require("wibox")
local naughty = require("naughty")
local beautiful = require("beautiful")

local backup_state_file = '/var/cache/dsa/nagios/borgmatic.stamp'
local max_age_s = 3600*24


function backup_status_get_mtime()
    local f = io.popen("stat -c %Y " .. backup_state_file)
    local last_modified = f:read()
    if last_modified == nil then
      return 0
    else
      return last_modified
    end
end

function backup_status_age()
    local mtime = backup_status_get_mtime()
    local current_time = os.time(os.date("*t"))
    return current_time - mtime
end

function backup_status_update(widget)
    local age = backup_status_age()
    widget.checked = (age > max_age_s)
end

backup_status_widget = wibox.widget {
    checked      = false,
    check_color  = "#ff0000",
    border_color = "#499644",
    border_width = 2,
    shape        = gears.shape.circle,
    widget       = wibox.widget.checkbox
}

local backup_status_tooltip = awful.tooltip { }
backup_status_tooltip:add_to_object(backup_status_widget)

backup_status_widget:connect_signal('mouse::enter', function()
    -- local mtime = backup_status_get_mtime()
    -- backup_status_tooltip.text = os.date('state file last touched %c', mtime)
    if backup_status_widget.checked then
        backup_status_tooltip.text = 'Backup state file too old.'
    else
      backup_status_tooltip.text = 'Backup state file is recent.'
    end
end)


backup_status_timer = gears.timer({timeout = 900,
                                   call_now = true,
                                   autostart = true,
                                   callback = function()
                                     backup_status_update(backup_status_widget)
                                   end,
                                  })

backup_status_widget:buttons(awful.util.table.join(
    awful.button({}, 1, function() backup_status_update(backup_status_widget) end)
))