check-taskr 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -0
- data/README.rdoc +17 -0
- data/Rakefile +60 -0
- data/VERSION +1 -0
- data/bin/check-taskr +28 -0
- data/jobs.rb +17 -0
- data/lib/check-taskr.rb +5 -0
- data/lib/check-taskr/app.rb +73 -0
- data/lib/check-taskr/base.rb +149 -0
- data/lib/check-taskr/cli.rb +26 -0
- data/lib/check-taskr/task/http_code.rb +71 -0
- data/lib/check-taskr/task/http_json.rb +75 -0
- data/lib/check-taskr/task/http_result.rb +71 -0
- data/lib/check-taskr/task/sockets.rb +61 -0
- data/test/helper.rb +9 -0
- data/test/test_check-runner.rb +7 -0
- metadata +145 -0
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 crazycode
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
= check-runner
|
2
|
+
|
3
|
+
Description goes here.
|
4
|
+
|
5
|
+
== Note on Patches/Pull Requests
|
6
|
+
|
7
|
+
* Fork the project.
|
8
|
+
* Make your feature addition or bug fix.
|
9
|
+
* Add tests for it. This is important so I don't break it in a
|
10
|
+
future version unintentionally.
|
11
|
+
* Commit, do not mess with rakefile, version, or history.
|
12
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
13
|
+
* Send me a pull request. Bonus points for topic branches.
|
14
|
+
|
15
|
+
== Copyright
|
16
|
+
|
17
|
+
Copyright (c) 2010 crazycode. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require 'rubygems'
|
3
|
+
require 'rake'
|
4
|
+
|
5
|
+
begin
|
6
|
+
require 'jeweler'
|
7
|
+
Jeweler::Tasks.new do |gem|
|
8
|
+
gem.name = "check-taskr"
|
9
|
+
gem.executables = %W(check-taskr)
|
10
|
+
gem.summary = %Q{check taskr}
|
11
|
+
gem.description = %Q{check taskr for sdo}
|
12
|
+
gem.email = "crazycode@gmail.com"
|
13
|
+
gem.homepage = "http://github.com/crazycode/check-taskr"
|
14
|
+
gem.authors = ["crazycode"]
|
15
|
+
|
16
|
+
gem.add_dependency "sinatra"
|
17
|
+
gem.add_dependency "fastthread"
|
18
|
+
gem.add_dependency "json"
|
19
|
+
gem.add_dependency "haml"
|
20
|
+
gem.add_dependency "log4r"
|
21
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
22
|
+
end
|
23
|
+
Jeweler::GemcutterTasks.new
|
24
|
+
rescue LoadError
|
25
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
26
|
+
end
|
27
|
+
|
28
|
+
require 'rake/testtask'
|
29
|
+
Rake::TestTask.new(:test) do |test|
|
30
|
+
test.libs << 'lib' << 'test'
|
31
|
+
test.pattern = 'test/**/test_*.rb'
|
32
|
+
test.verbose = true
|
33
|
+
end
|
34
|
+
|
35
|
+
begin
|
36
|
+
require 'rcov/rcovtask'
|
37
|
+
Rcov::RcovTask.new do |test|
|
38
|
+
test.libs << 'test'
|
39
|
+
test.pattern = 'test/**/test_*.rb'
|
40
|
+
test.verbose = true
|
41
|
+
end
|
42
|
+
rescue LoadError
|
43
|
+
task :rcov do
|
44
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
task :test => :check_dependencies
|
49
|
+
|
50
|
+
task :default => :test
|
51
|
+
|
52
|
+
require 'rake/rdoctask'
|
53
|
+
Rake::RDocTask.new do |rdoc|
|
54
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
55
|
+
|
56
|
+
rdoc.rdoc_dir = 'rdoc'
|
57
|
+
rdoc.title = "check-taskr #{version}"
|
58
|
+
rdoc.rdoc_files.include('README*')
|
59
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
60
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.1.0
|
data/bin/check-taskr
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# -*- coding: utf-8 -*-
|
3
|
+
|
4
|
+
# for development
|
5
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
6
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
7
|
+
|
8
|
+
|
9
|
+
require 'rubygems'
|
10
|
+
require 'sinatra'
|
11
|
+
|
12
|
+
require 'log4r'
|
13
|
+
|
14
|
+
require "log4r/outputter/datefileoutputter"
|
15
|
+
|
16
|
+
include Log4r
|
17
|
+
|
18
|
+
# create a logger named 'mylog' that logs to stdout
|
19
|
+
log = Logger.new 'default'
|
20
|
+
log.outputters = Log4r::DateFileOutputter.new('check_log', :dirname => "./logs")
|
21
|
+
log.level = WARN
|
22
|
+
|
23
|
+
require "check-taskr"
|
24
|
+
|
25
|
+
CheckTaskr::Cli.execute
|
26
|
+
config = CheckTaskr::JobsConfiguration.instance
|
27
|
+
|
28
|
+
CheckTaskr::App.run! :port => config.listen_port
|
data/jobs.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
JobsConfiguration.init(:port => 4899) do |check|
|
3
|
+
check.log_level WARN
|
4
|
+
|
5
|
+
check.setup_tcp_port :error_code => 10231
|
6
|
+
|
7
|
+
check.tcp_port "HudsonServer", :hosts => "10.241.12.38", :port => 8099
|
8
|
+
|
9
|
+
check.tcp_port "NotExistsDB", :hosts => "10.251.251.38", :port => 18299, :error_msg => "这一服务没有打开"
|
10
|
+
|
11
|
+
check.http_returncode "HudsonWeb", :hosts => ["10.241.12.38", "10.241.12.40"],
|
12
|
+
:port => 8099, :error_code => 909915
|
13
|
+
|
14
|
+
check.http_json "Sujie", :hosts => ["10.241.38.75", "10.241.38.22", "10.241.12.38", "10.241.14.35"],
|
15
|
+
:port => 8080, :path => "/admin/msg_admin_check_status", :error_code => 324234
|
16
|
+
|
17
|
+
end
|
data/lib/check-taskr.rb
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require "haml"
|
3
|
+
require 'sinatra/base'
|
4
|
+
require "check-taskr"
|
5
|
+
require "json"
|
6
|
+
|
7
|
+
module CheckTaskr
|
8
|
+
|
9
|
+
class App < Sinatra::Base
|
10
|
+
before do
|
11
|
+
content_type :html, 'charset' => 'utf-8'
|
12
|
+
end
|
13
|
+
|
14
|
+
get '/' do
|
15
|
+
redirect '/stats.html'
|
16
|
+
end
|
17
|
+
|
18
|
+
get '/stats' do
|
19
|
+
config = CheckTaskr::JobsConfiguration.instance
|
20
|
+
config.results.to_json
|
21
|
+
end
|
22
|
+
|
23
|
+
get '/lock' do
|
24
|
+
config = CheckTaskr::JobsConfiguration.instance
|
25
|
+
config.lock
|
26
|
+
redirect '/stats.html'
|
27
|
+
end
|
28
|
+
|
29
|
+
get '/unlock' do
|
30
|
+
config = CheckTaskr::JobsConfiguration.instance
|
31
|
+
config.unlock
|
32
|
+
redirect '/stats.html'
|
33
|
+
end
|
34
|
+
|
35
|
+
get '/stats.html' do
|
36
|
+
config = CheckTaskr::JobsConfiguration.instance
|
37
|
+
@result = config.results
|
38
|
+
@locked = config.locked
|
39
|
+
haml <<HAML
|
40
|
+
%html
|
41
|
+
%head
|
42
|
+
%title 业务自检
|
43
|
+
%style <!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}-->
|
44
|
+
%body
|
45
|
+
%h1.title 自检结果
|
46
|
+
- if @locked
|
47
|
+
%p 现在不再执行自动检查,自检结果保持为最后一次检查的结果.
|
48
|
+
%a{:href => "/unlock"} 重新开始检查
|
49
|
+
- else
|
50
|
+
%a{:href => "/lock"} 锁定检查结果(发布时避免影响检查结果)
|
51
|
+
%br
|
52
|
+
%table{:width=>"98%", :border=>1}
|
53
|
+
%tr
|
54
|
+
%th 名称
|
55
|
+
%th IP
|
56
|
+
%th 状态码
|
57
|
+
%th error_id
|
58
|
+
%th 时间戳
|
59
|
+
%th msg
|
60
|
+
- @result.each do |name, hash|
|
61
|
+
- ts = hash[:timestamp] || hash["timestamps"] || 0
|
62
|
+
%tr
|
63
|
+
%td= name
|
64
|
+
%td{:align=>"center"}= hash[:ip] || hash["ip"] || " "
|
65
|
+
%td{:align=>"center"}= hash[:stat] || hash["stat"] || " "
|
66
|
+
%td{:align=>"center"}= hash[:error_id] || hash["error_id"] || " "
|
67
|
+
%td{:align=>"center"}= hash[:timestamp] || hash["timestamp"] || " "
|
68
|
+
%td= hash[:msg] || hash["msg"] || " "
|
69
|
+
HAML
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
@@ -0,0 +1,149 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require 'singleton'
|
3
|
+
|
4
|
+
module CheckTaskr
|
5
|
+
|
6
|
+
class JobsAction
|
7
|
+
attr_accessor :name
|
8
|
+
|
9
|
+
@@default_error_code = nil
|
10
|
+
@@default_error_msg = nil
|
11
|
+
def self.setup(options={})
|
12
|
+
@@default_error_code = options[:default_error_code]
|
13
|
+
@@default_error_msg = options[:default_error_msg]
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class JobsConfiguration
|
18
|
+
|
19
|
+
include Singleton
|
20
|
+
|
21
|
+
attr_accessor :sleep_time, :results, :listen_port, :locked
|
22
|
+
attr_reader :load_paths, :actions
|
23
|
+
|
24
|
+
def initialize
|
25
|
+
@actions = []
|
26
|
+
@sleep_time = 8
|
27
|
+
@results = Hash.new
|
28
|
+
@listen_port = 4567
|
29
|
+
@locked = false
|
30
|
+
end
|
31
|
+
|
32
|
+
def add_item(item)
|
33
|
+
if @items.nil?
|
34
|
+
@items = Array.new
|
35
|
+
end
|
36
|
+
@items.add(item)
|
37
|
+
end
|
38
|
+
|
39
|
+
def lock
|
40
|
+
@locked = true
|
41
|
+
end
|
42
|
+
|
43
|
+
def unlock
|
44
|
+
@locked = false
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.init(options = {})
|
48
|
+
_instance = self.instance
|
49
|
+
_instance.sleep_time = options[:sleep_time] || 8
|
50
|
+
_instance.listen_port = options[:port] || 4567
|
51
|
+
if block_given?
|
52
|
+
yield _instance
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def execute_all
|
57
|
+
return if @locked
|
58
|
+
log = Logger['default']
|
59
|
+
|
60
|
+
results = Hash.new
|
61
|
+
had_error = false
|
62
|
+
|
63
|
+
fail_actions = run_actions(@actions, results)
|
64
|
+
# 如果有失败,过0.1秒后重试失败的
|
65
|
+
if fail_actions.size > 0
|
66
|
+
sleep(0.1)
|
67
|
+
fail_actions2 = run_actions(fail_actions, results)
|
68
|
+
if fail_actions2.size > 0
|
69
|
+
# 还失败的话,过1秒后重新试一次
|
70
|
+
sleep(1)
|
71
|
+
run_actions(fail_actions2, results)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
@results.clear
|
75
|
+
@results = results
|
76
|
+
end
|
77
|
+
|
78
|
+
def run_actions(actions, results)
|
79
|
+
fail_actions = []
|
80
|
+
log = Logger['default']
|
81
|
+
actions.each do |action|
|
82
|
+
hash = action.execute
|
83
|
+
unless hash.nil?
|
84
|
+
if hash["ip"].nil? && hash[:ip].nil?
|
85
|
+
hash.each do |k, v|
|
86
|
+
unless (v["ip"].nil? && v[:ip].nil?)
|
87
|
+
results["#{action.name}_#{k}"] = v
|
88
|
+
end
|
89
|
+
end
|
90
|
+
else
|
91
|
+
results[action.name] = hash
|
92
|
+
end
|
93
|
+
state_code = hash[:stat] || hash['stat']
|
94
|
+
if !"0".eql?(state_code) && !0.eql?(state_code)
|
95
|
+
log.error "#{Time.now}:#{hash.to_json}"
|
96
|
+
fail_actions << action
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
fail_actions
|
101
|
+
end
|
102
|
+
|
103
|
+
def load_from_file(file, name=nil)
|
104
|
+
file = find_file_in_load_path(file) unless File.file?(file)
|
105
|
+
string = File.read(file)
|
106
|
+
instance_eval(string, name || "<eval>")
|
107
|
+
end
|
108
|
+
|
109
|
+
|
110
|
+
# set log level
|
111
|
+
def log_level(level)
|
112
|
+
log = Logger['default']
|
113
|
+
log.level = level
|
114
|
+
end
|
115
|
+
|
116
|
+
# process hosts from options
|
117
|
+
def process_hosts(options)
|
118
|
+
log = Logger['default']
|
119
|
+
hosts = options.delete(:hosts)
|
120
|
+
if block_given?
|
121
|
+
if hosts.nil?
|
122
|
+
throw Exception.new("Must include :hosts option")
|
123
|
+
end
|
124
|
+
if hosts.class.eql?(String)
|
125
|
+
yield hosts
|
126
|
+
else
|
127
|
+
hosts.each do |host|
|
128
|
+
yield host
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
private
|
135
|
+
|
136
|
+
def find_file_in_load_path(file)
|
137
|
+
[".", File.expand_path(File.join(File.dirname(__FILE__), "../recipes"))].each do |path|
|
138
|
+
["", ".rb"].each do |ext|
|
139
|
+
name = File.join(path, "#{file}#{ext}")
|
140
|
+
return name if File.file?(name)
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
raise LoadError, "no such file to load -- #{file}"
|
145
|
+
end
|
146
|
+
|
147
|
+
end
|
148
|
+
|
149
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require "fastthread"
|
3
|
+
require "check-taskr/base"
|
4
|
+
|
5
|
+
Dir[File.join(File.dirname(__FILE__), 'task/*.rb')].sort.each { |lib| require lib }
|
6
|
+
|
7
|
+
module CheckTaskr
|
8
|
+
class Cli
|
9
|
+
|
10
|
+
def self.execute(options = {})
|
11
|
+
|
12
|
+
config = CheckTaskr::JobsConfiguration.instance
|
13
|
+
config.load_from_file("jobs")
|
14
|
+
|
15
|
+
t = Thread.new do
|
16
|
+
while true do
|
17
|
+
hash = config.execute_all
|
18
|
+
sleep(config.sleep_time)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require 'net/http'
|
3
|
+
|
4
|
+
module CheckTaskr
|
5
|
+
|
6
|
+
class JobsConfiguration
|
7
|
+
|
8
|
+
def setup_http_returncode(options)
|
9
|
+
HttpReturnCodeAction.setup(options)
|
10
|
+
end
|
11
|
+
|
12
|
+
def http_returncode(name, options = {})
|
13
|
+
process_hosts(options) do |host|
|
14
|
+
action = HttpReturnCodeAction.new({:name => "#{name}-#{host}", :ip => host}.merge(options))
|
15
|
+
@actions << action
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
class HttpReturnCodeAction < JobsAction
|
21
|
+
attr_accessor :ip, :port, :path, :method, :post_data, :expect_code, :error_code, :error_msg
|
22
|
+
|
23
|
+
include Socket::Constants
|
24
|
+
|
25
|
+
def initialize(options)
|
26
|
+
@name ||= options[:name]
|
27
|
+
@ip = options[:ip]
|
28
|
+
@port = options[:port] || 80
|
29
|
+
@path = options[:path] || "/"
|
30
|
+
@method = options[:method] || :get
|
31
|
+
@post_data = options[:post_data]
|
32
|
+
@expect_code = options[:expect_code] || "200" #默认期望返回200
|
33
|
+
@error_code = options[:error_code] || @@default_error_code
|
34
|
+
@error_msg = options[:error_msg] || @@default_error_msg
|
35
|
+
end
|
36
|
+
|
37
|
+
def execute
|
38
|
+
log = Logger['default']
|
39
|
+
log.debug "http action: ip=#{@ip}, port=#{@port}, name=#{@name}"
|
40
|
+
hash = {:stat => 0, :ip => @ip, :msg => "OK", :error_id => @error_code }
|
41
|
+
begin
|
42
|
+
Net::HTTP.start(@ip, @port) do |http|
|
43
|
+
http.read_timeout = 5
|
44
|
+
if @method == :get
|
45
|
+
response = http.get(@path)
|
46
|
+
end
|
47
|
+
case @method
|
48
|
+
when :get
|
49
|
+
response = http.get(@path)
|
50
|
+
when :post
|
51
|
+
response = http.post(@path, @post_data)
|
52
|
+
end
|
53
|
+
code = response.code
|
54
|
+
hash[:timestamp] = Time.now.to_i
|
55
|
+
unless @expect_code.eql?(code)
|
56
|
+
hash[:stat] = 1
|
57
|
+
hash[:msg] = "HTTP #{@method.to_s} #{@path}期望返回#{@expect_code},但返回#{code}"
|
58
|
+
log.warn hash.to_json
|
59
|
+
end
|
60
|
+
end
|
61
|
+
rescue Exception => e
|
62
|
+
hash[:stat] = 2
|
63
|
+
hash[:timestamp] = Time.now.to_i
|
64
|
+
hash[:msg] = "HTTP #{@method.to_s} #{@path}出现异常:#{e}"
|
65
|
+
log.error hash.to_json
|
66
|
+
end
|
67
|
+
hash
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require 'net/http'
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
module CheckTaskr
|
6
|
+
|
7
|
+
class JobsConfiguration
|
8
|
+
|
9
|
+
def setup_http_json(options)
|
10
|
+
HttpJsonAction.setup(options)
|
11
|
+
end
|
12
|
+
|
13
|
+
def http_json(name, options = {})
|
14
|
+
process_hosts(options) do |host|
|
15
|
+
action = HttpJsonAction.new({:name => "#{name}-#{host}", :ip => host}.merge(options))
|
16
|
+
@actions << action
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
class HttpJsonAction < JobsAction
|
22
|
+
attr_accessor :ip, :port, :path, :method, :post_data, :error_code, :error_msg
|
23
|
+
|
24
|
+
include Socket::Constants
|
25
|
+
|
26
|
+
def initialize(options)
|
27
|
+
@name ||= options[:name]
|
28
|
+
@ip = options[:ip]
|
29
|
+
@port = options[:port] || 80
|
30
|
+
@path = options[:path] || "/"
|
31
|
+
@method = options[:method] || :get
|
32
|
+
@post_data = options[:post_data]
|
33
|
+
@error_code = options[:error_code] || @@default_error_code
|
34
|
+
@error_msg = options[:error_msg] || @@default_error_msg
|
35
|
+
end
|
36
|
+
|
37
|
+
def execute
|
38
|
+
log = Logger['default']
|
39
|
+
hash = {:stat => 0, :ip => @ip, :msg => "OK", :error_id => @error_code }
|
40
|
+
begin
|
41
|
+
Net::HTTP.start(@ip, @port) do |http|
|
42
|
+
http.read_timeout = 5
|
43
|
+
if @method == :get
|
44
|
+
response = http.get(@path)
|
45
|
+
end
|
46
|
+
case @method
|
47
|
+
when :get
|
48
|
+
response = http.get(@path)
|
49
|
+
when :post
|
50
|
+
response = http.post(@path, @post_data)
|
51
|
+
end
|
52
|
+
body = response.body
|
53
|
+
log.debug "body=#{body}"
|
54
|
+
hash = JSON.load(body)
|
55
|
+
# hash[:timestamp] = Time.now.to_i
|
56
|
+
#if hash["stat"] && hash["stat"].to_i > 0
|
57
|
+
hash.each do |k, v|
|
58
|
+
v[:error_id] = @error_code
|
59
|
+
v[:ip] = @ip
|
60
|
+
end
|
61
|
+
#end
|
62
|
+
end
|
63
|
+
rescue Exception => e
|
64
|
+
hash[:stat] = 2
|
65
|
+
hash[:timestamp] = Time.now.to_i
|
66
|
+
hash[:msg] = "HTTP #{@method.to_s} #{@path}出现异常:#{e}"
|
67
|
+
log.error hash.to_json
|
68
|
+
end
|
69
|
+
hash
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
|
74
|
+
end
|
75
|
+
|
@@ -0,0 +1,71 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require 'net/http'
|
3
|
+
|
4
|
+
module CheckTaskr
|
5
|
+
|
6
|
+
class JobsConfiguration
|
7
|
+
|
8
|
+
def setup_http_result(options)
|
9
|
+
HttpResultAction.setup(options)
|
10
|
+
end
|
11
|
+
|
12
|
+
def check_http_result(name, options = {})
|
13
|
+
process_hosts(options) do |host|
|
14
|
+
action = HttpResultAction.new({:name => "#{name}-#{host}", :ip => host}.merge(options))
|
15
|
+
@actions << action
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
class HttpResultAction < JobsAction
|
21
|
+
attr_accessor :ip, :port, :path, :method, :post_data, :expect_result, :error_code, :error_msg
|
22
|
+
|
23
|
+
include Socket::Constants
|
24
|
+
|
25
|
+
def initialize(options)
|
26
|
+
@name ||= options[:name]
|
27
|
+
@ip = options[:ip]
|
28
|
+
@port = options[:port] || 80
|
29
|
+
@path = options[:path] || "/"
|
30
|
+
@method = options[:method] || :get
|
31
|
+
@post_data = options[:post_data]
|
32
|
+
@expect_result = options[:expect_result] || "200" #默认期望返回200
|
33
|
+
@error_code = options[:error_code] || @@default_error_code
|
34
|
+
@error_msg = options[:error_msg] || @@default_error_msg
|
35
|
+
end
|
36
|
+
|
37
|
+
def execute
|
38
|
+
log = Logger['default']
|
39
|
+
log.debug "http action: ip=#{@ip}, port=#{@port}, name=#{@name}"
|
40
|
+
hash = {:stat => 0, :ip => @ip, :msg => "OK", :error_id => @error_code }
|
41
|
+
begin
|
42
|
+
Net::HTTP.start(@ip, @port) do |http|
|
43
|
+
http.read_timeout = 5
|
44
|
+
if @method == :get
|
45
|
+
response = http.get(@path)
|
46
|
+
end
|
47
|
+
case @method
|
48
|
+
when :get
|
49
|
+
response = http.get(@path)
|
50
|
+
when :post
|
51
|
+
response = http.post(@path, @post_data)
|
52
|
+
end
|
53
|
+
result = response.body
|
54
|
+
hash[:timestamp] = Time.now.to_i
|
55
|
+
unless @expect_result.eql?(result)
|
56
|
+
hash[:stat] = 1
|
57
|
+
hash[:msg] = "HTTP #{@method.to_s} #{@path}期望返回\"#{@expect_result}\",但返回\"#{result}\""
|
58
|
+
log.warn hash.to_json
|
59
|
+
end
|
60
|
+
end
|
61
|
+
rescue Exception => e
|
62
|
+
hash[:stat] = 2
|
63
|
+
hash[:timestamp] = Time.now.to_i
|
64
|
+
hash[:msg] = "HTTP #{@method.to_s} #{@path}出现异常:#{e}"
|
65
|
+
log.error hash.to_json
|
66
|
+
end
|
67
|
+
hash
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require 'socket'
|
3
|
+
require 'timeout'
|
4
|
+
|
5
|
+
module CheckTaskr
|
6
|
+
|
7
|
+
class JobsConfiguration
|
8
|
+
|
9
|
+
def setup_tcp_port(options)
|
10
|
+
SocketAction.setup(options)
|
11
|
+
end
|
12
|
+
|
13
|
+
def tcp_port(name, options = {})
|
14
|
+
process_hosts(options) do |host|
|
15
|
+
action = SocketAction.new(:name => "#{name}-#{host}", :ip => host, :port => options.fetch(:port))
|
16
|
+
action.error_code ||= options[:error_code]
|
17
|
+
action.error_msg ||= options[:error_msg]
|
18
|
+
|
19
|
+
@actions << action
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
class SocketAction < JobsAction
|
25
|
+
attr_accessor :ip, :port, :error_code, :error_msg
|
26
|
+
|
27
|
+
include Socket::Constants
|
28
|
+
|
29
|
+
def initialize(options)
|
30
|
+
@name = options[:name]
|
31
|
+
@ip = options[:ip]
|
32
|
+
@port = options[:port]
|
33
|
+
@error_code = options[:error_code] || @@default_error_code
|
34
|
+
@error_msg = options[:error_msg] || @@default_error_msg
|
35
|
+
end
|
36
|
+
|
37
|
+
def execute
|
38
|
+
log = Logger['default']
|
39
|
+
log.debug "action: ip=#{@ip}, port=#{@port}, name=#{@name}"
|
40
|
+
hash = { :stat => 0, :ip => @ip, :msg => "OK", :timestamp => Time.now.to_i, :error_id => @error_code }
|
41
|
+
begin
|
42
|
+
timeout(5) do
|
43
|
+
socket = Socket.new(AF_INET, SOCK_STREAM, 0) #生成新的套接字
|
44
|
+
sockaddr = Socket.pack_sockaddr_in(@port, @ip)
|
45
|
+
socket.connect(sockaddr)
|
46
|
+
log.debug "Port:#{@ip}:#{@port} is Opend!\n"
|
47
|
+
socket.close
|
48
|
+
end
|
49
|
+
rescue Timeout::Error
|
50
|
+
hash = {:error_id => @error_code, :stat => 2, :ip => @ip, :msg => "网络访问超时", :timestamp => Time.now.to_i }
|
51
|
+
log.error hash.to_json
|
52
|
+
rescue Exception => e
|
53
|
+
hash = {:error_id => @error_code, :stat => 1, :ip => @ip, :msg => @error_msg || e.to_s, :timestamp => Time.now.to_i }
|
54
|
+
log.error hash.to_json
|
55
|
+
end
|
56
|
+
return hash
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
|
61
|
+
end
|
data/test/helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,145 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: check-taskr
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 1
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 1.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- crazycode
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2011-01-07 00:00:00 +08:00
|
18
|
+
default_executable: check-taskr
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: sinatra
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 0
|
30
|
+
version: "0"
|
31
|
+
type: :runtime
|
32
|
+
version_requirements: *id001
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: fastthread
|
35
|
+
prerelease: false
|
36
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
37
|
+
none: false
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
segments:
|
42
|
+
- 0
|
43
|
+
version: "0"
|
44
|
+
type: :runtime
|
45
|
+
version_requirements: *id002
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: json
|
48
|
+
prerelease: false
|
49
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
segments:
|
55
|
+
- 0
|
56
|
+
version: "0"
|
57
|
+
type: :runtime
|
58
|
+
version_requirements: *id003
|
59
|
+
- !ruby/object:Gem::Dependency
|
60
|
+
name: haml
|
61
|
+
prerelease: false
|
62
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
63
|
+
none: false
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
segments:
|
68
|
+
- 0
|
69
|
+
version: "0"
|
70
|
+
type: :runtime
|
71
|
+
version_requirements: *id004
|
72
|
+
- !ruby/object:Gem::Dependency
|
73
|
+
name: log4r
|
74
|
+
prerelease: false
|
75
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
76
|
+
none: false
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
segments:
|
81
|
+
- 0
|
82
|
+
version: "0"
|
83
|
+
type: :runtime
|
84
|
+
version_requirements: *id005
|
85
|
+
description: check taskr for sdo
|
86
|
+
email: crazycode@gmail.com
|
87
|
+
executables:
|
88
|
+
- check-taskr
|
89
|
+
extensions: []
|
90
|
+
|
91
|
+
extra_rdoc_files:
|
92
|
+
- LICENSE
|
93
|
+
- README.rdoc
|
94
|
+
files:
|
95
|
+
- LICENSE
|
96
|
+
- README.rdoc
|
97
|
+
- Rakefile
|
98
|
+
- VERSION
|
99
|
+
- bin/check-taskr
|
100
|
+
- jobs.rb
|
101
|
+
- lib/check-taskr.rb
|
102
|
+
- lib/check-taskr/app.rb
|
103
|
+
- lib/check-taskr/base.rb
|
104
|
+
- lib/check-taskr/cli.rb
|
105
|
+
- lib/check-taskr/task/http_code.rb
|
106
|
+
- lib/check-taskr/task/http_json.rb
|
107
|
+
- lib/check-taskr/task/http_result.rb
|
108
|
+
- lib/check-taskr/task/sockets.rb
|
109
|
+
- test/helper.rb
|
110
|
+
- test/test_check-runner.rb
|
111
|
+
has_rdoc: true
|
112
|
+
homepage: http://github.com/crazycode/check-taskr
|
113
|
+
licenses: []
|
114
|
+
|
115
|
+
post_install_message:
|
116
|
+
rdoc_options: []
|
117
|
+
|
118
|
+
require_paths:
|
119
|
+
- lib
|
120
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
121
|
+
none: false
|
122
|
+
requirements:
|
123
|
+
- - ">="
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
segments:
|
126
|
+
- 0
|
127
|
+
version: "0"
|
128
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
129
|
+
none: false
|
130
|
+
requirements:
|
131
|
+
- - ">="
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
segments:
|
134
|
+
- 0
|
135
|
+
version: "0"
|
136
|
+
requirements: []
|
137
|
+
|
138
|
+
rubyforge_project:
|
139
|
+
rubygems_version: 1.3.7
|
140
|
+
signing_key:
|
141
|
+
specification_version: 3
|
142
|
+
summary: check taskr
|
143
|
+
test_files:
|
144
|
+
- test/helper.rb
|
145
|
+
- test/test_check-runner.rb
|