From ab6e28417a9195fe03b8355a808a81276d0435ff Mon Sep 17 00:00:00 2001 From: Nat Lasseter Date: Tue, 3 Mar 2020 14:25:32 +0000 Subject: Added command line config --- lib/jobs/slurm_job_states.rb | 5 +++-- lib/longboat.rb | 4 +++- lib/longboat/config.rb | 16 ++++++++++++++++ lib/longboat/jobs.rb | 16 ++++++++++++---- lib/longboat/server.rb | 12 ++++++++++-- 5 files changed, 44 insertions(+), 9 deletions(-) create mode 100644 lib/longboat/config.rb (limited to 'lib') diff --git a/lib/jobs/slurm_job_states.rb b/lib/jobs/slurm_job_states.rb index bc3e8a1..9d4ea94 100644 --- a/lib/jobs/slurm_job_states.rb +++ b/lib/jobs/slurm_job_states.rb @@ -1,10 +1,11 @@ class SlurmJobStates - def initialize(collector) + def initialize(collector, config) @collector = collector + @interval = config[:collect_every] end def run - start_time = (Time.now - 60).strftime("%H:%M:%S") + start_time = (Time.now - @interval).strftime("%H:%M:%S") raw = `sacct -a -P -o State -S #{start_time}`.lines.map(&:strip)[1..-1] tally = Hash.new{0} diff --git a/lib/longboat.rb b/lib/longboat.rb index 09c9a98..6383558 100644 --- a/lib/longboat.rb +++ b/lib/longboat.rb @@ -1,2 +1,4 @@ -require 'longboat/jobs' require 'longboat/collector' +require 'longboat/config' +require 'longboat/jobs' +require 'longboat/server' diff --git a/lib/longboat/config.rb b/lib/longboat/config.rb new file mode 100644 index 0000000..874a5be --- /dev/null +++ b/lib/longboat/config.rb @@ -0,0 +1,16 @@ +require 'optimist' + +module Longboat + module Config + def self.parse! + Optimist::options do + # Collection interval + opt :collect_every, "Collection interval", type: Integer, default: 60 + + # Sinatra server + opt :server_bind, "Server listening address", type: String, default: "127.0.0.1:8564" + opt :server_path, "Path to metrics", type: String, default: "/metrics" + end + end + end +end diff --git a/lib/longboat/jobs.rb b/lib/longboat/jobs.rb index c6bbb17..86d3c64 100644 --- a/lib/longboat/jobs.rb +++ b/lib/longboat/jobs.rb @@ -1,10 +1,12 @@ module Longboat class Jobs - def initialize + def initialize(collector, config) + @collector = collector @jobs = [] + @config = config end - def load(collector) + def load! Dir.entries("./lib/jobs/").each do |file| next if file =~ /^\./ @@ -12,7 +14,7 @@ module Longboat cname = reqname.split('_').map(&:capitalize).join require "jobs/#{reqname}" - @jobs << Kernel.const_get(cname).new(collector) + @jobs << Kernel.const_get(cname).new(@collector, job_config) end end @@ -20,7 +22,7 @@ module Longboat @jobs.each(&:run) end - def collect_every(time = 60, async = true) + def collect_every(time = @config[:collect_every], async = true) if async Thread.new do loop do @@ -35,5 +37,11 @@ module Longboat end end end + + private + + def job_config + @config.slice(:collect_every) + end end end diff --git a/lib/longboat/server.rb b/lib/longboat/server.rb index 474a7ed..eb4c56c 100644 --- a/lib/longboat/server.rb +++ b/lib/longboat/server.rb @@ -2,9 +2,17 @@ require 'sinatra/base' module Longboat module Server - def self.serve!(collector) + def self.serve!(collector, config) + addr, port = config[:server_bind].split(":") + addr = "127.0.0.1" if addr.nil? or addr == "" + port = 8564 if port.nil? or port == "8564" + Sinatra.new { - get '/metrics' do + set :bind, addr + set :port, port.to_i + set :environment, :production + + get config[:server_path] do collector.prometheus_metrics end }.run! -- cgit v1.2.1