Skip to content

Instantly share code, notes, and snippets.

@akinnard
Created January 4, 2016 00:48
Show Gist options
  • Save akinnard/dfa399709ec9f7e345c7 to your computer and use it in GitHub Desktop.
Save akinnard/dfa399709ec9f7e345c7 to your computer and use it in GitHub Desktop.
Send an email in chef
# use notifies to send en email when the config file changes
template "/etc/my.cnf" do
source "my.cnf.erb"
owner "mysql"
group "mysql"
mode "0650"
notifies :run, "script[sendmail_mycnf]"
end
script "sendmail_mycnf" do
interpreter "bash"
cwd "/tmp"
user "root"
code <<-EOH
EXOUT=$(mktemp /tmp/sendmail.XXXXXXXXXX) || { echo "Failed to create temp file"; exit 1; }
>> $EXOUT
echo "`date` - The /etc/my.cnf file has changed via chef-client on #{node['hostname']}. If this was done on purpose we need to schedule a window to bounce the server so the changes can take place, if not we need to figure out what happened before the server is restarted. Some variables can be changed with out a restart. Check the documentation on the changes to see if you can avoid a restart." >> $EXOUT
echo "" >> $EXOUT
echo "Chef by default, will store the last 5 versions of a file it manages in a backup directory. The newest file in that directory should be the version just before we made this change. We use the default directory in our chef install. You can see all the backups by going to /var/chef/backup" >> $EXOUT
echo "You can diff the files if you login to the server. The command will look like this, where the file in the backup directory is the latest file." >> $EXOUT
echo "diff /etc/my.cnf /var/chef/backup/etc/my.cnf.chef-20130101021419" >> $EXOUT
echo "" >> $EXOUT
echo " I am going to try to do this for you now by grabing the latest backup and running the diff command on it. This should work. I am geting the latest file by running this [ls -t /var/chef/backup/etc |grep my.cnf |head -1]" >> $EXOUT
echo "-----------------------------------------------------------------------------------------" >> $EXOUT
echo "diff /etc/my.cnf /var/chef/backup/etc/`ls -t /var/chef/backup/etc |grep my.cnf |head -1`" >> $EXOUT
echo "-----------------------------------------------------------------------------------------" >> $EXOUT
diff /etc/my.cnf /var/chef/backup/etc/`ls -t /var/chef/backup/etc |grep my.cnf |head -1` >> $EXOUT
mail -s "#{node['hostname']} MY.CNF file has changed" #{node['mysql']['email_toaddress']} < $EXOUT
EOH
action :nothing
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment