Class: EY::BigBrother

Inherits:
Object
  • Object
show all
Defined in:
lib/ey-flex/big-brother.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dna) ⇒ BigBrother

Returns a new instance of BigBrother.



18
19
20
21
# File 'lib/ey-flex/big-brother.rb', line 18

def initialize(dna)
  @dna = dna
  @result = {}
end

Class Method Details

.check(chef_dna = '/etc/chef/dna.json') ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/ey-flex/big-brother.rb', line 3

def self.check(chef_dna = '/etc/chef/dna.json')
  json = JSON.parse(File.read(chef_dna))

  # {'skip':[
  #   'mysqld'
  # ],
  # 'check':[
  #   'ttsrv'
  # ]}

  skips = JSON.parse(File.read('/etc/ey-alerts.json')) rescue {}

  new(json.merge(skips)).check
end

Instance Method Details

#checkObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ey-flex/big-brother.rb', line 41

def check
  case @dna['instance_role']
  when 'solo'
    check_process(nginx_or_apache) unless skip?(nginx_or_apache)
    check_mysql unless skip?('mysqld')
  when 'app', 'app_master'
    check_process(nginx_or_apache) unless skip?(nginx_or_apache)
    check_process('haproxy') unless skip?('haproxy')
  when 'db_master', 'db_slave'
    check_mysql unless skip?('mysqld')
  when 'util'
  end
  (@dna['check']||[]).each do |check|
    check_process(check)
  end
  @result.to_json
end

#check_mysqlObject



59
60
61
62
63
64
# File 'lib/ey-flex/big-brother.rb', line 59

def check_mysql
  check_process('mysqld')
  DBI.connect("DBI:Mysql:mysql:#{@dna['db_host']}", 'root', @dna['users'].first['password'])
rescue DBI::DatabaseError => e
  @result['mysqld'] = 'down'
end

#check_process(name) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/ey-flex/big-brother.rb', line 66

def check_process(name)
  return if name == ''
  pids = `pgrep #{name}`.split("\n")
  if pids.empty?
    @result[name] = 'down'
  else
    if pids.detect {|p| `kill -0 #{p}; echo $?`.chomp.to_i != 0}
      @result[name] = 'down'
    else
      @result[name] = 'up'
    end
  end
end

#nginx_or_apacheObject



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ey-flex/big-brother.rb', line 23

def nginx_or_apache
  server = ''
  @dna['applications'].each do |name, app_data|
    if app_data['recipes'].detect { |r| r == 'nginx' }
      server = 'nginx'
    end
    
    if app_data['recipes'].detect { |r| r == 'passenger' }
      server = 'apache2'
    end
  end
  server
end

#skip?(name) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/ey-flex/big-brother.rb', line 37

def skip?(name)
  (@dna['skip']||[]).include?(name)
end