Class: Fluent::Exec::CLI
- Inherits:
-
Object
- Object
- Fluent::Exec::CLI
- Defined in:
- lib/fluent-exec.rb
Instance Method Summary collapse
- #exec ⇒ Object
- #init_optp ⇒ Object
-
#initialize ⇒ CLI
constructor
A new instance of CLI.
- #io_copy_thr(from, to, str = nil) ⇒ Object
- #parse!(args) ⇒ Object
- #run(args) ⇒ Object
Constructor Details
#initialize ⇒ CLI
Returns a new instance of CLI.
16 17 18 19 20 21 |
# File 'lib/fluent-exec.rb', line 16 def initialize @host = DEFAULT_OPTS[:host] @port = DEFAULT_OPTS[:port] @tag = DEFAULT_OPTS[:tag] init_optp end |
Instance Method Details
#exec ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/fluent-exec.rb', line 52 def exec env = {} in_str = '' out_str = '' err_str = '' begin_time = Time.now sin, sout, serr, thr = Open3.popen3(env, *@cmd) sin_t = io_copy_thr $stdin, sin sout_t = io_copy_thr sout, $stdout, out_str serr_t = io_copy_thr serr, $stderr, err_str status = thr.value end_time = Time.now #sin_t.join sout_t.join serr_t.join es = status.exitstatus { :command => @cmd, :exitstatus => es, :stdout => out_str, :stderr => err_str, :runtime => end_time - begin_time, } end |
#init_optp ⇒ Object
23 24 25 26 27 28 29 30 |
# File 'lib/fluent-exec.rb', line 23 def init_optp @optp = OptionParser.new @optp. = "Usage: fluent-exec [options] [--] command..." @optp.separator "" @optp.on('-t', '--tag TAG', "Tag to use (default #{DEFAULT_OPTS[:tag]})") {|v| @tag = v} @optp.on('-h', '--host HOST', "Host to send (default #{DEFAULT_OPTS[:host]})") {|v| @host = v} @optp.on('-p', '--port PORT', /^\d+$/, "Port to send (default #{DEFAULT_OPTS[:port]})") {|v| @port = v} end |
#io_copy_thr(from, to, str = nil) ⇒ Object
42 43 44 45 46 47 48 49 50 |
# File 'lib/fluent-exec.rb', line 42 def io_copy_thr(from, to, str=nil) Thread.new do while s = from.read(1024) to.write s str << s if str end to.close end end |
#parse!(args) ⇒ Object
32 33 34 35 36 37 38 39 40 |
# File 'lib/fluent-exec.rb', line 32 def parse!(args) begin @cmd = @optp.order args rescue => ex $stderr.puts ex.to_s $stderr.puts @optp.help exit 1 end end |
#run(args) ⇒ Object
80 81 82 83 84 85 86 87 88 89 |
# File 'lib/fluent-exec.rb', line 80 def run(args) parse! args record = exec begin log = Fluent::Logger::FluentLogger.new(nil, :host => @host, :port => @port) log.post @tag, record rescue => ex end exit record[:exitstatus] end |