Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an optional agent name #31

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions attributes/plugin-agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@
default['newrelic-ng']['plugin-agent']['config_file'] = '/etc/newrelic/newrelic-plugin-agent.cfg'
default['newrelic-ng']['plugin-agent']['mode'] = 00640
default['newrelic-ng']['plugin-agent']['pip_package'] = 'newrelic-plugin-agent'
default['newrelic-ng']['plugin-agent']['agent_name'] = 'default'
85 changes: 80 additions & 5 deletions providers/plugin_agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,19 @@
#

action :configure do
filenames = calculate_filenames

install_prerequisites
generate_config_file filenames
generate_init_script filenames
start_service filenames
end

def defaulted? attribute
new_resource.send(attribute.to_sym) == node['newrelic-ng']['plugin-agent'][attribute]
end

def install_prerequisites
# postgresql and pgbouner need pg_config
if new_resource.service_config.include? 'postgresql:' or
new_resource.service_config.include? 'pgbouncer:'
Expand All @@ -34,9 +46,49 @@
python_pip 'newrelic_plugin_agent[mongodb]' if new_resource.service_config.include? 'mongodb:'
python_pip 'newrelic_plugin_agent[pgbouncer]' if new_resource.service_config.include? 'pgbouncer:'
python_pip 'newrelic_plugin_agent[postgresql]' if new_resource.service_config.include? 'postgresql:'
end

def calculate_filenames
pidfile = if defaulted? :agent_name
new_resource.pidfile
elsif defaulted? :pidfile
"/var/run/newrelic/newrelic-plugin-agent-#{new_resource.agent_name}.pid"
else
new_resource.pidfile
end

r = template new_resource.config_file do
logfile = if defaulted? :agent_name
new_resource.logfile
elsif defaulted? :logfile
"/var/log/newrelic/newrelic-plugin-agent-#{new_resource.agent_name}.log"
else
new_resource.logfile
end

config_file = if defaulted? :agent_name
new_resource.logfile
elsif defaulted? :config_file
"/etc/newrelic/newrelic-plugin-agent-#{new_resource.agent_name}.cfg"
else
new_resource.config_file
end

service_name = if defaulted? :agent_name
'newrelic-plugin-agent'
else
"newrelic-plugin-agent-#{new_resource.agent_name}"
end

{
pidfile: pidfile,
logfile: logfile,
config_file: config_file,
service_name: service_name
}
end

def generate_config_file filenames
r = template filenames[:config_file] do
cookbook new_resource.cookbook
source new_resource.source
owner new_resource.owner
Expand All @@ -46,16 +98,39 @@
variables license_key: new_resource.license_key,
poll_interval: new_resource.poll_interval,
user: new_resource.owner,
pidfile: new_resource.pidfile,
logfile: new_resource.logfile,
pidfile: filenames[:pidfile],
logfile: filenames[:logfile],
service_config: new_resource.service_config
end

new_resource.updated_by_last_action(true) if r.updated_by_last_action?
end

def generate_init_script filenames
unless defaulted? :agent_name
init_script_template = value_for_platform_family(
rhel: 'plugin-agent-init-rhel.erb',
debian: 'plugin-agent-init-deb.erb'
)

# deploy initscript
i = template "/etc/init.d/#{filenames[:service_name]}" do
mode 00755
cookbook 'newrelic-ng'
source init_script_template
variables config_file: filenames[:config_file],
user: new_resource.owner,
group: new_resource.group
end

new_resource.updated_by_last_action(true) if i.updated_by_last_action?
end
end

service 'newrelic-plugin-agent' do
def start_service filenames
service filenames[:service_name] do
supports status: true, restart: true
subscribes :restart, "template[#{new_resource.config_file}]"
subscribes :restart, "template[#{filenames[:config_file]}]"
action [ :enable, :start ]
end
end
1 change: 1 addition & 0 deletions resources/plugin_agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
attribute :pidfile, kind_of: String, default: node['newrelic-ng']['plugin-agent']['pidfile']
attribute :logfile, kind_of: String, default: node['newrelic-ng']['plugin-agent']['logfile']
attribute :service_config, kind_of: String, default: node['newrelic-ng']['plugin-agent']['service_config']
attribute :agent_name, kind_of: String, default: node['newrelic-ng']['plugin-agent']['default_agent_name']

attribute :owner, kind_of: String, default: node['newrelic-ng']['user']['name']
attribute :group, kind_of: String, default: node['newrelic-ng']['user']['group']
Expand Down
8 changes: 4 additions & 4 deletions templates/default/plugin-agent-init-rhel.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
# Application
APP="/usr/bin/newrelic-plugin-agent"

# PID File
PID_FILE="/var/run/newrelic/newrelic-plugin-agent.pid"

# Additional arguments
OPTS=""

Expand All @@ -30,7 +27,10 @@ if [ ! -f "${CONF}" ]; then
failure $"cannot find newrelic-plugin-agent configuration file: '${CONF}'"
echo
exit 1
fi
fi

# PID File
PIDFILE=$(sed -n -e 's/^[ ]*pidfile[ ]*:[ ]*//p' -e 's/[ ]*$//' ${CONF})

OPTS="-c ${CONF} ${OPTS}"

Expand Down