linux_stat 0.6.3 → 0.6.4

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,16 +1,22 @@
1
1
  module LinuxStat
2
2
  module Kernel
3
3
  class << self
4
+ ##
4
5
  # Returns the Linux Kernel version.
6
+ #
5
7
  # If the information isn't available, it will return a frozen empty string.
8
+ #
6
9
  # The output is also cached (memoized) ; as changing the value in runtime is unexpected.
7
10
  def version
8
11
  return ''.freeze if string.empty?
9
12
  @@version ||= splitted[2]
10
13
  end
11
14
 
15
+ ##
12
16
  # Returns the name of the user who built the kernel using KBUILD_FLAGS.
17
+ #
13
18
  # If the information isn't available, it will return a frozen empty string.
19
+ #
14
20
  # The output is also cached (memoized) ; as changing the value in runtime is unexpected.
15
21
  def build_user
16
22
  @@build_user ||= string.split(/(\(.+\))/).each(&:strip!)
@@ -18,8 +24,11 @@ module LinuxStat
18
24
  .split[0].to_s[1..-2].to_s.freeze
19
25
  end
20
26
 
27
+ ##
21
28
  # Returns the compiler used to compile the Linux Kernel.
29
+ #
22
30
  # If the information isn't available, it will return a frozen empty string.
31
+ #
23
32
  # The output is also cached (memoized) ; as changing the value in runtime is unexpected.
24
33
  def compiler
25
34
  return ''.freeze if string.empty?
@@ -37,8 +46,11 @@ module LinuxStat
37
46
  end << compiler_version
38
47
  end
39
48
 
49
+ ##
40
50
  # Returns the compiler version used to compile the Linux Kernel.
51
+ #
41
52
  # If the information isn't available, it will return a frozen empty string.
53
+ #
42
54
  # The output is also cached (memoized) ; as changing the value in runtime is unexpected.
43
55
  def compiler_version
44
56
  @@compiler_version ||= string.split(/(\(.+?\))/).each(&:strip!)
@@ -46,20 +58,28 @@ module LinuxStat
46
58
  .find { |x| x[/[\d.]+/] }.to_s[/[\d.]+/].to_s.freeze
47
59
  end
48
60
 
61
+ ##
49
62
  # Returns the time when the kernel was compiled.
63
+ #
50
64
  # The return value is a Time object.
65
+ #
51
66
  # If the information isn't available, it will return nil
52
67
  #
53
68
  # The time will be searched in specific order.
54
- # It will match any date matching any of these formats:
69
+ #
70
+ # * It will match any date matching any of these formats:
55
71
  # 1. %b %d %H:%M:%S %z %Y
56
72
  # 2. %d %b %Y %H:%M:%S %z
57
73
  # 3. %Y-%m-%d
74
+ #
58
75
  # Most kernels have date in them in this format.
59
76
  #
60
77
  # Do note that Ruby sometimes fails to work with timezones like BST for example.
78
+ #
61
79
  # In such case, the timezone is unrealiable and often returns the local timezone.
80
+ #
62
81
  # You have to use regexp yourself to get the proper zone.
82
+ #
63
83
  # Use LinuxStat::Kernel.build_date_string to get the original string if you need that.
64
84
  #
65
85
  # The output is also cached (memoized) ; as changing the value in runtime is unexpected.
@@ -91,20 +111,29 @@ module LinuxStat
91
111
  end
92
112
  end
93
113
 
114
+ ##
94
115
  # Returns the time when the kernel was compiled.
116
+ #
95
117
  # The return value is a String.
118
+ #
96
119
  # If the information isn't available, it will return nil
97
120
  #
98
121
  # The time will be searched in specific order.
99
- # It will match any date matching any of these formats:
122
+ #
123
+ # * It will match any date matching any of these formats:
124
+ #
100
125
  # 1. %b %d %H:%M:%S %z %Y
101
126
  # 2. %d %b %Y %H:%M:%S %z
102
127
  # 3. %Y-%m-%d
128
+ #
103
129
  # Most kernels have date in them in this format.
104
130
  #
105
131
  # Do note that Ruby sometimes fails to work with timezones like BST for example.
