linux_stat 0.1.3 → 0.3.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,20 +1,34 @@
1
1
  #!/usr/bin/env ruby
2
- require 'bundler/setup'
3
- require 'linux_stat'
4
- require 'io/console'
2
+ begin
3
+ require 'linux_stat'
4
+ rescue LoadError
5
+ require 'bundler/setup'
6
+ require 'linux_stat'
7
+ end
8
+
9
+ $-v = true
5
10
 
6
11
  # Print time each method takes unless --no-time or -nt option is passed
7
12
  MARKDOWN = ARGV.any? { |x| x[/^\-\-markdown$/] || x[/^\-md$/] }
8
13
  PRINT_TIME = MARKDOWN ? false : !ARGV.any? { |x| x[/^\-\-no-time$/] || x[/^\-nt$/] }
9
14
 
10
- $-v = true
15
+ %w(--markdown -md --no-time -nt).each(&ARGV.method(:delete))
16
+
17
+ # Run only desired classes / modules
18
+ constants = LinuxStat.constants
19
+
20
+ execute = constants.map(&:downcase).map.with_index { |x, i|
21
+ constants[i] if ARGV.find { |y| y.downcase.to_sym == x }
22
+ }.compact
23
+
24
+ execute.replace(constants) if execute.empty?
11
25
 
12
- LinuxStat.constants.sort.each do |c|
26
+ execute.sort.each do |c|
13
27
  e = eval("LinuxStat::#{c}")
14
28
 
15
29
  next if e.class != Module && e.class != Class
16
30
 
17
- meths = e.methods(false)
31
+ meths = e.methods(false).sort
18
32
 
19
33
  if meths.length > 0
20
34
  if MARKDOWN
@@ -26,9 +40,11 @@ LinuxStat.constants.sort.each do |c|
26
40
 
27
41
  meths.each do |meth|
28
42
  time = Time.now
29
- v = e.send(meth).to_s
30
- time = Time.now.-(time).*(1000).round(3)
43
+ v = e.send(meth)
44
+ time2 = Time.now
45
+ time = time2.-(time).*(1_000_000).round(3)
31
46
 
47
+ v = v.inspect
32
48
  dis = v.length > 253 ? v[0..250].strip + '...'.freeze : v
33
49
 
34
50
  if MARKDOWN
@@ -38,13 +54,13 @@ LinuxStat.constants.sort.each do |c|
38
54
  end
39
55
 
40
56
  puts( "(" +
41
- if time > 10
42
- "\e[1;38;2;255;80;80m"
43
- elsif time > 5
57
+ if time > 10_000
58
+ "\e[1;38;2;255;50;50m"
59
+ elsif time > 5_000
44
60
  "\e[1;38;2;255;170;0m"
45
61
  else
46
62
  "\e[1;38;2;0;170;0m"
47
- end + "Time taken: #{time}ms\e[0m)"
63
+ end + "Time taken: #{time}\u03BCs\e[0m)"
48
64
  ) if PRINT_TIME
49
65
 
50
66
  puts
@@ -0,0 +1,4 @@
1
+ require 'mkmf'
2
+ create_makefile 'linux_stat/fs_stat'
3
+
4
+ # abort 'missing'
@@ -0,0 +1,28 @@
1
+ #include <sys/statvfs.h>
2
+ #include "ruby.h"
3
+
4
+ static VALUE statfs(VALUE obj, VALUE dir) {
5
+ struct statvfs buf ;
6
+ char *d = StringValuePtr(dir) ;
7
+ VALUE hash = rb_hash_new() ;
8
+
9
+ if(statvfs(d, &buf) < 0) return hash ;
10
+
11
+ rb_hash_aset(hash, ID2SYM(rb_intern("block_size")), INT2NUM(buf.f_bsize)) ;
12
+ rb_hash_aset(hash, ID2SYM(rb_intern("fragment_size")), INT2NUM(buf.f_frsize)) ;
13
+ rb_hash_aset(hash, ID2SYM(rb_intern("blocks")), INT2NUM(buf.f_blocks)) ;
14
+ rb_hash_aset(hash, ID2SYM(rb_intern("block_free")), INT2NUM(buf.f_bfree)) ;
15
+ rb_hash_aset(hash, ID2SYM(rb_intern("block_avail_unpriv")), INT2NUM(buf.f_bavail)) ;
16
+ rb_hash_aset(hash, ID2SYM(rb_intern("inodes")), INT2NUM(buf.f_files)) ;
17
+ rb_hash_aset(hash, ID2SYM(rb_intern("free_inodes")), INT2NUM(buf.f_ffree)) ;
18
+ rb_hash_aset(hash, ID2SYM(rb_intern("filesystem_id")), INT2NUM(buf.f_fsid)) ;
19
+ rb_hash_aset(hash, ID2SYM(rb_intern("mount_flags")), INT2NUM(buf.f_flag)) ;
20
+ rb_hash_aset(hash, ID2SYM(rb_intern("max_filename_length")), INT2NUM(buf.f_namemax)) ;
21
+
22
+ return hash ;
23
+ }
24
+
25
+ void Init_fs_stat() {
26
+ VALUE fs = rb_define_module("FS") ;
27
+ rb_define_module_function(fs, "stat", statfs, 1) ;
28
+ }
@@ -1,10 +1,4 @@
1
1
  require "linux_stat/version"
2
-
3
- module LinuxStat
4
- class StatUnavailable < StandardError
5
- end
6
- end
7
-
8
2
  require "linux_stat/battery"
9
3
  require "linux_stat/bios"
10
4
  require "linux_stat/cpu"
@@ -14,3 +8,6 @@ require "linux_stat/net"
14
8
  require "linux_stat/os"
15
9
  require "linux_stat/process"
16
10
  require "linux_stat/swap"
11
+ require "linux_stat/mounts"
12
+ require "linux_stat/fs_stat"
13
+ require "linux_stat/filesystem"
@@ -3,10 +3,13 @@ module LinuxStat
3
3
  PATH = "/sys/class/power_supply/BAT0"
4
4
 
5
5
  class << self
6
+ # Returns true or false based on the presence of the battery.
6
7
  def present?
7
8
  @@present ||= Dir.exist?(PATH)
8
9
  end
9
10
 
11
+ # Returns the details of the battery.
12
+ #If the battery is not present it will return an empty Hash.
10
13
  def stat
11
14
  st = status.downcase
12
15
  return {} unless present?
@@ -23,40 +26,60 @@ module LinuxStat
23
26
  }
24
27
  end
25
28
 
29
+ # Returns the model of the battery.
30
+ #If the battery is not present or the information isn't available it will return an empty String.
26
31
  def model
27
32
  return ''.freeze unless model_readable?
28
33
  IO.read(File.join(PATH, 'model_name')).tap(&:strip!)
29
34
  end
30
35
 
36
+ # Returns the manufacturer of the battery.
37
+ # If the battery is not present or the information is not available, it will return an empty String.
31
38
  def manufacturer
32
39
  return ''.freeze unless manufacturer_readable?
33
40
  IO.read(File.join(PATH, 'manufacturer')).tap(&:strip!)
34
41
  end
35
42
 
43
+ # Returns the technology of the battery.
44
+ # If the battery is not present or the information is not available, it will return an empty String.
36
45
  def technology
37
46
  return ''.freeze unless tech_readable?
38
47
  IO.read(File.join(PATH, 'technology')).tap(&:strip!)
39
48
  end
40
49
 
50
+ # Returns the status of the battery.
51
+ # If the battery is not present or the information is not available, it will return an empty String.
52
+ # The status generally includes either of the full, charging, discharging and unknown states in most cases.
41
53
  def status
42
54
  return ''.freeze unless status_readable?
43
55
  IO.read(File.join(PATH, 'status')).tap(&:strip!)
44
56
  end
45
57
 
58
+ # Returns true if the battery is charging, false if the battery is not charging.
59
+ # If the battery is not present or the information is not available, it will return nil.
46
60
  def charging?
