Class: StatsCollector

Inherits:
Object
  • Object
show all
Defined in:
lib/pimon/stats_collector.rb

Instance Method Summary collapse

Constructor Details

#initialize(config, redis) ⇒ StatsCollector

Returns a new instance of StatsCollector.



10
11
12
13
14
# File 'lib/pimon/stats_collector.rb', line 10

def initialize(config, redis)
  @config = config
  @redis = redis
  @probes = [Probe::CpuUsage, Probe::MemoryUsage, Probe::SwapUsage, Probe::DiskUsage, Probe::Temperature]
end

Instance Method Details

#collect_statsObject



16
17
18
19
20
21
22
23
# File 'lib/pimon/stats_collector.rb', line 16

def collect_stats
  pop_old_stats
  
  @redis.rpush(@config.queues[:time], Time.now.strftime("%Y-%m-%d %H:%M:%S"))
  @probes.each do |probe|
    @redis.rpush(@config.queues[probe.symbol], probe.check)
  end
end

#last_updateObject



25
26
27
28
29
# File 'lib/pimon/stats_collector.rb', line 25

def last_update
  time = @redis.lindex(@config.queues[:time], @config.stats[:number_of_stats] - 1)
  
  DateTime.parse(time) if time
end

#show_statsObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/pimon/stats_collector.rb', line 31

def show_stats
  time = @redis.lrange(@config.queues[:time], 0, -1)
  
  stats = {
            :time => { :stats => time.map { |t| (/\d\d:\d\d:\d\d/.match(t))[0] } },
            :refresh_interval_in_millis => @config.stats[:time_period_in_min] * 60 * 1000
          }
  
  @probes.each do |probe|
    stats[probe.symbol] =
     { 
       :stats => @redis.lrange(@config.queues[probe.symbol],  0, -1).map(&:to_i),
       :color => @config.chart[probe.symbol][:color],
       :unit => probe.unit
     }
  end
  
  stats.to_json
end