Module: LinuxStat::OS
- Defined in:
- lib/linux_stat/os.rb
Overview
Shows various OS related information of the current system.
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
Returns the hostname from LinuxStat::Sysconf.hostname.
-
.loadavg ⇒ Object
or waiting for disk I/O (state D) averaged over 1, 5, and 15 minutes.
-
.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: LinuxStat::OS.uptime.
-
.uptime_f ⇒ Object
Returns Float uptime of the system reported by /proc/uptime LinuxStat::OS.uptime_f.
-
.uptime_i ⇒ Object
Returns uptime of the system reported by LinuxStat::Sysinfo.uptime() LinuxStat::OS.uptime_i.
-
.version ⇒ Object
(also: distrib_version)
Gives you the version of the OS you are using.
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.
133 134 135 136 137 138 139 |
# File 'lib/linux_stat/os.rb', line 133 def bits @@bits ||= if machine.end_with?('64') || RbConfig::CONFIG['host_cpu'].end_with?('64') || RUBY_PLATFORM.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.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/linux_stat/os.rb', line 50 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'.freeze) IO.read('/etc/issue'.freeze, encoding: 'ASCII-8bit'.freeze).strip else 'Unknown'.freeze end end end |
.hostname ⇒ Object
Returns the hostname from LinuxStat::Sysconf.hostname.
The return type is String. If the info info isn’t available, it will return an empty String.
121 122 123 |
# File 'lib/linux_stat/os.rb', line 121 def hostname LinuxStat::Sysconf.hostname end |
.loadavg ⇒ Object
or waiting for disk I/O (state D) averaged over 1, 5, and 15 minutes. They are the same as the load average numbers given by uptime(1) and other programs. man7.org/linux/man-pages/man5/procfs.5.html
The return type is an Hash containing the values that maps to 1, 5, and 15. This method calls LinuxStat::Sysinfo.loads() directly.
However, if the info isn’t available, it will return nil as values.
208 209 210 211 212 213 214 215 216 |
# File 'lib/linux_stat/os.rb', line 208 def loadavg loads = LinuxStat::Sysinfo.loads { 1 => loads[0], 5 => loads[1], 15 => loads[2] } end |
.lsb_release ⇒ Object
Reads /etc/lsb-release and returns a Hash. For example:
LinuxStat::OS.lsb_release
=> {: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.
39 40 41 |
# File 'lib/linux_stat/os.rb', line 39 def lsb_release @@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
104 105 106 |
# File 'lib/linux_stat/os.rb', line 104 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.
112 113 114 |
# File 'lib/linux_stat/os.rb', line 112 def nodename LinuxStat::Uname.nodename end |
.os_release ⇒ Object
Reads /etc/os-release and returns a Hash. For example:
LinuxStat::OS.os_release
=> {:NAME=>"Arch Linux", :PRETTY_NAME=>"Arch Linux", :ID=>"arch", :BUILD_ID=>"rolling",
:ANSI_COLOR=>“38;2;23;147;209”, :HOME_URL=>“www.archlinux.org/”, :DOCUMENTATION_URL=>“wiki.archlinux.org/”, :SUPPORT_URL=>“bbs.archlinux.org/”, :BUG_REPORT_URL=>“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.
23 24 25 |
# File 'lib/linux_stat/os.rb', line 23 def os_release @@os_release ||= File.readable?('/etc/os-release') ? release('/etc/os-release') : {} end |
.uptime ⇒ Object
Reads /proc/uptime and returns the system uptime:
LinuxStat::OS.uptime
=> {:hour=>16, :minute=>10, :second=>11, :jiffy=>20}
Using uptime is 10x slower than using uptime_i
If the stat isn’t available, an empty hash is returned.
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/linux_stat/os.rb', line 150 def uptime _uptime = LinuxStat::ProcFS.uptime_f return {} unless _uptime uptime_i = _uptime.to_i h = uptime_i / 3600 m = uptime_i % 3600 / 60 s = uptime_i.%(60) j = _uptime.-(uptime_i) * 100 { hour: h, minute: m, second: s, jiffy: j.to_i } end |
.uptime_f ⇒ Object
Returns Float uptime of the system reported by /proc/uptime
LinuxStat::OS.uptime_f
=> 28956.34
The value is generally rounded to 2 decimal places.
Using uptime_f is 10x slower than using uptime_i
If the stat isn’t available, nil is returned.
180 181 182 |
# File 'lib/linux_stat/os.rb', line 180 def uptime_f LinuxStat::ProcFS.uptime_f end |
.uptime_i ⇒ Object
Returns uptime of the system reported by LinuxStat::Sysinfo.uptime()
LinuxStat::OS.uptime_i
=> 28956
If the stat isn’t available, nil is returned.
191 192 193 |
# File 'lib/linux_stat/os.rb', line 191 def uptime_i LinuxStat::Sysinfo.uptime end |
.version ⇒ Object Also known as: distrib_version
Gives you the version of the OS you are using.
On ArchLinux for example:
LinuxStat::OS.version
=> "rolling"
On Ubuntu 20.04:
LinuxStat::OS.version
=> "20.04"
On CentOS 26:
LinuxStat::OS.version
=> "26"
The return type is String. But if the information isn’t available, it will return an empty frozen String.
88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/linux_stat/os.rb', line 88 def version @@distrib_version ||= if os_release.key?(:VERSION_ID) os_release[:VERSION_ID] elsif lsb_release.key?(:DISTRIB_RELEASE) lsb_release[:DISTRIB_RELEASE] elsif os_release.key?(:VERSION) os_release[:VERSION] else ''.freeze end end |