From 9972b2889bcb60db28c8c58814f08ffd64872c7f Mon Sep 17 00:00:00 2001 From: Greg Symons Date: Thu, 4 Sep 2014 00:56:56 -0500 Subject: [PATCH] Add an optional agent name Note that once you're running multiple agents, you need to have unique log, pid, init, and config files for them, otherwise things will get wonky real fast. So unless they're either defaulted or set explicitly, the name will be generated based the agent name. Refactored configure action so the process is clearer. --- attributes/plugin-agent.rb | 1 + providers/plugin_agent.rb | 85 ++++++++++++++++++-- resources/plugin_agent.rb | 1 + templates/default/plugin-agent-init-rhel.erb | 8 +- 4 files changed, 86 insertions(+), 9 deletions(-) diff --git a/attributes/plugin-agent.rb b/attributes/plugin-agent.rb index d32d389..7870d23 100644 --- a/attributes/plugin-agent.rb +++ b/attributes/plugin-agent.rb @@ -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' diff --git a/providers/plugin_agent.rb b/providers/plugin_agent.rb index f15ce6d..08bf9e7 100644 --- a/providers/plugin_agent.rb +++ b/providers/plugin_agent.rb @@ -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:' @@ -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 @@ -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 diff --git a/resources/plugin_agent.rb b/resources/plugin_agent.rb index 7571bac..a0a2809 100644 --- a/resources/plugin_agent.rb +++ b/resources/plugin_agent.rb @@ -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'] diff --git a/templates/default/plugin-agent-init-rhel.erb b/templates/default/plugin-agent-init-rhel.erb index 7e72cd7..f38fa9c 100644 --- a/templates/default/plugin-agent-init-rhel.erb +++ b/templates/default/plugin-agent-init-rhel.erb @@ -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="" @@ -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}"