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.5"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.cron_job_commandObject



35
36
37
38
39
# File 'lib/apocalypse-client.rb', line 35

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



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

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

.host_fileObject

Was “#File.dirname(__FILE__)/../host.yml”



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

def self.host_file
  # Try to move the old configuration file to the new location
  old_file = "#{File.dirname(__FILE__)}/../host.yml"
  if !File.exists?("/etc/apocalypse.yml") && File.exists?(old_file)
    FileUtils.mv old_file, "/etc/apocalypse.yml", :force => true
  end

  return "/etc/apocalypse.yml"
end

.rvm?Boolean

Returns:

  • (Boolean)


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

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

Instance Method Details

#blockdevice_metricsObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/apocalypse-client.rb', line 114

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



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/apocalypse-client.rb', line 54

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 }
    exit(1)
  end
end

#client_informationObject



100
101
102
# File 'lib/apocalypse-client.rb', line 100

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

#cpu_coresObject

Returns the number of CPU Cores for this system



105
106
107
# File 'lib/apocalypse-client.rb', line 105

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

#cpu_loadavgObject

Gather load average data



110
111
112
# File 'lib/apocalypse-client.rb', line 110

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

#gather_metricsObject

Gather metrics



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/apocalypse-client.rb', line 86

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



79
80
81
82
83
# File 'lib/apocalypse-client.rb', line 79

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

#memory_metricsObject



132
133
134
135
136
137
# File 'lib/apocalypse-client.rb', line 132

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



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/apocalypse-client.rb', line 146

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



75
76
77
# File 'lib/apocalypse-client.rb', line 75

def now(options)
  install(options)
end

#report(options) ⇒ Object

Report metrics



42
43
44
45
46
47
48
49
50
51
# File 'lib/apocalypse-client.rb', line 42

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



139
140
141
142
143
144
# File 'lib/apocalypse-client.rb', line 139

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



70
71
72
73
# File 'lib/apocalypse-client.rb', line 70

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