Permalink
Browse files

Send email with error messages in syslog_aggregator

  • Loading branch information...
1 parent c709adf commit 16a2ed64261bbbb596e4e553b931e7f8d1e88d1b Maria Shaldibina and Mark Rushakoff committed Apr 11, 2013
@@ -2,11 +2,11 @@
name: syslog_aggregator
templates:
rsyslogd.conf.erb: config/rsyslogd.conf
+ syslog_aggregator.yml.erb: config/syslog_aggregator.yml
syslog_aggregator_ctl: bin/syslog_aggregator_ctl
gzip_old_logs: bin/gzip_old_logs
reap_old_logs: bin/reap_old_logs
symlink_logs: bin/symlink_logs
packages:
- common
-
-
+- syslog_aggregator
@@ -0,0 +1,12 @@
+---
+domain: <%= p("domain") %>
+
+<% if_p("syslog_aggregator.log_email") do %>
+enabled: <%= p("syslog_aggregator.log_email.enabled") %>
+from_email: <%= p("syslog_aggregator.log_email.from_email") %>
+from_password: <%= p("syslog_aggregator.log_email.from_password") %>
+to_email: <%= p("syslog_aggregator.log_email.to_email") %>
+smtp_server: <%= p("syslog_aggregator.log_email.smtp_server") %>
+smtp_port: <%= p("syslog_aggregator.log_email.smtp_port") %>
+smtp_domain: <%= p("syslog_aggregator.log_email.smtp_domain") %>
+<% end %>
@@ -3,6 +3,7 @@
RUN_DIR=/var/vcap/sys/run/syslog_aggregator
LOG_DIR=/var/vcap/store/log
JOB_DIR=/var/vcap/jobs/syslog_aggregator
+PACKAGE_DIR=/var/vcap/packages/syslog_aggregator
BIN_DIR=$JOB_DIR/bin
PIDFILE=$RUN_DIR/syslog_aggregator.pid
@@ -21,6 +22,7 @@ case $1 in
cp -a $BIN_DIR/gzip_old_logs /etc/cron.daily
cp -a $BIN_DIR/reap_old_logs /etc/cron.hourly
cp -a $BIN_DIR/symlink_logs /etc/cron.hourly
+ cp -a $PACKAGE_DIR/send_error_mail /etc/cron.daily
exec /usr/sbin/rsyslogd -f $JOB_DIR/config/rsyslogd.conf -i $PIDFILE -c 4 \
>>$LOG_DIR/rsyslogd.stdout.log \
@@ -1,4 +1,4 @@
# abort script on any command that exit with a non zero value
set -e
-cp syslog_aggregator/setup_syslog_forwarder.sh $BOSH_INSTALL_TARGET/setup_syslog_forwarder.sh
+cp -a syslog_aggregator/* ${BOSH_INSTALL_TARGET}
@@ -1,4 +1,7 @@
---
name: syslog_aggregator
+dependencies:
+- ruby_next
files:
-- syslog_aggregator/setup_syslog_forwarder.sh
+- syslog_aggregator/setup_syslog_forwarder.sh
+- syslog_aggregator/send_error_mail
@@ -0,0 +1,95 @@
+#!/var/vcap/packages/ruby_next/bin/ruby
+# Send error log messages to operators
+
+# We are not using local smtp server, as the only solution right now
+# would be to install it as a bosh package. Which are planned to be
+# moved to stemcell packages
+# Please consider in future: http://www.rsyslog.com/doc/ommail.html
+require "net/smtp"
+require "fileutils"
+require "yaml"
+
+LOG_DIR = "/var/vcap/store/log"
+TMP_DIR = "/var/vcap/data/tmp/error_mail"
+
+CONFIG = YAML.load_file("/var/vcap/jobs/syslog_aggregator/config/syslog_aggregator.yml")
+
+unless CONFIG["enabled"]
+ warn("Syslog Aggregator digest emails are not enabled.")
+ exit(0)
+end
+
+def save_mail_log_files
+ Dir.chdir(LOG_DIR) do
+ log_files = `find . | grep #{yesterday_date}/.*.log`.split("\n")
+ log_files.each do |log_file|
+ dest = File.join(TMP_DIR, File.basename(log_file))
+ `grep -i "error" #{log_file} >> #{dest}`
+ end
+ end
+end
+
+def yesterday_date
+ (Time.now - (60 * 60 * 24)).strftime("%Y/%m/%d")
+end
+
+def errors_count
+ Dir.chdir(TMP_DIR) do
+ `wc -l * | sort -nr -k1`
+ end
+end
+
+def zip_mail_log_files
+ Dir.chdir(TMP_DIR) do
+ `zip #{TMP_DIR}/logs.zip *`
+ end
+end
+
+def send_mail(body)
+ from_email = CONFIG["from_email"]
+ from_password = CONFIG["from_password"]
+ to_email = CONFIG["to_email"]
+ boundary = "SYSLOGBOUNDARY"
+
+ filecontent = File.read(File.join(TMP_DIR, "logs.zip"))
+ encodedcontent = [filecontent].pack("m")
+
+ message = <<MESSAGE_END
+From: Cloud Foundry Syslog Aggregator <#{from_email}>
+To: CFPI Backend <#{to_email}>
+Subject: Syslog Aggregator Errors
+MIME-Version: 1.0
+Content-Type: multipart/mixed; boundary=#{boundary}
+--#{boundary}
+Content-Type: text/plain
+Content-Transfer-Encoding:8bit
+
+Data for #{yesterday_date} from #{CONFIG["domain"]}:
+
+#{body}
+--#{boundary}
+Content-Type: multipart/mixed; name=\"logs.zip\"
+Content-Transfer-Encoding:base64
+Content-Disposition: attachment; filename="logs.zip"
+
+#{encodedcontent}
+--#{boundary}--
+MESSAGE_END
+
+ smtp = Net::SMTP.new(CONFIG["smtp_server"], CONFIG["smtp_port"])
+ smtp.enable_starttls
+
+ smtp.start(CONFIG["smtp_domain"], from_email, from_password, :login) do
+ smtp.send_message(message, from_email, to_email)
+ end
+end
+
+begin
+ FileUtils.mkdir_p(TMP_DIR)
+ save_mail_log_files
+ body = errors_count
+ zip_mail_log_files
+ send_mail(body)
+ensure
+ FileUtils.rm_rf(TMP_DIR)
+end

0 comments on commit 16a2ed6

Please sign in to comment.