From 988a7b0adfede19fdea0662e54948e4c605f3322 Mon Sep 17 00:00:00 2001
From: ballen4705 <ballen4705@4ea69e1a-61f1-4043-bf83-b5c94c648137>
Date: Wed, 13 Nov 2002 07:39:50 +0000
Subject: [PATCH] Improved email message format; eliminated un-necessary shell
 command (echo) from system() command argument.

git-svn-id: https://smartmontools.svn.sourceforge.net/svnroot/smartmontools/trunk@253 4ea69e1a-61f1-4043-bf83-b5c94c648137
---
 sm5/VERSION            |  2 +-
 sm5/smartd.c           | 31 ++++++++++++++++++++++---------
 sm5/smartd.cpp         | 31 ++++++++++++++++++++++---------
 sm5/smartmontools.spec |  7 ++++++-
 4 files changed, 51 insertions(+), 20 deletions(-)

diff --git a/sm5/VERSION b/sm5/VERSION
index a2720097d..425151f3a 100644
--- a/sm5/VERSION
+++ b/sm5/VERSION
@@ -1 +1 @@
-39
+40
diff --git a/sm5/smartd.c b/sm5/smartd.c
index 00c9b6f3f..b5773ecfc 100644
--- a/sm5/smartd.c
+++ b/sm5/smartd.c
@@ -45,7 +45,7 @@
 
 // CVS ID strings
 extern const char *CVSid1, *CVSid2;
-const char *CVSid6="$Id: smartd.c,v 1.60 2002/11/12 22:39:21 ballen4705 Exp $" 
+const char *CVSid6="$Id: smartd.c,v 1.61 2002/11/13 07:39:50 ballen4705 Exp $" 
 CVSID1 CVSID2 CVSID3 CVSID4 CVSID7;
 
 // global variable used for control of printing, passing arguments, etc.