132
+ #
106
133
  # In such case, the timezone is unrealiable and often returns the local timezone.
134
+ #
107
135
  # You have to use regexp yourself to get the proper zone.
136
+ #
108
137
  # Use LinuxStat::Kernel.build_date_string to get the original string if you need that.
109
138
  #
110
139
  # The output is also cached (memoized) ; as changing the value in runtime is unexpected.
@@ -133,14 +162,19 @@ module LinuxStat
133
162
  end
134
163
  end
135
164
 
165
+ ##
136
166
  # Reads maximum 1024 bytes from /proc/version and returns the string.
167
+ #
137
168
  # The output is also cached ; as changing the value in runtime is unexpected.
138
169
  def string
139
170
  @@string ||= File.readable?('/proc/version') ? IO.read('/proc/version', 1024).tap(&:strip!) : ''
140
171
  end
141
172
 
173
+ ##
142
174
  # Returns the sc_clk_tck or the same output from command `getconf CLK_TCK`.
175
+ #
143
176
  # Also, clk_tck is an alias of this method.
177
+ #
144
178
  # The output is also cached ; as changing the value in runtime is unexpected.
145
179
  def ticks
146
180
  @@tick ||= LinuxStat::Sysconf.sc_clk_tck
@@ -1,6 +1,7 @@
1
1
  module LinuxStat
2
2
  module Memory
3
3
  class << self
4
+ ##
4
5
  # Returns the memory details reported by /proc/meminfo. In this format:
5
6
  # {:total=>3836264, :used=>3097952, :available=>738312, :percent_used=>80.75, :percent_available=>19.25}
6
7
  #
@@ -20,6 +21,7 @@ module LinuxStat
20
21
  percent_available = total == 0 ? 0.0 : available.*(100).fdiv(total).round(2)
21
22
 
22
23
  # We have all the methods, but each methods reads the same file
24
+ #
23
25
  # So better to use the above calculation
24
26
  {
25
27
  total: total,
@@ -30,6 +32,7 @@ module LinuxStat
30
32
  }
31
33
  end
32
34
 
35
+ ##
33
36
  # Returns the total memory details reported by /proc/meminfo.
34
37
  # The value is in Kilobyte.
35
38
  #
@@ -39,6 +42,7 @@ module LinuxStat
39
42
  IO.foreach('/proc/meminfo').first.split[1].to_i
40
43
  end
41
44
 
45
+ ##
42
46
  # Returns the total memory details reported by /proc/meminfo.
43
47
  # The value is in Kilobyte.
44
48
  #
@@ -48,6 +52,7 @@ module LinuxStat
48
52
  IO.foreach('/proc/meminfo').first(3)[-1].split[1].to_i
49
53
  end
50
54
 
55
+ ##
51
56
  # Returns the amount of memory used reported by /proc/meminfo.
52
57
  # The value is in Kilobyte.
53
58
  #
@@ -58,6 +63,7 @@ module LinuxStat
58
63
  memory[0].split[1].to_i - memory[2].split[1].to_i
59
64
  end
60
65
 
66
+ ##
61
67
  # Returns the percentage of memory used reported by /proc/meminfo.
62
68
  #
63
69
  # It retuns an Integer but if the info is not available, it will return nil
@@ -68,6 +74,7 @@ module LinuxStat
68
74
  total.-(memory[2].split[1].to_i).*(100).fdiv(total).round(2)
69
75
  end
70
76
 
77
+ ##
71
78
  # Returns the percentage of memory used reported by /proc/meminfo.
72
79
  #
73
80
  # It retuns an Integer but if the info is not available, it will return nil
@@ -1,7 +1,9 @@
1
1
  module LinuxStat
2
2
  module Mounts
3
3
  class << self
4
+ ##
4
5
  # Reads /proc/mounts and returns the output splitted with \n.
6
+ #
5
7
  # In other words, it's same as running IO.readlines('/proc/mounts').each(&:strip!)
6
8
  #
7
9
  # It returns an Array.
@@ -10,6 +12,7 @@ module LinuxStat
10
12
  mounts
11
13
  end
12
14
 
15
+ ##
13
16
  # Reads /proc/mounts and returns list of devices.
