linux_stat 0.6.1 → 0.7.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.
@@ -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.
@@ -1,13 +1,131 @@
1
1
  module LinuxStat
2
2
  module Net
3
3
  class << self
4
+ DEV = '/proc/net/dev'.freeze
5
+
6
+ ##
4
7
  # Returns the local IP address of the system as a String.
8
+ #
5
9
  # If the information isn't available, it will a frozen empty string.
6
10
  def ipv4_private
7
- require 'socket'
11
+ require 'socket' unless defined?(Socket)
8
12
  ip = Socket.ip_address_list.find(&:ipv4_private?)
9
13
  ip ? ip.ip? ? ip.ip_unpack[0].freeze : ''.freeze : ''.freeze
10
14
  end
15
+
16
+ ##
17
+ # Returns the total bytes received and transmitted as Hash.
18
+ #
19
+ # For example:
20
+ # {:received=>56602867, :transmitted=>6940922}
21
+ #
22
+ # But if the status isn't available it will return an empty Hash.
23
+ def total_bytes
24
+ return {} unless File.readable?(DEV)
25
+
26
+ data = IO.readlines(DEV).drop(2)
27
+ indices = find_index_of_bytes
28
+ data.reject! { |x| x.strip.start_with?('lo:') }
29
+ r, t = data.map { |x| x.split.values_at(*indices).map(&:to_i) }.transpose.map(&:sum)
30
+
31
+ {
32
+ received: r,
33
+ transmitted: t
34
+ }
35
+ end
36
+
37
+ ##
38
+ # Returns the total bytes received as Integer.
39
+ #
40
+ # But if the status isn't available it will return nil.
41
+ def total_bytes_received
42
+ return nil unless File.readable?(DEV)
43
+
44
+ data = IO.readlines(DEV).drop(2)
45
+ index = find_index_of_bytes[0]
46
+ data.reject! { |x| x.strip.start_with?('lo:') }
47
+ data.map { |x| x.split[index].to_i }.sum
48
+ end
49
+
50
+ ##
51
+ # Returns the total bytes transmitted as Integer.
52
+ #
53
+ # But if the status isn't available it will return nil.
54
+ def total_bytes_transmitted
55
+ return nil unless File.readable?(DEV)
56
+
57
+ data = IO.readlines(DEV).drop(2)
58
+ index = find_index_of_bytes[-1]
59
+ data.reject! { |x| x.strip.start_with?('lo:') }
60
+ data.map { |x| x.split[index].to_i }.sum
61
+ end
62
+
63
+ ##
64
+ # usage(interval = 0.1)
65
+ #
66
+ # Where interval is the time between polling in seconds. The default is 0.1 seconds.
67
+ #
68
+ # The return type is a Hash, containg the current internet usage (received, transmit) in B/s.
69
+ #
70
+ # For example:
71
+ # {:received=>436060.0, :transmitted=>50350.0}
72
+ #
73
+ # If the system transmits 100 kb in the interval,
74
+ #
75
+ # this method will return 1000 kb/s. That is, it estimates
76
+ #
77
+ # the data it will transmit in one second. Thus, a good and reliable interval is 1 second
78
+ #
79
+ # It will return an empty Hash if the info (/proc/net/dev) isn't available.
80
+ def usage(interval = 0.1)
81
+ return {} unless File.readable?(DEV)
82
+
83
+ data = IO.readlines(DEV).drop(2)
84
+ indices = find_index_of_bytes
85
+ data.reject! { |x| x.strip.start_with?('lo:') }
86
+ r, t = data.map { |x| x.split.values_at(*indices).map(&:to_i) }.transpose.map(&:sum)
87
+
88
+ sleep(interval)
89
+
90
+ data2 = IO.readlines(DEV).drop(2)
91
+ data2.reject! { |x| x.strip.start_with?('lo:') }
92
+ r2, t2 = data2.map { |x| x.split.values_at(*indices).map(&:to_i) }.transpose.map(&:sum)
93
+
94
+ # Measure the difference
95
+ dr, dt = r2.-(r).fdiv(interval), t2.-(t).fdiv(interval)
96
+
97
+ {
98
+ received: dr,
99
+ transmitted: dt
100
+ }
101
+ end
102
+
103
+ alias current_usage usage
104
+
105
+ private
106
+ # Returns the index containing the received and transmitted bytes
107
+ def find_index_of_bytes
108
+ @@index_of_bytes ||= nil
109
+
110
+ unless @@index_of_bytes
111
+ data = IO.foreach(DEV)
112
+
113
+ r, h = data.next.split, {}
114
+
115
+ r.each_with_index { |x, i|
116
+ downcased = x.downcase
117
+ h.merge!(:r => i) if downcased.start_with?('receive')
118
+ h.merge!(:t => i) if downcased.start_with?('transmit')
119
+ }
120
+
121
+ data_0 = data.next.gsub(?|.freeze, ' %'.freeze)
122
+ @@index_of_bytes = []
123
+ data_0.split.each_with_index { |x, i| @@index_of_bytes << i if x == '%bytes'.freeze }
124
+ h[:r] > h[:t] ? @@index_of_bytes.reverse : @@index_of_bytes
125
+ else
126
+ @@index_of_bytes
127
+ end
128
+ end
11
129
  end
12
130
  end
13
131
  end
@@ -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
  #
@@ -123,8 +135,8 @@ module LinuxStat
123
135
  key = splitted[0].to_s.strip
124
136
  value = splitted[1..-1].join(?=).to_s.strip
125
137
 
126
- value[0] = '' if value[0] == ?"
127
- value[-1] = '' if value[-1] == ?"
138
+ value[0] = ''.freeze if value[0] == ?".freeze
139
+ value[-1] = ''.freeze if value[-1] == ?".freeze
128
140
 
129
141
  h.merge!( key.to_sym => value )
130
142
  }
@@ -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