Module: LocalStats

Extended by:
LocalStats
Included in:
LocalStats
Defined in:
lib/droid/heroku/local_stats.rb

Constant Summary collapse

IFSTAT_INTERVAL =

sample ifstat every 30 seconds

30

Instance Method Summary collapse

Instance Method Details

#attachObject

called when a droid starts up - setup timers and whatnot



136
137
138
139
140
# File 'lib/droid/heroku/local_stats.rb', line 136

def attach
  @rx, @tx, @rxrate, @txrate = nil
  update_counters
  EM.add_periodic_timer(IFSTAT_INTERVAL) { update_counters }
end

#disk_statsObject



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/droid/heroku/local_stats.rb', line 51

def disk_stats
  raw = `df -P | grep -v tmpfs | grep -v udev | grep -v slugs | awk '{print $1 " ## " $6 " ##  " $5}'`.split("\n").collect { |d| d }
  raw.shift
  raw.collect do |d|
    tokens = d.split("##").collect { |t| t.strip }
    {
      :device => tokens[0],
      :path => tokens[1],
      :usage => tokens[2].gsub('%','').to_i
    }
  end
end

#extended_statsObject



94
95
96
97
98
# File 'lib/droid/heroku/local_stats.rb', line 94

def extended_stats
  {
    :disk_stats => disk_stats
  }
end

#fetch_local_ipObject



35
36
37
# File 'lib/droid/heroku/local_stats.rb', line 35

def fetch_local_ip
  `/sbin/ifconfig eth0 | grep inet | awk '{print $2}'`.gsub('addr:', '').strip
end

#ifconfigObject



142
143
144
# File 'lib/droid/heroku/local_stats.rb', line 142

def ifconfig
  `/sbin/ifconfig eth0`
end

#instance_nameObject Also known as: this_instance_name



72
73
74
75
# File 'lib/droid/heroku/local_stats.rb', line 72

def instance_name
  return nil if slot.nil? or ion_instance_id.nil?
  "#{slot}.#{ion_instance_id}"
end

#ion_instance_idObject



64
65
66
# File 'lib/droid/heroku/local_stats.rb', line 64

def ion_instance_id
  @ion_instance_id ||= File.read('/etc/heroku/ion_instance_id').strip rescue nil
end

#load_avgObject



6
7
8
# File 'lib/droid/heroku/local_stats.rb', line 6

def load_avg
  File.read('/proc/loadavg').split(' ', 2).first.to_f
end

#local_ipObject



31
32
33
# File 'lib/droid/heroku/local_stats.rb', line 31

def local_ip
  @local_ip ||= fetch_local_ip
end

#memory_infoObject



10
11
12
13
14
15
16
17
# File 'lib/droid/heroku/local_stats.rb', line 10

def memory_info
  info = {}
  File.read('/proc/meminfo').split("\n").each do |line|
    name, value = line.split(/:\s+/, 2)
    info[name] = value.to_i
  end
  info
end

#memory_useObject



19
20
21
22
23
# File 'lib/droid/heroku/local_stats.rb', line 19

def memory_use
  info = memory_info
  used = info['MemTotal'] - info['MemFree'] - info['Cached'] - info['Buffers']
  (100 * used / info['MemTotal']).to_i
end

#public_ipObject



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/droid/heroku/local_stats.rb', line 39

def public_ip
  retries = 0
  @public_ip ||= begin
    RestClient.get("http://169.254.169.254/latest/meta-data/public-ipv4").to_s.strip
  rescue RestClient::RequestTimeout => e
    retries += 1
    raise if retries > 5
    sleep 1
    retry
  end
end

#receive_rateObject

bytes received per second



110
111
112
# File 'lib/droid/heroku/local_stats.rb', line 110

def receive_rate
  @rxrate || 0
end

#sampleObject

sample the current RX and TX bytes from ifconfig and return as a two-tuple.



121
122
123
124
125
126
# File 'lib/droid/heroku/local_stats.rb', line 121

def sample
  data = ifconfig.match(/RX bytes:(\d+).*TX bytes:(\d+)/)
  [data[1].to_i, data[2].to_i]
rescue => boom
  Log.notice "error sampling network rate: #{boom.class} #{boom.message}"
end

#slotObject



68
69
70
# File 'lib/droid/heroku/local_stats.rb', line 68

def slot
  @slot ||= File.read('/etc/heroku/slot').strip.gsub(/64$/,'').split('-').first rescue nil
end

#statsObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/droid/heroku/local_stats.rb', line 79

def stats
  {
    :slot            => slot,
    :ion_id          => ion_instance_id,
    :load_avg        => load_avg,
    :memory_use      => memory_use,
    :swap_use        => swap_use,
    :local_ip        => local_ip,
    :instance_name   => this_instance_name,
    :public_ip       => public_ip,
    :net_in_rate     => receive_rate,
    :net_out_rate    => transmit_rate
  }
end

#swap_useObject



25
26
27
28
29
# File 'lib/droid/heroku/local_stats.rb', line 25

def swap_use
  _, total, use, _ = `free | grep Swap:`.strip.split
  return 0 if total.to_i == 0
  (100 * use.to_i / total.to_i).to_i
end

#transfer_ratesObject

bytes received per second and bytes transmitted per second as a two tuple



105
106
107
# File 'lib/droid/heroku/local_stats.rb', line 105

def transfer_rates
  [receive_rate, transmit_rate]
end

#transmit_rateObject

bytes transmitted per second



115
116
117
# File 'lib/droid/heroku/local_stats.rb', line 115

def transmit_rate
  @txrate || 0
end

#update_countersObject



128
129
130
131
132
133
# File 'lib/droid/heroku/local_stats.rb', line 128

def update_counters
  rx, tx = sample
  @rxrate = (rx - @rx) / IFSTAT_INTERVAL if @rx
  @txrate = (tx - @tx) / IFSTAT_INTERVAL if @tx
  @rx, @tx = rx, tx
end