linux_stat 0.1.4 → 0.3.1

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.
@@ -1,6 +1,11 @@
1
1
  module LinuxStat
2
2
  module Memory
3
3
  class << self
4
+ # Returns the memory details reported by /proc/meminfo. In this format:
5
+ # {:total=>3836264, :used=>3097952, :available=>738312, :percent_used=>80.75, :percent_available=>19.25}
6
+ #
7
+ # The value is in Kilobyte.
8
+ # If the statistics is not available, it will return an empty Hash.
4
9
  def stat
5
10
  return {} unless meminfo?
6
11
 
@@ -24,31 +29,44 @@ module LinuxStat
24
29
  }
25
30
  end
26
31
 
32
+ # Returns the total memory details reported by /proc/meminfo.
33
+ # The value is in Kilobyte.
34
+ # It retuns an Integer but if the info is not available, it will return nil.
27
35
  def total
28
- return 0 unless meminfo?
36
+ return nil unless meminfo?
29
37
  IO.foreach('/proc/meminfo').first.split[1].to_i
30
38
  end
31
39
 
40
+ # Returns the total memory details reported by /proc/meminfo.
41
+ # The value is in Kilobyte.
42
+ # It retuns an Integer but if the info is not available, it will return nil
32
43
  def available
33
- return 0 unless meminfo?
44
+ return nil unless meminfo?
34
45
  IO.foreach('/proc/meminfo').first(3)[-1].split[1].to_i
35
46
  end
36
47
 
48
+ # Returns the amount of memory used reported by /proc/meminfo.
49
+ # The value is in Kilobyte.
50
+ # It retuns an Integer but if the info is not available, it will return nil.
37
51
  def used
38
- return 0 unless meminfo?
52
+ return nil unless meminfo?
39
53
  memory = IO.foreach('/proc/meminfo').first(3)
40
54
  memory[0].split[1].to_i - memory[2].split[1].to_i
41
55
  end
42
56
 
57
+ # Returns the percentage of memory used reported by /proc/meminfo.
58
+ # It retuns an Integer but if the info is not available, it will return nil
43
59
  def percent_used
44
- return 0.0 unless meminfo?
60
+ return nil unless meminfo?
45
61
  memory = IO.foreach('/proc/meminfo').first(3)
46
62
  total = memory[0].split[1].to_i
47
63
  total.-(memory[2].split[1].to_i).*(100).fdiv(total).round(2)
48
64
  end
49
65
 
66
+ # Returns the percentage of memory used reported by /proc/meminfo.
67
+ # It retuns an Integer but if the info is not available, it will return nil
50
68
  def percent_available
51
- return 0.0 unless meminfo?
69
+ return nil unless meminfo?
52
70
  memory = IO.foreach('/proc/meminfo').first(3)
53
71
  memory[2].split[1].to_i.*(100).fdiv(memory[0].split[1].to_i).round(2)
54
72
  end
@@ -0,0 +1,64 @@
1
+ module LinuxStat
2
+ module Mounts
3
+ class << self
4
+ # Reads /proc/mounts and returns list of devices.
5
+ #
6
+ # It returns an Array.
7
+ # If the info isn't available or /proc/mounts is not readable, it will return an empty Array.
8
+ def list
9
+ mounts
10
+ end
11
+
12
+ # Reads /proc/mounts and returns partition name of the device mounted at /.
13
+ #
14
+ # It returns a String.
15
+ # But if the info isn't available or /proc/mounts is not readable, it will return an empty frozen String.
16
+ def root
17
+ find_root[0].to_s
18
+ end
19
+
20
+ # Reads /proc/mounts and returns the file system of the device mounted at /.
21
+ #
22
+ # It returns a String.
23
+ # But if the info isn't available or /proc/mounts is not readable, it will return an empty frozen String.
24
+ def root_fs
25
+ find_root[2].to_s
26
+ end
27
+
28
+ # Reads /proc/mounts and returns the options used for mounting /.
29
+ #
30
+ # It returns a String.
31
+ # But if the info isn't available or /proc/mounts is not readable, it will return an empty frozen string.
32
+ def root_mount_options
33
+ find_root[3].to_s
34
+ end
35
+
36
+ # Reads /proc/mounts and finds all tmpfs.
37
+ #
38
+ # It returns a Hash
39
+ # But if the info isn't available or /proc/mounts is not readable, it will return an empty Hash.
40
+ def tmpfs
41
+ ret = {}
42
+ mounts.each { |x|
43
+ ret.merge!({x.split[1] => x}) if x.start_with?('tmpfs '.freeze)
44
+ }
45
+ ret
46
+ end
47
+
48
+ private
49
+ def mount_readable?
50
+ @@mount_readable ||= File.readable?('/proc/mounts')
51
+ end
52
+
53
+ def mounts
54
+ return [] unless mount_readable?
55
+ IO.readlines('/proc/mounts').each(&:strip!)
56
+ end
57
+
58
+ def find_root
59
+ return [] unless mount_readable?
60
+ @@root ||= IO.foreach('/proc/mounts').find { |x| x.split[1] == '/'.freeze }.split
61
+ end
62
+ end
63
+ end
64
+ end
@@ -1,10 +1,12 @@
1
1
  module LinuxStat
