module Dysnomia class << self @@ipv4_file = "/proc/net/sockstat" @@ipv6_file = "/proc/net/sockstat6" 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 def disk_usage_percent # return percent @command = `df --total` @command.split(" ").last.to_f.round(2) end 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 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 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 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 end end