aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorNat Lasseter <nat.lasseter@york.ac.uk>2020-03-03 14:25:32 +0000
committerNat Lasseter <nat.lasseter@york.ac.uk>2020-03-03 14:25:32 +0000
commitab6e28417a9195fe03b8355a808a81276d0435ff (patch)
tree3bf903f2dea07ac27b7de2168e76182ce0974a0c /lib
parent220ebc31c057bbf47eec1cccbbae8b3cf2190d24 (diff)
Added command line config
Diffstat (limited to 'lib')
-rw-r--r--lib/jobs/slurm_job_states.rb5
-rw-r--r--lib/longboat.rb4
-rw-r--r--lib/longboat/config.rb16
-rw-r--r--lib/longboat/jobs.rb16
-rw-r--r--lib/longboat/server.rb12
5 files changed, 44 insertions, 9 deletions
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!