-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
100 lines (86 loc) · 2.82 KB
/
main.lua
File metadata and controls
100 lines (86 loc) · 2.82 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
local socket = require("socket")
local lfs = require("lfs")
-- Create a directory if it doesn't exist
local function create_directory_if_not_exists(dir)
local attr = lfs.attributes(dir)
if not attr then
-- Directory does not exist, create it
local success, err = lfs.mkdir(dir)
if success then
print("Directory created:", dir)
else
print("Failed to create directory:", err)
end
else
print("Directory already exists:", dir)
end
end
-- CPU utilization in %
function get_cpu()
local handle = io.popen("top -bn1 | grep 'Cpu(s)' | awk '{print $2 + $4}'")
local cpu_util = handle:read("*a")
handle:close()
cpu_util = cpu_util:match("^%s*(.-)%s*$") -- Trim spaces
return cpu_util or "N/A"
end
-- RAM used, in MB
function get_ram()
local handle = io.popen("free -m | grep Mem | awk '{print $3}'")
local used_ram = handle:read("*a")
handle:close()
used_ram = used_ram:match("^%s*(.-)%s*$") -- Trim spaces
return used_ram or "N/A"
end
-- calculate tracked metrics
function get_metric_values()
local metrics = {}
metrics["cpu_utilization"] = get_cpu()
metrics["ram_utilization"] = get_ram()
return metrics
end
function write_to_logfile(logfile_dest, line)
local logfile = io.open(logfile_dest..".log", "a")
logfile:write(line.."\n")
logfile:close()
end
function write_header_to_csvlogfile(logfile_dest)
local logfile = io.open(logfile_dest..".csv", "a")
local line_header = "timestamp;cpu_utilization;ram_utilization"
logfile:write(line_header.."\n")
logfile:close()
end
function write_to_csvlogfile(logfile_dest, line)
local logfile = io.open(logfile_dest..".csv", "a")
logfile:write(line.."\n")
logfile:close()
end
function log(logfile_dest, metrics)
local metrics_string = string.format("CPU Utilization: %s%%, RAM Utilization: %s MB", metrics.cpu_utilization, metrics.ram_utilization)
local timestamp = os.date().." | "
local logline = timestamp..metrics_string
print(logline)
write_to_logfile(logfile_dest, logline)
local logline_csv = os.time()..";"..metrics.cpu_utilization..";"..metrics.ram_utilization
write_to_csvlogfile(logfile_dest, logline_csv)
end
function exec_cycle_of_logging(logfile_dest)
local metrics = get_metric_values()
log(logfile_dest, metrics)
end
function get_logfile_dest()
local logfile_name = "logfile_"..os.date("%Y-%m-%d__%H-%M-%S")
local logfile_dest = "./logs/"..logfile_name
print("Log will be saved to ./logs/ as "..logfile_name)
return logfile_dest
end
function main()
create_directory_if_not_exists("logs")
local logfile_dest = get_logfile_dest()
write_header_to_csvlogfile(logfile_dest)
while (true)
do
exec_cycle_of_logging(logfile_dest)
socket.sleep(1)
end
end
main()