-
Notifications
You must be signed in to change notification settings - Fork 12
/
newrelic_passenger_agent
executable file
·109 lines (73 loc) · 3.21 KB
/
newrelic_passenger_agent
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
77
78
79
80
81
82
83
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
#! /usr/bin/env ruby
#
# This Agent uses the passenger commands to track the number of running
# and active passenger processes as well as their memory usage
#
# The inital version was mainly based on https://github.com/barttenbrinke/munin-plugins-rails
#
require "rubygems"
require "bundler/setup"
require "newrelic_plugin"
require File.join(File.dirname(__FILE__), 'lib/passenger_memory_stats_parser')
require File.join(File.dirname(__FILE__), 'lib/passenger_status_parser')
module PassengerAgent
class ConfigurationError < StandardError ; end
class Agent < NewRelic::Plugin::Agent::Base
agent_guid "net.theimp.newrelic.passenger"
agent_version "0.2.3"
agent_config_options :passenger_status, :passenger_memory_stats, :passenger_version
agent_human_labels("Passenger") { "Passenger on #{`hostname`}" }
#
# This method is called on initializing the Agent and should check configuration and select version depending regexes
#
def setup_metrics
valid_passenger_versions = [3, 4, 5]
raise ConfigurationError, "passenger_version config variable must be set" unless defined?(passenger_version)
raise ConfigurationError, "Passenger version must be one of #{valid_passenger_versions}" unless valid_passenger_versions.include?(passenger_version)
@memory_stats_parser = PassengerMemoryStatsParser.new
@status_parser = PassengerStatusParser.new(passenger_version)
end
def report_passenger_status
@status_parser.output_to_parse = run_command(passenger_status)
report_metric "passenger.processes.max", "Processes", @status_parser.processes_max
report_metric "passenger.processes.running", "Processes", @status_parser.processes_running
report_metric("passenger.processes.active", "Processes", @status_parser.processes_active) if passenger_version.eql?(3)
report_metric "passenger.queue.waiting", "Requests", @status_parser.queue_waiting
report_metric "passenger.sessions.total", "Sessions", @status_parser.sessions_total
end
def report_passenger_memory_stats
@memory_stats_parser.output_to_parse = run_command(passenger_memory_stats)
report_metric "passenger.memory.total", "Megabyte", @memory_stats_parser.passenger_memory_total
@memory_stats_parser.passenger_memory_per_app.each do |app, memory_usage|
report_metric "passenger.memory.#{app}", "Megabyte", memory_usage
end
end
def poll_cycle
report_passenger_status
report_passenger_memory_stats
end
private
def run_command(command)
#uncomment to test example_output files
#return File.read("example_output/#{command.split('/').last}_version-#{passenger_version}")
result = `#{command}`
unless $?.success?
$stderr.puts "failed executing #{command}"
exit 1
end
result
end
end
#
#Starting via daemons needs to load the correct config file
#
NewRelic::Plugin::Config.config_file = File.dirname(__FILE__) + '/config/newrelic_plugin.yml'
#
# Register this agent with the component.
#
NewRelic::Plugin::Setup.install_agent :passenger, PassengerAgent
#
# Launch the agent; this never returns.
#
NewRelic::Plugin::Run.setup_and_run
end