lib/usagewatch_ext/mac.rb in usagewatch_ext-0.0.3 vs lib/usagewatch_ext/mac.rb in usagewatch_ext-0.0.4.pre1

- old
+ new

@@ -13,19 +13,16 @@ sum.round(2) end # Show the percentage of disk used. def self.uw_diskused_perc - df = `df -kl` - total = 0.0 - used = 0.0 + df, total, used = `df -kl`, 0.0, 0.0 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 - total += ((line[3].to_f)/1024)/1024 - used +=((line[2].to_f)/1024)/1024 + 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 # Show the percentage of cpu used @@ -36,17 +33,11 @@ end # return hash of top ten proccesses by cpu consumption # example [["apache2", 12.0], ["passenger", 13.2]] 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(/[\[\]]/, "").split("/").last, line.last] - end - array + top %w"$11 $3" end # todo #def uw_tcpused # @@ -58,17 +49,11 @@ #end # return hash of top ten proccesses by mem consumption # example [["apache2", 12.0], ["passenger", 13.2]] def self.uw_memtop - ps = `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(/[\[\]]/, "").split("/").last, line.last] - end - array + top %w"$11 $4" end # Percentage of mem used def self.uw_memused top = `top -l1 | awk '/PhysMem/'` @@ -104,6 +89,28 @@ #todo #def uw_diskiowrites # #end + + # Show the current http connections on 80 port + def self.uw_httpconns + `netstat -an | grep :80 |wc -l`.to_i + end + + private + + + def self.top(lines) + ps = `ps aux | awk '{print #{lines.join(", ")}}' | sort -k2nr | head -n 10` + array = [] + ps.each_line do |line| + line = line.chomp.split(" ") + array << [line.first.gsub(/[\[\]]/, "").split("/").last, line.last] + end + array + end + + def self.to_gb(bytes) + (bytes/1024)/1024 + end end \ No newline at end of file