# 数组
req_list = %w( http://10.4.2.86:9292/sessions/new http://10.4.1.177:9292/sessions/new http://10.4.10.139:9292/sessions/new http://10.4.6.221:9292/sessions/new )
# 哈希
req_hash = {
'task-server' => 'http://10.4.2.86:9292/sessions/new',
'web-server' => 'http://10.4.10.139:9292/sessions/new',
'app-server' => 'http://10.4.6.221:9292/sessions/new',
'back-server' => 'http://10.4.1.177:9292/sessions/new',
}
s = StatusMonitor.new(req_list)
status = s.process(req_hash)
s.status
=> {0=>{:status=>true, :time=>404.0},
1=>{:status=>true, :time=>464.0},
2=>{:status=>true, :time=>350.0},
3=>{:status=>true, :time=>474.0}}
s.errors
=> {:network=>
{:url=>"http://10.4.1.177:9292",
:error=>"Faraday::ConnectionFailed",
:message=>"Connection refused - Connection refused (Errno::ECONNREFUSED)"}}
status
=> {"task-server"=>{:status=>true, :time=>421.0},
"web-server"=>{:status=>true, :time=>345.0},
"app-server"=>{:status=>true, :time=>492.0},
"back-server"=>{:status=>true, :time=>434.0}}
Last active
August 29, 2015 13:58
-
-
Save mimosa/9930668 to your computer and use it in GitHub Desktop.
服务器监控
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
require 'addressable/uri' unless defined?(::Addressable::URI) | |
require 'faraday' unless defined?(::Faraday) | |
class StatusMonitor | |
def initialize(map, settings={}) | |
@settings = settings | |
@mapping = map || [] | |
@result = self.process || {} | |
end | |
def status | |
@result | |
end | |
def errors | |
@errors ||= {} | |
end | |
def process(map=[]) | |
result = {} | |
mapping = if map.empty? | |
@mapping | |
else | |
map | |
end | |
unless mapping.empty? | |
i = 0 | |
mapping.each do |a, b| | |
if b.nil? | |
name = i | |
url = a | |
i += 1 | |
end | |
name ||= a | |
url ||= b | |
# 开始时间 | |
start_at = Time.now | |
# | |
result[name] = if valid?(url) | |
begin | |
response = conn.get do |req| | |
req.url url | |
req.options.timeout = 5 # 响应超时 | |
req.options.open_timeout = 2 # 连接超时 | |
end | |
status!(response.status == 200, duration(start_at)) | |
rescue => error | |
error!(:network, url, error) | |
status!(false, duration(start_at)) | |
end | |
else | |
status!(false, duration(start_at)) | |
end | |
end | |
end | |
result | |
end | |
private | |
def status!(status, time) | |
{ | |
status: status, | |
time: time | |
} | |
end | |
def error!(type, url, error) | |
# 错误 | |
_error = { | |
url: url, | |
error: error.class.name, | |
message: error.message | |
} | |
# | |
if errors.has_key?(type) | |
# 其他错误,已存在 | |
errors[type] = [ errors[type] ] if errors[type].is_a?(Hash) | |
# 多条 | |
errors[type] << _error if errors[type].is_a?(Array) | |
else | |
errors[type] = _error | |
end | |
nil | |
end | |
def conn | |
@conn ||= Faraday.new | |
end | |
def valid?(value) | |
begin | |
uri = Addressable::URI.parse(value) | |
return true if uri && schemes.include?( uri.scheme ) | |
rescue => error | |
error!(:args, value, error) | |
return false | |
end | |
false | |
end | |
def schemes | |
%w(http https) | |
end | |
# 响应时间 | |
def duration(started_at, ended_at = Time.now) | |
(ended_at - started_at) * 1000.0 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment