Class: AbstractMonitor

Inherits:
Object
  • Object
show all
Defined in:
lib/monitor_agent.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, token) ⇒ AbstractMonitor

Returns a new instance of AbstractMonitor.



69
70
71
72
73
# File 'lib/monitor_agent.rb', line 69

def initialize(host, token)
  @host = host
  @token = token
  @result = {}
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



67
68
69
# File 'lib/monitor_agent.rb', line 67

def host
  @host
end

#resultObject (readonly)

Returns the value of attribute result.



67
68
69
# File 'lib/monitor_agent.rb', line 67

def result
  @result
end

#tokenObject (readonly)

Returns the value of attribute token.



67
68
69
# File 'lib/monitor_agent.rb', line 67

def token
  @token
end

Instance Method Details

#checkObject



79
80
81
82
# File 'lib/monitor_agent.rb', line 79

def check
  @result = check_ssh
  self
end

#check_sshObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/monitor_agent.rb', line 90

def check_ssh
  Utils::LOG.info "checking ssh: #{host}"
  result = {}
  begin
    Net::SSH.start(host, "root", :keys => ["/root/.ssh/awsm"], :paranoid => false, :timeout => 10) do |ssh|
      begin
        # ignore any STDOUT and only grab the final json from the scripts output
        script_output = ssh.exec!("bash -lc \"ey-agent\"").split("\n").last
        result = JSON.parse(script_output)
        result['ssh'] = 'up'
      rescue JSON::ParserError
        Utils::LOG.error '*' * 80
        Utils::LOG.error "#{host}>>>>>>>>>>>\n#{script_output}"
        result['ey-agent'] = 'down'
      end
    end
  rescue Net::SSH::HostKeyMismatch => e
    e.remember_host!
    retry
  end
  result
rescue SocketError, Errno::ECONNREFUSED, Errno::EHOSTUNREACH, Errno::ETIMEDOUT, Timeout::Error, Net::SSH::AuthenticationFailed
  result['ssh'] = 'down'
  result
end

#roleObject



75
76
77
# File 'lib/monitor_agent.rb', line 75

def role
  self.class.name.snake_case.chomp("_monitor")
end

#to_hashObject



84
85
86
87
88
# File 'lib/monitor_agent.rb', line 84

def to_hash
  {"host" => host, "result" => result, 
   "timestamp" => Utils.epoch_time,
   'token' => token, 'role' => role}
end