-
Notifications
You must be signed in to change notification settings - Fork 0
/
slimmemeter.rb
executable file
·46 lines (34 loc) · 1.05 KB
/
slimmemeter.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'optparse'
require_relative 'lib/p1_reader'
require_relative 'lib/rrd'
options = { rrd: ENV['POWER_RRD'] }
parser = OptionParser.new do |opts|
opts.banner = "Usage: #{$PROGRAM_NAME} [options]"
opts.on '-d', '--daemon', 'Daemonize process' do
options[:daemon] = true
end
opts.on '-r', '--rrd FILE', 'Set the RRD file' do |file|
options[:rrd] = file
end
end
parser.parse!
if options[:rrd].nil?
puts 'Please supply the RRD file with -r or by setting the POWER_RRD environment variable.'
exit 1
end
rrd = RRD.new(options[:rrd])
unless rrd.exists?
puts "RRD file #{rrd.filename} does not exist, creating..."
unless rrd.create
puts "Failed to create #{rrd.filename} RRD file."
exit 1
end
end
Process.daemon(true) if options[:daemon]
P1Reader.read(wait: 60) do |telegram|
delivered_currently = (telegram.delivered_currently * 1000).to_i
returned_currently = (telegram.returned_currently * 1000).to_i
rrd.update(telegram.time, delivered_currently, returned_currently)
end