Module: Usagewatch

Defined in:
lib/usagewatch_ext/mac.rb,
lib/usagewatch_ext/linux.rb,
lib/usagewatch_ext/skcript_linux.rb

Class Method Summary collapse

Class Method Details

.bandrxObject

Bandwidth Received Method



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
# File 'lib/usagewatch_ext/skcript_linux.rb', line 154

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

.bandtxObject

Bandwidth Transmitted Method



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
# File 'lib/usagewatch_ext/skcript_linux.rb', line 209

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

.diskioObject

Disk Usage Method



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
# File 'lib/usagewatch_ext/skcript_linux.rb', line 264

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

.uw_bandrxObject

Current Bandwidth Received Calculation in Mbit/s



197
198
199
200
201
202
# File 'lib/usagewatch_ext/skcript_linux.rb', line 197

def self.uw_bandrx
  read1 =`netstat -ib | grep -e "en1" -m 1 | awk '{print $7}'`
  sleep 1
  read2=`netstat -ib | grep -e "en1" -m 1 | awk '{print $7}'`
  (((read2.to_f - read1.to_f)/1024)/1024).round(3)
end

.uw_bandtxObject

Current Bandwidth Transmitted in Mbit/s



252
253
254
255
256
257
# File 'lib/usagewatch_ext/skcript_linux.rb', line 252

def self.uw_bandtx
  send1=`netstat -ib | grep -e "en1" -m 1 | awk '{print $10}'`
  sleep 1
  send2=`netstat -ib | grep -e "en1" -m 1 | awk '{print $10}'`
  (((send2.to_f - send1.to_f)/1024)/1024).round(3)
end

.uw_cputopObject

return hash of top ten proccesses by cpu consumption example [[“apache2”, 12.0], [“passenger”, 13.2]]



76
77
78
# File 'lib/usagewatch_ext/mac.rb', line 76

def self.uw_cputop
  top %w"$11 $3"
end

.uw_cpuusedObject

Show the percentage of CPU used



68
69
70
71
72
# File 'lib/usagewatch_ext/mac.rb', line 68

def self.uw_cpuused
  top = `top -l1 | awk '/CPU usage/'`
  top = top.gsub(/[\,a-zA-Z:]/, "").split(" ")
  top[0].to_f
end

.uw_diskavailableObject

Show disk space available in GB



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/usagewatch_ext/mac.rb', line 30

def self.uw_diskavailable
  df = `df -kl`
  sum = 0.00
  df.each_line.with_index do |line, line_index|
    next if line_index.eql? 0
    line = line.split(" ")
    next if line[0] =~ /localhost/  #ignore backup filesystem
    sum += ((line[3].to_f)/1024)/1024
  end
  totaldiskavailable = sum.round(2)
end

.uw_diskavailable_on(location) ⇒ Object

Show disk space available on location(partition) in GB



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/usagewatch_ext/mac.rb', line 43

def self.uw_diskavailable_on(location)
  df = `df`
  df.split("\n")[1..-1].each do |line|
    parts = line.split(" ")
    if parts.last == location
      diskavailableon = ((parts[3].to_i.round(2)/1024)/1024).round(2)
      break
    end
  end
  diskavailableon ? diskavailableon : "location invalid"
end

.uw_diskioreadsObject

Current Disk Reads Completed



305
306
307
308
309
310
311
312
# File 'lib/usagewatch_ext/skcript_linux.rb', line 305

def self.uw_diskioreads

  @new0 = self.diskio
  sleep 1
  @new1 = self.diskio

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

.uw_diskiowritesObject

Current Disk Writes Completed



315
316
317
318
319
320
321
322
# File 'lib/usagewatch_ext/skcript_linux.rb', line 315

def self.uw_diskiowrites

  @new0 = self.diskio
  sleep 1
  @new1 = self.diskio

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

.uw_diskusedObject

Show the amount of total disk used in Gigabytes



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

def self.uw_diskused
  df = `df -kl`
  sum = 0.00
  df.each_line.with_index do |line, line_index|
    next if line_index.eql? 0
    line = line.split(" ")
    next if line[0] =~ /localhost/  #ignore backup filesystem
    sum += ((line[2].to_f)/1024)/1024
  end
  sum.round(2)
end

.uw_diskused_on(location) ⇒ Object

Show disk space used on location(partition) in GB



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/usagewatch_ext/mac.rb', line 17

def self.uw_diskused_on(location)
  df = `df`
  df.split("\n")[1..-1].each do |line|
    parts = line.split(" ")
    if parts.last == location
      diskusedon = ((parts[2].to_i.round(2)/1024)/1024).round(2)
      break
    end
  end
  diskusedon ? diskusedon : "location invalid"
end

.uw_diskused_percObject

Show the percentage of disk used.



56
57
58
59
60
61
62
63
64
65
# File 'lib/usagewatch_ext/mac.rb', line 56

def self.uw_diskused_perc
  df, total, used  = `df -kl`, 0.0, 0.0
  df.each_line.with_index do |line, line_index|
    line = line.split(" ")
    next if line_index.eql? 0 or line[0] =~ /localhost/ #ignore backup filesystem
    total  += to_gb line[3].to_f
    used   += to_gb line[2].to_f
  end
  ((used/total) * 100).round(2)
end

.uw_httpconnsObject

Show the current http connections on 80 port



139
140
141
# File 'lib/usagewatch_ext/mac.rb', line 139

def self.uw_httpconns
  `netstat -an | grep :80 |wc -l`.to_i
end

.uw_loadObject

Show the average system load of the past minute



104
105
106
107
108
109
110
111
112
# File 'lib/usagewatch_ext/mac.rb', line 104

def self.uw_load
  iostat = `iostat -w1 -c 2 | awk '{print $7}'`
  cpu = 0.0
  iostat.each_line.with_index do |line, line_index|
    next if line_index.eql? 0 or  line_index.eql? 1 or  line_index.eql? 2
    cpu = line.split(" ").last.to_f.round(2)
  end
  cpu
end

.uw_memtopObject

return hash of top ten proccesses by mem consumption example [[“apache2”, 12.0], [“passenger”, 13.2]]



92
93
94
# File 'lib/usagewatch_ext/mac.rb', line 92

def self.uw_memtop
  top %w"$11 $4"
end

.uw_memusedObject

Show the percentage of Active Memory used



97
98
99
100
101
# File 'lib/usagewatch_ext/mac.rb', line 97

def self.uw_memused
  top = `top -l1 | awk '/PhysMem/'`
  top = top.gsub(/[\.\,a-zA-Z:]/, "").split(" ").reverse
  ((top[1].to_f / (top[0].to_f + top[1].to_f)) * 100).round(2)
end

.uw_tcpusedObject

Show the number of TCP connections used



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/usagewatch_ext/skcript_linux.rb', line 69

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

.uw_udpusedObject

Show the number of UDP connections used



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/usagewatch_ext/skcript_linux.rb', line 93

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