linux-kstat 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES ADDED
@@ -0,0 +1,2 @@
1
+ = 0.1.0 - 10-Feb-2011
2
+ * Initial release
@@ -0,0 +1,7 @@
1
+ * CHANGES
2
+ * README
3
+ * MANIFEST
4
+ * Rakefile
5
+ * linux-kstat.gemspec
6
+ * lib/linux/kstat.rb
7
+ * test/test_linux_kstat.rb
data/README ADDED
@@ -0,0 +1,43 @@
1
+ = Description
2
+ A Ruby library for gathering Linux kernel statistics out of /proc/stat.
3
+
4
+ = Installation
5
+ gem install linux-kstat
6
+
7
+ = Synopsis
8
+ require 'linux/kstat'
9
+
10
+ kstat = Linux::Kstat.new
11
+
12
+ p kstat[:cpu]
13
+ p kstat[:procs_running]
14
+
15
+ = Details
16
+ The values for most of the keys are a single numeric value. However, in the
17
+ case of "cpu" keys, the result is a 7 element hash of numeric values. In
18
+ the case of the "intr" key, the value is an array containing the list of
19
+ interrupts.
20
+
21
+ = Information about /proc/stat
22
+ See http://www.linuxhowtos.org/System/procstat.htm for more information
23
+ about the meaning of each of the fields.
24
+
25
+ = Known Bugs
26
+ None known. Please report any bugs on the github project page.
27
+
28
+ http://www.github.com/djberg96/linux-kstat
29
+
30
+ = License
31
+ Artistic 2.0
32
+
33
+ = Copyright
34
+ (C) 2003-2011 Daniel J. Berger
35
+ All Rights Reserved.`
36
+
37
+ = Warranty
38
+ This package is provided "as is" and without any express or
39
+ implied warranties, including, without limitation, the implied
40
+ warranties of merchantability and fitness for a particular purpose.
41
+
42
+ = Author
43
+ Daniel J. Berger
@@ -0,0 +1,27 @@
1
+ require 'rake'
2
+ require 'rake/clean'
3
+ require 'rake/testtask'
4
+
5
+ CLEAN.include('**/*.gem', '**/*.rbc')
6
+
7
+ namespace :gem do
8
+ desc 'Build the linux-kstat gem'
9
+ task :create => [:clean] do
10
+ spec = eval(IO.read('linux-kstat.gemspec'))
11
+ Gem::Builder.new(spec).build
12
+ end
13
+
14
+ desc "Install the linux-kstat gem"
15
+ task :install => [:create] do
16
+ file = Dir["*.gem"].first
17
+ sh "gem install #{file}"
18
+ end
19
+ end
20
+
21
+ Rake::TestTask.new do |t|
22
+ task :test => :clean
23
+ t.warning = true
24
+ t.verbose = true
25
+ end
26
+
27
+ task :default => :test
@@ -0,0 +1,76 @@
1
+ require 'forwardable'
2
+
3
+ # The Linux module serves as a namespace only.
4
+ module Linux
5
+ # The Kstat class encapsulates Linux kernel statistics derived from /proc/stat.
6
+ class Kstat
7
+ extend Forwardable
8
+
9
+ # The version of the linux-kstat library
10
+ VERSION = '0.1.0'
11
+
12
+ # :stopdoc:
13
+
14
+ # Delegate the the [] and inspect methods to the @hash variable
15
+ def_delegator(:@hash, :[])
16
+ def_delegator(:@hash, :inspect)
17
+
18
+ # :startdoc:
19
+
20
+ # Create a new Linux::Kstat instance. You can access the information
21
+ # stored in this object the same way you would access a hash key. Note
22
+ # that all keys are symbols.
23
+ #
24
+ # Example:
25
+ #
26
+ # kstat = Linux::Kstat.new
27
+ #
28
+ # kstat[:cpu] => {
29
+ # :idle => 250713454,
30
+ # :iowait => 2745691,
31
+ # :irq => 39717,
32
+ # :softirq => 31323,
33
+ # :system => 1881655,
34
+ # :nice => 117158,
35
+ # :user => 7137418
36
+ # }
37
+ #
38
+ # kstat[:processes] # => 1299560
39
+ #
40
+ def initialize
41
+ @hash = get_proc_stat_info
42
+ end
43
+
44
+ private
45
+
46
+ # Parse the information out of /proc/stat and assign keys and values to
47
+ # a hash that can be accessed via the Forwardable module.
48
+ #
49
+ def get_proc_stat_info
50
+ hash = {}
51
+
52
+ IO.readlines('/proc/stat').each{ |line|
53
+ info = line.split
54
+ if info.first =~ /^cpu/i
55
+ hash[info.first.to_sym] = {
56
+ :user => info[1].to_i,
57
+ :nice => info[2].to_i,
58
+ :system => info[3].to_i,
59
+ :idle => info[4].to_i,
60
+ :iowait => info[5].to_i,
61
+ :irq => info[6].to_i,
62
+ :softirq => info[7].to_i
63
+ }
64
+ else
65
+ if info.size > 2
66
+ hash[info.first.to_sym] = info[1..-1].map{ |e| e.to_i }
67
+ else
68
+ hash[info.first.to_sym] = info[1].to_i
69
+ end
70
+ end
71
+ }
72
+
73
+ hash
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,25 @@
1
+ require 'rubygems'
2
+ require 'rbconfig'
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.name = 'linux-kstat'
6
+ gem.version = '0.1.0'
7
+ gem.license = 'Artistic 2.0'
8
+ gem.author = 'Daniel J. Berger'
9
+ gem.email = 'djberg96@gmail.com'
10
+ gem.homepage = 'http://www.rubyforge.org/projects/shards'
11
+ gem.summary = 'Ruby interface for Linux kernel stats in /proc/stat'
12
+ gem.test_files = Dir['test/test*']
13
+ gem.has_rdoc = true
14
+ gem.files = Dir['**/*'].reject{ |f| f.include?('git') }
15
+
16
+ gem.rubyforge_project = 'shards'
17
+ gem.extra_rdoc_files = ['README', 'CHANGES', 'MANIFEST']
18
+
19
+ gem.description = <<-EOF
20
+ The linux-kstat library provides a hash style interface for reading
21
+ Linux kernel statistics read out of /proc/stat.
22
+ EOF
23
+
24
+ gem.add_development_dependency('test-unit', '>= 2.1.2')
25
+ end
@@ -0,0 +1,34 @@
1
+ require 'rubygems'
2
+ gem 'test-unit'
3
+
4
+ require 'test/unit'
5
+ require 'linux/kstat'
6
+
7
+ class TC_Linux_Kstat < Test::Unit::TestCase
8
+ def setup
9
+ @kstat = Linux::Kstat.new
10
+ end
11
+
12
+ test "version constant is set to the expected value" do
13
+ assert_equal('0.1.0', Linux::Kstat::VERSION)
14
+ end
15
+
16
+ test "kstat object can be accessed like a hash" do
17
+ assert_respond_to(@kstat, :[])
18
+ assert_nothing_raised{ @kstat[:cpu] }
19
+ end
20
+
21
+ test "kstat object contains expected keys" do
22
+ assert_kind_of(Hash, @kstat[:cpu])
23
+ assert_kind_of(Numeric, @kstat[:btime])
24
+ assert_kind_of(Array, @kstat[:intr])
25
+ end
26
+
27
+ test "values cannot be assigned to keys" do
28
+ assert_raise(NoMethodError){ @kstat[:cpu] = 'test' }
29
+ end
30
+
31
+ def teardown
32
+ @kstat = nil
33
+ end
34
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: linux-kstat
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Daniel J. Berger
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-02-10 00:00:00 -07:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: test-unit
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 15
30
+ segments:
31
+ - 2
32
+ - 1
33
+ - 2
34
+ version: 2.1.2
35
+ type: :development
36
+ version_requirements: *id001
37
+ description: " The linux-kstat library provides a hash style interface for reading\n Linux kernel statistics read out of /proc/stat.\n"
38
+ email: djberg96@gmail.com
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files:
44
+ - README
45
+ - CHANGES
46
+ - MANIFEST
47
+ files:
48
+ - Rakefile
49
+ - README
50
+ - lib/linux/kstat.rb
51
+ - CHANGES
52
+ - test/test_linux_kstat.rb
53
+ - MANIFEST
54
+ - linux-kstat.gemspec
55
+ has_rdoc: true
56
+ homepage: http://www.rubyforge.org/projects/shards
57
+ licenses:
58
+ - Artistic 2.0
59
+ post_install_message:
60
+ rdoc_options: []
61
+
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ hash: 3
70
+ segments:
71
+ - 0
72
+ version: "0"
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ hash: 3
79
+ segments:
80
+ - 0
81
+ version: "0"
82
+ requirements: []
83
+
84
+ rubyforge_project: shards
85
+ rubygems_version: 1.3.7
86
+ signing_key:
87
+ specification_version: 3
88
+ summary: Ruby interface for Linux kernel stats in /proc/stat
89
+ test_files:
90
+ - test/test_linux_kstat.rb