Class: RCS::SystemStatus

Inherits:
Object
  • Object
show all
Includes:
SystemStatusCodes
Defined in:
lib/rcs-common/systemstatus.rb

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

Constants included from SystemStatusCodes

RCS::SystemStatusCodes::ERROR, RCS::SystemStatusCodes::OK, RCS::SystemStatusCodes::WARN

Class Method Summary collapse

Class Method Details

.cpu_loadObject

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
  # cpu load in the last minute
  avg = CPU.load_avg
  if avg.is_a? Array
    # under unix like, there are 3 values (1, 15 and 15 minutes)
    load_last_minute = avg.first
    # default values for systems where the number is not reported (linux)
    num_cpu = 1
    num_cpu = CPU.num_cpu if CPU.num_cpu
    # on multi core systems we have to divide by the number of CPUs
    percentage = (load_last_minute / num_cpu * 100).floor
  else
    # under windows there is only one value that is the percentage
    percentage = avg
    # sometimes it returns nil under windows
    percentage ||= 0
  end

  return percentage
end

.disk_freeObject

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)
  # get the free and total blocks
  free = stat.blocks_free.to_f
  total = stat.blocks.to_f
  # return the percentage (pessimistic)
  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_pathObject



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

.messageObject



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)

  # the first call to it, keep track of different thread calling the method
  @@prev_cpu[me] ||= Process.times
  @@prev_time[me] ||= Time.now

  # calculate the current cpu time
  current_cpu = Process.times

  # diff them and divide by the call interval
  cpu_time = (current_cpu.utime + current_cpu.stime) - (@@prev_cpu[me].utime + @@prev_cpu[me].stime)
  time_diff = Time.now - @@prev_time[me]
  # prevent division by zero on low res systems
  time_diff = (time_diff == 0) ? 1 : time_diff
  # calculate the percentage
  cpu_percent = cpu_time / time_diff

  # remember it for the next iteration
  @@prev_cpu[me] = Process.times
  @@prev_time[me] = Time.now

  return cpu_percent.ceil
end

.resetObject



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

.statusObject



45
46
47
# File 'lib/rcs-common/systemstatus.rb', line 45

def self.status
  @@current[:status]
end

.windows?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/rcs-common/systemstatus.rb', line 53

def self.windows?
  @@windowsOS
end