61
+ return nil if status.empty?
47
62
  %w(full charging unknown).each(&:freeze).include?(status.downcase)
48
63
  end
49
64
 
65
+ # Returns true if the battery is discharging, false if the battery is not discharging.
66
+ # If the battery is not present or the information is not available, it will return nil.
50
67
  def discharging?
68
+ return nil if status.empty?
51
69
  status.downcase == 'discharging'
52
70
  end
53
71
 
72
+ # Returns true if the battery status if full, false if the battery status is not full.
73
+ # If the battery is not present or the information is not available, it will return nil.
54
74
  def full?
75
+ return nil if status.empty?
55
76
  status.downcase == 'full'
56
77
  end
57
78
 
79
+ # Returns the charge of the battery.
80
+ # If the battery is not present or the information is not available, it will return nil.
58
81
  def charge
59
- return 0.0 unless charge_now_readable?
82
+ return nil unless charge_now_readable?
60
83
  charge_now = IO.read(File.join(PATH, 'charge_now')).to_i
61
84
  charge_full = IO.read(File.join(PATH, 'charge_full')).to_i
62
85
 
@@ -1,6 +1,9 @@
1
1
  module LinuxStat
2
2
  module BIOS
3
3
  class << self
4
+ # Returns the model of the BIOS.
5
+ # If the information is not available it will return a frozen empty string.
6
+ # The output is also cached ; as changing the value in runtime is unexpected.
4
7
  def model
5
8
  # Cached ; as changing the value in runtime is unexpected
6
9
  @@model ||= if File.readable?('/sys/devices/virtual/dmi/id/product_name')
@@ -12,6 +15,9 @@ module LinuxStat
12
15
  end
13
16
  end
14
17
 
18
+ # Returns the vendor of the BIOS.
19
+ # If the information is not available it will return a frozen empty string.
20
+ # The output is also cached ; as changing the value in runtime is unexpected.
15
21
  def vendor
16
22
  # Cached ; as changing the value in runtime is unexpected
17
23
  @@vendor ||= if File.readable?('/sys/devices/virtual/dmi/id/bios_vendor')
@@ -21,8 +27,10 @@ module LinuxStat
21
27
  end
22
28
  end
23
29
 
30
+ # Returns the version of the BIOS.
31
+ # If the information is not available it will return a frozen empty string.
32
+ # The output is also cached ; as changing the value in runtime is unexpected.
24
33
  def version
25
- # Cached ; as changing the value in runtime is unexpected
26
34
  @@version ||= if File.readable?('/sys/devices/virtual/dmi/id/bios_version')
27
35
  IO.read('/sys/devices/virtual/dmi/id/bios_version').tap(&:strip!)
28
36
  else
@@ -30,8 +38,10 @@ module LinuxStat
30
38
  end
31
39
  end
32
40
 
41
+ # Returns the date of the BIOS.
42
+ # If the information is not available it will return a frozen empty string.
43
+ # The output is also cached ; as changing the value in runtime is unexpected.
33
44
  def date
34
- # Cached ; as changing the value in runtime is unexpected
35
45
  @@date ||= if File.readable?('/sys/devices/virtual/dmi/id/bios_date')
36
46
  IO.read('/sys/devices/virtual/dmi/id/bios_date').tap(&:strip!)
37
47
  else
@@ -1,6 +1,15 @@
1
1
  module LinuxStat
2
2
  module CPU
3
3
  class << self
4
+ # Returns the cpu usage of all threads.
5
+ #
6
+ # The first one is aggregated CPU usage reported by the Linux kernel.
7
+ # And the consecutive ones are the real core usages.
8
+ #
9
+ # On a system with 4 threads, the output will be like::
10
+ # {0=>84.38, 1=>100.0, 2=>50.0, 3=>87.5, 4=>87.5}
11
+ #
12
+ # If the information is not available, it will return an empty Hash
4
13
  def stat(sleep = 0.075)
5
14
  return {} unless stat?
6
15
 
