linux_stat 1.2.2 → 1.5.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,4 +1,6 @@
1
1
  module LinuxStat
2
+ # Shows various Swap devices related information of the current system.
3
+
2
4
  module Swap
3
5
  class << self
4
6
  ##
@@ -8,7 +10,7 @@ module LinuxStat
8
10
  def list
9
11
  return {} unless swaps_readable?
10
12
 
11
- file = IO.readlines('/proc/swaps').drop(1)
13
+ file = IO.readlines('/proc/swaps'.freeze).drop(1)
12
14
  file.reduce({}) do |h, x|
13
15
  name, *stats = x.strip.split
14
16
  h.merge!(name => stats.map! { |v| v.to_i.to_s == v ? v.to_i : v.to_sym })
@@ -20,7 +22,7 @@ module LinuxStat
20
22
  #
21
23
  # If the info isn't available, it will return an empty Hash.
22
24
  def any?
23
- !!IO.foreach('/proc/swaps').drop(1).first
25
+ !!IO.foreach('/proc/swaps'.freeze).first(2)[1]
24
26
  end
25
27
 
26
28
  # Show aggregated used and available swap.
@@ -55,8 +57,19 @@ module LinuxStat
55
57
  #
56
58
  # The return type is a Integer but if the info isn't available, it will return nil.
57
59
  def total
58
- return nil unless swaps_readable?
59
- read_usage[0].sum
60
+ v = LinuxStat::Sysinfo.totalswap
61
+ v ? v.fdiv(1024).to_i : nil
62
+ end
63
+
64
+ ##
65
+ # Shows free swap.
66
+ #
67
+ # The value is in kilobytes.
68
+ #
69
+ # The return type is a Integer but if the info isn't available, it will return nil.
70
+ def free
71
+ v = LinuxStat::Sysinfo.freeswap
72
+ v ? v.fdiv(1024).to_i : nil
60
73
  end
61
74
 
62
75
  ##
@@ -114,7 +127,7 @@ module LinuxStat
114
127
  def read_usage
115
128
  return [[], []] unless swaps_readable?
116
129
 
117
- val = IO.readlines('/proc/swaps').drop(1)
130
+ val = IO.readlines('/proc/swaps'.freeze).drop(1)
118
131
  return [[], []] if val.empty?
119
132
 
