Skip to content

Instantly share code, notes, and snippets.

@jspaleta
Created October 11, 2019 22:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jspaleta/8a64708ec31c4a5cda6de29442d9a742 to your computer and use it in GitHub Desktop.
Save jspaleta/8a64708ec31c4a5cda6de29442d9a742 to your computer and use it in GitHub Desktop.
Populate Sensu Agent Name from EC2 metadata
# Extend the sensu-agent service init to call the update_name.sh script
# place this file in: /etc/systemd/system/sensu-agent.service.d
# With systemd you don't have to edit the vendor provided service init script.
# local admins can extend/override vendor settings by using correctly named extension directory structures
# Ex: /etc/systemd/system/sensu-agent.service.d/
# This .d directory will be parsed for systemd directives to extend/override the vendor supplied sensu-agent.service
# For now all we want to do is add an ExecStartPre directive,
# to populate the sensu-agent EnvironmentFile the vendor provided service unit knows how to read in.
# Note: the /etc/default/sensu-agent file must be writable by the sensu user.
[Service]
ExecStartPre=/etc/sensu/scripts/update_name.sh
#!/bin/bash
#
# Simple example of how to use the EC2 metadata to populate sensu-agent environment variables
# Consider placing this in /etc/sensu/scripts/
# Let's make sure the Sensu Agent name encodes the ec2 instance-id
SENSU_NAME=ec2_$(curl -sS http://169.254.169.254/latest/meta-data/instance-id)
# Lets output the envvars we want sensu-agent to use into one of the supported file locations
# NOTE: Must make sure /etc/default/sensu-agent is writable by the sensu user
cat << EOF > /etc/default/sensu-agent
SENSU_NAME=${SENSU_NAME}
EOF
@johannagnarsson
Copy link

Those are the ones we care about, possibly backend based on your environment and then deregistration: and the handler for it.

@jspaleta
Copy link
Author

I should definitely look into the set-environment verb as an alternative to using the /etc/default-sensu-agent to hold envvar state.
That is probably a cleaner solution for sure. But trying to do everythng inline instead of a script on disk feels a little...dirty...once it gets to the point where i'm setting 6 or 7 things.

@calebhailey
Copy link

Leaving behind a breadcrumb for the next time I don't read the instructions carefully enough:

"Note: the /etc/default/sensu-agent file must be writable by the sensu user."

To future me: don't forget to create the /etc/default/sensu-agent file, and ensure it is owned by the sensu user! 🤦‍♂️

$ sudo touch /etc/default/sensu-agent && sudo chown sensu:sensu /etc/default/sensu-agent

Derpity derpity derp...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment