-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathlinux.rb
More file actions
320 lines (251 loc) · 7.43 KB
/
Copy pathlinux.rb
File metadata and controls
320 lines (251 loc) · 7.43 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# License: (MIT), Copyright (C) 2013 usagewatch Author Phil Chen, contributor Ruben Espinosa
module Usagewatch
# Show the amount of total disk used in Gigabytes
def self.uw_diskused
@df = `df`
@parts = @df.split(" ").map { |s| s.to_i }
@sum = 0
for i in (9..@parts.size - 1).step(6) do
@sum += @parts[i]
end
@round = @sum.round(2)
@totaldiskused = ((@round/1024)/1024).round(2)
end
# Show the percentage of disk used.
def self.uw_diskused_perc
df = `df --total`
df.split(" ").reverse.each do |l|
return l.to_f.round(2) if l.include?("%")
end
end
# Show the percentage of CPU used
def self.uw_cpuused
@proc0 = File.readlines('/proc/stat').grep(/^cpu /).first.split(" ")
sleep 1
@proc1 = File.readlines('/proc/stat').grep(/^cpu /).first.split(" ")
@proc0usagesum = @proc0[1].to_i + @proc0[2].to_i + @proc0[3].to_i
@proc1usagesum = @proc1[1].to_i + @proc1[2].to_i + @proc1[3].to_i
@procusage = @proc1usagesum - @proc0usagesum
@proc0total = 0
for i in (1..4) do
@proc0total += @proc0[i].to_i
end
@proc1total = 0
for i in (1..4) do
@proc1total += @proc1[i].to_i
end
@proctotal = (@proc1total - @proc0total)
@cpuusage = (@procusage.to_f / @proctotal.to_f)
@cpuusagepercentage = (100 * @cpuusage).to_f.round(2)
end
# return hash of top ten proccesses by cpu consumption
# example [["apache2", 12.0], ["passenger", 13.2]]
def self.uw_cputop
ps = `ps aux | awk '{print $11, $3}' | sort -k2nr | head -n 10`
array = []
ps.each_line do |line|
line = line.chomp.split(" ")
array << [line.first.gsub(/[\[\]]/, ""), line.last]
end
array
end
# Show the number of TCP connections used
def self.uw_tcpused
if File.exists?("/proc/net/sockstat")
File.open("/proc/net/sockstat", "r") do |ipv4|
@sockstat = ipv4.read
end
@tcp4data = @sockstat.split
@tcp4count = @tcp4data[5]
end
if File.exists?("/proc/net/sockstat6")
File.open("/proc/net/sockstat6", "r") do |ipv6|
@sockstat6 = ipv6.read
end
@tcp6data = @sockstat6.split
@tcp6count = @tcp6data[2]
end
@totaltcpused = @tcp4count.to_i + @tcp6count.to_i
end
# Show the number of UDP connections used
def self.uw_udpused
if File.exists?("/proc/net/sockstat")
File.open("/proc/net/sockstat", "r") do |ipv4|
@sockstat = ipv4.read
end
@udp4data = @sockstat.split
@udp4count = @udp4data[16]
end
if File.exists?("/proc/net/sockstat6")
File.open("/proc/net/sockstat6", "r") do |ipv6|
@sockstat6 = ipv6.read
end
@udp6data = @sockstat6.split
@udp6count = @udp6data[5]
end
@totaludpused = @udp4count.to_i + @udp6count.to_i
end
# Show the percentage of Active Memory used
def self.uw_memused
if File.exists?("/proc/meminfo")
File.open("/proc/meminfo", "r") do |file|
@result = file.read
end
end
memhash = Hash.new
@result.each_line do |l|
key, val = l.split(':')
if val.include?('kB') then val = val.gsub(/\s+kB/, ''); end
memhash["#{key}"] = val.strip
end
@memactivecalc = (memhash["Active"].to_f * 100) / memhash["MemTotal"].to_f
@memusagepercentage = @memactivecalc.round
end
# return hash of top ten proccesses by mem consumption
# example [["apache2", 12.0], ["passenger", 13.2]]
def self.uw_memtop
ps = `ps aux | awk '{print $11, $4}' | sort -k2nr | head -n 10`
array = []
ps.each_line do |line|
line = line.chomp.split(" ")
array << [line.first.gsub(/[\[\]]/, ""), line.last]
end
array
end
# Show the average system load of the past minute
def self.uw_load
if File.exists?("/proc/loadavg")
File.open("/proc/loadavg", "r") do |file|
@loaddata = file.read
end
@load = @loaddata.split(/ /).first.to_f
end
end
# Bandwidth Received Method
def self.bandrx
if File.exists?("/proc/net/dev")
File.open("/proc/net/dev", "r") do |file|
@result = file.read
end
end
@arrRows = @result.split("\n")
@arrEthLoRows = @arrRows.grep(/eth|lo/)
rowcount = (@arrEthLoRows.count - 1)
for i in (0..rowcount)
@arrEthLoRows[i] = @arrEthLoRows[i].gsub(/\s+/m, ' ').strip.split(" ")
end
@arrColumns = Array.new
for l in (0..rowcount)
@temp = Array.new
@temp[0] = @arrEthLoRows[l][1]
@temp[1] = @arrEthLoRows[l][9]
@arrColumns << @temp
end
columncount = (@arrColumns[0].count - 1)
@arrTotal = Array.new
for p in (0..columncount)
@arrTotal[p] = 0
end
for j in (0..columncount)
for k in (0..rowcount)
@arrTotal[j] = @arrColumns[k][j].to_i + @arrTotal[j]
end
end
@bandrxtx = @arrTotal
end
# Current Bandwidth Received Calculation in Mbit/s
def self.uw_bandrx
@new0 = self.bandrx
sleep 1
@new1 = self.bandrx
@bytesreceived = @new1[0].to_i - @new0[0].to_i
@bitsreceived = (@bytesreceived * 8)
@megabitsreceived = (@bitsreceived.to_f / 1024 / 1024).round(3)
end
# Bandwidth Transmitted Method
def self.bandtx
if File.exists?("/proc/net/dev")
File.open("/proc/net/dev", "r") do |file|
@result = file.read
end
end
@arrRows = @result.split("\n")
@arrEthLoRows = @arrRows.grep(/eth|lo/)
rowcount = (@arrEthLoRows.count - 1)
for i in (0..rowcount)
@arrEthLoRows[i] = @arrEthLoRows[i].gsub(/\s+/m, ' ').strip.split(" ")
end
@arrColumns = Array.new
for l in (0..rowcount)
@temp = Array.new
@temp[0] = @arrEthLoRows[l][1]
@temp[1] = @arrEthLoRows[l][9]
@arrColumns << @temp
end
columncount = (@arrColumns[0].count - 1)
@arrTotal = Array.new
for p in (0..columncount)
@arrTotal[p] = 0
end
for j in (0..columncount)
for k in (0..rowcount)
@arrTotal[j] = @arrColumns[k][j].to_i + @arrTotal[j]
end
end
@bandrxtx = @arrTotal
end
# Current Bandwidth Transmitted in Mbit/s
def self.uw_bandtx
@new0 = self.bandtx
sleep 1
@new1 = self.bandtx
@bytestransmitted = @new1[1].to_i - @new0[1].to_i
@bitstransmitted = (@bytestransmitted * 8)
@megabitstransmitted = (@bitstransmitted.to_f / 1024 / 1024).round(3)
end
# Disk Usage Method
def self.diskio
if File.exists?("/proc/diskstats")
File.open("/proc/diskstats", "r") do |file|
@result = file.read
end
end
@arrRows = @result.split("\n")
rowcount = (@arrRows.count - 1)
for i in (0..rowcount)
@arrRows[i] = @arrRows[i].gsub(/\s+/m, ' ').strip.split(" ")
end
@arrColumns = Array.new
for l in (0..rowcount)
@temp = Array.new
@temp[0] = @arrRows[l][3]
@temp[1] = @arrRows[l][7]
@arrColumns << @temp
end
columncount = (@arrColumns[0].count - 1)
@arrTotal = Array.new
for p in (0..columncount)
@arrTotal[p] = 0
end
for j in (0..columncount)
for k in (0..rowcount)
@arrTotal[j] = @arrColumns[k][j].to_i + @arrTotal[j]
end
end
@diskiorw= @arrTotal
end
# Current Disk Reads Completed
def self.uw_diskioreads
@new0 = self.diskio
sleep 1
@new1 = self.diskio
@diskreads = @new1[0].to_i - @new0[0].to_i
end
# Current Disk Writes Completed
def self.uw_diskiowrites
@new0 = self.diskio
sleep 1
@new1 = self.diskio
@diskwrites = @new1[1].to_i - @new0[1].to_i
end
end