2
2
  module Net
3
3
  class << self
4
+ # Returns the local IP address of the system as a String.
5
+ # If the information isn't available, it will a frozen empty string.
4
6
  def ipv4_private
5
7
  require 'socket'
6
8
  ip = Socket.ip_address_list.find(&:ipv4_private?)
7
- ip ? ip.ip? ? ip.ip_unpack[0].freeze : '' : ''
9
+ ip ? ip.ip? ? ip.ip_unpack[0].freeze : ''.freeze : ''.freeze
8
10
  end
9
11
  end
10
12
  end
@@ -1,16 +1,38 @@
1
1
  module LinuxStat
2
2
  module OS
3
3
  class << self
4
+ # Reads /etc/os-release and returns a Hash. For example:
5
+ # {:NAME=>"Arch Linux", :PRETTY_NAME=>"Arch Linux", :ID=>"arch", :BUILD_ID=>"rolling", :ANSI_COLOR=>"38;2;23;147;209", :HOME_URL=>"https://www.archlinux.org/", :DOCUMENTATION_URL=>"https://wiki.archlinux.org/", :SUPPORT_URL=>"https://bbs.archlinux.org/", :BUG_REPORT_URL=>"https://bugs.archlinux.org/", :LOGO=>"archlinux"}
6
+ #
7
+ # If the info isn't available, it will return an empty Hash.
8
+ #
9
+ # The amount of data read is 4096 bytes. Any more than that will result in truncated output.
10
+ #
11
+ # The information is also cached, and once loaded, won't change in runtime. Because changing the /etc/lsb-release
12
+ # isn't expected in runtime.
4
13
  def os_release
5
14
  # Cached ; as changing the value in runtime is unexpected
6
15
  @@os_release ||= File.readable?('/etc/os-release') ? release('/etc/os-release') : {}
7
16
  end
8
17
 
18
+ # Reads /etc/lsb-release and returns a Hash. For example:
19
+ # {:LSB_VERSION=>"1.4", :DISTRIB_ID=>"Arch", :DISTRIB_RELEASE=>"rolling", :DISTRIB_DESCRIPTION=>"Arch Linux"}
20
+ #
21
+ # If the info isn't available, it will return an empty Hash.
22
+ #
23
+ # The amount of data read is 4096 bytes. Any more than that will result in truncated output.
24
+ #
25
+ # The information is also cached, and once loaded, won't change in runtime. Because changing the /etc/lsb-release
26
+ # isn't expected in runtime.
9
27
  def lsb_release
10
28
  # Cached ; as changing the value in runtime is unexpected
11
29
  @@lsb_release ||= File.readable?('/etc/lsb-release') ? release('/etc/lsb-release') : {}
12
30
  end
13
31
 
32
+ # Reads /etc/lsb-release or /etc/os-release tries to get information about the distribution.
33
+ # If the information isn't available, it will read and return /etc/issue.
34
+ # The return type is String.
35
+ # If none of the info is available, it will return an empty frozen String.
14
36
  def distribution
15
37
  @@distribution ||= if os_release.key?(:NAME)
16
38
  os_release[:NAME]
@@ -29,6 +51,9 @@ module LinuxStat
29
51
  end
30
52
  end
31
53
 
54
+ # Reads /etc/hostname and returns the hostname.
55
+ # The return type is String.
56
+ # If the info info isn't available, it will return 'localhost'.
32
57
  def hostname
33
58
  @@hostname ||= if File.exist?('/etc/hostname')
34
59
  IO.read('/etc/hostname').strip
@@ -37,6 +62,8 @@ module LinuxStat
37
62
  end
38
63
  end
39
64
 
65
+ # Reads ruby configuration and tries to guess if the system is 32 bit or 64 bit.
66
+ # The return type is Integer.
40
67
  def bits
41
68
  @@bits ||= if RbConfig::CONFIG['host_cpu'].end_with?('64') || RUBY_PLATFORM[/x86_64/]
42
69
  64