120
133
  val.map! { |x|
@@ -123,7 +136,7 @@ module LinuxStat
123
136
  end
124
137
 
125
138
  def swaps_readable?
126
- @@swaps_readable ||= File.readable?('/proc/swaps')
139
+ @@swaps_readable ||= File.readable?('/proc/swaps'.freeze)
127
140
  end
128
141
  end
129
142
  end
@@ -0,0 +1,122 @@
1
+ module LinuxStat
2
+ # Sensors are available to /sys/class/hwmon
3
+ # This module just reads the files inside them to get the sensor information.
4
+ #
5
+ # https://www.kernel.org/doc/Documentation/thermal/sysfs-api.txt
6
+
7
+ module Thermal
8
+ class << self
9
+ ##
10
+ # Get temperatures from all sensors.
11
+ #
12
+ # The return value is an Array of Hashes.
13
+ #
14
+ # It opens various files from /sys/, and it should be
15
+ # considered very slow.
16
+ #
17
+ # Each hash may contain information including:
18
+ #
19
+ # path, name, label
20
+ # temperature, temp_crit, temp_max
21
+ #
22
+ # But if the info isn't available, it will return an empty Array.
23
+ def temperatures
24
+ query_hwmon('temp'.freeze, :temperature, true)
25
+ end
26
+
27
+ ##
28
+ # Get RPM from all fans.
29
+ #
30
+ # The return value is an Array of Hashes.
31
+ #
32
+ # It opens various files from /sys/, and it should be
33
+ # considered very slow.
34
+ #
35
+ # Each hash may contain information including:
36
+ #
37
+ # path, name, label
38
+ # rpm, temp_crit, temp_max
39
+ #
40
+ # But if the info isn't available, it will return an empty Array.
41
+ def fans
42
+ query_hwmon('fan'.freeze, :rpm)
43
+ end
44
+
45
+ ##
46
+ # Counts the number of sensors (fans are not counted)
47
+ #
48
+ # The return type is an Integer.
49
+ def count_sensors
50
+ return 0 unless hwmon_readable?
51
+
52
+ Dir["/sys/class/hwmon/hwmon[0-9]*/temp[0-9]*_input".freeze].size
53
+ end
54
+
55
+ ##
56
+ # Counts the number of fans (sensors are not counted)
57
+ #
58
+ # The return type is an Integer.
59
+ def count_fans
60
+ return 0 unless hwmon_readable?
61
+
62
+ Dir["/sys/class/hwmon/hwmon[0-9]*/fan[0-9]*_input".freeze].size
63
+ end
64
+
65
+ private
66
+ def hwmon_readable?
67
+ @@hwmon_readable ||= File.readable?("/sys/class/hwmon/")
68
+ end
69
+
70
+ def query_hwmon(mon, key, div = false)
71
+ return [] unless hwmon_readable?
72
+
73
+ name = dir = ''.freeze
74
+
75
+ files = Dir["/sys/class/hwmon/hwmon[0-9]*/#{mon}[0-9]*_input".freeze]
76
+ i = -1
77
+ ret = []
78
+
79
+ while x = files[i += 1]
80
+ splitted = File.split(x)
81
+ path = File.join(splitted[0..-2])
82
+
83
+ n = splitted[-1][/.*_/]
84
+
85
+ label_f = "#{path}/#{n}label"
86
+ label = File.readable?(label_f) ? IO.read(label_f, encoding: 'ASCII-8BIT'.freeze).strip : nil
87
+
88
+ temp_crit_f = "#{path}/#{n}crit"
89
+ temp_crit = File.readable?(temp_crit_f) ? IO.read(temp_crit_f, encoding: 'ASCII-8BIT'.freeze).to_i : nil
90
+
91
+ temp_max_f = "#{path}/#{n}max"
92
+ temp_max = File.readable?(temp_max_f) ? IO.read(temp_max_f, encoding: 'ASCII-8BIT'.freeze).to_i : nil
93
+
94
+ value = File.readable?(x) ? IO.read(x).to_i : nil
95
+
96
+ if dir != path
97
+ dir = path
98
+ name_f = "#{path}/name"
99
+ name = File.readable?(name_f) ? IO.read(name_f, encoding: 'ASCII-8BIT'.freeze).tap(&:strip!) : ''.freeze
100
+ end
101
+
102
+ if div
103
+ value /= 1000.0
104
+ temp_max /= 1000.0 if temp_max
105
+ temp_crit /= 1000.0 if temp_crit
106
+ end
107
+
108
+ h = {path: path, name: name}
109
+
110
+ h.merge!(label: label) if label
111
+ h.merge!(key => value)
112
+ h.merge!(temp_crit: temp_crit) if temp_crit
113
+ h.merge!(temp_crit: temp_max) if temp_max
114
+
115
+ ret.push(h)
116
+ end
117
+
118
+ ret
119
+ end
120
+ end
121
+ end
122
+ end
@@ -1,4 +1,6 @@
1
1
  module LinuxStat
2
+ # Shows various USB device related information of the current system.
3
+
2
4
  module USB
3
5
  class << self
4
6
  ##
@@ -1,4 +1,6 @@
1
1
  module LinuxStat
2
+ # Shows various User related information of the current system.
3
+
2
4
  module User
3
5
  class << self
4
6
  ##
@@ -55,6 +57,7 @@ module LinuxStat
55
57
  # But if the status isn't available it will return an empty Hash.
56
58
  def gids
57
59
  return {} unless passwd_readable?
60
+
58
61
  passwd_splitted.reduce({}) { |h, x|
59
62
  h.merge!(x[0].to_sym => x[3].to_i)
60
63
  }
@@ -103,7 +106,7 @@ module LinuxStat
103
106
 
104
107
  uid, gid = LinuxStat::Sysconf.get_uid, LinuxStat::Sysconf.get_gid
105
108
 
106
- username = ''
109
+ username = ''.freeze
107
110
  passwd.each { |x|
108
111
  splitted = x.split(?:).freeze
109
112
  if splitted[2].to_i == uid && splitted[3].to_i == gid
@@ -187,7 +190,7 @@ module LinuxStat
187
190
  def username_by_gid(gid = get_gid)
188
191
  return ''.freeze unless passwd_readable?
189
192
 
190
- username = ''
193
+ username = ''.freeze
191
194
  passwd.each do |x|
192
195
  splitted = x.split(?:.freeze)
193
196
  if splitted[2].to_i == gid
@@ -314,7 +317,7 @@ module LinuxStat
314
317
  def home_by_gid(id = get_gid)
315
318
  return ''.freeze unless passwd_readable?
316
319
 
317
- home = ''
320
+ home = ''.freeze
318
321
  passwd.each do |x|
319
322
  splitted = x.split(?:.freeze)
320
323
 
@@ -1,3 +1,3 @@
1
1
  module LinuxStat
2
- VERSION ||= "1.2.2"
2
+ VERSION ||= "1.5.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: linux_stat
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.2
4
+ version: 1.5.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-12-28 00:00:00.000000000 Z
11
+ date: 2021-01-22 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Linux only, efficient linux system utilization reporting and system monitoring
14
14
  gem
@@ -20,6 +20,7 @@ extensions:
20
20
  - ext/fs_stat/extconf.rb
21
21
  - ext/nproc/extconf.rb
22
22
  - ext/sysconf/extconf.rb
23
+ - ext/sysinfo/extconf.rb
23
24
  - ext/utsname/extconf.rb
24
25
  extra_rdoc_files:
25
26
  - README.md
@@ -35,6 +36,8 @@ files:
35
36
  - ext/nproc/nproc.c
36
37
  - ext/sysconf/extconf.rb
37
38
  - ext/sysconf/sysconf.c
39
+ - ext/sysinfo/extconf.rb
40
+ - ext/sysinfo/sysinfo.c
38
41
  - ext/utsname/extconf.rb
39
42
  - ext/utsname/utsname.c
40
43
  - lib/linux_stat.rb
@@ -52,6 +55,7 @@ files:
52
55
  - lib/linux_stat/process.rb
53
56
  - lib/linux_stat/process_info.rb
54
57
  - lib/linux_stat/swap.rb
58
+ - lib/linux_stat/thermal.rb
55
59
  - lib/linux_stat/usb.rb
56
60
  - lib/linux_stat/user.rb
57
61
  - lib/linux_stat/version.rb