Class: RubyStatsPsm

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/rubystats_psm.rb,
lib/rubystats_psm/linux.rb,
lib/rubystats_psm/version.rb,
lib/rubystats_psm/skcript_linux.rb

Constant Summary collapse

VERSION =
"1.0.2"
OS =
RUBY_PLATFORM

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *args) ⇒ Object (private)



22
23
24
# File 'lib/rubystats_psm.rb', line 22

private def method_missing(symbol, *args)
  instance.send(symbol, *args)
end

Class Method Details

.bandrxObject

Bandwidth Received Method



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

def self.bandrx
  if File.exist?("/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



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

def self.bandtx
  if File.exist?("/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



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

def self.diskio
  if File.exist?("/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



173
174
175
176
177
178
179
180
181
# File 'lib/rubystats_psm/skcript_linux.rb', line 173

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

.uw_bandtxObject

Current Bandwidth Transmitted in Mbit/s



226
227
228
229
230
231
232
233
234
# File 'lib/rubystats_psm/skcript_linux.rb', line 226

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

.uw_cputopObject



36
37
38
39
40
41
42
43
44
# File 'lib/rubystats_psm/skcript_linux.rb', line 36

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

.uw_diskioreadsObject

Current Disk Reads Completed



277
278
279
280
281
282
283
# File 'lib/rubystats_psm/skcript_linux.rb', line 277

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



286
287
288
289
290
291
292
# File 'lib/rubystats_psm/skcript_linux.rb', line 286

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

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

.uw_loadObject

Show the average system load of the past minute



120
121
122
123
124
125
126
127
128
# File 'lib/rubystats_psm/skcript_linux.rb', line 120

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

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

.uw_memtopObject

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



109
110
111
112
113
114
115
116
117
# File 'lib/rubystats_psm/skcript_linux.rb', line 109

def self.uw_memtop
  ps = execute_command("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

.uw_memusedObject

Show the percentage of Active Memory used



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/rubystats_psm/skcript_linux.rb', line 93

def self.uw_memused
  if File.exist?("/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
end

.uw_tcpusedObject

Show the number of TCP connections used



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rubystats_psm/skcript_linux.rb', line 47

def self.uw_tcpused
  if File.exist?("/proc/net/sockstat")
    File.open("/proc/net/sockstat", "r") do |ipv4|
      @sockstat = ipv4.read
    end

    @tcp4data = @sockstat.split
    @tcp4count = @tcp4data[5]
  end

  if  File.exist?("/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



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

def self.uw_udpused
  if File.exist?("/proc/net/sockstat")
    File.open("/proc/net/sockstat", "r") do |ipv4|
      @sockstat = ipv4.read
    end

    @udp4data = @sockstat.split
    @udp4count = @udp4data[16]
  end

  if File.exist?("/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

Instance Method Details

#execute_command(cmd) ⇒ Object



29
30
31
# File 'lib/rubystats_psm.rb', line 29

def execute_command(cmd)
  `#{cmd}`
end

#uw_cpuusedObject



32
33
34
# File 'lib/rubystats_psm/skcript_linux.rb', line 32

def uw_cpuused
  execute_command("grep 'cpu ' /proc/stat | awk '{usage=($2+$4)*100/($2+$4+$5)} END {print usage }'").to_f
end

#uw_diskavailableObject



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rubystats_psm/skcript_linux.rb', line 16

def uw_diskavailable
  df = execute_command("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 /localhost/.match?(line[0])  # ignore backup filesystem
    sum += ((line[3].to_f) / 1024) / 1024
  end
  sum.round(2)
end

#uw_diskusedObject

Show the amount of total disk used in Gigabytes



5
6
7
8
9
10
11
12
13
14
# File 'lib/rubystats_psm/skcript_linux.rb', line 5

def uw_diskused
  df = execute_command("df")
  parts = df.split(" ").map { |s| s.to_i }
  sum = 0
  (9..parts.size - 1).step(6).each { |i|
    sum += parts[i]
  }
  round = sum.round(2)
  ((round / 1024) / 1024).round(2)
end

#uw_diskused_percObject



28
29
30
# File 'lib/rubystats_psm/skcript_linux.rb', line 28

def uw_diskused_perc
  execute_command("df --output=pcent / | tr -dc '0-9'").to_i
end

#uw_httpconnsObject



4
5
6
# File 'lib/rubystats_psm/linux.rb', line 4

def uw_httpconns
  execute_command("netstat -an | grep :80 |wc -l").to_i
end