Module: LinuxStat::OS
- Defined in:
- lib/linux_stat/os.rb
Class Method Summary collapse
-
.bits ⇒ Object
Reads ruby configuration and tries to guess if the system is 64 bit.
-
.distribution ⇒ Object
Reads /etc/lsb-release or /etc/os-release tries to get information about the distribution.
-
.hostname ⇒ Object
Reads /etc/hostname and returns the hostname.
-
.lsb_release ⇒ Object
Reads /etc/lsb-release and returns a Hash.
-
.machine ⇒ Object
Uses utsname.h to determine the machine.
-
.nodename ⇒ Object
Uses utsname.h to determine the system nodename.
-
.os_release ⇒ Object
Reads /etc/os-release and returns a Hash.
-
.uptime ⇒ Object
Reads /proc/uptime and returns the system uptime: :minute=>34, :second=>12.59.
Class Method Details
.bits ⇒ Object
Reads ruby configuration and tries to guess if the system is 64 bit. If it fails then it runs utsname.h to guess the machine. It the machine is 64 bits, it will return 64, else it returns 32.
The return type is strictly Integer and doesn’t fail.
86 87 88 89 90 91 92 |
# File 'lib/linux_stat/os.rb', line 86 def bits @@bits ||= if RbConfig::CONFIG['host_cpu'].end_with?('64') || RUBY_PLATFORM.end_with?('64') || machine.end_with?('64') 64 else 32 end end |
.distribution ⇒ Object
Reads /etc/lsb-release or /etc/os-release tries to get information about the distribution. If the information isn’t available, it will read and return /etc/issue.
The return type is String. If none of the info is available, it will return an empty frozen String.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/linux_stat/os.rb', line 37 def distribution @@distribution ||= if os_release.key?(:NAME) os_release[:NAME] else v = lsb_release if v.key?(:DISTRIB_DESCRIPTION) v[:DISTRIB_DESCRIPTION] elsif v.key?(:DISTRIB_ID) v[:DISTRIB_ID] elsif File.readable?('/etc/issue') IO.read('/etc/issue').strip else 'Unknown'.freeze end end end |
.hostname ⇒ Object
Reads /etc/hostname and returns the hostname.
The return type is String. If the info info isn’t available, it will return ‘localhost’.
73 74 75 76 77 78 79 |
# File 'lib/linux_stat/os.rb', line 73 def hostname @@hostname ||= if File.exist?('/etc/hostname') IO.read('/etc/hostname').strip else 'localhost' end end |
.lsb_release ⇒ Object
Reads /etc/lsb-release and returns a Hash. For example:
{:LSB_VERSION=>"1.4", :DISTRIB_ID=>"Arch", :DISTRIB_RELEASE=>"rolling", :DISTRIB_DESCRIPTION=>"Arch Linux"}
If the info isn’t available, it will return an empty Hash.
The amount of data read is 4096 bytes. Any more than that will result in truncated output.
The information is also cached, and once loaded, won’t change in runtime. Because changing the /etc/lsb-release isn’t expected in runtime.
27 28 29 30 |
# File 'lib/linux_stat/os.rb', line 27 def lsb_release # cached (memoized) ; as changing the value in runtime is unexpected @@lsb_release ||= File.readable?('/etc/lsb-release') ? release('/etc/lsb-release') : {} end |
.machine ⇒ Object
Uses utsname.h to determine the machine
It returns a String but if the info isn’t available, it will return an empty String
58 59 60 |
# File 'lib/linux_stat/os.rb', line 58 def machine @@machine ||= LinuxStat::Uname.machine end |
.nodename ⇒ Object
Uses utsname.h to determine the system nodename
It returns String but if the info isn’t available, it will return an empty String
65 66 67 |
# File 'lib/linux_stat/os.rb', line 65 def nodename @@nodename ||= LinuxStat::Uname.nodename end |
.os_release ⇒ Object
Reads /etc/os-release and returns a Hash. For example:
{: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"}
If the info isn’t available, it will return an empty Hash.
The amount of data read is 4096 bytes. Any more than that will result in truncated output.
The information is also cached, and once loaded, won’t change in runtime. Because changing the /etc/lsb-release isn’t expected in runtime.
13 14 15 16 |
# File 'lib/linux_stat/os.rb', line 13 def os_release # cached (memoized) ; as changing the value in runtime is unexpected @@os_release ||= File.readable?('/etc/os-release') ? release('/etc/os-release') : {} end |
.uptime ⇒ Object
Reads /proc/uptime and returns the system uptime:
{:hour=>10, :minute=>34, :second=>12.59}
If the stat isn’t available, an empty hash is returned.
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/linux_stat/os.rb', line 98 def uptime return {} unless uptime_readable? uptime = IO.read('/proc/uptime').to_f uptime_i = uptime.to_i h = uptime_i / 3600 m = uptime_i % 3600 / 60 s = uptime.%(3600).%(60).round(2) { hour: h, minute: m, second: s } end |