class RubyStatsPsm

Constants

OS
VERSION

Public Class Methods

bandrx() click to toggle source

Bandwidth Received Method

# File lib/rubystats_psm/skcript_linux.rb, line 144
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
bandtx() click to toggle source

Bandwidth Transmitted Method

# File lib/rubystats_psm/skcript_linux.rb, line 197
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
diskio() click to toggle source

Disk Usage Method

# File lib/rubystats_psm/skcript_linux.rb, line 250
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_bandrx() click to toggle source

Current Bandwidth Received Calculation in Mbit/s

# File lib/rubystats_psm/skcript_linux.rb, line 186
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_bandtx() click to toggle source

Current Bandwidth Transmitted in Mbit/s

# File lib/rubystats_psm/skcript_linux.rb, line 239
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_diskioreads() click to toggle source

Current Disk Reads Completed

# File lib/rubystats_psm/skcript_linux.rb, line 290
def self.uw_diskioreads
  @new0 = self.diskio
  sleep 1
  @new1 = self.diskio

  @diskreads = @new1[0].to_i - @new0[0].to_i
end
uw_diskiowrites() click to toggle source

Current Disk Writes Completed

# File lib/rubystats_psm/skcript_linux.rb, line 299
def self.uw_diskiowrites
  @new0 = self.diskio
  sleep 1
  @new1 = self.diskio

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

Private Class Methods

method_missing(symbol, *args) click to toggle source
# File lib/rubystats_psm.rb, line 34
        def method_missing(symbol, *args)
  instance.send(symbol, *args)
end

Public Instance Methods

execute_command(cmd) click to toggle source
# File lib/rubystats_psm.rb, line 29
def execute_command(cmd)
  `#{cmd}`
end
uw_cputop() click to toggle source
# File lib/rubystats_psm/skcript_linux.rb, line 36
def 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_cpuused() click to toggle source
# 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_diskavailable() click to toggle source
# 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_diskused() click to toggle source

Show the amount of total disk used in Gigabytes

# 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_perc() click to toggle source
# 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_httpconns() click to toggle source
# File lib/rubystats_psm/linux.rb, line 4
def uw_httpconns
  execute_command("netstat -an | grep :80 |wc -l").to_i
end
uw_load() click to toggle source

Show the average system load of the past minute

# File lib/rubystats_psm/skcript_linux.rb, line 131
def uw_load
  if File.exist?("/proc/loadavg")
    load_data = nil

    File.open("/proc/loadavg", "r") do |file|
      load_data = file.read
    end

    load_data.split(/ /).first.to_f
  end
end
uw_memtop() click to toggle source

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

# File lib/rubystats_psm/skcript_linux.rb, line 120
def 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_memused() click to toggle source

Show the percentage of Active Memory used

# File lib/rubystats_psm/skcript_linux.rb, line 103
def uw_memused
  result = nil
  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
  memactivecalc.round
end
uw_tcpused() click to toggle source

Show the number of TCP connections used

# File lib/rubystats_psm/skcript_linux.rb, line 47
def uw_tcpused
  tcp4count = nil
  tcp6count = nil

  if File.exist?("/proc/net/sockstat")
    sockstat = nil
    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")
    sockstat6 = nil
    File.open("/proc/net/sockstat6", "r") do |ipv6|
      sockstat6 = ipv6.read
    end

    tcp6data = sockstat6.split
    tcp6count = tcp6data[2]
  end

  tcp4count.to_i + tcp6count.to_i
end
uw_udpused() click to toggle source

Show the number of UDP connections used

# File lib/rubystats_psm/skcript_linux.rb, line 75
def uw_udpused
  udp4count = nil
  udp6count = nil

  if File.exist?("/proc/net/sockstat")
    sockstat = nil
    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")
    sockstat6 = nil
    File.open("/proc/net/sockstat6", "r") do |ipv6|
      sockstat6 = ipv6.read
    end

    udp6data = sockstat6.split
    udp6count = udp6data[5]
  end

  udp4count.to_i + udp6count.to_i
end

Private Instance Methods

method_missing(symbol, *args) click to toggle source
Calls superclass method
# File lib/rubystats_psm.rb, line 22
        def method_missing(symbol, *args)
  if Usagewatch.respond_to?(symbol)
    Usagewatch.send(symbol, *args)
  end
  super
end