00001 #!/usr/bin/perl
00002 ##
00003 ## Script to email database log entries.
00004 ##
00005 ## 21 Feb 04 1.0 Initial version
00006 ##
00007 ## written by Matt White (whitem@arts.usask.ca)
00008 ##
00009 ## Remember to edit the settings below to your taste
00010 ##
00011
00012 use DBI;
00013
00014 ##
00015 ## User Configurable Settings
00016 ##
00017
00018 # The email address to send the logs to
00019 # ** REMEMBER TO ESCAPE THE @ SYMBOL!! **
00020 $mailto = "someone\@somewhere.changeme";
00021
00022 # The "from" address used in the sent mail
00023 # ** REMEMBER TO ESCAPE THE @ SYMBOL!! **
00024 $mailfrom = "someoneelse\@somewhere.changeme";
00025
00026 # Location of your sendmail binary
00027 $sendmail = "/usr/sbin/sendmail";
00028
00029 # What do you want to get?
00030 # 1 = Mail out all unacknowledged log entries
00031 # 2 = Mail out all entries since last email was sent
00032 $mailwhat = 1;
00033
00034 # Do you want to automatically acknowledge entries that
00035 # were sent out? Yes=1, No=0
00036 $autoack = 1;
00037
00038 # Database connection details for Myth DB
00039 $dbhost = "localhost";
00040 $dbname = "mythconverg";
00041 $dbuser = "mythtv";
00042 $dbpass = "mythtv";
00043
00044 ##
00045 ## End of User-configurable settings
00046 ##
00047
00048 my @priorities = ("Emergency","Alert","Critical","Error","Warning","Notice",
00049 "Info","Debug");
00050 my $dbh =
00051 DBI->connect("dbi:mysql:database=$dbname:host=$dbhost","$dbuser","$dbpass") or
00052 die "Cannot connect to database ($!)\n";
00053 if ($mailwhat == 2) {
00054 $q = "select data from settings where value = 'LogLastEmail'";
00055 $sth = $dbh->prepare($q);
00056 $numrows = $sth->execute or die "Could not execute ($q)\n";
00057 if ($numrows!=0) {
00058 @row=$sth->fetchrow_array;
00059 $lastemail = 0 + $row[0];
00060 } else {
00061 $lastemail = 0;
00062 }
00063 ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
00064 localtime($lastemail);
00065 $lastdate = sprintf("%4d-%02d-%02dT%02d:%02d:%02d",$year+1900,$mon+1,
00066 $mday,$hour,$min,$sec);
00067
00068 $where = "(logdate > '$lastdate')";
00069 } else {
00070 $where = "(acknowledged = 0)";
00071 }
00072
00073 $now = time();
00074 $email = "";
00075 $q = "select logid,module,priority,acknowledged,logdate,host," .
00076 "message,details from mythlog where $where order by logdate";
00077 $sth = $dbh->prepare($q);
00078 $numrows = $sth->execute or die "Could not execute ($q)\n";
00079 while (@row = $sth->fetchrow_array) {
00080 $logid = $row[0];
00081 $module = $row[1];
00082 $priority = $row[2];
00083 $ack = $row[3];
00084 $logdate = $row[4];
00085 $host = $row[5];
00086 $message = $row[6];
00087 $details = $row[7];
00088 if ($mailwhat == 2) {
00089 if ($ack == 1) {
00090 $printack = "Acknowledged: Yes";
00091 } else {
00092 $printack = "Acknowledged: No";
00093 }
00094 } else {
00095 $printack = "";
00096 }
00097
00098 $email .= sprintf("%-20s %-15s %s\n",$logdate,$host,$message);
00099 $email .= sprintf(" Module: %-20s Priority: %-12s %s\n\n",$module,$priority,
00100 $printack);
00101 }
00102
00103 if ($numrows == 0) {
00104 exit(0);
00105 }
00106
00107 if ($autoack == 1) {
00108 $q = "update mythlog set acknowledged = 1 where $where";
00109 $sth = $dbh->prepare($q);
00110 $sth->execute or die "Could not execute ($q)\n";
00111 }
00112
00113 if ($mailwhat == 2) {
00114 if ($lastemail == 0) {
00115 $q = "insert into settings (value,data) values ('LogLastEmail','$now')";
00116 } else {
00117 $q = "update settings set data='$now' where value='LogLastEmail'";
00118 }
00119 $sth = $dbh->prepare($q);
00120 $sth->execute or die "Could not execute ($q)\n";
00121 }
00122
00123 ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
00124 localtime($now);
00125 $subject = sprintf("Myth Event Report for %4d-%02d-%02d",$year+1900,$mon+1,$mday);
00126 open MAIL,"| $sendmail -t";
00127 print MAIL "From: $mailfrom\n";
00128 print MAIL "To: $mailto\n";
00129 print MAIL "Subject: $subject\n\n";
00130 print MAIL $email;
00131 close MAIL;
00132
00133 exit(0);