@@ -45,8 +72,12 @@ module LinuxStat
45
72
  end
46
73
  end
47
74
 
75
+ # Reads /proc/uptime and returns the system uptime:
76
+ # {:hour=>10, :minute=>34, :second=>12.59}
77
+ #
78
+ # If the stat isn't available, an empty hash is returned.
48
79
  def uptime
49
- raise StatUnavailable, 'Cannot read /proc/uptime' unless uptime_readable?
80
+ return {} unless uptime_readable?
50
81
 
51
82
  uptime = IO.read('/proc/uptime').to_f
52
83
  uptime_i = uptime.to_i
@@ -64,7 +95,7 @@ module LinuxStat
64
95
 
65
96
  private
66
97
  def release(file)
67
- IO.readlines(file, 3000).reduce({}) { |h, x|
98
+ IO.readlines(file, 4096).reduce({}) { |h, x|
68
99
  x.strip!
69
100
  next h if x.empty?
70
101
 
@@ -1,6 +1,8 @@
1
1
  module LinuxStat
2
2
  module Process
3
3
  class << self
4
+ # Returns the list of processes from /proc/.
5
+ # The return type is an Array of Integers.
4
6
  def list
5
7
  Dir['/proc/*'].select! { |x|
6
8
  pid = File.split(x)[1]
@@ -8,10 +10,13 @@ module LinuxStat
8
10
  }.map! { |x| File.split(x)[-1].to_i }
9
11
  end
10
12
 
13
+ # Counts and returns the total number of process running on the system.
14
+ # The return type is Integer.
11
15
  def count
12
16
  list.count
13
17
  end
14
18
 
19
+ # Returns all the id of processes mapped with their names as a Hash.
15
20
  def names
16
21
  list.reduce({}) { |h, x|
17
22
  begin
@@ -22,6 +27,7 @@ module LinuxStat
22
27
  }
23
28
  end
24
29
 
30
+ # Returns all the id of processes mapped with their status as a Hash.
25
31
  def types
26
32
  list.reduce({}) { |h, x|
27
33
  begin
@@ -40,6 +46,8 @@ module LinuxStat
40
46
  }
41
47
  end
42
48
 
49
+ # Returns all the id of processes that are sleeping.
50
+ # The return type is an Array of Integers.
43
51
  def sleeping
44
52
  list.select { |x|
45
53
  begin
@@ -50,6 +58,8 @@ module LinuxStat
50
58
  }
51
59
  end
52
60
 
61
+ # Returns all the id of processes that are idle.
62
+ # The return type is an Array of Integers.
53
63
  def idle
54
64
  list.select { |x|
55
65
  begin
@@ -60,6 +70,8 @@ module LinuxStat
60
70
  }
61
71
  end
62
72
 
73
+ # Returns all the id of processes that are zombies.
74
+ # The return type is an Array of Integers.
63
75
  def zombie
64
76
  list.select { |x|
65
77
  begin
@@ -70,6 +82,8 @@ module LinuxStat
70
82
  }
71
83
  end
72
84
 
85
+ # Returns all the id of processes that are running.
86
+ # The return type is an Array of Integers.
73
87
  def running
74
88
  list.select { |x|
75
89
  begin
@@ -1,8 +1,11 @@
1
1
  module LinuxStat
2
2
  module Swap
3
3
  class << self
4
- # List all swap devices
4
+ # List all swap devices and returns a Hash.
5
+ # If the info isn't available, it will return an empty Hash.
5
6
  def list
7
+ return {} unless swaps_readable?
8
+
6
9
  file = IO.readlines('/proc/swaps').drop(1)
7
10
  file.reduce({}) do |h, x|
8
11
  name, *stats = x.strip.split
@@ -10,8 +13,18 @@ module LinuxStat
10
13
  end
11
14
  end
12
15
 
13
- # Show aggregated used and available swap
16
+ # Returns true if any swap device is available, else returns false.
17
+ # If the info isn't available, it will return an empty Hash.
18
+ def any?
19
+ !!IO.foreach('/proc/swaps').drop(1).first
20
+ end
21
+
22
+ # Show aggregated used and available swap.
23
+ # The values are in kilobytes.
24
+ # The return type is Hash.
25
+ # If the info isn't available, the return type is an empty Hash.
14
26
  def stat
27
+ return {} unless swaps_readable?
15
28
  values_t = read_usage
16
29
 
17
30
  total, used = values_t[0].sum, values_t[-1].sum
@@ -29,20 +42,35 @@ module LinuxStat
29
42
  }
30
43
  end
31
44
 
45
+ # Show total amount of swap.
46
+ # The value is in kilobytes.
47
+ # The return type is a Integer but if the info isn't available, it will return nil.
32
48
  def total
49
+ return nil unless swaps_readable?
33
50
  read_usage[0].sum
34
51
  end
35
52
 
53
+ # Show total amount of available swap.
54
+ # The value is in kilobytes.
55
+ # The return type is a Integer but if the info isn't available, it will return nil.
36
56
  def available
57
+ return nil unless swaps_readable?
37
58
  values_t = read_usage
38
59
  values_t[0].sum - values_t[1].sum
39
60
  end
40
61
 
62
+ # Show total amount of used swap.
63
+ # The value is in kilobytes.
64
+ # The return type is a Integer but if the info isn't available, it will return nil.
41
65
  def used
66
+ return nil unless swaps_readable?
42
67
  read_usage[-1].sum
43
68
  end
44
69
 
70
+ # Show percentage of swap used.
71
+ # The return type is a Float but if the info isn't available, it will return nil.
45
72
  def percent_used
73
+ return nil unless swaps_readable?
46
74
  values_t = read_usage
47
75
 
48
76
  total = values_t[0].sum
@@ -51,21 +79,26 @@ module LinuxStat
51
79
  values_t[-1].sum.*(100).fdiv(total).round(2)
52
80
  end
53
81
 
82
+ # Show percentage of swap available.
83
+ # The return type is a Float but if the info isn't available, it will return nil.
54
84
  def percent_available
85
+ return nil unless swaps_readable?
55
86
  values_t = read_usage
56
87
 
57
88
  total = values_t[0].sum
58
89
  return 0.0 if total == 0
59
90
 
60
91
  total.-(values_t[-1].sum).*(100).fdiv(total).round(2)
61
-
62
92
  end
63
93
 
64
94
  private
65
95
  def read_usage
66
96
  return [[], []] unless swaps_readable?
67
97
 
68
- IO.readlines('/proc/swaps').drop(1).map { |x|
98
+ val = IO.readlines('/proc/swaps').drop(1)
99
+ return [[], []] if val.empty?
100
+
101
+ val.map { |x|
69
102
  x.strip.split.values_at(2, 3).map!(&:to_i)
70
103
  }.transpose
71
104
  end
@@ -1,3 +1,3 @@
1
1
  module LinuxStat
2
- VERSION = "0.1.4"
2
+ VERSION = "0.3.1"
3
3
  end
metadata CHANGED
@@ -1,46 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: linux_stat
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sourav Goswami
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-11-23 00:00:00.000000000 Z
11
+ date: 2020-12-01 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Efficient linux system reporting gem. Linux Only | Efficient | Reliable
14
14
  email:
15
15
  - souravgoswami@protonmail.com
16
16
  executables: []
17
- extensions: []
18
- extra_rdoc_files: []
17
+ extensions:
18
+ - ext/fs_stat/extconf.rb
19
+ extra_rdoc_files:
20
+ - README.md
19
21
  files:
20
- - ".gitignore"
21
- - ".rspec"
22
- - ".travis.yml"
23
- - CODE_OF_CONDUCT.md
24
- - Gemfile
25
- - Gemfile.lock
26
- - LICENSE.txt
27
22
  - README.md
28
- - Rakefile
29
23
  - bin/console
24
+ - bin/linuxstat.rb
30
25
  - bin/setup
26
+ - ext/fs_stat/extconf.rb
27
+ - ext/fs_stat/fs_stat.c
31
28
  - lib/linux_stat.rb
32
29
  - lib/linux_stat/battery.rb
33
30
  - lib/linux_stat/bios.rb
34
31
  - lib/linux_stat/cpu.rb
32
+ - lib/linux_stat/filesystem.rb
35
33
  - lib/linux_stat/kernel.rb
36
34
  - lib/linux_stat/memory.rb
35
+ - lib/linux_stat/mounts.rb
37
36
  - lib/linux_stat/net.rb
38
37
  - lib/linux_stat/os.rb
39
38
  - lib/linux_stat/process.rb
40
39
  - lib/linux_stat/swap.rb
41
40
  - lib/linux_stat/version.rb
42
- - linux_stat.gemspec
43
- - run_all_methods.rb
44
41
  homepage: https://github.com/Souravgoswami/linux_stat/
45
42
  licenses:
46
43
  - MIT
@@ -63,7 +60,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
63
60
  - !ruby/object:Gem::Version
64
61
  version: '0'
65
62
  requirements: []
66
- rubygems_version: 3.1.2
63
+ rubygems_version: 3.1.4
67
64
  signing_key:
68
65
  specification_version: 4
69
66
  summary: Efficient linux system reporting gem