Last active
December 28, 2017 05:40
-
-
Save warmwaffles/99da77b72fd9d027926cabd18c9ce3ea to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
HOSTNAME = %x{hostname}.chomp | |
PROM_FILE = ARGV[0] != 'test' ? '/var/lib/node_exporter/textfile_collector/smartctl.prom' : '/tmp/smartctl.prom' | |
DEVICES = [ | |
'/dev/sda', | |
'/dev/sdb', | |
'/dev/sdc', | |
'/dev/sdd', | |
'/dev/sde', | |
'/dev/sdf', | |
'/dev/sdg', | |
'/dev/sdh', | |
'/dev/sdi', | |
'/dev/sdj', | |
'/dev/sdk', | |
'/dev/sdl', | |
'/dev/sdm' | |
] | |
class Attribute | |
attr_accessor :id, :name, :flag, :value, :worst, :threshold, :type, :updated, | |
:when_failed, :raw_value, :device | |
def self.from_list(device, list) | |
model = new(device) | |
model.id = list[0].to_i | |
model.name = list[1].downcase.tr('-','_') | |
model.flag = list[2] | |
model.value = list[3].to_i | |
model.worst = list[4].to_i | |
model.threshold = list[5].to_i | |
model.type = list[6].downcase | |
model.updated = list[7].downcase | |
model.when_failed = list[8] == '-' ? 0 : list[8] | |
model.raw_value = list[9].to_i | |
model | |
end | |
def initialize(device) | |
@device = device | |
end | |
def to_exposition | |
<<~TXT | |
smartctl_#{name}_threshold{host="#{HOSTNAME}",device="#{device}",id="#{id}",flag="#{flag}",updated="#{updated}",type="#{type}"} #{threshold} | |
smartctl_#{name}_worst{host="#{HOSTNAME}",device="#{device}",id="#{id}",flag="#{flag}",updated="#{updated}",type="#{type}"} #{worst} | |
smartctl_#{name}_value{host="#{HOSTNAME}",device="#{device}",id="#{id}",flag="#{flag}",updated="#{updated}",type="#{type}"} #{value} | |
smartctl_#{name}_raw{host="#{HOSTNAME}",device="#{device}",id="#{id}",flag="#{flag}",updated="#{updated}",type="#{type}"} #{raw_value} | |
TXT | |
end | |
end | |
attributes = [] | |
DEVICES.each do |device| | |
result = %x{smartctl -A #{device}} | |
result.each_line do |line| | |
line = line.strip | |
if line =~ %r{\A\d+\s\w+} | |
attributes << Attribute.from_list(device, line.split(/\s+/)) | |
end | |
end | |
end | |
open(PROM_FILE, 'w') do |file| | |
attributes.each do |a| | |
file.puts a.to_exposition | |
end | |
end | |
puts "completed" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Unit] | |
Description=Exports the smart data | |
[Service] | |
Type=oneshot | |
ExecStart=/usr/bin/ruby /usr/src/smartexport/run.rb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Unit] | |
Description=Run the smart export tool for prometheus to consume | |
[Timer] | |
OnUnitActiveSec=30min | |
[Install] | |
WantedBy=timers.target |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment