From 666ea91e473554acabe6f6c3477eb915e70a7538 Mon Sep 17 00:00:00 2001 From: Nat Lasseter Date: Mon, 16 Mar 2020 10:15:21 +0000 Subject: Refactor: renamed jobs to raiders, becaus pun, and jobs is confusing given the intended application of longboat by the original authors. --- lib/jobs/slurm_job_states.rb | 41 ---------------------------------- lib/longboat.rb | 2 +- lib/longboat/config.rb | 10 ++++----- lib/longboat/jobs.rb | 49 ----------------------------------------- lib/longboat/raiders.rb | 49 +++++++++++++++++++++++++++++++++++++++++ lib/raiders/slurm_job_states.rb | 41 ++++++++++++++++++++++++++++++++++ 6 files changed, 96 insertions(+), 96 deletions(-) delete mode 100644 lib/jobs/slurm_job_states.rb delete mode 100644 lib/longboat/jobs.rb create mode 100644 lib/longboat/raiders.rb create mode 100644 lib/raiders/slurm_job_states.rb (limited to 'lib') diff --git a/lib/jobs/slurm_job_states.rb b/lib/jobs/slurm_job_states.rb deleted file mode 100644 index 1211f11..0000000 --- a/lib/jobs/slurm_job_states.rb +++ /dev/null @@ -1,41 +0,0 @@ -class SlurmJobStates - def initialize(collector, config) - @collector = collector - @interval = config[:collect_every] - end - - def run - start_time = (Time.now - @interval).strftime("%H:%M:%S") - - # Get raw data from sacct, - # read jobs into an array, - # remove any whitespace from the ends of each string, - # drop the header, - # and split each line into state and partition - raw = `sacct -a -P -o State,Partition -S #{start_time}`. - lines. - map(&:strip)[1..-1]. - map{|l|l.split("|")} - - # Make a tally of each state/partition combo - tally = Hash.new{0} - raw.each do |job| - tally[job] += 1 - end - - # Clean up any previously reported metrics - # to prevent stale labelsets - @collector.redact!("slurm_job_states") - - # Report new metrics - tally.each do |labelset, number| - @collector.report!( - "slurm_job_states", - number, - help: "Number of jobs in each state", - type: "gauge", - labels: {state: labelset[0], partition: labelset[1]} - ) - end - end -end diff --git a/lib/longboat.rb b/lib/longboat.rb index 6383558..bbbac52 100644 --- a/lib/longboat.rb +++ b/lib/longboat.rb @@ -1,4 +1,4 @@ require 'longboat/collector' require 'longboat/config' -require 'longboat/jobs' +require 'longboat/raiders' require 'longboat/server' diff --git a/lib/longboat/config.rb b/lib/longboat/config.rb index 3fd1510..7e793c1 100644 --- a/lib/longboat/config.rb +++ b/lib/longboat/config.rb @@ -5,15 +5,15 @@ module Longboat def self.parse! Optimist::options do # Collection interval - opt :collect_every, "Collection interval", type: Integer, default: 60 + opt :raid_every, "Collection interval", type: Integer, default: 60 # Job data - opt :jobs_path, "Paths to search for jobs", type: String, default: "./lib/jobs", multi: true - opt :metric_prefix, "Prefix for metric names", type: String, default: "longboat_" + opt :raiders_path, "Paths to search for raiders", type: String, default: "./lib/raiders", multi: true + opt :metric_prefix, "Prefix for metric names", type: String, default: "longboat_" # 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" + 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 diff --git a/lib/longboat/jobs.rb b/lib/longboat/jobs.rb deleted file mode 100644 index 5166993..0000000 --- a/lib/longboat/jobs.rb +++ /dev/null @@ -1,49 +0,0 @@ -module Longboat - class Jobs - def initialize(collector, config) - @collector = collector - @jobs = [] - @config = config - - @config[:jobs_path].each do |dir| - next unless Dir.exist?(dir) - - Dir.entries(dir).each do |file| - next if file =~ /^\./ - - reqname = File.basename(file, ".rb") - cname = reqname.split('_').map(&:capitalize).join - - require "jobs/#{reqname}" - @jobs << Kernel.const_get(cname).new(@collector, job_config) - end - end - end - - def collect! - @jobs.each(&:run) - end - - def collect_every(time = @config[:collect_every], async = true) - if async - Thread.new do - loop do - collect! - sleep(time) - end - end - else - loop do - collect! - sleep(time) - end - end - end - - private - - def job_config - @config.slice(:collect_every, :metric_prefix) - end - end -end diff --git a/lib/longboat/raiders.rb b/lib/longboat/raiders.rb new file mode 100644 index 0000000..103cac5 --- /dev/null +++ b/lib/longboat/raiders.rb @@ -0,0 +1,49 @@ +module Longboat + class Raiders + def initialize(collector, config) + @collector = collector + @raiders = [] + @config = config + + @config[:raiders_path].each do |dir| + next unless Dir.exist?(dir) + + Dir.entries(dir).each do |file| + next if file =~ /^\./ + + reqname = File.basename(file, ".rb") + cname = reqname.split('_').map(&:capitalize).join + + require "raiders/#{reqname}" + @raiders << Kernel.const_get(cname).new(@collector, raider_config) + end + end + end + + def raid! + @raiders.each(&:raid) + end + + def raid_every(time = @config[:raid_every], async = true) + if async + Thread.new do + loop do + raid! + sleep(time) + end + end + else + loop do + raid! + sleep(time) + end + end + end + + private + + def raider_config + @config.slice(:raid_every, :metric_prefix) + end + end +end diff --git a/lib/raiders/slurm_job_states.rb b/lib/raiders/slurm_job_states.rb new file mode 100644 index 0000000..20b69c8 --- /dev/null +++ b/lib/raiders/slurm_job_states.rb @@ -0,0 +1,41 @@ +class SlurmJobStates + def initialize(collector, config) + @collector = collector + @interval = config[:raid_every] + end + + def raid + start_time = (Time.now - @interval).strftime("%H:%M:%S") + + # Get raw data from sacct, + # read jobs into an array, + # remove any whitespace from the ends of each string, + # drop the header, + # and split each line into state and partition + raw = `sacct -a -P -o State,Partition -S #{start_time}`. + lines. + map(&:strip)[1..-1]. + map{|l|l.split("|")} + + # Make a tally of each state/partition combo + tally = Hash.new{0} + raw.each do |job| + tally[job] += 1 + end + + # Clean up any previously reported metrics + # to prevent stale labelsets + @collector.redact!("slurm_job_states") + + # Report new metrics + tally.each do |labelset, number| + @collector.report!( + "slurm_job_states", + number, + help: "Number of jobs in each state", + type: "gauge", + labels: {state: labelset[0], partition: labelset[1]} + ) + end + end +end -- cgit v1.2.1