Last active
December 17, 2015 08:49
-
-
Save mikeda/5582422 to your computer and use it in GitHub Desktop.
Nagiosで複数URLをまとめてHTTP監視するプラグイン
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
#!/usr/local/bin/ruby | |
# -*- encoding: utf-8 -*- | |
require 'net/http' | |
require 'uri' | |
require 'pp' | |
configs = [ | |
{ desc: '正常', url: 'http://mikeda.jp/wiki/', regex: /MikedaWiki/, timeout: 1}, | |
{ desc: '中身がおかしい', url: 'http://mikeda.jp/wiki/', regex: /XXXYYYZZZ/, timeout: 1}, | |
{ desc: '重たい', url: 'http://test01.mikeda.jp/5sec.php', regex: /test/, timeout: 1}, | |
{ desc: 'NotFound', url: 'http://test01.mikeda.jp/notfound', regex: /test/, timeout: 1}, | |
{ desc: '接続できない', url: 'http://s01.mikeda.jp/', regex: /test/, timeout: 1}, | |
{ desc: '名前解決できない', url: 'http://XXX.mikeda.jp', regex: /test/, timeout: 1}, | |
] | |
errors = [] | |
configs.each do |config| | |
# puts "check #{config[:desc]}" | |
url = URI.parse(config[:url]) | |
begin | |
http = Net::HTTP.new(url.host, url.port) | |
http.open_timeout = 1 # とりあえず | |
res = http.start {|http| | |
http.read_timeout = config[:timeout] | |
http.get(url.path) | |
} | |
rescue Timeout::Error | |
errors << "Error #{config[:desc]}: timeout(#{config[:timeout]}sec)" | |
next | |
rescue => e | |
errors << e.to_s | |
next | |
end | |
if res.code != "200" | |
errors << "Error #{config[:desc]}: bad response(#{res.code})" | |
elsif res.body !~ config[:regex] | |
errors << "Error #{config[:desc]}: node include '#{config[:regex].to_s}'" | |
end | |
end | |
pp errors | |
exit errors.empty? ? 0 : 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment