bin/metrics-iostat-extended.rb in sensu-plugins-io-checks-0.0.3 vs bin/metrics-iostat-extended.rb in sensu-plugins-io-checks-1.0.0
- old
+ new
@@ -27,10 +27,11 @@
# Released under the same terms as Sensu (the MIT license); see LICENSE
# for details.
#
require 'sensu-plugin/metric/cli'
+require 'open3'
require 'socket'
class IOStatExtended < Sensu::Plugin::Metric::CLI::Graphite
option :scheme,
description: 'Metric naming scheme, text to prepend to .$parent.$child',
@@ -93,23 +94,30 @@
end
stats
end
def run
- cmd = "iostat -x #{config[:interval]} 2"
+ cmd = 'iostat'
+ args = ['-x', config[:interval].to_s, '2']
+ args.push('-N') if config[:mappernames]
+ args.push(File.basename(config[:disk])) if config[:disk]
- cmd += " #{File.basename(config[:disk])}" if config[:disk]
- if config[:excludedisk]
- config[:excludedisk].each do |disk|
- cmd += " | grep -v #{disk}"
- end
- end
- cmd += ' -N' if config[:mappernames]
- stats = parse_results(`#{cmd}`)
+ exclude_disk = if config[:excludedisk]
+ config[:excludedisk].map { |d| File.basename(d) }
+ else
+ []
+ end
+ stdin, stdout, stderr = Open3.popen3(cmd, *args, unsetenv_others: true)
+ stdin.close
+ stderr.close
+ stats = parse_results(stdout.gets(nil))
+ stdout.close
+
timestamp = Time.now.to_i
stats.each do |disk, metrics|
+ next if exclude_disk.include? disk
metrics.each do |metric, value|
output [config[:scheme], disk, metric].join('.'), value, timestamp
end
end
ok