14
17
  #
15
18
  # It returns an Array.
@@ -18,6 +21,7 @@ module LinuxStat
18
21
  mounts.map { |x| x.split(?\s.freeze).first }
19
22
  end
20
23
 
24
+ ##
21
25
  # Reads /proc/mounts and returns partition name of the device mounted at /.
22
26
  #
23
27
  # It returns a String.
@@ -26,6 +30,7 @@ module LinuxStat
26
30
  find_root[0].to_s
27
31
  end
28
32
 
33
+ ##
29
34
  # Reads /proc/mounts and returns the file system of the device mounted at /.
30
35
  #
31
36
  # It returns a String.
@@ -34,6 +39,7 @@ module LinuxStat
34
39
  find_root[2].to_s
35
40
  end
36
41
 
42
+ ##
37
43
  # Reads /proc/mounts and returns the options used for mounting /.
38
44
  #
39
45
  # It returns a String.
@@ -42,6 +48,7 @@ module LinuxStat
42
48
  find_root[3].to_s
43
49
  end
44
50
 
51
+ ##
45
52
  # Reads /proc/mounts and finds all tmpfs.
46
53
  #
47
54
  # It returns a Hash
@@ -54,13 +61,18 @@ module LinuxStat
54
61
  ret
55
62
  end
56
63
 
64
+ ##
57
65
  # mount_point(dev = root)
66
+ #
58
67
  # Where device = block device.
68
+ #
59
69
  # The default argument is the root block device.
60
70
  #
61
71
  # It helps you find the mountpoint of a block device.
62
72
  # For example:
73
+ #
63
74
  # LinuxStat::Mounts.mount_point('/dev/sdb1')
75
+ #
64
76
  # => "/run/media/sourav/5c2b7af7-d4c3-4ab4-a035-06d18ffc8e6f"
65
77
  #
66
78
  # The return type is String.
@@ -81,12 +93,15 @@ module LinuxStat
81
93
  m
82
94
  end
83
95
 
96
+ ##
84
97
  # list_devices_mount_point()
85
98
  #
86
99
  # It shows all the block devices corresponding to mount points.
87
100
  #
88
101
  # For example:
102
+ #
89
103
  # LinuxStat::Mounts.list_devices_mount_point
104
+ #
90
105
  # => {"proc"=>"/proc", "sys"=>"/sys", "dev"=>"/dev", "run"=>"/run", "/dev/sda2"=>"/", "securityfs"=>"/sys/kernel/security", "tmpfs"=>"/run/user/1000", "devpts"=>"/dev/pts", "cgroup2"=>"/sys/fs/cgroup/unified", "cgroup"=>"/sys/fs/cgroup/perf_event", "pstore"=>"/sys/fs/pstore", "none"=>"/sys/fs/bpf", "systemd-1"=>"/proc/sys/fs/binfmt_misc", "debugfs"=>"/sys/kernel/debug", "mqueue"=>"/dev/mqueue", "hugetlbfs"=>"/dev/hugepages", "tracefs"=>"/sys/kernel/tracing", "configfs"=>"/sys/kernel/config", "fusectl"=>"/sys/fs/fuse/connections", "gvfsd-fuse"=>"/run/user/1000/gvfs", "/dev/sdb1"=>"/run/media/sourav/5c2b7af7-d4c3-4ab4-a035-06d18ffc8e6f", "binfmt_misc"=>"/proc/sys/fs/binfmt_misc"}
91
106
  #
92
107
  # The return type is Hash.
@@ -104,13 +119,17 @@ module LinuxStat
104
119
  m
105
120
  end
106
121
 
122
+ ##
107
123
  # devices_stat
124
+ #
108
125
  # [ Not to confuse this method with device_stat(dev) which shows only one device's info ]
109
126
  #
110
127
  # It shows all the block devices corresponding to mount points and data from LinuxStat::FS.stat(arg)
111
128
  #
112
129
  # For example:
130
+ #
113
131
  # LinuxStat::Mounts.devices_stat
132
+ #
114
133
  # => {"proc"=>{:mountpoint=>"/proc", :total=>0, :free=>0, :available=>0, :used=>0, :percent_used=>NaN, :percent_free=>NaN, :percent_available=>NaN}, "/dev/sdb1"=>{:mountpoint=>"/run/media/sourav/5c2b7af7-d4c3-4ab4-a035-06d18ffc8e6f", :total=>31466008576, :free=>2693931008, :available=>2693931008, :used=>28772077568, :percent_used=>91.44, :percent_free=>8.56, :percent_available=>8.56}}
115
134
  #
116
135
  # The return type is Hash.
@@ -142,12 +161,17 @@ module LinuxStat
142
161
  m
143
162
  end
144
163
 
164
+ ##
145
165
  # device_stat(dev = root)
166
+ #
146
167
  # [ Not to confuse this method with devices_stat() which shows all devices ]
168
+ #
147
169
  # It shows all the block devices corresponding to mount points and data from LinuxStat::FS.stat(arg)
148
170
  #
149
171
  # For example:
172
+ #
150
173
  # LinuxStat::Mounts.device_stat('/dev/sda2')
174
+ #
151
175
  # => {"/dev/sda2"=>{:mountpoint=>"/", :total=>119981191168, :free=>35298562048, :available=>35298562048, :used=>84682629120, :percent_used=>70.58, :percent_free=>29.42, :percent_available=>29.42}}
152
176
  #
153
177
  # The return type is Hash.
@@ -2,7 +2,10 @@ module LinuxStat
2
2
  module Net
3
3
  class << self
4
4
  DEV = '/proc/net/dev'.freeze
5
+
6
+ ##
5
7
  # Returns the local IP address of the system as a String.
8
+ #
6
9
  # If the information isn't available, it will a frozen empty string.
7
10
  def ipv4_private
8
11
  require 'socket' unless defined?(Socket)
@@ -10,7 +13,9 @@ module LinuxStat
10
13
  ip ? ip.ip? ? ip.ip_unpack[0].freeze : ''.freeze : ''.freeze
11
14
  end
12
15
 
16
+ ##
13
17
  # Returns the total bytes received and transmitted as Hash.
18
+ #
14
19
  # For example:
15
20
  # {:received=>56602867, :transmitted=>6940922}
16
21
  #
@@ -29,6 +34,7 @@ module LinuxStat
29
34
  }
30
35
  end
31
36
 
37
+ ##
32
38
  # Returns the total bytes received as Integer.
33
39
  #
34
40
  # But if the status isn't available it will return nil.
@@ -41,6 +47,7 @@ module LinuxStat
41
47
  data.map { |x| x.split[index].to_i }.sum
42
48
  end
43
49
 
50
+ ##
44
51
  # Returns the total bytes transmitted as Integer.
45
52
  #
46
53
  # But if the status isn't available it will return nil.
@@ -53,7 +60,9 @@ module LinuxStat
53
60
  data.map { |x| x.split[index].to_i }.sum
54
61
  end
55
62
 
63
+ ##
56
64
  # usage(interval = 0.1)
65
+ #
57
66
  # Where interval is the time between polling in seconds. The default is 0.1 seconds.
58
67
  #
59
68
  # The return type is a Hash, containg the current internet usage (received, transmit) in B/s.
@@ -62,8 +71,11 @@ module LinuxStat
62
71
  # {:received=>436060.0, :transmitted=>50350.0}
63
72
  #
64
73
  # If the system transmits 100 kb in the interval,
74
+ #
65
75
  # this method will return 1000 kb/s. That is, it estimates
76
+ #
66
77
  # the data it will transmit in one second. Thus, a good and reliable interval is 1 second
78
+ #
67
79
  # It will return an empty Hash if the info (/proc/net/dev) isn't available.
68
80
  def usage(interval = 0.1)
69
81
  return {} unless File.readable?(DEV)
@@ -1,6 +1,7 @@
1
1
  module LinuxStat
2
2
  module OS
3
3
  class << self
4
+ ##
4
5
  # Reads /etc/os-release and returns a Hash. For example:
5
6
  # {: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
  #
@@ -8,13 +9,15 @@ module LinuxStat
8
9
  #
9
10
  # The amount of data read is 4096 bytes. Any more than that will result in truncated output.
