log.pl
changeset 27 10c207a978e2
parent 26 cf9761eb2538
child 28 714d4a0ea0bc
equal deleted inserted replaced
26:cf9761eb2538 27:10c207a978e2
     1 #! /usr/bin/perl 
     1 #! /usr/bin/perl 
     2 # $Id$
     2 # © 2009 Schlittermann - internet & unix support
     3 # $URL$
       
     4 my $USAGE = <<'#';
       
     5 $ME [options]
       
     6     --[no]db	 insert into log database [$opt_db]
       
     7     --[no]mail	 send mails to @config::mailto [$opt_mail]
       
     8     --init-dir=s initialize specified directory with VCS repository
       
     9     --message=s  message
       
    10                 - if the message starts with '\@' it's considered
       
    11 		  to be a message FILE.
       
    12 		- message can be: apt[:text]\@<FILE> - this will
       
    13 		  start special file processing (assuming output
       
    14 		  from apt, with text (default: APT: upgrade) going
       
    15 		  to the first line)
       
    16     --logfile=s	  use this(!) logfile
       
    17 #
       
    18 
       
    19 # something about charsets
     3 # something about charsets
    20 # * We assume the LOG file is always UTF-8!
     4 # * We assume the LOG file is always UTF-8!
    21 #   (I know, it's not true for historical entries, may be we can
     5 #   (I know, it's not true for historical entries, may be we can
    22 #   build some tool to convert the file line-by-line, or at least
     6 #   build some tool to convert the file line-by-line, or at least
    23 #   entry-by-entry -- and our database too.
     7 #   entry-by-entry -- and our database too.
    45 use Mail::Mailer;
    29 use Mail::Mailer;
    46 use DBI;
    30 use DBI;
    47 use MIME::QuotedPrint;
    31 use MIME::QuotedPrint;
    48 use I18N::Langinfo qw(langinfo CODESET);
    32 use I18N::Langinfo qw(langinfo CODESET);
    49 use Text::Iconv;
    33 use Text::Iconv;
       
    34 use Pod::Usage;
    50 
    35 
    51 use Logbuch::HG;
    36 use Logbuch::HG;
    52 
    37 
    53 use lib "/etc/logbuch";
    38 use lib "/etc/logbuch";
    54 use config;
    39 use config;
    91 sub check_hg_bin();
    76 sub check_hg_bin();
    92 
    77 
    93 MAIN: {
    78 MAIN: {
    94 
    79 
    95     GetOptions(
    80     GetOptions(
    96         "db!"        => \$opt_db,
    81         "db!"         => \$opt_db,
    97         "mail!"      => \$opt_mail,
    82         "mail!"       => \$opt_mail,
    98         "message=s"  => \$opt_message,
    83         "m|message=s" => \$opt_message,
    99         "type=s"     => \$opt_apt,
    84         "type=s"      => \$opt_apt,
   100         "init-dir=s" => \$opt_initdir,
    85         "init-dir=s"  => \$opt_initdir,
   101         "file=s"     => \$opt_file,
    86         "f|file=s"    => \$opt_file,
   102     ) or die eval "\"$USAGE\"";
    87         "man"         => sub { pod2usage(-verbose => 2, -exit => 0) },
       
    88         "help"        => sub { pod2usage(-verbose => 0, -exit => 0) },
       
    89     ) or pod2usage();
   103 
    90 
   104     if ($opt_message =~ /^@(.*)/) {
    91     if ($opt_message =~ /^@(.*)/) {
   105         @ARGV = $1;
    92         @ARGV = $1;
   106         $opt_message = join "", <>;
    93         $opt_message = join "", <>;
   107     }
    94     }
   156             $hg_status_text .= $repo->status();
   143             $hg_status_text .= $repo->status();
   157         }
   144         }
   158     }
   145     }
   159 
   146 
   160     if ($opt_db) {
   147     if ($opt_db) {
   161         $Dbh = DBI->connect($DSN, $USER, $PW, { RaiseError => 1 })
       
   162           or die $DBI::errstr;
       
   163         END { $Dbh->disconnect() if $Dbh; }
   148         END { $Dbh->disconnect() if $Dbh; }
       
   149         $Dbh = DBI->connect($DSN, $USER, $PW, { RaiseError => 0 });
       
   150         if (!$Dbh or !$Dbh->connected) {
       
   151             warn $DBI::errstr;
       
   152         }
   164     }
   153     }
   165 
   154 
   166     # Temporärfile öffnen
   155     # Temporärfile öffnen
   167     my ($fh, $file) = tempfile(DIR => "/tmp", UNLINK => 1);
   156     my ($fh, $file) = tempfile(DIR => "/tmp", UNLINK => 1);
   168 
   157 
   227         $text =~ s/\s*$//s;    # Leerzeichen am Ende weg
   216         $text =~ s/\s*$//s;    # Leerzeichen am Ende weg
   228 
   217 
   229         truncate $fh, $pos;
   218         truncate $fh, $pos;
   230         seek $fh, 0, 2;
   219         seek $fh, 0, 2;
   231 
   220 
   232         if ($opt_db) {
   221         if ($opt_db and $Dbh) {
   233             my $sth = $Dbh->prepare("
   222             my $sth = $Dbh->prepare("
   234 		    INSERT INTO log (host, date, user, mailto, text)
   223 		    INSERT INTO log (host, date, user, mailto, text)
   235 		    VALUES(?, now(), ?, ?, ?)");
   224 		    VALUES(?, now(), ?, ?, ?)");
   236             $sth->execute(hostname(), $user, $mailto, $text);
   225             $sth->execute(hostname(), $user, $mailto, $text);
   237             print STDERR "Database entry inserted\n";
   226             print STDERR "Database entry inserted\n";
   312 
   301 
   313 Exiting!
   302 Exiting!
   314 EOF
   303 EOF
   315     }
   304     }
   316 }
   305 }
       
   306 __END__
       
   307 
       
   308 =head1 NAME
       
   309 
       
   310     log -- log utility (ius)
       
   311 
       
   312 =head1 SYNOPSIS
       
   313 
       
   314     log [--[no]db] [--[no]mail] [--init-dir <dir>] [--message <msg>] [-f|--logfile <file>]
       
   315 
       
   316 =head1 DESCRIPTION
       
   317 
       
   318 This (ius) tool prepends a custom log message to a log file (typically
       
   319 /root/LOG.<hostname>). 
       
   320 
       
   321 =head1 OPTIONS
       
   322 
       
   323 B<Beware>: the defaults of some of these options may be changed through
       
   324 a configuration file.
       
   325 
       
   326 =over
       
   327 
       
   328 =item B<--[no]db>
       
   329 
       
   330 (Don't) write the log messages into the specified database (default: on)
       
   331 
       
   332 =item B<--[no]mail>
       
   333 
       
   334 (Don't) send the log message as mail. (default: on)
       
   335 
       
   336 =item B<--init-dir> I<dir>
       
   337 
       
   338 Initialize the directory I<dir> with a VCS repository. When you've done
       
   339 this, further changes to I<dir> will be logged too.
       
   340 
       
   341 =item B<--message> I<msg>
       
   342 
       
   343 The message to use. Otherwise an editor ($ENV{EDITOR}) is started.
       
   344 If the message starts with a "@", it's considered to be a file.
       
   345 
       
   346 When the messages starts as "apt[:I<text>]@I<FILE>" it starts special
       
   347 apt message processing. The default I<text> is "APT: upgrade".
       
   348 This text becomes the first line of the log message.
       
   349 
       
   350 =item B<-f>|B<--file> I<file>
       
   351 
       
   352 The logfile to use. (default: F< /root/LOG.<NODENAME>>)
       
   353 
       
   354 =back
       
   355 
       
   356 =cut
   317 
   357 
   318 # vim:sts=4 sw=4 aw ai sm:
   358 # vim:sts=4 sw=4 aw ai sm:
   319 
   359