Skip to content

Instantly share code, notes, and snippets.

@jmassardo
Created August 14, 2018 19:32
Show Gist options
  • Save jmassardo/736e5ac6ee31228daa9c61976e969d93 to your computer and use it in GitHub Desktop.
Save jmassardo/736e5ac6ee31228daa9c61976e969d93 to your computer and use it in GitHub Desktop.
Simple script to email Chef Server node counts via SendGrid
BU_NAME='The Awesome Group!'
NODE_COUNT="$(/path/to/chef-repo/knife exec -E "puts api.get('/nodes').size")"
MONTH="$(date +%B)"
YEAR="$(date +%Y)"
MAIL_URL="https://api.sendgrid.com/v3/mail/send"
HEADER="Authorization: Bearer My-Super-Secret-Token"
HEADER2="Content-Type: application/json"
EMAIL_TO='name@example.com'
EMAIL_FROM='name@example.com'
EMAIL_SUBJECT="Chef Server Node Count for ${BU_NAME}"
EMAIL_BODY="<!DOCTYPE html><html><head><style>p {font-family: arial, sans-serif;}table {font-family: arial, sans-serif;border-collapse: collapse;width: 350px;}td, th {border: 1px solid #dddddd;text-align: left;padding: 8px;}tr:nth-child(even) {background-color: #dddddd;}.item_col {font-weight: bold;text-align: right;width: 25%;}</style></head><body><p>Greetings,</p><p>Here are the node counts for the following group:</p><table><tr><td class="item_col">Month:</td><td>${MONTH}, ${YEAR}</td></tr><tr><td class="item_col">BU:</td><td>${BU_NAME}</td></tr><tr><td class="item_col">Count:</td><td>${NODE_COUNT}</td></tr></table><p>Thank you,<br>Your friendly neighborhood shell script</p></body></html>"
EMAIL_DATA="'{\"personalizations\": [{\"to\": [{\"email\": \"${EMAIL_TO}\"}]}],\"from\": {\"email\": \"${EMAIL_FROM}\"},\"subject\": \"${EMAIL_SUBJECT}\",\"content\": [{\"type\": \"text/html\", \"value\": \"${EMAIL_BODY}\"}]}'"
curl --request POST --url "${MAIL_URL}" --header "${HEADER}" --header "${HEADER2}" --data "${EMAIL_DATA}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment