Class: NewRelic::IA::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/new_relic/ia/cli.rb

Constant Summary collapse

LOGFILE =
"newrelic_ia.log"

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.logObject

Returns the value of attribute log.



10
11
12
# File 'lib/new_relic/ia/cli.rb', line 10

def log
  @log
end

Class Method Details

.execute(stdout, arguments = []) ⇒ Object

Run the command line args. Return nil if running or an exit status if not.



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
47
48
49
50
51
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
# File 'lib/new_relic/ia/cli.rb', line 17

def execute(stdout, arguments=[])
  @aspects = []
  @log = Logger.new LOGFILE
  @log_level = Logger::INFO
  parser = OptionParser.new do |opts|
    opts.banner = <<-BANNER.gsub(/^ */,'')

      Monitor different aspects of your environment with New Relic RPM.  

      Usage: #{File.basename($0)} [ options ] aspect, aspect.. 

      aspect: one or more of 'iostat' or 'disk' (more to come)
    BANNER
    opts.separator ""
    opts.on("-a", "--all",
            "use all available aspects") { @aspects = %w[iostat disk] }
    opts.on("-v", "--verbose",
            "debug output") { @log_level = Logger::DEBUG }
    opts.on("-q", "--quiet",
            "quiet output") { @log_level = Logger::ERROR }
    opts.on("-e", "--environment=ENV",
            "use ENV section in newrelic.yml") { |e| @env = e }
    opts.on("--install",
            "create a default newrelic.yml") { |e| return self.install(stdout) }
    
    opts.on("-h", "--help",
            "Show this help message.") { stdout.puts "#{opts}\n"; return 0 }
    begin
      args = opts.parse! arguments
      unless args.empty?
        @aspects = args
      end
    rescue => e
      stdout.puts e
      stdout.puts opts
      return 1
    end
  end
  @aspects.delete_if do |aspect|
    unless self.instance_methods(false).include? aspect
      stdout.puts "Unknown aspect: #{aspect}"
      true
    end
  end
  if @aspects.empty?
    stdout.puts "No aspects specified."
    stdout.puts parser
    return 1
  end
  
  @log.level = @log_level 
  gem 'newrelic_rpm'
  require 'newrelic_rpm'
  NewRelic::Agent.manual_start :log => @log, :env => @env, :enabled => true
  cli = new
  @aspects.each do | aspect |
    cli.send aspect
  end
  return nil
end

.level=(l) ⇒ Object



11
12
13
# File 'lib/new_relic/ia/cli.rb', line 11

def level= l
  @log.level = l      
end

Instance Method Details

#diskObject



86
87
88
89
90
# File 'lib/new_relic/ia/cli.rb', line 86

def disk
  self.class.log.info "Starting disk sampler..."
  require 'new_relic/ia/disk_sampler'
  NewRelic::Agent.instance.stats_engine.add_harvest_sampler NewRelic::IA::DiskSampler.new    
end

#iostatObject

Aspect definitions



79
80
81
82
83
84
# File 'lib/new_relic/ia/cli.rb', line 79

def iostat # :nodoc:
  self.class.log.info "Starting iostat monitor..."
  require 'new_relic/ia/iostat_reader'
  reader = NewRelic::IA::IostatReader.new
  Thread.new { reader.run }
end