10
11
  #
11
- # The information is also cached, and once loaded, won't change in runtime. Because changing the /etc/lsb-release
12
+ # The information is also cached, and once loaded, won't change in runtime.
13
+ # Because changing the /etc/lsb-release
12
14
  # isn't expected in runtime.
13
15
  def os_release
14
16
  # cached (memoized) ; as changing the value in runtime is unexpected
15
17
  @@os_release ||= File.readable?('/etc/os-release') ? release('/etc/os-release') : {}
16
18
  end
17
19
 
20
+ ##
18
21
  # Reads /etc/lsb-release and returns a Hash. For example:
19
22
  # {:LSB_VERSION=>"1.4", :DISTRIB_ID=>"Arch", :DISTRIB_RELEASE=>"rolling", :DISTRIB_DESCRIPTION=>"Arch Linux"}
20
23
  #
@@ -22,14 +25,16 @@ module LinuxStat
22
25
  #
23
26
  # The amount of data read is 4096 bytes. Any more than that will result in truncated output.
24
27
  #
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.
28
+ # The information is also cached, and once loaded, won't change in runtime.
29
+ # Because changing the /etc/lsb-release isn't expected in runtime.
27
30
  def lsb_release
28
31
  # cached (memoized) ; as changing the value in runtime is unexpected
29
32
  @@lsb_release ||= File.readable?('/etc/lsb-release') ? release('/etc/lsb-release') : {}
30
33
  end
31
34
 
35
+ ##
32
36
  # Reads /etc/lsb-release or /etc/os-release tries to get information about the distribution.
37
+ #
33
38
  # If the information isn't available, it will read and return /etc/issue.
34
39
  #
35
40
  # The return type is String.
@@ -52,6 +57,7 @@ module LinuxStat
52
57
  end
53
58
  end
54
59
 
60
+ ##
55
61
  # Uses utsname.h to determine the machine
56
62
  #
57
63
  # It returns a String but if the info isn't available, it will return an empty String
@@ -59,6 +65,7 @@ module LinuxStat
59
65
  @@machine ||= LinuxStat::Uname.machine
60
66
  end
61
67
 
68
+ ##
62
69
  # Uses utsname.h to determine the system nodename
63
70
  #
64
71
  # It returns String but if the info isn't available, it will return an empty String
@@ -66,6 +73,7 @@ module LinuxStat
66
73
  @@nodename ||= LinuxStat::Uname.nodename
67
74
  end
68
75
 
76
+ ##
69
77
  # Reads /etc/hostname and returns the hostname.
70
78
  #
71
79
  # The return type is String.
@@ -78,8 +86,11 @@ module LinuxStat
78
86
  end
79
87
  end
80
88
 
89
+ ##
81
90
  # Reads ruby configuration and tries to guess if the system is 64 bit.
91
+ #
82
92
  # If it fails then it runs utsname.h to guess the machine.
93
+ #
83
94
  # It the machine is 64 bits, it will return 64, else it returns 32.
84
95
  #
85
96
  # The return type is strictly Integer and doesn't fail.
@@ -91,6 +102,7 @@ module LinuxStat
91
102
  end
92
103
  end
93
104
 
105
+ ##
94
106
  # Reads /proc/uptime and returns the system uptime:
95
107
  # {:hour=>10, :minute=>34, :second=>12.59}
96
108
  #
@@ -1,12 +1,21 @@
1
1
  module LinuxStat
2
2
  module PrettifyBytes
3
3
  class << self
4
+ ##
4
5
  # Converts a number to decimal byte units and outputs with the metric prefix
5
6
  # For example,
6
7
  #
7
- # LinuxStat::PrettifyBytes.convert_decimal(1000) # => "1.0 kilobyte"
8
- # LinuxStat::PrettifyBytes.convert_decimal(1000 ** 3) # => "1.0 gigabyte"
9
- # LinuxStat::PrettifyBytes.convert_decimal(1024 ** 3) # => "1.07 gigabytes"
8
+ # LinuxStat::PrettifyBytes.convert_decimal(1000)
9
+ #
10
+ # => "1.0 kilobyte"
11
+ #
12
+ # LinuxStat::PrettifyBytes.convert_decimal(1000 ** 3)
13
+ #
14
+ # => "1.0 gigabyte"
15
+ #
16
+ # LinuxStat::PrettifyBytes.convert_decimal(1024 ** 3)
17
+ #
18
+ # => "1.07 gigabytes"
10
19
  def convert_decimal(n)
