Class: Apocalypse::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/apocalypse-client.rb,
lib/apocalypse-client/version.rb

Constant Summary collapse

VERSION =
"0.0.3"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.cron_job_commandObject



25
26
27
28
29
# File 'lib/apocalypse-client.rb', line 25

def self.cron_job_command
  return rvm? \
    ? " * * * * * root PATH=$PATH:/sbin:/usr/sbin rvm use $RUBY_VERSION ; /usr/local/bin/rvm exec apocalypse-client report > /dev/null" \
    : "* * * * * root PATH=$PATH:/sbin:/usr/sbin /usr/bin/env apocalypse-client report > /dev/null"
end

.cron_job_fileObject



22
# File 'lib/apocalypse-client.rb', line 22

def self.cron_job_file; "/etc/cron.d/apocalyse"; end

.host_fileObject



21
# File 'lib/apocalypse-client.rb', line 21

def self.host_file;     "#{File.dirname(__FILE__)}/../host.yml"; end

.rvm?Boolean

Returns:

  • (Boolean)


23
# File 'lib/apocalypse-client.rb', line 23

def self.rvm?;          !`which rvm`.chomp.empty? end

Instance Method Details

#blockdevice_metricsObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/apocalypse-client.rb', line 102

def blockdevice_metrics
  columns = ["tps", "rps", "wps", "size", "used", "available", "usage", "mount"]

  io_data = `/usr/bin/env iostat -dk | tail -n+4`.split("\n").map { |line| line.split[0...-2] }
  usage_data = `df -l --block-size=M | grep -i ^/dev/[sh]`.split("\n").map { |line| line.split }

  io_data.collect do |device_data|
    device_name = device_data.shift
    device_usage = usage_data.select { |x| x[0].include? device_name }.flatten

    unless device_usage.empty?
      device_data += device_usage[1..-1]
    end

    { device_name => Hash.zip(columns, device_data) }
  end
end

#check(options) ⇒ Object

Check if all local deps are available



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/apocalypse-client.rb', line 44

def check(options)
  errors = []
  [
    ["/usr/bin/iostat",   "sysstat"]
  ].each do |filename, package|
    errors << "Cannot find `#{filename}`. Please run `apt-get install #{package}` to resolve this." if !File.exists?(filename)
  end

  if errors.empty?
    puts "Everything seems to be in place. You're good to go."
  else
    errors.each { |error| puts errors }
  end
end

#client_informationObject



88
89
90
# File 'lib/apocalypse-client.rb', line 88

def client_information
  { 'version' => Apocalypse::Client::VERSION }
end

#cpu_coresObject

Returns the number of CPU Cores for this system



93
94
95
# File 'lib/apocalypse-client.rb', line 93

def cpu_cores
  `cat /proc/cpuinfo | grep bogomips | wc -l`.strip
end

#cpu_loadavgObject

Gather load average data



98
99
100
# File 'lib/apocalypse-client.rb', line 98

def cpu_loadavg
  `cat /proc/loadavg`.split
end

#gather_metricsObject

Gather metrics



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/apocalypse-client.rb', line 74

def gather_metrics
  {
    'cpu' => {
      'cores' => cpu_cores.strip,
      'loadavg' => cpu_loadavg
    },
    'memory' => memory_metrics,
    'swap' => swap_metrics,
    'blockdevices' => blockdevice_metrics,
    'network' => network_metrics,
    'client'  => client_information
  }
end

#install(options) ⇒ Object



68
69
70
71
# File 'lib/apocalypse-client.rb', line 68

def install(options)
  installation = Apocalyse::Client::Install.new
  installation.install!
end

#memory_metricsObject



120
121
122
123
124
125
# File 'lib/apocalypse-client.rb', line 120

def memory_metrics
  {
    'free' => `cat /proc/meminfo | grep MemFree | awk '{print $2}'`.strip,
    'total' => `cat /proc/meminfo | grep MemTotal | awk '{print $2}'`.strip
  }
