Class: Emissary::Agent::Stats

Inherits:
Emissary::Agent show all
Defined in:
lib/emissary/agent/stats.rb

Constant Summary collapse

STATISTIC_TYPES =
[ :cpu, :network, :disk ]

Instance Attribute Summary

Attributes inherited from Emissary::Agent

#args, #config, #message, #method, #name, #operator

Instance Method Summary collapse

Methods inherited from Emissary::Agent

#activate, #initialize, #post_init, #send

Constructor Details

This class inherits a constructor from Emissary::Agent

Instance Method Details

#cpuObject



66
67
68
69
70
# File 'lib/emissary/agent/stats.rb', line 66

def cpu
  load_average = Sys::CPU.load_avg
  ::Emissary.logger.notice "[statistics] CPU: #{load_average.join ', '}"
  load_average
end

#diskObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/emissary/agent/stats.rb', line 51

def disk
  @cmd = "/usr/bin/env df -B K -P -T -x devfs -x tmpfs | /usr/bin/env tail -n +2"
  
  data = IO.popen(@cmd){ |f| f.readlines }.collect { |l| l.split(/\s+/) }
  data.inject([]) { |data,line|
    device = Hash[[:device, :type, :size, :used, :avail, :percent, :mount].zip(line.collect!{|v| v =~ /^\d+/ ? v[/^(\d+)/].to_i : v })]
    
    ::Emissary.logger.notice("[statistics] Disk#%s: type:%s mount:%s size:%d used:%d in-use:%d%%",
      device[:device], device[:type], device[:mount], device[:size], device[:used], device[:percent]
    )
    
    data << device
  }
end

#gatherObject



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/emissary/agent/stats.rb', line 38

def gather
  message.recipient = "#{config[:stats][:queue_base]}:#{message.exchange_type.to_s}"
  message.args = STATISTIC_TYPES.inject([]) do |args, type|
    unless (data = self.__send__(type)).nil?
      args << { type => data }
    end
    args
  end

  throw :skip_implicit_response unless not message.args.empty?
  return message
end

#networkObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/emissary/agent/stats.rb', line 72

def network
  interfaces = (ifconfig = IfconfigWrapper.new.parse).interfaces.inject([]) do |interfaces, name|
    interfaces << (interface = {
      :name => name,
      :tx   => ifconfig[name].tx.symbolize,
      :rx   => ifconfig[name].rx.symbolize,
      :up   => ifconfig[name].status,
      :ips  => ifconfig[name].addresses('inet').collect { |ip| ip.to_s }
    })
    
    ::Emissary.logger.notice("[statistics] Network#%s: state:%s tx:%d rx:%d inet:%s",
      name,
      (interface[:up] ? 'up' : 'down'),
      interface[:tx][:bytes],
      interface[:rx][:bytes],
      interface[:ips].join(',')
    ) unless interface.try(:[], :tx).nil?
    
    interfaces
  end
  
  return interfaces
end

#valid_methodsObject



34
35
36
# File 'lib/emissary/agent/stats.rb', line 34

def valid_methods
  [ :gather ]
end