-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rb
executable file
·49 lines (42 loc) · 970 Bytes
/
main.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env ruby
require File.expand_path('../lib/filesize.rb', __FILE__)
module OS
def self.linux?
RUBY_PLATFORM =~ /linux/i
end
def self.freebsd?
RUBY_PLATFORM =~ /freebsd/i
end
end
module Info
def self.cpu
if OS.linux?
`grep 'model name' /proc/cpuinfo`.gsub /^[^:]+: /, ''
elsif OS.freebsd?
`/sbin/sysctl hw.model`.gsub /^[^:]+: /, ''
else
'unkonwn'
end.strip.gsub /\s+/, ' '
end
def self.memory
if OS.linux?
Filesize.from(`grep 'MemTotal' /proc/meminfo`.gsub(/^[^:]+: /, '').strip).to_s 'MiB'
elsif OS.freebsd?
Filesize.from(`sysctl hw.realmem`.gsub(/^[^:]+: /, '').strip + 'B').to_s 'MiB'
else
'unknown'
end
end
def self.online_count
`users | wc -w`.strip
end
end
motd = File.open File.expand_path(File.join('../lib/motd', `hostname`.strip), __FILE__), 'r' do |f|
f.read
end
puts motd % {
:cpu => Info.cpu,
:mem => Info.memory,
:current_time => Time.now.asctime,
:online_users => Info.online_count
}