end

#network_metricsObject



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/apocalypse-client.rb', line 134

def network_metrics
  devices = `/usr/bin/env ifconfig | egrep 'Link encap' | grep -v 'lo' | awk '{print $1}'`.split

  devices.collect do |device_name|
    { device_name => {
      'hwaddr' => `/usr/bin/env ifconfig #{device_name} | egrep -o 'HWaddr \([0-9a-fA-F]\{2\}\:*\){6}' | awk '{print $2}'`.strip,
      'mtu' => `/usr/bin/env ifconfig #{device_name} | egrep -o MTU\:[0-9]+ | tr -s ':' ' ' | awk '{print $2}'`.strip,
      'metric' => `/usr/bin/env ifconfig #{device_name} | egrep -o Metric\:[0-9]+ | tr -s ':' ' ' | awk '{print $2}'`.strip,
      'encapsulation' => `/usr/bin/env ifconfig #{device_name} | egrep -o 'Link encap\:[a-zA-Z]+' | cut -d":" -f2`.strip,
      'rxbytes' => `/usr/bin/env ifconfig #{device_name} | grep bytes | awk '/RX/ {print $2}' | tr -s ':' ' ' | awk '{print $2}'`.strip,
      'txbytes' => `/usr/bin/env ifconfig #{device_name} | grep bytes | awk '/TX/ {print $6}' | tr -s ':' ' ' | awk '{print $2}'`.strip,
      'ipv4address' => `/usr/bin/env ifconfig #{device_name} | egrep -o 'inet addr\:[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' | tr -s ':' ' ' | awk '{print $3}'`.strip,
      'broadcast' => `/usr/bin/env ifconfig #{device_name} | egrep -o Bcast\:[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\} | tr -s ':' ' ' | awk '{print $2}'`.strip,
      'netmask' => `/usr/bin/env ifconfig #{device_name} | egrep -o Mask\:[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\} | tr -s ':' ' ' | awk '{print $2}'`.strip,
      'gateway' => `netstat -rn | grep ^0.0.0.0 | grep UG | grep #{device_name} | awk '{print $2}' | head -n1`.strip,
      'ipv6addr' => `/usr/bin/env ifconfig #{device_name} | egrep -o 'inet6 addr\:\ \([a-fA-F0-9]\{1,4}\:\{1,2\}[a-fA-F0-9]\{1,4}\:\{1,2\}\)+[A-Fa-f0-9\/^\ ]+' | awk '{print $3}'`.strip,
      'ipv6scope' => `/usr/bin/env ifconfig #{device_name} | egrep -o Scope\:[a-zA-Z]+ | cut -d":" -f2`.strip
    }}
  end
end

#now(options) ⇒ Object



64
65
66
# File 'lib/apocalypse-client.rb', line 64

def now(options)
  install(options)
end

#report(options) ⇒ Object

Report metrics



32
33
34
35
36
37
38
39
40
41
# File 'lib/apocalypse-client.rb', line 32

def report(options)
  request       = Net::HTTP::Post.new("/api/metrics/#{properties[:hostname]}", initheader = {'Content-Type' =>'application/json'})
  request.body  = gather_metrics.to_json
  Net::HTTP.start(properties[:server_address], properties[:port]) do |http|
    request.basic_auth(properties[:username], properties[:password])
    response              = http.request(request)
    
    Apocalyse::Client::Response.parse!(response)
  end
end

#swap_metricsObject



127
128
129
130
131
132
# File 'lib/apocalypse-client.rb', line 127

def swap_metrics
  {
    'free' => `cat /proc/meminfo | grep SwapFree | awk '{print $2}'`.strip,
    'total' => `cat /proc/meminfo | grep SwapTotal | awk '{print $2}'`.strip
  }
end

#update(options) ⇒ Object



59
60
61
62
# File 'lib/apocalypse-client.rb', line 59

def update(options)
  installation = Apocalyse::Client::Install.new
  installation.update!
end