Module: Usagewatch

Defined in:
lib/usagewatch.rb,
lib/usagewatch/version.rb

Constant Summary collapse

VERSION =
"0.0.3"

Instance Method Summary collapse

Instance Method Details

#bandrxObject



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
# File 'lib/usagewatch.rb', line 127

def 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

  return @bandrxtx
end

#bandtxObject



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
# File 'lib/usagewatch.rb', line 184

def 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

  return @bandrxtx
end

#diskioObject



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
# File 'lib/usagewatch.rb', line 241

def 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

  return @diskiorw
end

#uw_bandrxObject



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
# File 'lib/usagewatch.rb', line 125

def uw_bandrx

  def 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

    return @bandrxtx
  end

  @new0 = bandrx
  sleep 1
  @new1 = bandrx

  @bytesreceived = @new1[0].to_i - @new0[0].to_i
  @bitsreceived = (@bytesreceived * 8)
  @megabitsreceived = (@bitsreceived.to_f / 1024 / 1024)

  return @megabitsreceived.round(3)
end

#uw_bandtxObject



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
# File 'lib/usagewatch.rb', line 182

def uw_bandtx

  def 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

    return @bandrxtx
  end

  @new0 = bandtx
  sleep 1
  @new1 = bandtx

  @bytestransmitted = @new1[1].to_i - @new0[1].to_i
  @bitstransmitted = (@bytestransmitted * 8)
  @megabitstransmitted = (@bitstransmitted.to_f / 1024 / 1024)

  return @megabitstransmitted.round(3)
end

#uw_cpuusedObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/usagewatch.rb', line 23

def 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)

  return @cpuusagepercentage
end

#uw_diskioreadsObject



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
# File 'lib/usagewatch.rb', line 239

def uw_diskioreads

  def 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

    return @diskiorw
  end

  @new0 = diskio
  sleep 1
  @new1 = diskio

  @diskreads = @new1[0].to_i - @new0[0].to_i

  return @diskreads
end

#uw_diskiowritesObject



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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'lib/usagewatch.rb', line 292

def uw_diskiowrites

  def 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

    return @diskiorw
  end

  @new0 = diskio
  sleep 1
  @new1 = diskio

  @diskwrites = @new1[1].to_i - @new0[1].to_i

  return @diskwrites
end

#uw_diskusedObject



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/usagewatch.rb', line 4

def 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)

  return @totaldiskused
end

#uw_diskused_percObject

Show the percentage of disk used.



18
19
20
21
# File 'lib/usagewatch.rb', line 18

def uw_diskused_perc
  df = `df --total`
  df.split(" ").last.to_f.round(2)
end

#uw_loadObject



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/usagewatch.rb', line 113

def uw_load
  if File.exists?("/proc/loadavg")
    File.open("/proc/loadavg", "r") do |file|
      @loaddata = file.read
    end

    @load = @loaddata.split(/ /).first
  end

  return @load
end

#uw_memusedObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/usagewatch.rb', line 97

def uw_memused
  if File.exists?("/proc/meminfo")
    File.open("/proc/meminfo", "r") do |file|
      @result = file.read
    end
  end

  @memstat = @result.split("\n").collect{|x| x.strip}
  @memtotal = @memstat[0].gsub(/[^0-9]/, "")
  @memactive = @memstat[5].gsub(/[^0-9]/, "")
  @memactivecalc = (@memactive.to_f * 100) / @memtotal.to_f
  @memusagepercentage = @memactivecalc.round

  return @memusagepercentage
end

#uw_tcpusedObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/usagewatch.rb', line 48

def 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

  return @totaltcpused
end

#uw_udpusedObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/usagewatch.rb', line 73

def 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

  return @totaludpused
end