Class: Metrix::CLI

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ CLI

Returns a new instance of CLI.



13
14
15
16
17
18
19
# File 'lib/metrix/cli.rb', line 13

def initialize(args)
  @args = args
  @system = false
  @interval = 10
  require "syslog/logger"
  Metrix.logger = Syslog::Logger.new("metrix")
end

Instance Attribute Details

#elastic_search_hostObject (readonly)

Returns the value of attribute elastic_search_host.



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

def elastic_search_host
  @elastic_search_host
end

#intervalObject (readonly)

Returns the value of attribute interval.



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

def interval
  @interval
end

#mongodb_hostObject (readonly)

Returns the value of attribute mongodb_host.



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

def mongodb_host
  @mongodb_host
end

#reporterObject (readonly)

Returns the value of attribute reporter.



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

def reporter
  @reporter
end

Instance Method Details

#elastic_search?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/metrix/cli.rb', line 73

def elastic_search?
  !!@elastic_search
end

#elastic_search_statusObject



85
86
87
88
# File 'lib/metrix/cli.rb', line 85

def elastic_search_status
  Metrix.logger.info "fetching elasticsearch metrix"
  Net::HTTP.get(URI("http://127.0.0.1:9200/_status"))
end

#log_levelObject



65
66
67
# File 'lib/metrix/cli.rb', line 65

def log_level
  @log_level || Logger::INFO
end

#log_to_stdoutObject



104
105
106
107
# File 'lib/metrix/cli.rb', line 104

def log_to_stdout
  Metrix.logger = Logger.new(STDOUT)
  Metrix.logger.level = Logger::INFO
end

#mongodb?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/metrix/cli.rb', line 77

def mongodb?
  !!@mongodb
end

#mongodb_statusObject



90
91
92
93
# File 'lib/metrix/cli.rb', line 90

def mongodb_status
  Metrix.logger.info "fetching mongodb metrix"
  Net::HTTP.get(URI("http://127.0.0.1:28017/serverStatus"))
end

#nginx?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/metrix/cli.rb', line 81

def nginx?
  !!@nginx
end

#nginx_statusObject



95
96
97
98
# File 'lib/metrix/cli.rb', line 95

def nginx_status
  Metrix.logger.info "fetching mongodb metrix"
  Net::HTTP.get(URI("http://127.0.0.1:8000/"))
end

#optsObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/metrix/cli.rb', line 109

def opts
  require "optparse"
  @opts ||= OptionParser.new do |o|
    o.on("-v", "--version") do
      puts "metrix #{Metrix::VERSION}"
      exit
    end

    o.on("--nginx") do
      @nginx = true
    end

    o.on("--mongodb") do
      @mongodb = true
    end

    o.on("--elasticsearch") do
      @elastic_search = true
    end

    o.on("--graphite-host HOST") do |value|
      require "metrix/graphite"
      @reporter = Metrix::Graphite.new(value, 2003)
    end

    o.on("--opentsdb-host HOST") do |value|
      require "metrix/opentsdb"
      @reporter = Metrix::OpenTSDB.new(value, 4242)
    end

    o.on("--stdout") do
      require "metrix/reporter/stdout"
      @reporter = Metrix::Reporter::Stdout.new
      log_to_stdout
    end

    o.on("--debug") do
      @log_level = Logger::DEBUG
    end

    o.on("--no-syslog") do
      log_to_stdout
    end

    o.on("--processes") do
      require "metrix/process"
      @processes = true
    end

    o.on("--system") do
      require "metrix/system"
      @system = true
    end
  end
end

#processes?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/metrix/cli.rb', line 69

def processes?
  !!@processes
end

#runObject



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
# File 'lib/metrix/cli.rb', line 21

def run
  opts.parse(@args)
  Metrix.logger.level = log_level
  if self.reporter.nil?
    puts "ERROR: at least one reporter must be specified"
    abort opts.to_s
  end
  cnt = -1
  started = Time.now
  while true
    begin
      cnt += 1
      now = Time.now.utc
      reporter << Metrix::ElasticSearch.new(elastic_search_status)  if elastic_search?
      reporter << Metrix::Mongodb.new(mongodb_status)               if mongodb?
      reporter << Metrix::Nginx.new(nginx_status)                   if nginx?
      if system?
        reporter << Metrix::System.new(File.read("/proc/stat"))
        reporter << Metrix::Load.new(File.read("/proc/loadavg"))
      end

      if processes?
        Metrix::Process.all.each do |m|
          reporter << m
        end
      end
      reporter.flush
    rescue SystemExit
      exit
    rescue => err
      Metrix.logger.error "#{err.message}"
      Metrix.logger.error "#{err.backtrace.inspect}"
    ensure
      sleep_for = @interval - (Time.now - started - cnt * interval)
      if sleep_for > 0
        Metrix.logger.info "finished run in %.06f, sleeping for %.06f" % [Time.now - now, sleep_for]
        sleep sleep_for
      else
        Metrix.logger.info "not sleeping because %.06f is negative" % [sleep_for]
      end
    end
  end
end

#system?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/metrix/cli.rb', line 100

def system?
  !!@system
end