Class: RCS::SystemStatus
Overview
Status of the process and system
Constant Summary
collapse
- DEFAULT =
{status: OK, message: "Idle"}
- @@prev_cpu =
{}
- @@prev_time =
{}
- @@current =
DEFAULT
- @@windowsOS =
RUBY_PLATFORM.downcase.include?("mingw")
- @@filesystem_stat_path =
nil
RCS::SystemStatusCodes::ERROR, RCS::SystemStatusCodes::OK, RCS::SystemStatusCodes::WARN
Class Method Summary
collapse
Class Method Details
.cpu_load ⇒ Object
returns an indicator of the CPU usage in the last minute not exactly the CPU usage percentage, but very close to it
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
# File 'lib/rcs-common/systemstatus.rb', line 90
def self.cpu_load
avg = CPU.load_avg
if avg.is_a? Array
load_last_minute = avg.first
num_cpu = 1
num_cpu = CPU.num_cpu if CPU.num_cpu
percentage = (load_last_minute / num_cpu * 100).floor
else
percentage = avg
percentage ||= 0
end
return percentage
end
|
.disk_free ⇒ Object
returns the percentage of free space
79
80
81
82
83
84
85
86
|
# File 'lib/rcs-common/systemstatus.rb', line 79
def self.disk_free
stat = Filesystem.stat(filesystem_stat_path)
free = stat.blocks_free.to_f
total = stat.blocks.to_f
return (free / total * 100).floor
end
|
.filesystem_stat(path) ⇒ Object
57
58
59
|
# File 'lib/rcs-common/systemstatus.rb', line 57
def self.filesystem_stat(path)
Filesystem.stat(path) rescue nil
end
|
.filesystem_stat_path ⇒ Object
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
# File 'lib/rcs-common/systemstatus.rb', line 61
def self.filesystem_stat_path
return @@filesystem_stat_path if @@filesystem_stat_path
pwd = Dir.pwd
if !windows?
return @@filesystem_stat_path = pwd
end
loop do
stat = filesystem_stat(pwd+"\\")
return @@filesystem_stat_path = stat.path if stat
pwd = File.dirname(pwd.gsub('\\', '/')).gsub('/', '\\')
raise "Unable to find a valid mount point" if pwd == "."
end
end
|
.message ⇒ Object
49
50
51
|
# File 'lib/rcs-common/systemstatus.rb', line 49
def self.message
@@current[:message]
end
|
.my_cpu_load(me) ⇒ Object
returns the CPU usage of the current process
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
# File 'lib/rcs-common/systemstatus.rb', line 112
def self.my_cpu_load(me)
@@prev_cpu[me] ||= Process.times
@@prev_time[me] ||= Time.now
current_cpu = Process.times
cpu_time = (current_cpu.utime + current_cpu.stime) - (@@prev_cpu[me].utime + @@prev_cpu[me].stime)
time_diff = Time.now - @@prev_time[me]
time_diff = (time_diff == 0) ? 1 : time_diff
cpu_percent = cpu_time / time_diff
@@prev_cpu[me] = Process.times
@@prev_time[me] = Time.now
return cpu_percent.ceil
end
|
.reset ⇒ Object
37
38
39
|
# File 'lib/rcs-common/systemstatus.rb', line 37
def self.reset
@@current = DEFAULT
end
|
.set(status, message) ⇒ Object
41
42
43
|
# File 'lib/rcs-common/systemstatus.rb', line 41
def self.set(status, message)
@@current = {status: status.to_s.upcase, message: message}
end
|
.status ⇒ Object
45
46
47
|
# File 'lib/rcs-common/systemstatus.rb', line 45
def self.status
@@current[:status]
end
|
.windows? ⇒ Boolean
53
54
55
|
# File 'lib/rcs-common/systemstatus.rb', line 53
def self.windows?
@@windowsOS
end
|