Class: StatsCollector

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

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ StatsCollector

Returns a new instance of StatsCollector.



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/pimon/stats_collector.rb', line 13

def initialize(config)
  @config = config
  @probes = [
    Probe::TimeCheck,
    Probe::CpuUsage,
    Probe::MemoryUsage,
    Probe::SwapUsage,
    Probe::DiskUsage,
    Probe::Temperature,
    Probe::Uptime
  ]
  @stats = Stats.new(@probes.map(&:symbol).concat([:time]))
end

Instance Method Details

#collect_statsObject



27
28
29
30
31
32
33
# File 'lib/pimon/stats_collector.rb', line 27

def collect_stats
  pop_old_stats
  
  @probes.each do |probe|
    @stats.push(probe.symbol, probe.check)
  end
end

#last_updateObject



35
36
37
38
39
# File 'lib/pimon/stats_collector.rb', line 35

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

#show_statsObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/pimon/stats_collector.rb', line 41

def show_stats
  time = @stats.range(:time)
  uptime = @stats.range(:uptime)
  
  stats = {
    :time => { :stats => time.map { |t| (/\d\d:\d\d:\d\d/.match(t))[0] } },
    :hostname => @config.hostname,
    :uptime => uptime
  }
  
  @probes.each do |probe|
    stats[probe.symbol] = {
      :stats => @stats.range(probe.symbol),
      :color => @config.chart[probe.symbol][:color],
      :unit => probe.unit
    } unless [:time, :uptime].include?(probe.symbol)
  end
  
  stats.to_json
end