Skip to content

Commit

Permalink
Merge pull request #7 from rderoldan1/master
Browse files Browse the repository at this point in the history
Mac Info ! :D
  • Loading branch information
nethacker committed Jul 22, 2013
2 parents d867623 + 27911df commit 180214f
Show file tree
Hide file tree
Showing 8 changed files with 207 additions and 51 deletions.
62 changes: 45 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,29 @@ gem install usagewatch
```ruby
require 'usagewatch'

include Usagewatch

uw_diskused
uw_diskused_perc
uw_cpuused
uw_tcpused
uw_udpused
uw_memused
uw_load
uw_bandrx
uw_bandtx
uw_diskioreads
uw_diskiowrites
uw_cputop
uw_memtop
usw = Usagewatch

usw.uw_diskused
usw.uw_diskused_perc
usw.uw_cpuused
usw.uw_tcpused
usw.uw_udpused
usw.uw_memused
usw.uw_load
usw.uw_bandrx
usw.uw_bandtx
usw.uw_diskioreads
usw.uw_diskiowrites
usw.uw_cputop
usw.uw_memtop
```

## Example

```bash
Run:

example.rb
linux_example.rb

Example Output:

Expand All @@ -57,6 +57,34 @@ Top Ten Processes By Memory Consumption:
[["unicorn", "4.8"], ["unicorn", "4.7"], ["unicorn", "4.6"], ["unicorn", "4.6"], ["unicorn", "4.5"], ["unicorn", "4.5"], ["unicorn", "4.3"], ["unicorn", "4.3"], ["unicorn", "4.2"], ["/usr/lib64/erlang/erts-5.8.5/bin/beam.smp", "4.0"]]
```


## Methods availables

##### Linux
uw_diskused
uw_diskused_perc
uw_cpuused
uw_tcpused
uw_udpused
uw_memused
uw_load
uw_bandrx
uw_bandtx
uw_diskioreads
uw_diskiowrites
uw_cputop
uw_memtop

##### Mac
uw_diskused
uw_diskused_perc
uw_cputop
uw_memtop
uw_load
uw_cpuused
uw_memused


## Notes

Disk Used is a sum of all partitions calculated in Gigabytes
Expand Down Expand Up @@ -85,4 +113,4 @@ RUBY VERSIONS:
ruby 1.9.3p429 (2013-05-15) [x86_64-linux]

OS VERSIONS:
CENTOS 5x 6x, Ubuntu 12.04, Fedora 18
CENTOS 5x 6x, Ubuntu 12.04, Fedora 18, Mountain Lion 10.8.4
20 changes: 0 additions & 20 deletions examples/example.rb

This file was deleted.

21 changes: 21 additions & 0 deletions examples/linux_example.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/ruby

#License: (MIT), Copyright (C) 2013 Author Phil Chen, Contributor Ruben Espinosa

require 'usagewatch'

usw = Usagewatch

puts "#{usw.uw_diskused} Gigabytes Used"
puts "#{usw.uw_diskused_perc} Perventage of Gigabytes Used"
puts "#{usw.uw_cpuused}% CPU Used"
puts "#{usw.uw_tcpused} TCP Connections Used"
puts "#{usw.uw_udpused} UDP Connections Used"
puts "#{usw.uw_memused}% Active Memory Used"
puts "#{usw.uw_load} Average System Load Of The Past Minute"
puts "#{usw.uw_bandrx} Mbit/s Current Bandwidth Received"
puts "#{usw.uw_bandtx} Mbit/s Current Bandwidth Transmitted"
puts "#{usw.uw_diskioreads}/s Current Disk Reads Completed"
puts "#{usw.uw_diskiowrites}/s Current Disk Writes Completed"
puts "Top Ten Processes By CPU Consumption: #{usw.uw_cputop}"
puts "Top Ten Processes By Memory Consumption: #{usw.uw_memtop}"
15 changes: 15 additions & 0 deletions examples/mac_example.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/ruby

#License: (MIT), Copyright (C) 2013 Author Phil Chen, Contributor Ruben Espinosa

require 'usagewatch'

usw = Usagewatch

puts "#{usw.uw_diskused} Gigabytes Used"
puts "#{usw.uw_diskused_perc} Percentage of Gigabytes Used"
puts "#{usw.uw_memused}% Active Memory Used"
puts "#{usw.uw_cpuused}% CPU Used"
puts "#{usw.uw_load} Average System Load Of The Past Minute"
puts "Top Ten Processes By CPU Consumption: #{usw.uw_cputop}"
puts "Top Ten Processes By Memory Consumption: #{usw.uw_memtop}"
3 changes: 2 additions & 1 deletion lib/usagewatch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
text = "OS is not supported in this version."

if os.include? "darwin"
puts "Mac" + text
require "usagewatch/mac"
puts "Mac version is under development"
elsif os.include? "linux"
require "usagewatch/linux"
elsif os =~ /cygwin|mswin|mingw|bccwin|wince|emx/
Expand Down
26 changes: 13 additions & 13 deletions lib/usagewatch/linux.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Usagewatch
def uw_diskused
def self.uw_diskused
@df = `df`
@parts = @df.split(" ").map { |s| s.to_i }
@sum = 0
Expand All @@ -11,12 +11,12 @@ def uw_diskused
end

# Show the percentage of disk used.
def uw_diskused_perc
def self.uw_diskused_perc
df = `df --total`
df.split(" ").last.to_f.round(2)
end

def uw_cpuused
def self.uw_cpuused
@proc0 = File.readlines('/proc/stat').grep(/^cpu /).first.split(" ")
sleep 1
@proc1 = File.readlines('/proc/stat').grep(/^cpu /).first.split(" ")
Expand All @@ -41,7 +41,7 @@ def uw_cpuused

# return hash of top ten proccesses by cpu consumption
# example [["apache2", 12.0], ["passenger", 13.2]]
def uw_cputop
def self.uw_cputop
ps = `ps aux | awk '{print $11, $3}' | sort -k2nr | head -n 10`
array = []
ps.each_line do |line|
Expand All @@ -52,7 +52,7 @@ def uw_cputop
end


def uw_tcpused
def self.uw_tcpused
if File.exists?("/proc/net/sockstat")
File.open("/proc/net/sockstat", "r") do |ipv4|
@sockstat = ipv4.read
Expand All @@ -75,7 +75,7 @@ def uw_tcpused
@totaltcpused = @tcp4count.to_i + @tcp6count.to_i
end

def uw_udpused
def self.uw_udpused
if File.exists?("/proc/net/sockstat")
File.open("/proc/net/sockstat", "r") do |ipv4|
@sockstat = ipv4.read
Expand All @@ -97,7 +97,7 @@ def uw_udpused
@totaludpused = @udp4count.to_i + @udp6count.to_i
end

def uw_memused
def self.uw_memused
if File.exists?("/proc/meminfo")
File.open("/proc/meminfo", "r") do |file|
@result = file.read
Expand All @@ -113,7 +113,7 @@ def uw_memused

# return hash of top ten proccesses by mem consumption
# example [["apache2", 12.0], ["passenger", 13.2]]
def uw_memtop
def self.uw_memtop
ps = `ps aux | awk '{print $11, $4}' | sort -k2nr | head -n 10`
array = []
ps.each_line do |line|
Expand All @@ -123,7 +123,7 @@ def uw_memtop
array
end

def uw_load
def self.uw_load
if File.exists?("/proc/loadavg")
File.open("/proc/loadavg", "r") do |file|
@loaddata = file.read
Expand All @@ -133,7 +133,7 @@ def uw_load
end
end

def uw_bandrx
def self.uw_bandrx

def bandrx

Expand Down Expand Up @@ -186,7 +186,7 @@ def bandrx
@megabitsreceived = (@bitsreceived.to_f / 1024 / 1024).round(3)
end

def uw_bandtx
def self.uw_bandtx

def bandtx

Expand Down Expand Up @@ -239,7 +239,7 @@ def bandtx
@megabitstransmitted = (@bitstransmitted.to_f / 1024 / 1024).round(3)
end

def uw_diskioreads
def self.uw_diskioreads

def diskio

Expand Down Expand Up @@ -288,7 +288,7 @@ def diskio
@diskreads = @new1[0].to_i - @new0[0].to_i
end

def uw_diskiowrites
def self.uw_diskiowrites

def diskio

Expand Down
109 changes: 109 additions & 0 deletions lib/usagewatch/mac.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
module Usagewatch

# Show disk used in GB
def self.uw_diskused
df = `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 line[0] =~ /localhost/ #ignore backup filesystem
sum += ((line[2].to_f)/1024)/1024
end
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.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
end
((used/total) * 100).round(2)
end

# Show the percentage of cpu used
def self.uw_cpuused
top = `top -l1 | awk '/CPU usage/'`
top = top.gsub(/[\,a-zA-Z:]/, "").split(" ")
top[0].to_f
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
end

# todo
#def uw_tcpused
#
#end

# todo
#def uw_udpused
#
#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
end

# Percentage of mem used
def self.uw_memused
top = `top -l1 | awk '/PhysMem/'`
top = top.gsub(/[\.\,a-zA-Z:]/, "").split(" ").reverse
((top[1].to_f / (top[0].to_f + top[1].to_f)) * 100).round(2)
end

# Show the average of load in the last minute
def self.uw_load
iostat = `iostat -w1 -c 2 | awk '{print $7}'`
cpu = 0.0
iostat.each_line.with_index do |line, line_index|
next if line_index.eql? 0 or line_index.eql? 1 or line_index.eql? 2
cpu = line.split(" ").last.to_f.round(2)
end
cpu
end

#todo
#def uw_bandrx
#
#end

#todo
#def uw_bandtx
#
#end

#todo
#def uw_diskioreads
#
#end

#todo
#def uw_diskiowrites
#
#end
end
2 changes: 2 additions & 0 deletions usagewatch.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Gem::Specification.new do |spec|
spec.summary = %q{Statistics on a Linux server}
spec.homepage = "https://github.com/nethacker/usagewatch"
spec.license = "MIT"
spec.rdoc_options << '--main' << 'README'

spec.files = `git ls-files`.split($/)
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
Expand All @@ -20,4 +21,5 @@ Gem::Specification.new do |spec|

spec.add_development_dependency "bundler", "~> 1.3"
spec.add_development_dependency "rake"
spec.post_install_message = "* Linux version are covered for our test.\n* Mac OS version is in development\nThanks for installing!"
end

0 comments on commit 180214f

Please sign in to comment.