Class: NewrelicSphinx::Agent

Inherits:
Object
  • Object
show all
Defined in:
lib/newrelic_sphinx/agent.rb

Instance Method Summary collapse

Constructor Details

#initialize(configuration = {}) ⇒ Agent

Returns a new instance of Agent.



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
# File 'lib/newrelic_sphinx/agent.rb', line 31

def initialize(configuration = {})
  # If you use localhost, MySQL insists on a socket connection, but Sphinx
  # requires a TCP connection. Using 127.0.0.1 fixes that.
  address = configuration[:host] || '127.0.0.1'
  address = '127.0.0.1' if address == 'localhost'

  options = {
    :host      => address,
    :port      => configuration[:port] || 9306,
    :username  => "root",
    :reconnect => true
  }

  @verbose = !! configuration[:verbose]
  @client = Mysql2::Client.new(options)
  @license_key = configuration[:license_key]
  @host = configuration[:hostname] || `hostname`
  @endpoint = configuration[:endpoint] || "https://platform-api.newrelic.com/platform/v1/metrics"
  @frequenzy = configuration[:frequenzy] || 20
  @component_name = 'Sphinx Stats'
  @component_guid = 'com.github.troelskn.Sphinx'
  @version = '0.0.1'
  @metrics = []
  @metrics << Metric.new("avg_query_wall", "avg/Avg Query Wall Time", "milisecond", :plain)
  @metrics << Metric.new("queries", "general/Queries", "Queries/second", :incremental)
  @metrics << Metric.new("connections", "general/Connections", "connections/second", :incremental)
  @metrics << Metric.new("maxed_out", "error/Maxed out connections", "connections/second", :incremental)
  @metrics << Metric.new("command_search", "commands/Command search", "command/second", :incremental)
  @metrics << Metric.new("command_excerpt", "commands/Command excerpt", "command/second", :incremental)
  @metrics << Metric.new("command_update", "commands/Command update", "command/second", :incremental)
  @metrics << Metric.new("command_keywords", "commands/Command keywords", "command/second", :incremental)
  @metrics << Metric.new("command_persist", "commands/Command persist", "command/second", :incremental)
  @metrics << Metric.new("command_flushattrs", "commands/Command flushattrs", "command/second", :incremental)
end

Instance Method Details

#build_metricsObject



143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/newrelic_sphinx/agent.rb', line 143

def build_metrics
  update_metrics
  struct = {}
  @metrics.each do |m|
    raise "Missing metric #{m.name}" if m.value.nil?
    if m.incremental?
      struct["Component/#{m.name}[#{m.units}]"] = m.value - m.last_value
    else
      struct["Component/#{m.name}[#{m.units}]"] = m.value
    end
  end
  struct
end

#executeObject



75
76
77
78
# File 'lib/newrelic_sphinx/agent.rb', line 75

def execute
  puts "*** execute" if @verbose
  send_stats(build_metrics) if since_last > @frequenzy
end

#runObject



66
67
68
69
70
71
72
73
# File 'lib/newrelic_sphinx/agent.rb', line 66

def run
  update_metrics
  @last = Time.now
  while true
    execute
    sleep 1
  end
end

#send_stats(metrics) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/newrelic_sphinx/agent.rb', line 84

def send_stats(metrics)
  puts "*** send_stats" if @verbose
  data = {
    'agent' => {
      'host' => @host,
      'version' => @version,
    },
    'components' => [
      {
        'name' => @component_name,
        'guid' => @component_guid,
        'duration' => since_last.round,
        'metrics' => metrics
      }
    ]
  }
  uri = URI.parse(@endpoint)
  request = Net::HTTP::Post.new(uri.request_uri)
  request.body = JSON.pretty_generate(data)
  request["Content-Type"] = "application/json"
  request["Accept"] = "application/json"
  request["X-License-Key"] = @license_key
  http = Net::HTTP.new(uri.hostname, uri.port)
  if uri.scheme == 'https'
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end
  http.set_debug_output $stderr if @verbose
  response = http.start do |http|
    http.request(request)
  end
  update_state if response.kind_of? Net::HTTPSuccess
end

#since_lastObject



80
81
82
# File 'lib/newrelic_sphinx/agent.rb', line 80

def since_last
  Time.now - @last
end

#update_metricsObject



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/newrelic_sphinx/agent.rb', line 125

def update_metrics
  data_source = {}
  @client.query("show status").each do |row|
    key = row.values[0]
    value = row.values[1]
    if value == "OFF"
      data_source[key] = nil
    elsif value =~ /^\d+$/
      data_source[key] = value.to_i
    else
      data_source[key] = value.to_f
    end
  end
  @metrics.each do |m|
    m.value = data_source[m.key]
  end
end

#update_stateObject



118
119
120
121
122
123
# File 'lib/newrelic_sphinx/agent.rb', line 118

def update_state
  @last = Time.now
  @metrics.each do |m|
    m.memorize if m.incremental?
  end
end