module Dysnomia

Constants

VERSION

Public Class Methods

average_load() click to toggle source
# File lib/Dysnomia/linux.rb, line 136
def average_load
  # get average load of the past minute

  $load_data = nil

  if File.exist?(@@load_avarange_file)
    File.open(@@load_avarange_file,"r") do |load|
      $load_data = load.read
    end
  end

  $load_data = $load_data.split(/ /).first.to_s

end
cpu_used() click to toggle source
# File lib/Dysnomia/linux.rb, line 41
def cpu_used

  # percentage cpu used

  # read proc stats and grep cpu
  $p1 = File.readlines('/proc/stat').grep('/^cpu /').first.split(' ')

  # sleep one moment
  sleep 1

  # read proc stats and grep cpu again
  $p2 = File.readlines('/proc/stat').grep('/^cpu /').first.split(' ')


  # Sum proc 1 usage
  $proc1usage = $p1[1].to_i + $p1[2].to_i + $p1[3].to_i

  # Sum proc 2 usage
  $proc2usage = $p2[1].to_i + $p2[2].to_i + $p2[3].to_i

  # Proc usage
  $procusageend = $proc2usage - $proc1usage


  $p1total = 0
  $p2total = 0

  # proc1 total
  for i in (1..4) do
    $p1total += $p1[i].to_i
  end

  # proc2 total
  for i in (1..4) do
    $p2total += $p2[i].to_i
  end

  # difference procs
  $total_usage = $p2total - $p1total

  # total cpu usage
  $total_cpu_usage = $procusageend.to_f / $total_usage

  # total cpu usage percentage
  $total_cpu_usage = (100 * $total_cpu_usage).to_f.round(2)

end
disk_usage() click to toggle source
# File lib/Dysnomia/linux.rb, line 11
def disk_usage

  # return gib

  # execute command
  $command = `df`

  # split command all spaces
  $explode = $command.split(" ").map { |s| s.to_i }

  $total = 0
  for i in (9..$explode.size - 1).step(6) do
    $total += $explode[i]
  end

  $total = $total.round(2)

  (($total/1024) / 1024).round(2)
end
disk_usage_percent() click to toggle source
# File lib/Dysnomia/linux.rb, line 31
def disk_usage_percent

  # return percent

  @command = `df --total`

  @command.split(" ").last.to_f.round(2)

end
memory_used() click to toggle source
# File lib/Dysnomia/linux.rb, line 109
def memory_used

  # get memory used percent
  $result = nil
  if File.exist?(@@memory_file)
    File.open(@@memory_file, "r") do |mem|
      $result = mem.read
    end
  end

  if ! nil?
    $m = $result.split("\n").collect{|x| x.strip}
    $total = $m[0].gsub(/[^0-9]/, "")
    $active = $m[5].gsub(/[^0-9]/, "")

    # Calc active memory
    $calc = ($active.to_f * 100) / $total.to_f

    # Round calc
    $calc.rond

  else
    $result = "Memory file not found !"
  end
end
tcp() click to toggle source
# File lib/Dysnomia/linux.rb, line 171
def tcp

  $sockstat4 = 0
  $sockstat6 = 0

  # Get ipv4 stat

  if File.exist?(@@ipv4_file)
    File.open(@@ipv4_file, "r") do |i4|
      $sockstat4 = i4.read
    end
    $t4_data = $sockstat4.split
    $t4_count = $t4_data[5]
  end

  if File.exist?(@@ipv6_file)
    File.open(@@ipv6_file, "r") do |i6|
      $sockstat6 = i6.read
    end

    $t6_data = $sockstat6.split
    $t6_count = $t6_data[2]

  end

  $total = $t4_count.to_i - $t6_count.to_i

end
top_cpu_processes() click to toggle source
# File lib/Dysnomia/linux.rb, line 90
def top_cpu_processes
  # top 10 cpu processes

  $command = `ps aux | awk '{print $11, $3}' | sort -k2nr  | head -n 10`
  $return_array = []

  # each to line

  $command.each_line do |pro|
    # split line is space
    $l = pro.chomp.split(' ')
    $return_array << $l.first + "," + $l.last
  end

  $return_array

end
top_memory_processes() click to toggle source
# File lib/Dysnomia/linux.rb, line 152
def top_memory_processes
  # top 10 mem processes

  $command = `ps aux | awk '{print $11, $4}' | sort -k2nr  | head -n 10`
  $return_array = []

  # each to line

  $command.each_line do |pro|
    # split line is space
    $l = pro.chomp.split(' ')
    $return_array << $l.first + "," + $l.last
  end

  $return_array


end