summaryrefslogtreecommitdiffstats
path: root/health-model.lua
blob: 638e0a0ef6b664f6db0ccefe51c24ceef8a9d2b1 (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
70
71
72
73
74
75
76
77
78
-- acf model for displaying logfiles recusivly 
module (..., package.seeall)

require("fs")
require("date")
require("format")

-- ###############################################################
-- Private functions
local function querycmd ( cmdline )
	local cmd = io.popen( cmdline )
	local cmd_result = cmd:read("*a") or "unknown"
	cmd:close()
	return cmd_result
end

local function diskfree ( media )
	if not (media) then media = "" end
	local cmd = io.popen( "df -h " .. media )
	local cmd_result = cmd:read("*a") or "unknown"
	cmd:close()
	return cmd_result
end

local function getpercentage ( data,grep,field1,field2 )
	local debugoutput = ""
	local grepline = format.string_to_table(string.match(data,grep .. ".*"),"%s+")
	local val1percentage = math.floor(((grepline[field1] / (grepline[field1]+grepline[field2]))*100)+0.5)
	return val1percentage
end

-- ###############################################################
-- Public functions
get_system = function (self)
	local system = {}
	system.uptime = querycmd("/usr/bin/uptime")
	system.date = querycmd("/bin/date")
	system.version = fs.read_file("/etc/alpine-release") or "Unknown"
	system.timezone = date.what_tz()
	system.uname = querycmd("/bin/uname -a")
	local used, useddebug = getpercentage(querycmd("/usr/bin/free"), "Total:", 3, 4)
	system.memory = {value=querycmd("/usr/bin/free"),
		used=used,
		used_label="Total:"}
	return system
end

get_storage = function (self)
	local storage = {}
	storage.floppycapacity = {value=diskfree("/media/floppy"),
		used=string.match(diskfree(),"/dev/fd0%s*%S*%s*%S*%s*%S*%s*(%S*)%%"),
		used_label="/dev/fd0"}
	storage.hdcapacity = {value=diskfree(),
		used=string.match(diskfree(),"/dev/none%s*%S*%s*%S*%s*%S*%s*(%S*)%%"),
		used_label="/dev/none"}
	storage.partitions = fs.read_file("/proc/partitions")
	return storage
end

get_network = function (self)
	local network = {}
	network.interfaces = querycmd("/bin/ip addr")
	network.routes = querycmd("/bin/ip route")
	return network
end

get_modules = function (self)
	local modules = {}
	modules.list = querycmd("/sbin/lsmod")
	return modules
end

get_proc = function (self)
	local proc = {}
	proc.processor = fs.read_file("/proc/cpuinfo")
	proc.memory = fs.read_file("/proc/meminfo")
	return proc
end