Skip to content
Snippets Groups Projects
Commit ee0ae45e authored by ballen4705's avatar ballen4705
Browse files

Wrote function to send email warning to users from smartd. Directive parsing...

Wrote function to send email warning to users from smartd.  Directive parsing command needs to set up mailinfo structure, and then the ataCheck and scsiCheck functions need to be modified to call printandmail function.


git-svn-id: https://smartmontools.svn.sourceforge.net/svnroot/smartmontools/trunk@227 4ea69e1a-61f1-4043-bf83-b5c94c648137
parent 3c593b60
No related branches found
No related tags found
No related merge requests found
32 33
...@@ -35,6 +35,7 @@ ...@@ -35,6 +35,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <errno.h> #include <errno.h>
#include <string.h> #include <string.h>
#include <time.h>
#include "atacmds.h" #include "atacmds.h"
#include "scsicmds.h" #include "scsicmds.h"
#include "smartd.h" #include "smartd.h"
...@@ -44,7 +45,7 @@ ...@@ -44,7 +45,7 @@
// CVS ID strings // CVS ID strings
extern const char *CVSid1, *CVSid2; extern const char *CVSid1, *CVSid2;
const char *CVSid6="$Id: smartd.c,v 1.52 2002/11/07 11:00:56 ballen4705 Exp $" const char *CVSid6="$Id: smartd.c,v 1.53 2002/11/08 10:51:51 ballen4705 Exp $"
CVSID1 CVSID2 CVSID3 CVSID4 CVSID7; CVSID1 CVSID2 CVSID3 CVSID4 CVSID7;
// global variable used for control of printing, passing arguments, etc. // global variable used for control of printing, passing arguments, etc.
...@@ -86,6 +87,59 @@ void printout(int priority,char *fmt, ...){ ...@@ -86,6 +87,59 @@ void printout(int priority,char *fmt, ...){
return; return;
} }
void printandmail(mailinfo *mail, int priority, char *fmt, ...){
int pid;
va_list ap;
// iitialize variable argument list, then log message to SYSLOG or
// stdout, then finish with argument list
va_start(ap,fmt);
printout(priority, fmt, ap);
va_end(ap);
// See if user wants us to send mail
if (mail==NULL)
return;
// Have we already sent a message about this?
if (mail->logged)
return;
// Need to send a message -- fork and send
pid=fork();
if (pid<0){
// We are parent, and were unable to fork to send email. Log
// warning then return.
if (errno<sys_nerr)
printout(LOG_CRIT,"Unable to send email, %s, fork() failed\n", sys_errlist[errno]);
else
printout(LOG_CRIT,"Unable to send email, fork() failed\n");
return;
}
else if (pid) {
// we are the parent process, record the time of the mail message,
// and increment counter, then return.
mail->logged++;
mail->lastsent=time(NULL);
return;
}
else {
// We are the child process, send email
char command[1024], message[256];
// print warning string into message
va_start(ap, fmt);
vsnprintf(message, 256, fmt, ap);
va_end(ap);
// now construct a command to send this as EMAIL, and issue it.
snprintf(command,1024, "echo '%s' | mail -s 'smartd warning: S.M.A.R.T. errors' %s > /dev/null 2> /dev/null", message, mail->address);
exit(system(command));
}
}
// Printing function for watching ataprint commands, or losing them // Printing function for watching ataprint commands, or losing them
void pout(char *fmt, ...){ void pout(char *fmt, ...){
va_list ap; va_list ap;
......
...@@ -35,6 +35,7 @@ ...@@ -35,6 +35,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <errno.h> #include <errno.h>
#include <string.h> #include <string.h>
#include <time.h>
#include "atacmds.h" #include "atacmds.h"
#include "scsicmds.h" #include "scsicmds.h"
#include "smartd.h" #include "smartd.h"
...@@ -44,7 +45,7 @@ ...@@ -44,7 +45,7 @@
// CVS ID strings // CVS ID strings
extern const char *CVSid1, *CVSid2; extern const char *CVSid1, *CVSid2;
const char *CVSid6="$Id: smartd.cpp,v 1.52 2002/11/07 11:00:56 ballen4705 Exp $" const char *CVSid6="$Id: smartd.cpp,v 1.53 2002/11/08 10:51:51 ballen4705 Exp $"
CVSID1 CVSID2 CVSID3 CVSID4 CVSID7; CVSID1 CVSID2 CVSID3 CVSID4 CVSID7;
// global variable used for control of printing, passing arguments, etc. // global variable used for control of printing, passing arguments, etc.
...@@ -86,6 +87,59 @@ void printout(int priority,char *fmt, ...){ ...@@ -86,6 +87,59 @@ void printout(int priority,char *fmt, ...){
return; return;
} }
void printandmail(mailinfo *mail, int priority, char *fmt, ...){
int pid;
va_list ap;
// iitialize variable argument list, then log message to SYSLOG or
// stdout, then finish with argument list
va_start(ap,fmt);
printout(priority, fmt, ap);
va_end(ap);
// See if user wants us to send mail
if (mail==NULL)
return;
// Have we already sent a message about this?
if (mail->logged)
return;
// Need to send a message -- fork and send
pid=fork();
if (pid<0){
// We are parent, and were unable to fork to send email. Log
// warning then return.
if (errno<sys_nerr)
printout(LOG_CRIT,"Unable to send email, %s, fork() failed\n", sys_errlist[errno]);
else
printout(LOG_CRIT,"Unable to send email, fork() failed\n");
return;
}
else if (pid) {
// we are the parent process, record the time of the mail message,
// and increment counter, then return.
mail->logged++;
mail->lastsent=time(NULL);
return;
}
else {
// We are the child process, send email
char command[1024], message[256];
// print warning string into message
va_start(ap, fmt);
vsnprintf(message, 256, fmt, ap);
va_end(ap);
// now construct a command to send this as EMAIL, and issue it.
snprintf(command,1024, "echo '%s' | mail -s 'smartd warning: S.M.A.R.T. errors' %s > /dev/null 2> /dev/null", message, mail->address);
exit(system(command));
}
}
// Printing function for watching ataprint commands, or losing them // Printing function for watching ataprint commands, or losing them
void pout(char *fmt, ...){ void pout(char *fmt, ...){
va_list ap; va_list ap;
......
...@@ -23,7 +23,7 @@ ...@@ -23,7 +23,7 @@
*/ */
#ifndef CVSID7 #ifndef CVSID7
#define CVSID7 "$Id: smartd.h,v 1.17 2002/10/30 19:16:44 ballen4705 Exp $\n" #define CVSID7 "$Id: smartd.h,v 1.18 2002/11/08 10:51:51 ballen4705 Exp $\n"
#endif #endif
// Configuration file // Configuration file
...@@ -68,6 +68,17 @@ typedef struct scsidevices_s { ...@@ -68,6 +68,17 @@ typedef struct scsidevices_s {
} scsidevices_t; } scsidevices_t;
// If user has requested email warning messages, then this structure
// stores the information about them.
typedef struct mailinfo {
// number of times an email has been sent
int logged;
// time last email was sent, as defined by man 2 time
time_t lastsent;
// address to send email to
char *address;
} mailinfo;
// Used to store a list of devices and options that were in the // Used to store a list of devices and options that were in the
// configuration file. // configuration file.
typedef struct configfile_s { typedef struct configfile_s {
...@@ -83,6 +94,8 @@ typedef struct configfile_s { ...@@ -83,6 +94,8 @@ typedef struct configfile_s {
char usage; char usage;
char selftest; char selftest;
char errorlog; char errorlog;
// mailing information for each of the previous error types
mailinfo *mailinfop[6];
// counts of ata and self-test errors. Perhaps ought to be in the // counts of ata and self-test errors. Perhaps ought to be in the
// atadevices_t structure. // atadevices_t structure.
unsigned char selflogcount; unsigned char selflogcount;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment