File: statusd_drives.lua

package info (click to toggle)
notion 4.0.2%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 4,676 kB
  • sloc: ansic: 47,508; sh: 2,096; makefile: 603; perl: 270
file content (61 lines) | stat: -rw-r--r-- 1,455 bytes parent folder | download | duplicates (4)
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
-- Authors: Voker57 <voker57@gmail.com>
-- License: Public domain
-- Last Changed: Unknown
--
-- Disk drives monitor

-- %drives - list of currently connected drives

-- by Voker57 <voker57@gmail.com>
-- Public domain

local defaults={
    update_interval=500
}

local settings=table.join(statusd.get_config("drives"), defaults)

local drives_timer

local function get_drives()
	local lsres = io.popen("ls /dev/sd* | sort")
	return lsres:lines()
end

local function update_drives()
	local drives_list = get_drives()
	local drive_table = {}
	for drive in drives_list do
 		if drive:match("/([^/]+)$") then
 			local drive_name = drive:match("/([^/]+)$")
 			local drive_base = drive_name:sub(1,3)
 			local drive_partition = drive_name:sub(4,-1)
			if drive_table[drive_base] then
				table.insert(drive_table[drive_base], drive_partition)
			else
				drive_table[drive_base] = {drive_partition}
			end
 		end
	end
	local result_table = {}
	for drive, partitions in pairs(drive_table) do
		local result = drive
		local parts = ""
 		for k,i in pairs(partitions) do
 			if i ~= "" then
 				parts = parts..i.." "
 			end
 		end
 		if parts ~= "" then
 			result = result.."["..parts:sub(1,-2).."]"
 		end
 		table.insert(result_table, result)
	end
	table.sort(result_table)
	statusd.inform("drives", table.concat(result_table, " "))
	drives_timer:set(settings.update_interval, update_drives)
end

-- Init
drives_timer=statusd.create_timer()
update_drives()