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 Method Summary collapse

Instance Method Summary collapse

Class Method Details

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

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



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
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/new_relic/ia/cli.rb', line 25

def execute(stdout, arguments=[])
  log.level = Logger::INFO
  @aspects = []
  parser = OptionParser.new do |opts|
    opts.banner = <<-BANNER.gsub(/^ */,'')
    New Relic Infrastructure Agent (IA) version #{NewRelic::IA::VERSION}
    Monitor different aspects of your environment with New Relic RPM.  

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

    aspect: one or more of 'memcached', 'iostat' or 'disk' (more to come)
  BANNER
    opts.separator ""
    opts.on("-a", "--all",
            "use all available aspects") { @aspects = %w[iostat disk memcached] }
    opts.on("-v", "--verbose",
            "debug output") { NewRelic::IA::CLI.log.level = Logger::DEBUG }
    opts.on("-q", "--quiet",
            "quiet output") { NewRelic::IA::CLI.log.level = Logger::ERROR }
    opts.on("-e", "--environment=ENV",
            "use ENV section in newrelic.yml") { |e| @env = e }
    opts.on("--install license_key",
            "create a default newrelic.yml") { |key| return self.install(key, 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
  require_newrelic_rpm        
  NewRelic::Agent.manual_start  :env => @env, :monitor_mode => true, :log => self.log
  # connected? due in a future version
  if not (NewRelic::Agent.instance.connected? rescue true)
    raise InitError, "Unable to connect to RPM server.  Agent not started."
  end
  cli = new
  @aspects.each do | aspect |
    cli.send aspect
  end
  return nil
rescue InitError => e
  stdout.puts e.message
  return 1
end

.level=(l) ⇒ Object



19
20
21
# File 'lib/new_relic/ia/cli.rb', line 19

def level= l
  log.level = l
end

.logObject



15
16
17
# File 'lib/new_relic/ia/cli.rb', line 15

def log
  @log ||= Logger.new LOGFILE
end

Instance Method Details

#diskObject



98
99
100
101
102
# File 'lib/new_relic/ia/cli.rb', line 98

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



91
92
93
94
95
96
# File 'lib/new_relic/ia/cli.rb', line 91

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

#memcachedObject



104
105
106
107
108
109
# File 'lib/new_relic/ia/cli.rb', line 104

def memcached
  require 'new_relic/ia/memcached_sampler'
  s = NewRelic::IA::MemcachedSampler.new
  s.check
  NewRelic::Agent.instance.stats_engine.add_harvest_sampler s
end