Skip to content

Instantly share code, notes, and snippets.

@gionn
Created April 10, 2020 08:53
Show Gist options
  • Save gionn/c2dc03faca207a11b9a7084582281062 to your computer and use it in GitHub Desktop.
Save gionn/c2dc03faca207a11b9a7084582281062 to your computer and use it in GitHub Desktop.
Chef recipe to automatically restart docker containers or generic services affected by an update of dynatrace agent
ruby_block "check-restart-dynatrace" do
block do
headers = {:Authorization => "Api-Token #{provider['api_key']}"}
client = Chef::HTTP.new(provider['url'])
processes = JSON.parse(client.get('/processes?includeDetails=true', headers))
hosts = JSON.parse(client.get('/hosts?includeDetails=true', headers))
host_found = hosts.select { |host| host['discoveredName'] == node['hostname'] }.first
if host_found
host_id = host_found['entityId']
processes.select { |process| process['fromRelationships']['isProcessOf'].include?(host_id) }
.select { |process| process['monitoringState']['restartRequired'] }.each do |p|
if p['metadata']['dockerContainerNames']
container_name = p['metadata']['dockerContainerNames'].first
Chef::Log.info("#{container_name} needs restart")
resources(:docker_container => container_name).run_action(:restart)
else
begin
service_name = p['discoveredName']
resources(:service => service_name).run_action(:restart)
rescue Chef::Exceptions::ResourceNotFound
Chef::Log.info("#{service_name} not found")
end
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment