cpu-memory-stats 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "bundler", "~> 1.1.5"
10
+ gem "jeweler", "~> 1.8.4"
11
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Ondrej Bartas
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.markdown ADDED
@@ -0,0 +1,59 @@
1
+ # Cpu Memory Stats
2
+
3
+ Simple gem for getting information about cpu and memory usage
4
+
5
+ Currently only for Mac OS X & BSD
6
+
7
+ ## Usage
8
+
9
+ ``` ruby
10
+ puts CpuMemoryStats.get
11
+ # {
12
+ # :cpu=>{
13
+ # :user=>56.25,
14
+ # :system=>43.75,
15
+ # :idle=>0.0,
16
+ # :nice=>0,
17
+ # :interupt=>0
18
+ # },
19
+ # :memory=>{
20
+ # :wired=>1147142144,
21
+ # :active=>2091909120,
22
+ # :inactive=>1045430272,
23
+ # :used=>4283432960,
24
+ # :free=>10145792
25
+ # },
26
+ # :load=>[3.03, 3.17, 3.13],
27
+ # :cpu_percent=>{
28
+ # :user=>56.25,
29
+ # :system=>43.75,
30
+ # :idle=>0.0,
31
+ # :nice=>0.0,
32
+ # :interupt=>0.0
33
+ # },
34
+ # :memory_percent=>{
35
+ # :wired=>13.37,
36
+ # :active=>24.39,
37
+ # :inactive=>12.19,
38
+ # :used=>49.93,
39
+ # :free=>0.12
40
+ # }
41
+ # }
42
+ ```
43
+
44
+
45
+ ## Contributing to cpu-memory-stats
46
+
47
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
48
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
49
+ * Fork the project.
50
+ * Start a feature/bugfix branch.
51
+ * Commit and push until you are happy with your contribution.
52
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
53
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
54
+
55
+ ## Copyright
56
+
57
+ Copyright (c) 2012 Ondrej Bartas. See LICENSE.txt for
58
+ further details.
59
+
data/Rakefile ADDED
@@ -0,0 +1,35 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "cpu-memory-stats"
18
+ gem.homepage = "http://github.com/ondrejbartas/cpu-memory-stats"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Get system CPU and Memory usage}
21
+ gem.description = %Q{Simple gem for getting information about cpu and memory usage (Mac OS X, BSD)}
22
+ gem.email = "ondrej@bartas.cz"
23
+ gem.authors = ["Ondrej Bartas"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rake/testtask'
29
+ Rake::TestTask.new(:test) do |test|
30
+ test.libs << 'lib' << 'test'
31
+ test.pattern = 'test/**/test_*.rb'
32
+ test.verbose = true
33
+ end
34
+
35
+ task :default => :test
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,32 @@
1
+ # -*- encoding : utf-8 -*-
2
+ # CpuMemoryStats will automaticaly choose right system for getting stats
3
+ # author: Ondrej Bartas
4
+
5
+ module CpuMemoryStats
6
+
7
+ def self.get
8
+ unless self.constants.include?(OSDetection.detect)
9
+ raise ArgumentError, "cpu-memory-stats can not work on this system, uninstall them or write new module for your system"
10
+ else
11
+ system = CpuMemoryStats.const_get(OSDetection.detect).new
12
+ {
13
+ :cpu => system.cpu,
14
+ :memory => system.memory,
15
+ :load => system.load_avg,
16
+ :cpu_percent => percent(system.cpu),
17
+ :memory_percent => percent(system.memory),
18
+ }
19
+ end
20
+ end
21
+
22
+ def self.percent memory
23
+ maximal = memory.inject(0){|o,i| o+i.last}
24
+ memory.inject({}){|o,i| o[i.first] = (i.last.to_f / maximal.to_f * 100.0).round(2); o }
25
+ end
26
+ end
27
+
28
+ #include OS Detection
29
+ require File.join(File.dirname(__FILE__),"/os_detection.rb")
30
+
31
+ #including modules
32
+ Dir[File.join(File.dirname(__FILE__),"/modules/*.rb")].each {|file| require file }
@@ -0,0 +1,40 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module CpuMemoryStats
3
+
4
+ class Bsd
5
+
6
+ def get_stats
7
+
8
+ output = {}
9
+ output[:load_avg] = `sysctl -n vm.loadavg`.gsub(/\{|\}/,"").strip.split(" ").collect{|i| i.strip.to_f.round(2)}
10
+
11
+ cpu = `sysctl -n kern.cp_time`.strip.split(" ").collect{|i| i.strip.to_i}
12
+ output[:cpu] = Hash[[:user, :nice, :system, :interupt, :idle].zip(cpu)]
13
+
14
+ output[:memory] = {
15
+ :wired => `sysctl -n vm.stats.vm.v_wire_count`.to_i,
16
+ :active => `sysctl -n vm.stats.vm.v_active_count`.to_i,
17
+ :inactive => `sysctl -n vm.stats.vm.v_inactive_count`.to_i,
18
+ :used => 0, #fix missing columns
19
+ :free => `sysctl -n vm.stats.vm.v_free_count`.to_i
20
+ }
21
+ output
22
+ end
23
+
24
+ def stats
25
+ @stats ||= get_stats
26
+ end
27
+
28
+ def cpu
29
+ stats[:cpu]
30
+ end
31
+
32
+ def load_avg
33
+ stats[:load_avg]
34
+ end
35
+
36
+ def memory
37
+ stats[:memory]
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,53 @@
1
+ # -*- encoding : utf-8 -*-
2
+ # Module for geting informations from MAC OS X
3
+ # author: Ondrej Bartas
4
+
5
+ module CpuMemoryStats
6
+
7
+ class Mac
8
+
9
+ def get_stats
10
+ top = `top -F -R -l 1 -n 0`
11
+
12
+ output = {}
13
+ output[:load_avg] = top.scan(/Load Avg:(.+),(.+),(.+)/i).flatten.collect{|i| i.strip.to_f.round(2)}
14
+
15
+ cpu = top.scan(/CPU usage:\s+(.+)user,(.+)sys,(.+)idle/i).flatten.collect{|i| i.gsub("%","").strip.to_f.round(2)}
16
+ output[:cpu] = Hash[[:user, :system, :idle].zip(cpu)]
17
+ #fix missing columns
18
+ output[:cpu][:nice] = 0
19
+ output[:cpu][:interupt] = 0
20
+
21
+ memory = top.scan(/PhysMem:\s+(.+)wired,(.+)active,(.+)inactive,(.+)used,(.+)free/i).flatten.collect{|i| i.strip}
22
+ mem = Hash[[:wired, :active, :inactive, :used, :free].zip(memory)]
23
+ output[:memory] = mem.inject({}) do |o,v|
24
+ num = v.last.to_i
25
+ o[v.first] = case v.last
26
+ when /G/ then num*1024*1024*1024
27
+ when /M/ then num*1024*1024
28
+ when /K/ then num*1024
29
+ else num end
30
+ o
31
+ end
32
+
33
+ output
34
+ end
35
+
36
+ def stats
37
+ @stats ||= get_stats
38
+ end
39
+
40
+ def cpu
41
+ stats[:cpu]
42
+ end
43
+
44
+ def load_avg
45
+ stats[:load_avg]
46
+ end
47
+
48
+ def memory
49
+ stats[:memory]
50
+ end
51
+
52
+ end
53
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding : utf-8 -*-
2
+ # OS detection based on RB CONFIG by Zucker
3
+ # http://rbjl.net/35-how-to-properly-check-for-your-ruby-interpreter-version-and-os
4
+ # edited by Ondrej Bartas
5
+
6
+ require 'rbconfig';
7
+
8
+ module CpuMemoryStats
9
+ class OSDetection
10
+ def self.detect
11
+ case RbConfig::CONFIG['host_os']
12
+ when /linux|cygwin/ then :Linux
13
+ when /mac|darwin/ then :Mac
14
+ when /bsd/ then :Bsd
15
+ when /mswin|win|mingw/ then :Windows
16
+ when /solaris|sunos/ then :Solaris
17
+ else :unknown
18
+ end
19
+ end
20
+ end
21
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'cpu-memory-stats'
16
+
17
+ class Test::Unit::TestCase
18
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestCpuMemoryStats < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cpu-memory-stats
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ondrej Bartas
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-29 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 1.1.5
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 1.1.5
30
+ - !ruby/object:Gem::Dependency
31
+ name: jeweler
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 1.8.4
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 1.8.4
46
+ description: Simple gem for getting information about cpu and memory usage (Mac OS
47
+ X, BSD)
48
+ email: ondrej@bartas.cz
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files:
52
+ - LICENSE.txt
53
+ - README.markdown
54
+ files:
55
+ - .document
56
+ - Gemfile
57
+ - LICENSE.txt
58
+ - README.markdown
59
+ - Rakefile
60
+ - VERSION
61
+ - lib/cpu-memory-stats.rb
62
+ - lib/modules/bsd.rb
63
+ - lib/modules/mac.rb
64
+ - lib/os_detection.rb
65
+ - test/helper.rb
66
+ - test/test_cpu-memory-stats.rb
67
+ homepage: http://github.com/ondrejbartas/cpu-memory-stats
68
+ licenses:
69
+ - MIT
70
+ post_install_message:
71
+ rdoc_options: []
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ! '>='
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ segments:
81
+ - 0
82
+ hash: 3207594381875603395
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ! '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ requirements: []
90
+ rubyforge_project:
91
+ rubygems_version: 1.8.24
92
+ signing_key:
93
+ specification_version: 3
94
+ summary: Get system CPU and Memory usage
95
+ test_files: []