linux_stat 1.2.3 → 1.6.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,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
  ##
@@ -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
  ##
@@ -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
  ##
@@ -1,3 +1,3 @@
1
1
  module LinuxStat
2
- VERSION ||= "1.2.3"
2
+ VERSION ||= "1.6.0"
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.3
4
+ version: 1.6.0
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-29 00:00:00.000000000 Z
11
+ date: 2021-01-28 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
@@ -67,7 +71,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
67
71
  requirements:
68
72
  - - ">="
69
73
  - !ruby/object:Gem::Version
70
- version: 2.5.0
74
+ version: 2.4.0
71
75
  required_rubygems_version: !ruby/object:Gem::Requirement
72
76
  requirements:
73
77
  - - ">="