@@ -21,35 +30,56 @@ module LinuxStat
21
30
  end
22
31
  end
23
32
 
33
+ # Returns the total cpu usage.
34
+ # It's like running LinuxStat::CPU.stat[0] but it's much more efficient and calculates just the aggregated usage which is available at the top of the /proc/stat file.
35
+ #
36
+ # If the information is not available, it will return nil.
37
+ def total_usage(sleep = 0.075)
38
+ return nil unless stat?
39
+
40
+ data = IO.foreach('/proc/stat').first.split.tap(&:shift).map!(&:to_f)
41
+ sleep(sleep)
42
+ data2 = IO.foreach('/proc/stat').first.split.tap(&:shift).map!(&:to_f)
43
+
44
+ user, nice, sys, idle, iowait, irq, softirq, steal = *data
45
+ user2, nice2, sys2, idle2, iowait2, irq2, softirq2, steal2 = *data2
46
+
47
+ idle_then, idle_now = idle + iowait, idle2 + iowait2
48
+ totald = idle_now.+(user2 + nice2 + sys2 + irq2 + softirq2 + steal2) - idle_then.+(user + nice + sys + irq + softirq + steal)
49
+ totald.-(idle_now - idle_then).fdiv(totald).*(100).round(2).abs
50
+ end
51
+
52
+ # Returns the total number of CPU threads.
53
+ # If the information isn't available, it will return 0.
24
54
  def count
25
55
  # CPU count can change during the program runtime
26
56
  cpuinfo.count { |x| x.start_with?('processor') }
27
57
  end
28
58
 
59
+ # Returns the model of processor.
60
+ # If the information isn't available, it will return en empty string.
61
+ # The output is also cached ; as changing the value in runtime is unexpected.
29
62
  def model
30
- # Cached ; as changing the value in runtime is unexpected ; nobody is going
31
- # to add/remove CPUs during program runtime
32
63
  @@name ||= cpuinfo.find { |x| x.start_with?('model name') }.to_s.split(?:)[-1].to_s.strip
33
64
  end
34
65
 
35
- # Returns an array with current core frequencies corresponding to the usages
66
+ # Returns an array with current core frequencies corresponding to the usages.
67
+ # If the information isn't available, it will return an empty array.
36
68
  def cur_freq
37
- # Cached ; as changing the value in runtime is unexpected ; nobody is going
38
- # to add/remove CPUs during program runtime
39
-
40
69
  @@cpu_freqs ||= Dir["/sys/devices/system/cpu/cpu[0-9]*/cpufreq/scaling_cur_freq"]
41
70
  @@cpu_freqs.map { |x| IO.read(x).to_i }
42
71
  end
43
72
 
44
- # Returns an array with max core frequencies corresponding to the usages
73
+ # Returns an array with max core frequencies corresponding to the usages.
74
+ # If the information isn't available, it will return an empty array.
45
75
  def max_freq
46
- # Cached ; as changing the value in runtime is unexpected ; nobody is going
47
- # to add/remove CPUs during program runtime
48
-
49
76
  @@max_freqs ||= Dir["/sys/devices/system/cpu/cpu[0-9]*/cpufreq/scaling_max_freq"]
50
77
  @@max_freqs.map { |x| IO.read(x).to_i }
51
78
  end
52
79
 
80
+ alias usages stat
81
+ alias usage total_usage
82
+
53
83
  private
54
84
  def cpuinfo
55
85
  File.readable?('/proc/cpuinfo') ? IO.readlines('/proc/cpuinfo') : []
