Sha256: 0b06e939da1c84d4b3aabca5c17e3e516e894a53a97be60026f01b69facf1c5a

Contents?: true

Size: 1.91 KB

Versions: 7

Compression:

Stored size: 1.91 KB

Contents

module EY
  class BigBrother
    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

    def initialize(dna)
      @dna = dna
      @result = {}
    end
    
    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
    
    def skip?(name)
      (@dna['skip']||[]).include?(name)
    end
    
    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
    
    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
    
    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
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
ey_cloud_server-1.4.34 lib/ey-flex/big-brother.rb
ey_cloud_server-1.4.33 lib/ey-flex/big-brother.rb
ey_cloud_server-1.4.32 lib/ey-flex/big-brother.rb
ey_cloud_server-1.4.31 lib/ey-flex/big-brother.rb
ey_cloud_server-1.4.28 lib/ey-flex/big-brother.rb
ey_cloud_server-1.4.28.pre lib/ey-flex/big-brother.rb
ey_cloud_server-1.4.26 lib/ey-flex/big-brother.rb