-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstats_read.lua
More file actions
155 lines (130 loc) · 3.01 KB
/
Copy pathstats_read.lua
File metadata and controls
155 lines (130 loc) · 3.01 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
-- SPDX-FileCopyrightText: Robert Ryszard Paciorek <rrp@opcode.eu.org>
-- SPDX-License-Identifier: MIT
--------------------------------
--- Read stats functions ---
--------------------------------
-- Conky don't allow get raw value (i.e. net speed in bytes/s or cpu usage as float) so we use own implementations
local last_active = 0
local last_idle = 0
function get_cpu_stats()
local active = 0
local idle = 0
io.input("/proc/stat")
local stat1 = io.read("*line")
io.input():close()
local stat2 = string.gmatch(stat1, "%S+")
local n = 0
for v in stat2 do
if n == 4 or n == 5 then
idle = idle + tonumber(v)
elseif n > 0 then
active = active + tonumber(v)
end
n = n + 1
end
local a = active - last_active
local i = idle - last_idle
last_active = active
last_idle = idle
return (a)/(a+i) * 100
end
local last_down = {}
local last_up = {}
function get_net_stats(calls_per_sec, net_dev)
local down = 0
local up = 0
io.input("/proc/net/dev")
local stat1
while true do
stat1 = io.read("*l")
if stat1 == nil then return end
if string.match(stat1, "^ *" .. net_dev .. ":") then break end
end
io.input():close()
local stat2 = string.gmatch(stat1, "%S+")
local n = 0
for v in stat2 do
if n == 1 then
down = tonumber(v)
elseif n == 9 then
up = tonumber(v)
break
end
n = n + 1
end
local d = (down - last_down[net_dev]) * calls_per_sec
local u = (up - last_up[net_dev]) * calls_per_sec
last_down[net_dev] = down
last_up[net_dev] = up
return d, u
end
function init_net_stats(net_dev)
last_down[net_dev] = 0
last_up[net_dev] = 0
get_net_stats(0, net_dev)
end
local last_rd = {}
local last_wr = {}
function get_disk_stats(calls_per_sec, disk_dev)
local rd = 0
local wr = 0
io.input("/proc/diskstats")
local stat1
while true do
stat1 = io.read("*l")
if stat1 == nil then return end
local stat2 = string.gmatch(stat1, "%S+")
local n = 0
local has_data = false
for v in stat2 do
if n == 2 and v ~= disk_dev then
break
elseif n == 5 then
rd = tonumber(v)
elseif n == 9 then
wr = tonumber(v)
has_data = true
break
end
n = n + 1
end
if has_data then break end
end
io.input():close()
local r = (rd - last_rd[disk_dev]) * calls_per_sec * 512
local w = (wr - last_wr[disk_dev]) * calls_per_sec * 512
last_rd[disk_dev] = rd
last_wr[disk_dev] = wr
return r, w
end
function init_disk_stats(disk_dev)
last_rd[disk_dev] = 0
last_wr[disk_dev] = 0
get_disk_stats(0, disk_dev)
end
function get_mem_stats()
local f = 0
local a = 0
io.input("/proc/meminfo")
local stat1
local m = 3
while true do
stat1 = io.read("*l")
if stat1 == nil then return end
local stat2 = string.gmatch(stat1, "%S+")
local id = stat2()
if id == "MemTotal:" then
mem_total = tonumber(stat2())
m = m -1
elseif id == "MemFree:" then
f = tonumber(stat2())
m = m -1
elseif id == "MemAvailable:" then
a = tonumber(stat2())
m = m -1
end
if m == 0 then break end
end
io.input():close()
return mem_total-a, mem_total-f
end