@@ -0,0 +1,97 @@
1
+ # require 'linux_stat/fs_stat'
2
+
3
+ module LinuxStat
4
+ module Filesystem
5
+ extend FS
6
+
7
+ class << self
8
+ # stat(fs = '/')
9
+ # Where fs is the directory of the file system (like / or /tmp/ or /run/media/thumbdrive).
10
+ #
11
+ # It returns a Hash with the following info:
12
+ # 1. total size of the device (in bytes)
13
+ # 2. free space (in kilobytes)
14
+ # 3. used space (in kilobytes)
15
+ #
16
+ # In a hash format:
17
+ # {:total=>119981191168, :free=>43155574784, :used=>76825616384, :available=>43155574784}
18
+ #
19
+ # If the stat can't be acquired, this method will return an empty Hash.
20
+
21
+ def stat(fs = ?/.freeze)
22
+ s = stat_raw(fs)
23
+ return {} if s.empty?
24
+ s.default = 0
25
+
26
+ {
27
+ total: s[:block_size] * s[:blocks],
28
+ free: s[:block_size] * s[:block_free],
29
+ used: s[:blocks].-(s[:block_free]) * s[:block_size],
30
+ }
31
+ end
32
+
33
+ # stat(fs = '/')
34
+ # Where fs is the directory of the file system (like / or /tmp/ or /run/media/thumbdrive).
35
+ # It returns the total size of a given disk in bytes.
36
+ #
37
+ # If the stat can't be acquired, this method will return nil.
38
+ def total(fs = ?/.freeze)
39
+ s = stat_raw(fs)
40
+ return nil if s.empty?
41
+ s.default = 0
42
+ s[:block_size] * s[:blocks]
43
+ end
44
+
45
+ # stat(fs = '/')
46
+ # Where fs is the directory of the file system (like / or /tmp/ or /run/media/thumbdrive).
47
+ # It returns the total free space in a disk in bytes.
48
+ # It is to be noted that free is not same as available.
49
+ # Free returns the size of free blocks.
50
+ #
51
+ # If the stat can't be acquired, this method will return an empty Hash.
52
+ def free(fs = ?/.freeze)
53
+ s = stat_raw(fs)
54
+ return nil if s.empty?
55
+ s.default = 0
56
+ s[:block_size] * s[:block_free]
57
+ end
58
+
59
+ # stat(fs = '/')
60
+ # Where fs is the directory of the file system (like / or /tmp/ or /run/media/thumbdrive).
61
+ # It returns the used space of a given disk in bytes.
62
+ #
63
+ # If the stat can't be acquired, this method will return nil.
64
+ def used(fs = ?/.freeze)
65
+ s = stat_raw(fs)
66
+ return nil if s.empty?
67
+ s.default = 0
68
+ s[:blocks].-(s[:block_free]) * s[:block_size]
69
+ end
70
+
71
+ # stat(fs = '/')
72
+ # Where fs is the directory of the file system (like / or /tmp/ or /run/media/thumbdrive).
73
+ # It returns the total free space in a disk in bytes.
74
+ # It is to be noted that free is not same as available.
75
+ # Available returns the size of free blocks for unpriviledged users.
76
+ #
77
+ # If the stat can't be acquired, this method will return an empty Hash.
78
+ def available(fs = ?/.freeze)
79
+ s = stat_raw(fs)
80
+ return nil if s.empty?
81
+ s.default = 0
82
+ s[:block_size] * s[:block_avail_unpriv]
83
+ end
84
+
85
+ # stat(fs = '/')
86
+ # Where fs is the directory of the file system (like / or /tmp/ or /run/media/thumbdrive).
87
+ #
88
+ # It returns a Hash with the following data (for example):
89
+ # {:block_size=>4096, :fragment_size=>4096, :blocks=>29292283, :block_free=>10535967, :block_avail_unpriv=>10535967, :inodes=>58612160, :free_inodes=>56718550, :filesystem_id=>2050, :mount_flags=>1024, :max_filename_length=>255}
90
+ #
91
+ # If the stat can't be acquired, this method will return an empty Hash.
92
+ def stat_raw(fs = '/'.freeze)
93
+ FS.stat(fs)
94
+ end
95
+ end
96
+ end
97
+ end
@@ -1,37 +1,147 @@
1
1
  module LinuxStat
2
2
  module Kernel
3
3
  class << self
4
+ # Returns the Linux Kernel version.
5
+ # If the information isn't available, it will return a frozen empty string.
6
+ # The output is also cached ; as changing the value in runtime is unexpected.
4
7
  def version
