Module: RunCommand

Included in:
DF, MPstat, Netstat, PS
Defined in:
lib/city_watch/util/run_command.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



81
82
83
84
# File 'lib/city_watch/util/run_command.rb', line 81

def self.included(base)
	base.extend(ClassMethods)
	Commands.register(base)
end

Instance Method Details

#command_lineObject



17
18
19
# File 'lib/city_watch/util/run_command.rb', line 17

def command_line
	@command ||= "#{options[:command]} #{command_line_opts}#{options[:grep] && " | fgrep #{options[:grep]}"}"
end

#command_line_optsObject



21
22
23
24
25
26
# File 'lib/city_watch/util/run_command.rb', line 21

def command_line_opts
	options.inject([]) do |acc,(k,v)|
		acc << "-#{k} #{v}" unless [:command, :grep].include?(k)
		acc
	end.join(" ")
end

#command_outputObject



3
4
5
6
7
8
9
10
11
# File 'lib/city_watch/util/run_command.rb', line 3

def command_output
	unless !supported?
		puts "Running `#{command_line}`..." if CityWatch.debug?
		`#{command_line}`
	else
		puts "Command not present: #{options[:command]} (Skipping)" if CityWatch.debug?
		""
	end
end

#dataObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/city_watch/util/run_command.rb', line 37

def data
	headers = false
	output = []
	command_output.split("\n").map {|line| v = line.split("\s"); v.shift; v }.each do |line|
		if !headers
			headers = line.map {|hdr| hdr.downcase.to_sym}
			next
		end
		next unless headers
		pkt = {}
		line.each_with_index do |itm,idx|
			pkt[headers[idx]] = itm
		end
		output << pkt
	end
	output
end

#optionsObject



28
29
30
# File 'lib/city_watch/util/run_command.rb', line 28

def options
	@opts ||= self.class.options
end

#set_opts(opts = {}) ⇒ Object



32
33
34
35
# File 'lib/city_watch/util/run_command.rb', line 32

def set_opts(opts={})
	@opts = self.class.options.merge(opts)
	@command = nil
end

#supported?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/city_watch/util/run_command.rb', line 13

def supported?
	`which #{options[:command]}` != ""
end