aboutsummaryrefslogtreecommitdiff
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
parent220ebc31c057bbf47eec1cccbbae8b3cf2190d24 (diff)
Added command line config
-rw-r--r--Readme.textile4
-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
-rwxr-xr-xlongboat13
7 files changed, 55 insertions, 15 deletions
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)