cz_system_info 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b4f0caf7d6df393d6b50d35b25752511ab1909bd38cdc396c19c749f431a73fb
4
+ data.tar.gz: 6aa04968b9ccb3a3916cf91c1634158f5176fa4252b92b187be46cf9c9ff3e2e
5
+ SHA512:
6
+ metadata.gz: 9dc72951c2b646e19973017c7b6f0a04bedd0f6f9b939096503a47d014dd97d420fd34443cff375523cdb5a83d79bd198a1794539901fe9ee1116a0bc57f7ba2
7
+ data.tar.gz: 8bfd0dfab7191825d6e52c63847316ee078a1a700c0b2b1085c1f1b27a4e42f310d536b305c6496c43bdb4d2fb82bcbd24e22388f515be5790ee10d08225d17d
@@ -0,0 +1,18 @@
1
+ name: Ruby
2
+
3
+ on: [push,pull_request]
4
+
5
+ jobs:
6
+ build:
7
+ runs-on: ubuntu-latest
8
+ steps:
9
+ - uses: actions/checkout@v2
10
+ - name: Set up Ruby
11
+ uses: ruby/setup-ruby@v1
12
+ with:
13
+ ruby-version: 3.0.0
14
+ - name: Run the default task
15
+ run: |
16
+ gem install bundler -v 2.2.3
17
+ bundle install
18
+ bundle exec rake
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in cz_system_info.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "test-unit", "~> 3.0"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 hayate
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,39 @@
1
+ # CzSystemInfo
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/cz_system_info`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'cz_system_info'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install cz_system_info
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test-unit` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/cz_system_info.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/*_test.rb"]
10
+ end
11
+
12
+ task default: :test
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "cz_system_info"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ 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,36 @@
1
+ # frozen_string_literal: true
2
+ require_relative "lib/cz_system_info/version"
3
+
4
+ Gem::Specification.new do |spec|
5
+ spec.name = "cz_system_info"
6
+ spec.version = CzSystemInfo::VERSION
7
+ spec.authors = ["hayate"]
8
+ spec.email = ["holdstock@yeah.net"]
9
+
10
+ spec.summary = "Move Daniel Berger's sys code into one."
11
+ spec.description = "Move Daniel Berger's sys code into one."
12
+ spec.homepage = "https://github.com/hinagiku/cz_system_info"
13
+ spec.license = "MIT"
14
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
15
+
16
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
17
+
18
+ spec.metadata["homepage_uri"] = spec.homepage
19
+ spec.metadata["source_code_uri"] = "https://github.com/hinagiku/cz_system_info"
20
+ spec.metadata["changelog_uri"] = "https://github.com/hinagiku/cz_system_info"
21
+
22
+ # Specify which files should be added to the gem when it is released.
23
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
24
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
25
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
26
+ end
27
+ spec.bindir = "exe"
28
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ["lib"]
30
+
31
+ # Uncomment to register a new dependency of your gem
32
+ spec.add_dependency('ffi', '~> 1.1')
33
+
34
+ # For more information and examples about making a new gem, checkout our
35
+ # guide at: https://bundler.io/guides/creating_gem.html
36
+ end
@@ -0,0 +1,167 @@
1
+ # frozen_string_literal: true
2
+
3
+ ##########################################################
4
+ # linux.rb (sys-cpu) - pure Ruby version for Linux
5
+ ##########################################################
6
+
7
+ # The CzSystemInfo module is a namespace only.
8
+ module CzSystemInfo
9
+ # :stopdoc:
10
+
11
+ cpu_file = '/proc/cpuinfo'
12
+ cpu_hash = {}
13
+ CPU_ARRAY = []
14
+
15
+ private_constant :CPU_ARRAY
16
+
17
+ # Parse the info out of the /proc/cpuinfo file
18
+ File.foreach(cpu_file) do |line|
19
+ line.strip!
20
+ next if line.empty?
21
+
22
+ key, val = line.split(':')
23
+ key.strip!
24
+ key.gsub!(/\s+/, '_')
25
+ key.downcase!
26
+ val.strip! if val
27
+
28
+ if cpu_hash.key?(key)
29
+ CPU_ARRAY.push(cpu_hash.dup)
30
+ cpu_hash.clear
31
+ end
32
+
33
+ # Turn yes/no attributes into booleans
34
+ val = true if val == 'yes'
35
+ val = false if val == 'no'
36
+
37
+ cpu_hash[key] = val
38
+ end
39
+
40
+ CPU_ARRAY.push(cpu_hash)
41
+
42
+ # :startdoc:
43
+
44
+ # The CPU class encapsulates information about physical CPUs on your system.
45
+ class CPU
46
+ # :stopdoc:
47
+
48
+ CPUStruct = Struct.new('CPUStruct', *CPU_ARRAY.first.keys)
49
+
50
+ private_constant :CPUStruct
51
+
52
+ # :startdoc:
53
+
54
+ # In block form, yields a CPUStruct for each CPU on the system. In
55
+ # non-block form, returns an Array of CPUStruct's.
56
+ #
57
+ # The exact members of the struct vary on Linux systems.
58
+ #
59
+ def self.processors
60
+ array = []
61
+ CPU_ARRAY.each do |hash|
62
+ struct = CPUStruct.new
63
+ struct.members.each{ |m| struct.send("#{m}=", hash[m.to_s]) }
64
+ if block_given?
65
+ yield struct
66
+ else
67
+ array << struct
68
+ end
69
+ end
70
+ array unless block_given?
71
+ end
72
+
73
+ # Return the total number of logical CPU on the system.
74
+ #
75
+ def self.num_cpu
76
+ CPU_ARRAY.size
77
+ end
78
+
79
+ # Return the architecture of the CPU.
80
+ #
81
+ def self.architecture
82
+ case CPU_ARRAY.first['cpu_family']
83
+ when '3'
84
+ 'x86'
85
+ when '6'
86
+ 'x86_64'
87
+ end
88
+ end
89
+
90
+ # Returns a string indicating the CPU model.
91
+ #
92
+ def self.model
93
+ CPU_ARRAY.first['model_name']
94
+ end
95
+
96
+ # Returns an integer indicating the speed of the CPU.
97
+ #
98
+ def self.freq
99
+ CPU_ARRAY.first['cpu_mhz'].to_f.round
100
+ end
101
+
102
+ # Create singleton methods for each of the attributes.
103
+ #
104
+ def self.method_missing(id, arg = 0)
105
+ raise NoMethodError, "'#{id}'" unless CPU_ARRAY[arg].key?(id.to_s)
106
+ rv = CPU_ARRAY[arg][id.to_s]
107
+ if rv.nil?
108
+ id = "#{id}?"
109
+ rv = CPU_ARRAY[arg][id]
110
+ end
111
+ rv
112
+ end
113
+
114
+ def self.respond_to_missing?(method, _private_methods = false)
115
+ CPU_ARRAY.first.keys.include?(method.to_s)
116
+ end
117
+
118
+ private_class_method :method_missing
119
+
120
+ # Returns a 3 element Array corresponding to the 1, 5 and 15 minute
121
+ # load average for the system.
122
+ #
123
+ def self.load_avg
124
+ load_avg_file = '/proc/loadavg'
125
+ File.readlines(load_avg_file).first.split[0..2].map(&:to_f)
126
+ end
127
+
128
+ # Returns a hash of arrays that contains an array of the following
129
+ # information (as of 2.6.33), respectively:
130
+ #
131
+ # * user: time spent in user mode.
132
+ # * nice: time spent in user mode with low priority.
133
+ # * system: time spent in system mode.
134
+ # * idle: time spent in the idle task.
135
+ # * iowait: time waiting for IO to complete.
136
+ # * irq: time servicing interrupts.
137
+ # * softirq: time servicing softirqs.
138
+ # * steal: time spent in other operating systems when running in a virtualized environment.
139
+ # * guest: time spent running a virtual CPU for guest operating systems.
140
+ # * guest_nice: time spent running a niced guest, i.e a virtual CPU for guest operating systems.
141
+ #
142
+ # Note that older kernels may not necessarily include some of these fields.
143
+ #
144
+ def self.cpu_stats
145
+ cpu_stat_file = '/proc/stat'
146
+ hash = {} # Hash needed for multi-cpu systems
147
+
148
+ lines = File.readlines(cpu_stat_file)
149
+
150
+ lines.each_with_index do |line, i|
151
+ array = line.split
152
+ break unless array[0] =~ /cpu/ # 'cpu' entries always on top
153
+
154
+ # Some machines list a 'cpu' and a 'cpu0'. In this case only
155
+ # return values for the numbered cpu entry.
156
+ if lines[i].split[0] == 'cpu' && lines[i + 1].split[0] =~ /cpu\d/
157
+ next
158
+ end
159
+
160
+ vals = array[1..-1].map{ |e| e.to_i / 100 } # 100 jiffies/sec.
161
+ hash[array[0]] = vals
162
+ end
163
+
164
+ hash
165
+ end
166
+ end
167
+ end
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CzSystemInfo
4
+ class Filesystem
5
+ module Constants
6
+ MNT_RDONLY = 0x00000001 # read only filesystem
7
+ MNT_SYNCHRONOUS = 0x00000002 # file system written synchronously
8
+ MNT_NOEXEC = 0x00000004 # can't exec from filesystem
9
+ MNT_NOSUID = 0x00000008 # don't honor setuid bits on fs
10
+ MNT_NODEV = 0x00000010 # don't interpret special files
11
+ MNT_UNION = 0x00000020 # union with underlying filesystem
12
+ MNT_ASYNC = 0x00000040 # file system written asynchronously
13
+ MNT_CPROTECT = 0x00000080 # file system supports content protection
14
+ MNT_EXPORTED = 0x00000100 # file system is exported
15
+ MNT_QUARANTINE = 0x00000400 # file system is quarantined
16
+ MNT_LOCAL = 0x00001000 # filesystem is stored locally
17
+ MNT_QUOTA = 0x00002000 # quotas are enabled on filesystem
18
+ MNT_ROOTFS = 0x00004000 # identifies the root filesystem
19
+ MNT_DOVOLFS = 0x00008000 # FS supports volfs (deprecated)
20
+ MNT_DONTBROWSE = 0x00100000 # FS is not appropriate path to user data
21
+ MNT_IGNORE_OWNERSHIP = 0x00200000 # VFS will ignore ownership info on FS objects
22
+ MNT_AUTOMOUNTED = 0x00400000 # filesystem was mounted by automounter
23
+ MNT_JOURNALED = 0x00800000 # filesystem is journaled
24
+ MNT_NOUSERXATTR = 0x01000000 # Don't allow user extended attributes
25
+ MNT_DEFWRITE = 0x02000000 # filesystem should defer writes
26
+ MNT_MULTILABEL = 0x04000000 # MAC support for individual labels
27
+ MNT_NOATIME = 0x10000000 # disable update of file access time
28
+
29
+ MNT_VISFLAGMASK = (
30
+ MNT_RDONLY | MNT_SYNCHRONOUS | MNT_NOEXEC |
31
+ MNT_NOSUID | MNT_NODEV | MNT_UNION |
32
+ MNT_ASYNC | MNT_EXPORTED | MNT_QUARANTINE |
33
+ MNT_LOCAL | MNT_QUOTA |
34
+ MNT_ROOTFS | MNT_DOVOLFS | MNT_DONTBROWSE |
35
+ MNT_IGNORE_OWNERSHIP | MNT_AUTOMOUNTED | MNT_JOURNALED |
36
+ MNT_NOUSERXATTR | MNT_DEFWRITE | MNT_MULTILABEL |
37
+ MNT_NOATIME | MNT_CPROTECT
38
+ )
39
+
40
+ MS_RDONLY = 1
41
+ MS_NOSUID = 2
42
+ MS_NODEV = 4
43
+ MS_NOEXEC = 8
44
+ MS_SYNCHRONOUS = 16
45
+ MS_REMOUNT = 32
46
+ MS_MANDLOCK = 64
47
+ MS_DIRSYNC = 128
48
+ MS_NOATIME = 1024
49
+ MS_NODIRATIME = 2048
50
+ MS_BIND = 4096
51
+ MS_MOVE = 8192
52
+ MS_REC = 16384
53
+ MS_SILENT = 32768
54
+ MS_POSIXACL = 1 << 16
55
+ MS_UNBINDABLE = 1 << 17
56
+ MS_PRIVATE = 1 << 18
57
+ MS_SLAVE = 1 << 19
58
+ MS_SHARED = 1 << 20
59
+ MS_RELATIME = 1 << 21
60
+ MS_KERNMOUNT = 1 << 22
61
+ MS_I_VERSION = 1 << 23
62
+ MS_STRICTATIME = 1 << 24
63
+ MS_ACTIVE = 1 << 30
64
+ MS_NOUSER = 1 << 31
65
+
66
+ MNT_FORCE = 1
67
+ MNT_DETACH = 2
68
+ MNT_EXPIRE = 4
69
+ UMOUNT_NOFOLLOW = 8
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ffi'
4
+
5
+ module CzSystemInfo
6
+ class Filesystem
7
+ # A scoped module for internal FFI functions to be used by the main code.
8
+ module Functions
9
+ extend FFI::Library
10
+
11
+ ffi_lib FFI::Library::LIBC
12
+
13
+ def self.linux64?
14
+ if RUBY_PLATFORM == 'java'
15
+ RbConfig::CONFIG['host_os'] =~ /linux/i &&
16
+ ENV_JAVA['sun.arch.data.model'].to_i == 64
17
+ else
18
+ RbConfig::CONFIG['host_os'] =~ /linux/i &&
19
+ (RbConfig::CONFIG['arch'] =~ /64/ || RbConfig::CONFIG['DEFS'] =~ /64/)
20
+ end
21
+ end
22
+
23
+ def self.solaris?
24
+ RbConfig::CONFIG['host_os'] =~ /sunos|solaris/i
25
+ end
26
+
27
+ private_class_method :linux64?
28
+
29
+ if linux64? || solaris?
30
+ attach_function(:statvfs, :statvfs64, %i[string pointer], :int)
31
+ else
32
+ attach_function(:statvfs, %i[string pointer], :int)
33
+ end
34
+
35
+ attach_function(:strerror, [:int], :string)
36
+ attach_function(:mount_c, :mount, %i[string string string ulong string], :int)
37
+
38
+ begin
39
+ attach_function(:umount_c, :umount, [:string], :int)
40
+ rescue FFI::NotFoundError
41
+ if RbConfig::CONFIG['host_os'] =~ /darwin|osx|mach|bsd/i
42
+ attach_function(:umount_c, :unmount, [:string], :int)
43
+ end
44
+ end
45
+
46
+ private_class_method :statvfs, :strerror, :mount_c, :umount_c
47
+
48
+ begin
49
+ if RbConfig::CONFIG['host_os'] =~ /sunos|solaris/i
50
+ attach_function(:fopen, %i[string string], :pointer)
51
+ attach_function(:fclose, [:pointer], :int)
52
+ attach_function(:getmntent, %i[pointer pointer], :int)
53
+ private_class_method :fopen, :fclose, :getmntent
54
+ else
55
+ attach_function(:getmntent, [:pointer], :pointer)
56
+ attach_function(:setmntent, %i[string string], :pointer)
57
+ attach_function(:endmntent, [:pointer], :int)
58
+ attach_function(:umount2, %i[string int], :int)
59
+ private_class_method :getmntent, :setmntent, :endmntent, :umount2
60
+ end
61
+ rescue FFI::NotFoundError
62
+ if RbConfig::CONFIG['host_os'] =~ /darwin|osx|mach/i
63
+ begin
64
+ attach_function(:getmntinfo, :getmntinfo64, %i[pointer int], :int)
65
+ rescue FFI::NotFoundError
66
+ attach_function(:getmntinfo, %i[pointer int], :int) # Big Sur and later
67
+ end
68
+ else
69
+ attach_function(:getmntinfo, %i[pointer int], :int)
70
+ end
71
+ private_class_method :getmntinfo
72
+ end
73
+ end
74
+ end
75
+ end