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 --- Readme.textile | 4 +++- 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 ++++++++++-- longboat | 13 ++++++++----- 7 files changed, 55 insertions(+), 15 deletions(-) create mode 100644 lib/longboat/config.rb diff --git a/Readme.textile b/Readme.textile index aa4750d..d5f5f3a 100644 --- a/Readme.textile +++ b/Readme.textile @@ -9,7 +9,9 @@ Longboat will pick up all jobs in the lib/jobs directory. Each job consists of: * a file with a snake_case name, such as @my_job.rb@ * containing a single class with a CamelCase name matching the file name, such as @MyJob@ * with two methods: -** @initialize@, which takes a single argument of the collector to @report!@ the metrics to +** @initialize@, which takes two arguments of: +*** the collector to @report!@ the metrics to, and +*** a hash containing config relevant to jobs ** @run@, no arguments, which triggers a job run and metric report @Longboat::Collector#report!@ takes as arguments: 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! diff --git a/longboat b/longboat index a2d1de5..00e4ce5 100755 --- a/longboat +++ b/longboat @@ -3,11 +3,14 @@ $LOAD_PATH << './lib' require 'longboat' -collector = Longboat::Collector.new +# Handle command line options +config = Longboat::Config.parse! -jobs = Longboat::Jobs.new -jobs.load(collector) +# Start collection +collector = Longboat::Collector.new +jobs = Longboat::Jobs.new(collector, config) +jobs.load! t = jobs.collect_every -require 'longboat/server' -Longboat::Server.serve!(collector) +# Serve metrics on HTTP +Longboat::Server.serve!(collector, config) -- cgit v1.2.1