5
- return @@version ||= ''.freeze if string.empty?
6
- @@version ||= string.split[2]
8
+ return ''.freeze if string.empty?
9
+ @@version ||= splitted[2]
7
10
  end
8
11
 
12
+ # Returns the name of the user who built the kernel using KBUILD_FLAGS.
13
+ # If the information isn't available, it will return a frozen empty string.
14
+ # The output is also cached ; as changing the value in runtime is unexpected.
15
+ def build_user
16
+ @@build_user ||= string.split(/(\(.+\))/).each(&:strip!)
17
+ .reject(&:empty?).find { |x| x[/^\(.+\)$/] }.to_s
18
+ .split[0].to_s[1..-2].to_s.freeze
19
+ end
20
+
21
+ # Returns the compiler used to compile the Linux Kernel.
22
+ # If the information isn't available, it will return a frozen empty string.
23
+ # The output is also cached ; as changing the value in runtime is unexpected.
9
24
  def compiler
10
- return @@compiler ||= ''.freeze if string.empty?
25
+ return ''.freeze if string.empty?
26
+
27
+ @@compiler ||= string.split(/(\(.+\))/).each(&:strip!)
28
+ .reject(&:empty?)
29
+ .find { |x| x[/^\(.+\)$/] }.to_s
30
+ .split.find { |x| !x[/^(.+@.+)$/] }.to_s[/\w+/].to_s.freeze
11
31
 
12
- @@compiler ||= case string.split[4].to_s
32
+ @@compiler_val ||= case @@compiler
13
33
  when /gcc/i then [:gcc ]
14
34
  when /clang/i then [:clang]
15
35
  when /icc/i then [:icc]
16
- end << string[/\(.*\)/].split.drop(1).find { |x| x[/^\d.*\d/] }[/^\d.*\d/]
36
+ else [@@compiler &.to_sym]
37
+ end << compiler_version
38
+ end
39
+
40
+ # Returns the compiler version used to compile the Linux Kernel.
41
+ # If the information isn't available, it will return a frozen empty string.
42
+ # The output is also cached ; as changing the value in runtime is unexpected.
43
+ def compiler_version
44
+ @@compiler_version ||= string.split(/(\(.+?\))/).each(&:strip!)
45
+ .reject(&:empty?)[2..4].to_a
46
+ .find { |x| x[/[\d.]+/] }.to_s[/[\d.]+/].to_s.freeze
17
47
  end
18
48
 
49
+ # Returns the time when the kernel was compiled.
50
+ # The return value is a Time object.
51
+ # If the information isn't available, it will return nil
52
+ #
53
+ # The time will be searched in specific order.
54
+ # It will match any date matching any of these formats:
55
+ # 1. %b %d %H:%M:%S %z %Y
56
+ # 2. %d %b %Y %H:%M:%S %z
57
+ # 3. %Y-%m-%d
58
+ # Most kernels have date in them in this format.
59
+ #
60
+ # Do note that Ruby sometimes fails to work with timezones like BST for example.
61
+ # In such case, the timezone is unrealiable and often returns the local timezone.
62
+ # You have to use regexp yourself to get the proper zone.
63
+ # Use LinuxStat::Kernel.build_date_string to get the original string if you need that.
64
+ #
65
+ # The output is also cached ; as changing the value in runtime is unexpected.
19
66
  def build_date
20
- return @@time ||= Time.new(0) if string.empty?
67
+ return nil if splitted.empty?
21
68
 
22
69
  @@time ||= begin
23
- require 'time'
24
- Time.strptime(string.split[16..-1].join(' '), "%d %b %Y %H:%M:%S %z")
25
- rescue StandardError
26
- Time.new(0)
70
+ require 'time' unless Time.respond_to?(:strptime)
71
+
72
+ splitted.each_cons(5).map do |x|
73
+ joined = x.each(&:strip!).join(?\s.freeze)
74
+
75
+ # Match 21 Oct 2020 01:11:20 +0000
76
+ if joined[/^\d{1,2}\s\w{3}\s\d{4}\s\d{1,2}:\d{1,2}:\d{1,2}\s\+\d*$/]
77
+ Time.strptime(joined, '%d %b %Y %H:%M:%S %Z') rescue nil
78
+
79
+ # Match Aug 25 17:23:54 UTC 2020
80
+ elsif joined[/^\w{3}\s\d{1,2}\s\d{1,2}:\d{1,2}:\d{2}\s\w+\s\d*$/]
81
+ Time.strptime(joined, '%b %d %H:%M:%S %z %Y') rescue nil
82
+
83
+ # Match 2017-09-19
84
+ elsif joined[/\d{4}-\d{1,2}-\d{1,2}/]
85
+ Time.strptime(joined[/\d{4}-\d{2}-\d{2}/] + " +00:00", '%Y-%m-%d %z') rescue nil
86
+
87
+ else
88
+ nil
89
+ end
90
+ end.tap(&:compact!)[0]
27
91
  end
28
92
  end
29
93
 
94
+ # Returns the time when the kernel was compiled.
95
+ # The return value is a String.
96
+ # If the information isn't available, it will return nil
97
+ #
98
+ # The time will be searched in specific order.
99
+ # It will match any date matching any of these formats:
100
+ # 1. %b %d %H:%M:%S %z %Y
101
+ # 2. %d %b %Y %H:%M:%S %z
102
+ # 3. %Y-%m-%d
103
+ # Most kernels have date in them in this format.
104
+ #
105
+ # Do note that Ruby sometimes fails to work with timezones like BST for example.
106
+ # In such case, the timezone is unrealiable and often returns the local timezone.
107
+ # You have to use regexp yourself to get the proper zone.
108
+ # Use LinuxStat::Kernel.build_date_string to get the original string if you need that.
109
+ #
110
+ # The output is also cached ; as changing the value in runtime is unexpected.
111
+ def build_date_string
112
+ return nil if splitted.empty?
113
+
114
+ @@time2 ||= begin
115
+ require 'time' unless Time.respond_to?(:strptime)
116
+
117
+ splitted.each_cons(5).map do |x|
118
+ joined = x.each(&:strip!).join(?\s.freeze)
119
+
120
+ # Match 21 Oct 2020 01:11:20 +0000
121
+ if (joined[/^\d{1,2}\s\w{3}\s\d{4}\s\d{1,2}:\d{1,2}:\d{1,2}\s\+\d*$/] && (Time.strptime(joined, '%d %b %Y %H:%M:%S %Z') rescue nil)) ||
122
+
123
+ # Match Aug 25 17:23:54 UTC 2020
124
+ (joined[/^\w{3}\s\d{1,2}\s\d{1,2}:\d{1,2}:\d{1,2}\s\w+\s\d*$/] && (Time.strptime(joined, '%b %d %H:%M:%S %z %Y') rescue nil)) ||
125
+
126
+ # Match 2017-09-19
127
+ (joined[/\d{4}-\d{1,2}-\d{1,2}/] && (Time.strptime(joined[/\d{4}-\d{2}-\d{2}/] + " +00:00", '%Y-%m-%d %z') rescue nil))
128
+ joined
129
+ else
130
+ nil
131
+ end
132
+ end.tap(&:compact!)[0]
133
+ end
134
+ end
135
+
136
+ # Reads maximum 1024 bytes from /proc/version and returns the string.
137
+ # The output is also cached ; as changing the value in runtime is unexpected.
30
138
  def string
31
- # Cached ; as changing the value in runtime is unexpected
32
- # Hotfix update can be problem, but it's rare and might not
33
- # affect the version string during program runtime.
34
- @@string ||= File.readable?('/proc/version') ? IO.read('/proc/version').tap(&:strip!) : ''
139
+ @@string ||= File.readable?('/proc/version') ? IO.read('/proc/version', 1024).tap(&:strip!) : ''
140
+ end
141
+
142
+ private
143
+ def splitted
144
+ @@string_splitted ||= string.split
35
145
  end
36
146
  end
37
147
  end