11
20
  @@d_units ||= %W(#{''} kilo mega giga tera peta exa zetta)
12
21
  .map.with_index { |x, i| [x, 1000.**(i + 1)] }
@@ -19,9 +28,17 @@ module LinuxStat
19
28
  # Converts a number to binary byte units and outputs with the IEC prefix
20
29
  # For example,
21
30
  #
22
- # LinuxStat::PrettifyBytes.convert_binary(1000) # => "1000.0 bytes"
23
- # LinuxStat::PrettifyBytes.convert_binary(1000 ** 3) # => "953.67 mebibytes"
24
- # LinuxStat::PrettifyBytes.convert_binary(1024 ** 3) # => "1.0 gibibyte"
31
+ # LinuxStat::PrettifyBytes.convert_binary(1000)
32
+ #
33
+ # => "1000.0 bytes"
34
+ #
35
+ # LinuxStat::PrettifyBytes.convert_binary(1000 ** 3)
36
+ #
37
+ # => "953.67 mebibytes"
38
+ #
39
+ # LinuxStat::PrettifyBytes.convert_binary(1024 ** 3)
40
+ #
41
+ # => "1.0 gibibyte"
25
42
  def convert_binary(n)
26
43
  @@b_units ||= %W(#{''} kibi mebi gibi tebi pebi exbi zebi)
27
44
  .map.with_index { |x, i| [x, 1024.**(i + 1)] }
@@ -34,9 +51,17 @@ module LinuxStat
34
51
  # Converts a number to decimal byte units
35
52
  # For example,
36
53
  #
37
- # LinuxStat::PrettifyBytes.convert_short_decimal(1000) # => "1.0 kB"
38
- # LinuxStat::PrettifyBytes.convert_short_decimal(1000 ** 3) # => "1.0 GB"
39
- # LinuxStat::PrettifyBytes.convert_short_decimal(1024 ** 3) # => "1.07 GB"
54
+ # LinuxStat::PrettifyBytes.convert_short_decimal(1000)
55
+ #
56
+ # => "1.0 kB"
57
+ #
58
+ # LinuxStat::PrettifyBytes.convert_short_decimal(1000 ** 3)
59
+ #
60
+ # => "1.0 GB"
61
+ #
62
+ # LinuxStat::PrettifyBytes.convert_short_decimal(1024 ** 3)
63
+ #
64
+ # => "1.07 GB"
40
65
  def convert_short_decimal(n)
41
66
  @@sd_units ||= %W(#{''} k M G T P E Z)
42
67
  .map.with_index { |x, i| [x, 1000.**(i + 1)] }
@@ -46,12 +71,22 @@ module LinuxStat
46
71
  "#{pad_left(converted)} #{unit[0]}B"
47
72
  end
48
73
 
74
+ ##
49
75
  # Converts a number to binary byte units
76
+ #
50
77
  # For example,
51
78
  #
52
- # LinuxStat::PrettifyBytes.convert_short_binary(1000) # => "1000 B"
53
- # LinuxStat::PrettifyBytes.convert_short_binary(1000 ** 3) # => "953.67 MiB"
54
- # LinuxStat::PrettifyBytes.convert_short_binary(1024 ** 3) # => "1.0 GiB"
79
+ # LinuxStat::PrettifyBytes.convert_short_binary(1000)
80
+ #
81
+ # => "1000 B"
82
+ #
83
+ # LinuxStat::PrettifyBytes.convert_short_binary(1000 ** 3)
84
+ #
85
+ # => "953.67 MiB"
86
+ #
87
+ # LinuxStat::PrettifyBytes.convert_short_binary(1024 ** 3)
88
+ #
89
+ # => "1.0 GiB"
55
90
  def convert_short_binary(n)
56
91
  return "#{n} B" if n < 1024
57
92