proc_parser 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d02762173ed67bc907bd6ef6339abfa4dc9cf7df
4
+ data.tar.gz: 728440954994def2629aa26059190917358ae542
5
+ SHA512:
6
+ metadata.gz: 5229293647075b4fa4911d5e70bbc7a4c85597c028f97855900fa4d6dd294c07c31a7142f8c54d9e67b4ae6ed15fc6aef6d312a685ffb71a02921fc4119004e7
7
+ data.tar.gz: 1d3172a32f58b31b0b3cfa0412e529e2035bea4e2cdeb303235bab97a42bd94207f595a8f526a427d81c89b2ed9900587a012091009fc432cf1519799fd62956
data/.bundle/config ADDED
@@ -0,0 +1,3 @@
1
+ ---
2
+ BUNDLE_PATH: "./vendor/.bundle"
3
+ BUNDLE_DISABLE_SHARED_GEMS: "true"
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /vendor/.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.4.2
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.2
5
+ before_install: gem install bundler -v 1.15.4
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in proc_parser.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,35 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ proc_parser (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.3)
10
+ rake (12.1.0)
11
+ rspec (3.6.0)
12
+ rspec-core (~> 3.6.0)
13
+ rspec-expectations (~> 3.6.0)
14
+ rspec-mocks (~> 3.6.0)
15
+ rspec-core (3.6.0)
16
+ rspec-support (~> 3.6.0)
17
+ rspec-expectations (3.6.0)
18
+ diff-lcs (>= 1.2.0, < 2.0)
19
+ rspec-support (~> 3.6.0)
20
+ rspec-mocks (3.6.0)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.6.0)
23
+ rspec-support (3.6.0)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ bundler (~> 1.15)
30
+ proc_parser!
31
+ rake (~> 12.1)
32
+ rspec (~> 3.6)
33
+
34
+ BUNDLED WITH
35
+ 1.15.4
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 TODO: Write your name
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,70 @@
1
+ # proc_parser
2
+
3
+ proc_parser provides a Ruby wrapper for /proc data such as those contained in
4
+ /proc/mem_info, /proc/stat and /proc/loadavg.
5
+
6
+ Interested in support of more proc files? Contributions are welcome! You can
7
+ either create an [issue](https://github.com/EtienneM/mem_info/issues) or
8
+ propose a [pull request](https://github.com/EtienneM/mem_info/pulls).
9
+
10
+ Wondering the meaning of some fields? Read the manual with [`man 5
11
+ proc`](https://linux.die.net/man/5/proc).
12
+
13
+ ## Installation
14
+
15
+ Add this line to your application's Gemfile:
16
+
17
+ ```ruby
18
+ gem 'proc_parser'
19
+ ```
20
+
21
+ And then execute:
22
+
23
+ $ bundle
24
+
25
+ Or install it yourself as:
26
+
27
+ $ gem install proc_parser
28
+
29
+ ## Usage
30
+
31
+ ```ruby
32
+ meminfo = ProcParser::MemInfo
33
+ memtotal = meminfo.memtotal
34
+ memfree = meminfo.memfree
35
+ ```
36
+
37
+ ## Development
38
+
39
+ After checking out the repositories, run `bin/setup` to install dependencies.
40
+ Then, run `bundle exec rake spec` to run the tests. You can also run
41
+ `bin/console` for an interactive prompt that will allow you to experiment.
42
+
43
+ To install this gem onto your local machine, run `bundle exec rake install`. To
44
+ release a new version, update the version number in `version.rb`, and then run
45
+ `bundle exec rake release`, which will create a git tag for the version, push
46
+ git commits and tags, and push the `.gem` file to
47
+ [rubygems.org](https://rubygems.org).
48
+
49
+ ### Without installing Ruby globally
50
+
51
+ On my work station, I prefer to use a Docker image with Ruby installed instead
52
+ of installing Ruby locally. Hence, any `bundle …` command can be prefixed with
53
+ `docker-compose run proc-parser`. For instance, running the tests is done with
54
+ the command `docker-compose run proc-parser bundle exec rake spec`.
55
+
56
+ ## Acknowledgment
57
+
58
+ This gem is a generalization of the wonderful work from
59
+ [watsonian](https://github.com/watsonian/) and its
60
+ [`mem_info`](https://github.com/watsonian/mem_info/) gem.
61
+
62
+ ## Contributing
63
+
64
+ Bug reports and pull requests are welcome on GitHub at
65
+ https://github.com/EtienneM/proc_parser.
66
+
67
+ ## License
68
+
69
+ The gem is available as open source under the terms of the [MIT
70
+ License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'proc_parser'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require 'pry'
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,12 @@
1
+ version: '2'
2
+ services:
3
+ proc-parser:
4
+ image: ruby:2.4.2
5
+ environment:
6
+ BUNDLE_BIN: "/usr/src/app/vendor/.bundle/ruby/2.4.0/bin"
7
+ BUNDLE_PATH: "/usr/src/app/vendor/.bundle/ruby/2.4.0"
8
+ BUNDLE_APP_CONFIG: "/usr/src/app/.bundle"
9
+ working_dir: /usr/src/app
10
+ volumes:
11
+ - ./:/usr/src/app/
12
+ command: "true"
@@ -0,0 +1,8 @@
1
+ require 'proc_parser/version'
2
+
3
+ module ProcParser
4
+ class NoProcData < RuntimeError; end
5
+ end
6
+
7
+ require 'proc_parser/cpu_stat'
8
+ require 'proc_parser/mem_info'
@@ -0,0 +1,69 @@
1
+ module ProcParser
2
+ class CPUStat
3
+ #
4
+ # This class read CPU usage information from the /proc/stat file. The behavior for Kernel
5
+ # version prior to 2.5.41 is undefined.
6
+ #
7
+
8
+ attr_accessor :user, :nice, :system, :idle, :iowait, :irq, :softirq, :steal, :guest, :guest_nice
9
+ @@attributes = {
10
+ user: 1,
11
+ nice: 2,
12
+ system: 3,
13
+ idle: 4,
14
+ iowait: 5,
15
+ irq: 6,
16
+ softirq: 7,
17
+ steal: 8,
18
+ guest: 9,
19
+ guest_nice: 10,
20
+ }
21
+
22
+ attr_accessor :nb_cpu
23
+
24
+ def initialize(stat_file = '/proc/stat')
25
+ raise NoProcData, "This system doesn't have /proc/stat data." if !File.exist?(stat_file)
26
+
27
+ File.open(stat_file, 'r') do |file|
28
+ firstline = file.readline.strip.squeeze(' ').split(' ')
29
+ raise NoProcData, 'Unsupported format of /proc/stat' if firstline[0] != 'cpu'
30
+
31
+ @@attributes.each do |attribute, index|
32
+ instance_variable_set("@#{attribute}", firstline[index].to_i)
33
+ end
34
+
35
+ @nb_cpu = 0
36
+ file.each do |line|
37
+ splitted_line = line.strip.squeeze(' ').split(' ')
38
+ break if !splitted_line[0].start_with? 'cpu'
39
+ @nb_cpu += 1
40
+ end
41
+ end
42
+ end
43
+
44
+ #
45
+ ## Get the current CPU usage in percentage. It's a global percentage normalized on the number of
46
+ # CPU.
47
+ #
48
+ # The CPU usage is actually everything but the idle time of the CPU.
49
+ #
50
+ # cf. `htop` source code: https://github.com/hishamhm/htop/blob/e0209da88faf3b390d71ff174065abd407abfdfd/ProcessList.c#L947
51
+ # cf. man 5 proc
52
+ # cf. https://stackoverflow.com/a/23376195/1351436
53
+ #
54
+ def percentage_usage
55
+ # Guest time is already accounted in user time
56
+ user = @user - @guest
57
+ nice = @nice - @guest_nice
58
+
59
+ idlealltime = @idle + @iowait
60
+ systemalltime = @system + @irq + @softirq
61
+ virtalltime = @guest + @guest_nice
62
+ non_idlealltime = user + nice + systemalltime + @steal + virtalltime
63
+
64
+ total = idlealltime + non_idlealltime
65
+
66
+ return non_idlealltime / total.to_f / @nb_cpu
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,102 @@
1
+ module ProcParser
2
+ class MemInfo
3
+ #
4
+ # This class read memory information from the /proc/meminfo file.
5
+ #
6
+ # All the values returned by this class are in kilobytes.
7
+ #
8
+
9
+ # Contains the data in kilobytes
10
+ attr_accessor :memtotal, :memfree, :buffers, :cached, :swapcached, :active, :inactive,
11
+ :active_anon, :inactive_anon, :active_file, :inactive_file, :unevictable,
12
+ :mlocked, :swaptotal, :swapfree, :dirty, :writeback, :anonpages, :mapped,
13
+ :slab, :sreclaimable, :sunreclaim, :pagetables, :nfs_unstable, :bounce,
14
+ :writebacktmp, :commitlimit, :committed_as, :vmalloctotal, :vmallocused,
15
+ :vmallochunk, :directmap4k, :directmap2m
16
+
17
+ @@attributes = {
18
+ memtotal: 'MemTotal',
19
+ memfree: 'MemFree',
20
+ buffers: 'Buffers',
21
+ cached: 'Cached',
22
+ swapcached: 'SwapCached',
23
+ active: 'Active',
24
+ inactive: 'Inactive',
25
+ active_anon: 'Active\(anon\)',
26
+ inactive_anon: 'Inactive\(anon\)',
27
+ active_file: 'Active\(file\)',
28
+ inactive_file: 'Inactive\(file\)',
29
+ unevictable: 'Unevictable',
30
+ mlocked: 'Mlocked',
31
+ swaptotal: 'SwapTotal',
32
+ swapfree: 'SwapFree',
33
+ dirty: 'Dirty',
34
+ writeback: 'Writeback',
35
+ anonpages: 'AnonPages',
36
+ mapped: 'Mapped',
37
+ slab: 'Slab',
38
+ sreclaimable: 'SReclaimable',
39
+ sunreclaim: 'SUnreclaim',
40
+ pagetables: 'PageTables',
41
+ nfs_unstable: 'NFS_Unstable',
42
+ bounce: 'Bounce',
43
+ writebacktmp: 'WritebackTmp',
44
+ commitlimit: 'CommitLimit',
45
+ committed_as: 'Committed_AS',
46
+ vmalloctotal: 'VmallocTotal',
47
+ vmallocused: 'VmallocUsed',
48
+ vmallocchunk: 'VmallocChunk',
49
+ directmap4k: 'DirectMap4k',
50
+ directmap2m: 'DirectMap2M',
51
+ }
52
+
53
+ def initialize(meminfo_file = '/proc/meminfo')
54
+ raise NoProcData, "This system doesn't have /proc/meminfo data." if !File.exist?(meminfo_file)
55
+
56
+ File.open(meminfo_file, 'r') do |file|
57
+ data = file.read
58
+ @@attributes.each_key do |attribute|
59
+ value, unit = regex_match(attribute, data)
60
+ if unit != 'kB'
61
+ raise NoProcData, 'Unsupported unit stored in meminfo.'
62
+ end
63
+ instance_variable_set("@#{attribute}", value.to_i)
64
+ end
65
+ end
66
+ end
67
+
68
+ # Memory currently in use.
69
+ #
70
+ # We substract the free amount of memory to the total.
71
+ def memused
72
+ @memtotal - @memfree
73
+ end
74
+
75
+ # Swap currently in use.
76
+ #
77
+ # We substract the free amount of swap to the total.
78
+ def swapused
79
+ @swaptotal - @swapfree
80
+ end
81
+
82
+ # Memory available in the system. It is not just the free memory.
83
+ #
84
+ # The available memory is actually what the `free` command line tool calls `-/+ buffers/cache`. It
85
+ # uses information from /proc/meminfo: it sums the MemFree, the Buffers and the Cached.
86
+ #
87
+ # cf. `free` source code: https://github.com/mmalecki/procps/blob/fe4c4a7314f32907b9f558ad0d8b8d0ff1cc76be/free.c#L97
88
+ # cf. man 5 proc
89
+ #
90
+ def free_buffers
91
+ @memfree + @buffers + @cached
92
+ end
93
+
94
+ private
95
+
96
+ def regex_match(attribute, line)
97
+ regex = Regexp.new("#{@@attributes[attribute]}:[[:space:]]*([[:digit:]]*) ([[:alpha:]]*)")
98
+ m = regex.match(line)
99
+ return m[1], m[2] if line.match? regex
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,3 @@
1
+ module ProcParser
2
+ VERSION = '0.1.0'.freeze
3
+ end
@@ -0,0 +1,38 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'proc_parser/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'proc_parser'
9
+ spec.version = ProcParser::VERSION
10
+ spec.authors = ['Étienne Michon']
11
+ spec.email = ['etienne@scalingo.com']
12
+
13
+ spec.summary = 'ProcParser provides a Ruby wrapper for /proc data'
14
+ spec.description = 'ProcParser provides a Ruby wrapper for /proc data such as mem_info, stat
15
+ and loadavg'
16
+ spec.homepage = 'https://github.com/EtienneM/proc_parser'
17
+ spec.license = 'MIT'
18
+
19
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
20
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
21
+ if spec.respond_to?(:metadata)
22
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org/'
23
+ else
24
+ raise 'RubyGems 2.0 or newer is required to protect against ' \
25
+ 'public gem pushes.'
26
+ end
27
+
28
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
29
+ f.match(%r{^(test|spec|features)/})
30
+ end
31
+ spec.bindir = 'exe'
32
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
33
+ spec.require_paths = ['lib']
34
+
35
+ spec.add_development_dependency 'bundler', '~> 1.15'
36
+ spec.add_development_dependency 'rake', '~> 12.1'
37
+ spec.add_development_dependency 'rspec', '~> 3.6'
38
+ end
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: proc_parser
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Étienne Michon
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-10-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '12.1'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '12.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.6'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.6'
55
+ description: |-
56
+ ProcParser provides a Ruby wrapper for /proc data such as mem_info, stat
57
+ and loadavg
58
+ email:
59
+ - etienne@scalingo.com
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - ".bundle/config"
65
+ - ".gitignore"
66
+ - ".rspec"
67
+ - ".ruby-version"
68
+ - ".travis.yml"
69
+ - Gemfile
70
+ - Gemfile.lock
71
+ - LICENSE
72
+ - README.md
73
+ - Rakefile
74
+ - bin/console
75
+ - bin/setup
76
+ - docker-compose.yml
77
+ - lib/proc_parser.rb
78
+ - lib/proc_parser/cpu_stat.rb
79
+ - lib/proc_parser/mem_info.rb
80
+ - lib/proc_parser/version.rb
81
+ - proc_parser.gemspec
82
+ homepage: https://github.com/EtienneM/proc_parser
83
+ licenses:
84
+ - MIT
85
+ metadata:
86
+ allowed_push_host: https://rubygems.org/
87
+ post_install_message:
88
+ rdoc_options: []
89
+ require_paths:
90
+ - lib
91
+ required_ruby_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ requirements: []
102
+ rubyforge_project:
103
+ rubygems_version: 2.6.13
104
+ signing_key:
105
+ specification_version: 4
106
+ summary: ProcParser provides a Ruby wrapper for /proc data
107
+ test_files: []