@@ -90,7 +90,7 @@ void printout(int priority,char *fmt, ...){
 // If address is null, this just prints a warning message.  But if
 // address is non-null then send and log a warning email.
 void printandmail(char *address, mailinfo *mail, int priority, char *fmt, ...){
-  char command[1024], message[256], hostname[256];
+  char command[2048], message[256], hostname[256];
   int status;
   va_list ap;
   
@@ -103,11 +103,12 @@ void printandmail(char *address, mailinfo *mail, int priority, char *fmt, ...){
   // messages after some delay time.
   mail->logged++;
   mail->lastsent=time(NULL);
- 
-  // get system host name  
-  if (gethostname(hostname, 256)){
+  
+  // get system host name (not null terminated if length=MAX) 
+  if (gethostname(hostname, 256))
     sprintf(hostname,"Unknown host");
-  }
+  else
+    hostname[255]='\0';
   
   // print warning string into message
   va_start(ap, fmt);
@@ -115,15 +116,27 @@ void printandmail(char *address, mailinfo *mail, int priority, char *fmt, ...){
   va_end(ap);
   
   // now construct a command to send this as EMAIL, and issue it.
+  snprintf(command, 2048, "mail -s '%s: SMART errors detected' %s > /dev/null 2> /dev/null << \"ENDMAIL\"\n"
+	   "This email was generated by the smartd daemon running on machine:\n"
+	   "%s\n"
+	   "The following warning/error was logged by the smartd daemon:\n"
+	   "%s"
+	   "Further details can be found in the machine's syslog (/var/log/messages).\n"
+	   "You can also use the smartctl utility for further investigation.\n"
+	   "No additional email messages about this problem will be sent.\n"
+	   "ENDMAIL\n",
+	   hostname, address, hostname, message);
+#if (0)
   snprintf(command,1024, "echo '%s' | mail -s '%s: smartd detected SMART errors' %s > /dev/null 2> /dev/null",
 	   message, hostname, address);
-  status=system(command);
+#endif
 
+  status=system(command);
   if (WEXITSTATUS(status))
-    printout(LOG_CRIT,"Email warning message to %s failed (exit status %d)\n",address,status);
+    printout(LOG_CRIT,"Email warning message to %s failed (32-bit exit status: %d)\n",address,status);
   else
     printout(LOG_INFO,"Email warning message sent to %s\n",address);
-
+  
   return;
 }
 
diff --git a/sm5/smartd.cpp b/sm5/smartd.cpp
index 840c494e6..f1da8343d 100644
--- a/sm5/smartd.cpp
+++ b/sm5/smartd.cpp
@@ -45,7 +45,7 @@
 
 // CVS ID strings
 extern const char *CVSid1, *CVSid2;
-const char *CVSid6="$Id: smartd.cpp,v 1.60 2002/11/12 22:39:21 ballen4705 Exp $" 
+const char *CVSid6="$Id: smartd.cpp,v 1.61 2002/11/13 07:39:50 ballen4705 Exp $" 
 CVSID1 CVSID2 CVSID3 CVSID4 CVSID7;
 
 // global variable used for control of printing, passing arguments, etc.
@@ -90,7 +90,7 @@ void printout(int priority,char *fmt, ...){
 // If address is null, this just prints a warning message.  But if
 // address is non-null then send and log a warning email.
 void printandmail(char *address, mailinfo *mail, int priority, char *fmt, ...){
-  char command[1024], message[256], hostname[256];
+  char command[2048], message[256], hostname[256];
   int status;
   va_list ap;
   
@@ -103,11 +103,12 @@ void printandmail(char *address, mailinfo *mail, int priority, char *fmt, ...){
   // messages after some delay time.
   mail->logged++;
   mail->lastsent=time(NULL);
- 
-  // get system host name  
-  if (gethostname(hostname, 256)){
+  
+  // get system host name (not null terminated if length=MAX) 
+  if (gethostname(hostname, 256))
     sprintf(hostname,"Unknown host");
-  }
+  else
+    hostname[255]='\0';
   
   // print warning string into message
   va_start(ap, fmt);
@@ -115,15 +116,27 @@ void printandmail(char *address, mailinfo *mail, int priority, char *fmt, ...){
   va_end(ap);
   
   // now construct a command to send this as EMAIL, and issue it.
+  snprintf(command, 2048, "mail -s '%s: SMART errors detected' %s > /dev/null 2> /dev/null << \"ENDMAIL\"\n"
+	   "This email was generated by the smartd daemon running on machine:\n"
+	   "%s\n"
+	   "The following warning/error was logged by the smartd daemon:\n"
+	   "%s"
+	   "Further details can be found in the machine's syslog (/var/log/messages).\n"
+	   "You can also use the smartctl utility for further investigation.\n"
+	   "No additional email messages about this problem will be sent.\n"
+	   "ENDMAIL\n",
+	   hostname, address, hostname, message);
+#if (0)
   snprintf(command,1024, "echo '%s' | mail -s '%s: smartd detected SMART errors' %s > /dev/null 2> /dev/null",
 	   message, hostname, address);
-  status=system(command);
+#endif
 
+  status=system(command);
   if (WEXITSTATUS(status))
-    printout(LOG_CRIT,"Email warning message to %s failed (exit status %d)\n",address,status);
+    printout(LOG_CRIT,"Email warning message to %s failed (32-bit exit status: %d)\n",address,status);
   else
     printout(LOG_INFO,"Email warning message sent to %s\n",address);
-
+  
   return;
 }
 
diff --git a/sm5/smartmontools.spec b/sm5/smartmontools.spec
index 48d3c63f4..168b91aa7 100644
--- a/sm5/smartmontools.spec
+++ b/sm5/smartmontools.spec
@@ -29,7 +29,7 @@ Packager:       Bruce Allen <smartmontools-support@lists.sourceforge.net>
 # http://ftp1.sourceforge.net/smartmontools/smartmontools-%{version}-%{release}.tar.gz
 
 # CVS ID of this file is:
-# $Id: smartmontools.spec,v 1.58 2002/11/12 22:42:11 ballen4705 Exp $
+# $Id: smartmontools.spec,v 1.59 2002/11/13 07:39:50 ballen4705 Exp $
 
 # Copyright (C) 2002 Bruce Allen <smartmontools-support@lists.sourceforge.net>
 # Home page: http://smartmontools.sourceforge.net/
@@ -233,6 +233,11 @@ fi
 
 %define date	%(echo `LC_ALL="C" date +"%a %b %d %Y"`)
 %changelog
+* Wed Nov 13 2002 Bruce Allen <smartmontools-support@lists.sourceforge.net>
+- Added a new smartd configuration file Directive: -M ADDRESS.
+  This sends a single warning email to ADDRESS for failures or
+  errors detected with the -c, -L, -l, or -f Directives.
+
 * Mon Nov 11 2002 Bruce Allen <smartmontools-support@lists.sourceforge.net>
 - Modified perror() statements in atacmds.c so that printout for SMART
   commands errors is properly suppressed or queued depending upon users
-- 
GitLab