Created
December 8, 2014 11:44
-
-
Save nobuhito/8bca9172c5d6a130a968 to your computer and use it in GitHub Desktop.
SensuにCron的なスケジューリングを組み込んでみる ref: http://qiita.com/nobuhito@github/items/12959c28e1899f515617
This file contains 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
> diff -u sensu-0.14.0/lib/sensu/client.rb.org sensu-0.14.0/lib/sensu/client.rb | |
--- sensu-0.14.0/lib/sensu\clien.rb.org 2014-10-16 20:40:50 +0900 | |
+++ sensu-0.14.0/lib/sensu\clien.rb 2014-12-08 19:34:36 +0900 | |
@@ -1,5 +1,6 @@ | |
require 'sensu/daemon' | |
require 'sensu/socket' | |
+require 'rufus/scheduler' | |
module Sensu | |
class Client | |
@@ -102,7 +103,8 @@ | |
started = Time.now.to_f | |
Spawn.process(command, :timeout => check[:timeout]) do |output, stats| | |
check[:duration] = ('%.3f' % (Time.now.to_f - started)).to_f | |
- check[:output] = output | |
+ check[:output] = output.encode('UTF-8', 'UTF-8') | |
+ # check[:outpu] = output | |
check[:status] = status | |
publish_result(check) | |
@checks_in_progress.delete(check[:name]) | |
@@ -204,15 +206,39 @@ | |
end | |
end | |
+ def cron_checks(checks) | |
+ checks.each do |check| | |
+ check[:issued] = Time.now.to_i | |
+ scheduler = Rufus::Scheduler.new | |
+ cron_id = scheduler.cron check[:cron] do |cron| | |
+ unless @state == :paused | |
+ process_check(check) | |
+ end | |
+ @logger.info("cron checks resettings", { | |
+ :name => check[:name], :schedule => cron.next_time | |
+ }) | |
+ end | |
+ cron = scheduler.job(cron_id) | |
+ @logger.info("cron checks settings", { | |
+ :name => check[:name], :schedule => cron.next_time | |
+ }) | |
+ end | |
+ end | |
+ | |
def setup_standalone | |
@logger.debug('scheduling standalone checks') | |
standard_checks = @settings.checks.select do |check| | |
check[:standalone] | |
end | |
extension_checks = @extensions.checks.select do |check| | |
- check[:standalone] && check[:interval].is_a?(Integer) | |
+ check[:standalone] && check[:interval].is_a?(Integer) && !check[:cron] | |
end | |
schedule_checks(standard_checks + extension_checks) | |
+ | |
+ crons_checks = @extensions.checks.select do |check| | |
+ check[:cron] | |
+ end | |
+ cron_checks(crons_checks) | |
end | |
def setup_sockets |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment