Instead of executing run-my-service
use run-my-service || mail.sh SERVICE_NAME '[email protected],[email protected]' MACHINE_NAME
.
Created
July 10, 2015 15:21
-
-
Save soarez/b2a092c309ed846be4f6 to your computer and use it in GitHub Desktop.
Upstart service error email alert
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
srv=$1 | |
to=$2 | |
machine=$3 | |
loglinesinbody=50 | |
logfile="/var/log/upstart/$srv.log" | |
from="Upstart" | |
subject="$srv crashed in $machine" | |
boundary="ZZ_/afg6432dfgkl.94531q" | |
body="$(tail -n $loglinesinbody $logfile)" | |
declare -a attachments | |
attachments=( "$logfile" ) | |
get_mimetype(){ | |
# warning: assumes that the passed file exists | |
file --mime-type "$1" | sed 's/.*: //' | |
} | |
# Build headers | |
{ | |
printf '%s\n' "From: $from | |
To: $to | |
Subject: $subject | |
Mime-Version: 1.0 | |
Content-Type: multipart/mixed; boundary=\"$boundary\" | |
--${boundary} | |
Content-Type: text/plain; charset=\"US-ASCII\" | |
Content-Transfer-Encoding: 7bit | |
Content-Disposition: inline | |
$body | |
" | |
# now loop over the attachments, guess the type | |
# and produce the corresponding part, encoded base64 | |
for file in "${attachments[@]}"; do | |
[ ! -f "$file" ] && echo "Warning: attachment $file not found, skipping" >&2 && continue | |
mimetype=$(get_mimetype "$file") | |
printf '%s\n' "--${boundary} | |
Content-Type: $mimetype | |
Content-Transfer-Encoding: base64 | |
Content-Disposition: attachment; filename=\"$file\" | |
" | |
base64 "$file" | |
echo | |
done | |
# print last boundary with closing -- | |
printf '%s\n' "--${boundary}--" | |
} | sendmail -t -oi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment