Class: FFWD::Statistics::SystemStatistics

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/ffwd/statistics/system_statistics.rb

Constant Summary collapse

PID_SMAPS_FILE =
'/proc/self/smaps'
PID_STAT_FILE =
'/proc/self/stat'
STAT_FILE =
'/proc/stat'
MEMINFO_FILE =
'/proc/meminfo'

Instance Method Summary collapse

Methods included from Logging

included, #log

Constructor Details

#initialize(opts = {}) ⇒ SystemStatistics

Returns a new instance of SystemStatistics.



97
98
99
# File 'lib/ffwd/statistics/system_statistics.rb', line 97

def initialize opts={}
  @cpu_prev = nil
end

Instance Method Details

#checkObject



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/ffwd/statistics/system_statistics.rb', line 119

def check
  if not File.file? PID_SMAPS_FILE
    log.error "File does not exist: #{PID_SMAPS_FILE} (is this a linux system?)"
    return false
  end

  if not File.file? PID_STAT_FILE
    log.error "File does not exist: #{PID_STAT_FILE} (is this a linux system?)"
    return false
  end

  if not File.file? STAT_FILE
    log.error "File does not exist: #{STAT_FILE} (is this a linux system?)"
    return false
  end

  if not File.file? MEMINFO_FILE
    log.error "File does not exist: #{MEMINFO_FILE} (is this a linux system?)"
    return false
  end

  return true
end

#collect(channel) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/ffwd/statistics/system_statistics.rb', line 101

def collect channel
  memory_use = memory_usage
  cpu_use = cpu_usage

  cpu_use.each do |key, value|
    yield "statistics.cpu-#{key}", value
  end

  memory_use.each do |key, value|
    yield "statistics.memory-#{key}", value
  end

  channel << {
    :cpu => cpu_use,
    :memory => memory_use,
  }
end

#cpu_usageObject



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/ffwd/statistics/system_statistics.rb', line 153

def cpu_usage
  stat = read_pid_stat

  current = {
    :system => stat[:stime],
    :user => stat[:utime],
    :total => read_stat_total
  }

  prev = @cpu_prev

  if @cpu_prev.nil?
    @cpu_prev = prev = current
  else
    @cpu_prev = current
  end

  return {
    :system => current[:system] - prev[:system],
    :user => current[:user] - prev[:user],
    :total => current[:total] - prev[:total],
  }
end

#memory_usageObject



143
144
145
146
147
148
149
150
151
# File 'lib/ffwd/statistics/system_statistics.rb', line 143

def memory_usage
  result = {:resident => 0, :total => read_total_memory}

  read_smaps do |smap|
    result[:resident] += smap.rss
  end

  result
end