aboutsummaryrefslogtreecommitdiff
path: root/lib/longboat
diff options
context:
space:
mode:
authorNat Lasseter <nat.lasseter@york.ac.uk>2020-02-28 18:11:48 +0000
committerNat Lasseter <nat.lasseter@york.ac.uk>2020-02-28 18:11:48 +0000
commit6c192e01dad7e9947c466eff521fe22ac775fcd6 (patch)
tree5ab4c8a7fd92c57d8b02e1009acae173f64f7b36 /lib/longboat
Initial commit
Diffstat (limited to 'lib/longboat')
-rw-r--r--lib/longboat/jobs.rb28
-rw-r--r--lib/longboat/metrics.rb43
2 files changed, 71 insertions, 0 deletions
diff --git a/lib/longboat/jobs.rb b/lib/longboat/jobs.rb
new file mode 100644
index 0000000..eadae03
--- /dev/null
+++ b/lib/longboat/jobs.rb
@@ -0,0 +1,28 @@
+module Longboat
+ module Jobs
+ class << self
+ def collect!
+ start_time = (Time.now - 15 * 60).strftime("%H:%M:%S")
+ raw = `sacct -a -P -o State -S #{start_time}`.lines.map(&:strip)[1..-1]
+
+ tally = Hash.new{0}
+
+ raw.each do |state|
+ tally[state] += 1
+ end
+
+ tally.each do |state, number|
+ Longboat::Metrics.report!(
+ "longboat_slurm_job_state",
+ number,
+ help: "Number of jobs in each state",
+ type: "gauge",
+ labels: {state: state}
+ )
+ end
+ end
+ end
+
+ Longboat::Metrics.register!(self)
+ end
+end
diff --git a/lib/longboat/metrics.rb b/lib/longboat/metrics.rb
new file mode 100644
index 0000000..58b1cbe
--- /dev/null
+++ b/lib/longboat/metrics.rb
@@ -0,0 +1,43 @@
+module Longboat
+ module Metrics
+ class << self
+ def report!(name, value, help: nil, type: nil, labels: {})
+ @metrics ||= {}
+ @metrics[name] ||= {help: help, type: type}
+ @metrics[name][labels] = value
+ end
+
+ def to_s
+ timestamp = (Time.now.to_f * 1000).to_i
+ res = ""
+ (@metrics || {}).each do |name, metric|
+ res << "#HELP #{name} #{metric[:help]}\n" unless metric[:help].nil?
+ res << "#TYPE #{name} #{metric[:type]}\n" unless metric[:type].nil?
+
+ metric.each do |labels, value|
+ next if labels == :help
+ next if labels == :type
+ labellist = []
+ labels.each do |k, v|
+ labellist << "#{k}=\"#{v}\""
+ end
+ labellist = labellist.join(",")
+ res << "#{name}{#{labellist}} #{value} #{timestamp}\n"
+ end
+ end
+ res
+ end
+
+ def register!(collector)
+ @collectors ||= []
+ @collectors << collector
+ end
+
+ def collect!
+ (@collectors || []).each do |collector|
+ collector.collect!
+ end
+ end
+ end
+ end
+end