-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhddtemp-safe.lua
executable file
·48 lines (39 loc) · 1.39 KB
/
hddtemp-safe.lua
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
#!/usr/bin/env lua5.3
local argparse = require "argparse"
local socket = require "socket"
local luxio = require "luxio"
local DEFAULT_PORT = 7634
local function log_error(msg_template, ...)
io.stderr:write(string.format(msg_template .. "\n", table.unpack(arg)))
end
local function main ()
local parser = argparse("hddtemp-safe", "Get HDD temperature from hddtemp daemon")
parser:option("-p --port", string.format("hddtemp's listening port (default %d)", DEFAULT_PORT), DEFAULT_PORT)
parser:argument("disk", "Disk path, e.g. /dev/sda")
local args = parser:parse()
local port = args.port
local s, _ = luxio.stat(args.disk)
if s ~= 0 then
log_error("%s does not exist!", args.disk)
os.exit(-1)
end
local client = socket.tcp()
client:settimeout(1)
local sk = client:connect('127.0.0.1', port)
if sk == nil then
log_error("Failed to connect to hddtemp daemon. Did you configured it to run as daemon?")
os.exit(-2)
end
local content, err = client:receive('*a')
client:close()
if content == nil then
-- No data from hddtemp
print("")
return
end
-- Sample response from hddtemp: |/dev/sda|ST1000LM035-1RK172|37|C||/dev/sdb|KINGSTON SA400M8120G|36|C|
local pattern = "|" .. args.disk .. "|[- %w]+|(%d*)|([CF*])|"
local temp, unit = content:match(pattern)
print(temp)
end
main()