log.pl
changeset 28 714d4a0ea0bc
parent 27 10c207a978e2
child 32 68f8a7003db3
equal deleted inserted replaced
27:10c207a978e2 28:714d4a0ea0bc
    12 #   - On opening/reading the log file: convert from UTF-8 -> current codeset
    12 #   - On opening/reading the log file: convert from UTF-8 -> current codeset
    13 #   - If this fails, issue a warning, use "head <something>" to show the
    13 #   - If this fails, issue a warning, use "head <something>" to show the
    14 #     last LOG entry directly and then fire up the editor with an
    14 #     last LOG entry directly and then fire up the editor with an
    15 #     empty file (or just added notice why we do not show the old
    15 #     empty file (or just added notice why we do not show the old
    16 #     messages)
    16 #     messages)
    17 #   - After editing: convert the current messsage to from the current
    17 #   - After editing: convert the current messsage from the current
    18 #     codeset UTF-8
    18 #     codeset to UTF-8
    19 #   - The same is for message on command line (but this is more easy, we
    19 #   - The same is for message on command line (but this is more easy, we
    20 #     do not have to cope with the old message log
    20 #     do not have to cope with the old message log
    21 
    21 
    22 use strict;
    22 use strict;
    23 use warnings;
    23 use warnings;
    30 use DBI;
    30 use DBI;
    31 use MIME::QuotedPrint;
    31 use MIME::QuotedPrint;
    32 use I18N::Langinfo qw(langinfo CODESET);
    32 use I18N::Langinfo qw(langinfo CODESET);
    33 use Text::Iconv;
    33 use Text::Iconv;
    34 use Pod::Usage;
    34 use Pod::Usage;
       
    35 use Sys::Hostname;
    35 
    36 
    36 use Logbuch::HG;
    37 use Logbuch::HG;
    37 
    38 
    38 use lib "/etc/logbuch";
    39 use lib "/etc/logbuch";
    39 use config;
    40 use config;
    56 
    57 
    57 my $DSN  = "DBI:mysql:logbuch:pu.schlittermann.de";
    58 my $DSN  = "DBI:mysql:logbuch:pu.schlittermann.de";
    58 my $USER = "logbuch";
    59 my $USER = "logbuch";
    59 my $PW   = "HIDDEN";
    60 my $PW   = "HIDDEN";
    60 
    61 
    61 my $EDITOR = $ENV{VISUAL} || $ENV{EDITOR} || "vim";
    62 my $EDITOR   = $ENV{VISUAL} || $ENV{EDITOR} || "vim";
    62 my $MAGIC = "#--- all changes below are ignored ---#\n";
    63 my $MAGIC    = "#--- all changes below are ignored ---#\n";
       
    64 my $NODENAME = (split /\./, hostname)[0];
    63 
    65 
    64 my $opt_db      = 1;
    66 my $opt_db      = 1;
    65 my $opt_mail    = 1;
    67 my $opt_mail    = 1;
    66 my $opt_message = "";
    68 my $opt_message = "";
    67 my $opt_apt     = "";
    69 my $opt_apt     = "";
    68 my $opt_initdir = "";
    70 my $opt_initdir = "";
    69 my $opt_file    = "$ENV{HOME}/LOG";
    71 my $opt_file    = defined $config::logfile    # use it twice
       
    72   ? $config::logfile
       
    73   : "/root/LOG.$NODENAME";
       
    74 
    70 
    75 
    71 my $Dbh;
    76 my $Dbh;
    72 
    77 
    73 sub identity();
    78 sub identity();
    74 sub hostname();
       
    75 sub mailto();
    79 sub mailto();
    76 sub check_hg_bin();
    80 sub check_hg_bin();
       
    81 sub full_hostname();
       
    82 sub word_encoded($);
    77 
    83 
    78 MAIN: {
    84 MAIN: {
    79 
    85 
    80     GetOptions(
    86     GetOptions(
    81         "db!"         => \$opt_db,
    87         "db!"         => \$opt_db,
   170       "  * $opt_message",
   176       "  * $opt_message",
   171       "\n",
   177       "\n",
   172       $auto_message,
   178       $auto_message,
   173       "\n", $MAGIC, "\n";
   179       "\n", $MAGIC, "\n";
   174 
   180 
       
   181     # LOG.<hostname> wird in Zukunft genutzt und LOG nur ein Symlink
       
   182     # dorthin
       
   183     if ($opt_file =~ /(.*)\.$NODENAME$/ and !(-e $opt_file) and (-f $1)) {
       
   184         rename($1 => $opt_file) or die "Can't rename $1 => $opt_file: !\n";
       
   185         symlink($opt_file, $1) or die "Can't symlink $1 => $opt_file: $!\n";
       
   186     }
       
   187 
   175     if (!-e $opt_file) {
   188     if (!-e $opt_file) {
   176         open(X, $_ = ">>$opt_file") or die "Can't open $_: $!\n";
   189         open(X, $_ = ">>$opt_file") or die "Can't open $_: $!\n";
   177         close X;
   190         close X;
   178     }
   191     }
   179 
   192 
   220 
   233 
   221         if ($opt_db and $Dbh) {
   234         if ($opt_db and $Dbh) {
   222             my $sth = $Dbh->prepare("
   235             my $sth = $Dbh->prepare("
   223 		    INSERT INTO log (host, date, user, mailto, text)
   236 		    INSERT INTO log (host, date, user, mailto, text)
   224 		    VALUES(?, now(), ?, ?, ?)");
   237 		    VALUES(?, now(), ?, ?, ?)");
   225             $sth->execute(hostname(), $user, $mailto, $text);
   238             $sth->execute(full_hostname(), $user, $mailto, $text);
   226             print STDERR "Database entry inserted\n";
   239             print STDERR "Database entry inserted\n";
   227         }
   240         }
   228 
   241 
   229         if ($opt_mail and $mailto) {
   242         if ($opt_mail and $mailto) {
   230             my $mailer = new Mail::Mailer "sendmail"
   243             my $mailer = new Mail::Mailer "sendmail"
   231               or die "Can't create Mailer: $!\n";
   244               or die "Can't create Mailer: $!\n";
   232 
   245 
   233             my $subject = (split /\n/, $text)[0];
   246             my $subject = (split /\n/, $text)[0];
   234             $subject =~ s/^\s*\S\s//;    # cut the "itemizer"
   247             $subject =~ s/^\s*\S\s//;    # cut the "itemizer"
   235             $subject = encode_qp("Service [" . hostname() . "]: $subject\n");
   248 
   236             $subject =~ s/\n(.)/\n\t$1/g;
   249 	     # and now convert to quoted printable (UTF-8)
       
   250 	     # =?utf-8?q?St=C3=BCmper_am_Werk=3A_Shellscripte_aus_der?=
       
   251             $subject = word_encoded("Service [" . full_hostname() . "]: $subject");
   237 
   252 
   238             $mailer->open(
   253             $mailer->open(
   239                 {
   254                 {
   240                     "Content-Type"              => "text/plain; charset=utf-8",
   255                     "Content-Type"              => "text/plain; charset=utf-8",
   241                     "Content-Transfer-Encoding" => "8bit",
   256                     "Content-Transfer-Encoding" => "8bit",
   273     chomp $user;
   288     chomp $user;
   274     $user .= " [" . ($ENV{IUS_PROFILE} || $ENV{REMOTE_USER} || "-") . "]";
   289     $user .= " [" . ($ENV{IUS_PROFILE} || $ENV{REMOTE_USER} || "-") . "]";
   275     return $user;
   290     return $user;
   276 }
   291 }
   277 
   292 
   278 sub hostname() {
   293 sub full_hostname() {
   279     my $r = `hostname -f`;
   294     my $r = `hostname -f`;
   280     chomp($r);
   295     chomp($r);
   281     return $r;
   296     return $r;
   282 }
   297 }
   283 
   298 
   284 sub mailto() {
   299 sub mailto() {
   285     return join(", ", @config::mailto);
   300     return join(", ", @config::mailto);
   286 }
   301 }
   287 
   302 
       
   303 sub word_encoded($) {
       
   304     my $line = shift;
       
   305     # to get "Q" word encoding, we've to fix the result a bit
       
   306     # http://en.wikipedia.org/wiki/MIME
       
   307     # FIXME: The line may be longer than expected!
       
   308     $line = encode_qp($line);
       
   309     $line =~ s/([_?])/sprintf "=%02X", ord($1)/ige;
       
   310     $line =~ s/[ \t]/_/g;
       
   311     return join "\t", map { "=?UTF-8?Q?$_?=\n" } split /=\n/, $line;
       
   312 }
       
   313 
   288 sub check_hg_bin() {
   314 sub check_hg_bin() {
   289     if (not Logbuch::HG::hg_available()) {
   315     if (not Logbuch::HG::hg_available()) {
   290 
   316 
   291         die <<'EOF';
   317         die <<'EOF';
   292 
   318