[prev in list] [next in list] [prev in thread] [next in thread] 

List:       swatch-users
Subject:    Swatch-users Digest, Vol 11, Issue 3
From:       swatch-users-request () ucsb ! edu
Date:       2003-03-19 20:00:22
[Download RAW message or body]

Send Swatch-users mailing list submissions to
	swatch-users@ucsb.edu

To subscribe or unsubscribe via the World Wide Web, visit
	http://ucsb.edu/mailman/listinfo/swatch-users
or, via email, send a message with subject or body 'help' to
	swatch-users-request@ucsb.edu

You can reach the person managing the list at
	swatch-users-owner@ucsb.edu

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Swatch-users digest..."


Today's Topics:

   1. Re:  exec in swatch 3.0.5 (Stuart Kendrick)
   2. RE: Re:  exec in swatch 3.0.5 (Jonathan Sartin)
   3. Re:  exec in swatch 3.0.5 (Sylvain)


----------------------------------------------------------------------

Message: 1
Date: Tue, 18 Mar 2003 12:10:38 -0800 (PST)
From: Stuart Kendrick <skendric@fhcrc.org>
Subject: [Swatch-users] Re:  exec in swatch 3.0.5
To: swatch-users@ucsb.edu
Message-ID: <Pine.GSO.4.44.0303181203570.7650-100000@gnat.fhcrc.org>
Content-Type: TEXT/PLAIN; charset=X-UNKNOWN

J'ai rencountre le meme probleme ... je n'ai pas trouve un reponse.

http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&threadm=62dbf7f1.021106111 \
6.69ff875f%40posting.google.com&rnum=5&prev=/groups%3Fsafe%3Dimages%26ie%3DUTF-8%26oe%3DUTF-8%26as_uauthors%3DStuart%2520Kendrick%26lr%3D%26as_scoring%3Dd%26hl%3Den


--sk

Stuart Kendrick
FHCRC
Seattle, WA

> Date: Tue, 18 Mar 2003 14:39:25 +0100
> From: Sylvain <sa@nctech.fr>
> Subject: [Swatch-users] exec in swatch 3.0.5
> To: swatch-users@ucsb.edu
> Message-ID: <3E77218D.50909@nctech.fr>
> Content-Type: text/plain; charset="iso-8859-1"
> 
> 
> Je désire passer un ligne trouvé par swatch, a un script. Lorsque la
> ligne est trouvé, swatch execute bien le script mais ne lui renvoie
> rien. Problème lors de l'echo ?
> 
> Version de swatch :
> 
> # swatch --version
> This is swatch version 3.0.5
> Built on 24 Jan 2003
> Built by E. Todd Atkins <Todd.Atkins@StanfordAlumni.ORG>
> 
> 
> Conf utilisé :
> 
> #
> watchfor   /file system full/
> exec "echo $0 | /scripts/send-alert fs_full address@e-mail"
> #
> 
> Merci pour votre aide...
> 
> Sylvain
> 
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL: http://hub.ucsb.edu/pipermail/swatch-users/attachments/20030318/76dbccd4/attachment-0001.htm
>  
> ------------------------------
> 
> _______________________________________________
> Swatch-users mailing list
> Swatch-users@ucsb.edu
> http://ucsb.edu/mailman/listinfo/swatch-users
> 
> 
> End of Swatch-users Digest, Vol 11, Issue 2
> *******************************************
> 




------------------------------

Message: 2
Date: Tue, 18 Mar 2003 21:41:06 -0000
From: Jonathan Sartin <jonathan.sartin@ocado.com>
Subject: RE: [Swatch-users] Re:  exec in swatch 3.0.5
To: "'swatch-users@ucsb.edu '" <swatch-users@ucsb.edu>
Message-ID:
	<C49F6BA9FFB9AB4E9DD13B10B7E85F1605359E@hohhex01.ocado.com>
Content-Type: text/plain;	charset="iso-8859-1"

Hi,

perhaps:

> watchfor   /file system full/
> exec "echo $0 | /scripts/send-alert fs_full address@e-mail"

should be

watchfor   /file system full/
         exec echo $0 | /scripts/send-alert fs_full address\@e-mail

I think that double quoting the command will cause $0 to be interpolated in
the compiled swatch script, rather that when swatch execs the command.

Have a look at the compiled swatch script.

And apologies for the english reply ;-)

Regards ... Jonathan

This message has been checked for all known viruses by the MessageLabs Virus Control \
Centre.

	
*********************************************************************

Notice:  This email is confidential and may contain copyright material of Ocado \
Limited (the "Company"). Opinions and views expressed in this message may not \
necessarily reflect the opinions and views of the Company. If you are not the \
intended recipient, please notify us immediately and delete all copies of this \
message. Please note that it is your responsibility to scan this message for viruses.

Company reg. no. 3875000.  Swallowdale Lane, Hemel Hempstead HP2 7PY

*********************************************************************


------------------------------

Message: 3
Date: Wed, 19 Mar 2003 10:48:15 +0100
From: Sylvain <sa@nctech.fr>
Subject: [Swatch-users] Re:  exec in swatch 3.0.5
To: swatch-users@ucsb.edu
Message-ID: <3E783CDF.1040109@nctech.fr>
Content-Type: text/plain; charset=us-ascii; format=flowed

Test have been made with and without double quote, and the result is the 
same.

Join Conf + Dump

thanks for help..

Conf :

watchfor   /file system full/
        exec echo $0 | /scripts/send-alert fs_full address\@e-mail

Dump Script:

### Watcher Script BEGIN ###

#
#    swatch: The Simple WATCHdog
#    Copyright (C) 1993-2001 E. Todd Atkins
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#

#use strict;
use FileHandle;
use POSIX ":sys_wait_h";
use vars qw(%Msg_Rec);

$SIG{'TERM'} = $SIG{'HUP'} = 'goodbye';
$SIG{'CHLD'} = 'IGNORE';

## Constants
(my $Me = $0) =~ s%.*/%%;
my $BELL   = "";
my $MAILER = "/usr/lib/sendmail -oi -t -odq";
my $WRITE  = "/usr/bin/write";
$/ = "
";

use IO::Handle;
STDOUT->autoflush(1);;

sub goodbye {
  $| = 0;
  $Fh->close;

  close_pipe_if_open();
  exit(0);
}

#
# in_range($range, $number)
# returns 1 if $number is inside $range, 0 if not
#
sub in_range {
  my $range = shift;
  my $num = shift;

  foreach my $f (split(/,/, $range)) {
    if ($f =~ /-/) {
      my ($low,$high) = split(/-/, $f);
      return 1 if ($low <= $num and $num <= $high);
    } elsif ($f == $num) {
      return 1;
    }
  }
  return 0;
}

#
# inside_time_window($days,$hours)
# returns 1 if inside window, 0 if outside window
#
sub inside_time_window {
  my $range = shift;
  my($days, $hours) = split(/:/, $range);

  my ($hr, $wday) = (localtime(time))[2,6];

  if (($days eq '*' or in_range($days, $wday))
      and ($hours eq '*' or in_range($hours, $hr))) {
    return 1;
  } else {
    return 0;
  }
}

#
# write_pid_file(file_name) - writes a one line file that contains
# the current process id.
#
sub write_pid_file {
  my $name = shift;
  my $fh = new FileHandle "$name", "w";

  if (defined($fh)) {
    print $fh "$$\n";
    $fh->close;
  } else {
    warn "$Me: cannot write pid file named $name: $!\n";
  }
}

                print "\n*** swatch version 3.0.5 (pid:23924) started at " . `/b
in/date` . "\n";

use Date::Calc qw(:all);

##
## parse_dot -- parse day or time
##
sub parse_dot {
  my $message = shift;
  my $dot_loc = shift;
  my @dot = ();
  my @ranges = split(/:/, $dot_loc);

  foreach my $range (0..$#ranges) {
    if ($ranges[$range] != -1) {
      my ($begin, $end) = split(/-/, $ranges[$range]);
      $dot[$range] = substr($message, $begin, ($end - $begin + 1));
    } else {
      $dot[$range] = 0;
    }
  }
  return @dot;
}

my %months = (
              Jan => 1,
              Feb => 2,
              Mar => 3,
              Apr => 4,
              May => 5,
              Jun => 6,
              Jul => 7,
              Aug => 8,
              Sep => 9,
              Oct => 10,
              Nov => 11,
              Dec => 12
             );

# Returns an array of year, month, day, hours, minutes, and seconds.
#
sub YMDHMS {
  my $string = shift;  # A string with the date and time stamp
  my $date_loc = shift; # The location of the date in the string indicated
                         # by cut marks or -1 if non-existent
                         # The cut marks are separated by a colon
                         # and go in the order: year, month, day
  my $time_loc = shift;  # The location of the time stamp. The cut marks
                         # are similar to $date_loc and go by the order:
                         # hours, minutes, seconds
  my ($t_year,$t_month,$t_day) = Today();

  my ($y, $m, $d) = parse_dot($string, $date_loc);
  my ($hrs, $mins, $secs) = parse_dot($string, $time_loc);

  if ($m =~ /[A-Za-z]*/) {
    $m = $months{$m};
  }

  if ($y == 0) { $y = $t_year};
  if ($m == 0) { $m = $t_month };
  if ($d == 0) { $d = $t_day };

  return ($y, $m, $d, $hrs, $mins, $secs);
}

sub new_msg {
  my $use = shift;
  my $regex = shift;
  my $msg = shift;
  my $count = shift;
  my @delta = @_;
  my $delta;
  if ($delta[0] == 0) {
    $delta = sprintf("%d:%.2d:%.2d", $delta[1], $delta[2], $delta[3]);
  } else {
    $delta = sprintf("$delta[0] day%s %d:%.2d:%.2d", $delta[0] > 1 ? 's' : '',
                    $delta[1], $delta[2], $delta[3]);
  }
  if ($use eq 'regex') {
    return "$count in $delta matching $regex: $msg";
  } else {
    return "$count in $delta: $msg";
  }
}

#
# Stores message information in
#    $Msg_Rec = (
#      {<truncated message>|<pattern>} => {
#        dhms => [ array ], # days,hours,minutes,seconds
#        count => integer,

sub throttle {
  my %opts = (
              KEY        => $_,
              CUT_MARKS  => "0:16",
              USE        => 'message',
              DATE_LOC   => "-1:0-2:4-5",
              TIME_LOC   => "7-8:10-11:13-14",
              TS_FROM    => 'message',
              @_
             );

  my $msg = $opts{'KEY'};
  my $use = $opts{'USE'};
  my $key;
  my @min_dhms_delta = split(/:+/, $opts{'MIN_DELTA'});
  my @ymdhms = ();

  if ($opts{'TS_FROM'} eq 'message') {
    @ymdhms = YMDHMS($msg, $opts{'DATE_LOC'}, $opts{'TIME_LOC'});
  } else {
    @ymdhms = Today_and_Now();
  }

  foreach my $i (0..$#min_dhms_delta) {
    # strip out unwanted elements
    splice (@min_dhms_delta, $i, 1) if ($min_dhms_delta[$i] =~ /[: ]/);
    $min_dhms_delta[$i] = 0 if (length($min_dhms_delta[$i]) == 0);
  }

  if ($use eq 'regex') {
    $key = $opts{'REGEX'};
  } else {
    my ($begin, $end) = split(/:/, $opts{'CUT_MARKS'});
    $key = substr($msg, 0, $begin);
    $key .= substr($msg, $end);
  }

  while ($#min_dhms_delta < 3) {
    unshift(@min_dhms_delta, 0); # make sure that the dhms array is full
  }

  if (exists $Msg_Rec{$key} and defined $Msg_Rec{$key}->{ymdhms}) {
    my $passed = 1;
    $Msg_Rec{$key}->{count}++;
    if ($ymdhms[1] > $Msg_Rec{$key}->{ymdhms}[1]) { $ymdhms[0]--; }
    my @delta_dhms = Delta_DHMS(@{$Msg_Rec{$key}->{ymdhms}}, @ymdhms);
    foreach my $i (0..$#min_dhms_delta) {
      $passed = 0 if ($delta_dhms[$i] < $min_dhms_delta[$i]);
      last unless ($delta_dhms[$i] == $min_dhms_delta[$i]);
    }
    if ($passed) {
      my $new = '';
      $new = new_msg($use, $opts{'REGEX'}, $opts{'KEY'},
                     $Msg_Rec{$key}->{count}, @delta_dhms);
      $Msg_Rec{$key}->{ymdhms} = [ @ymdhms ];
      $Msg_Rec{$key}->{count} = 1;
      return $new;
    } else {
      return '';
    }
  } else {
    my $rec;
    $rec->{ymdhms} = [ @ymdhms ];
    $Msg_Rec{$key} = $rec;
    return $msg;
  }
}


##
## ACTION SUBROUTINES
##

my %text_modes = (
  "black"       => "\033[30;1m",
  "red"         => "\033[31;1m",
  "green"       => "\033[32;1m",
  "yellow"      => "\033[33;1m",
  "blue"        => "\033[34;1m",
  "magenta"     => "\033[35;1m",
  "cyan"        => "\033[36;1m",
  "white"       => "\033[37;1m",
  "black_h"     => "\033[40;1m",
  "red_h"       => "\033[41;1m",
  "green_h"     => "\033[42;1m",
  "yellow_h"    => "\033[43;1m",
  "blue_h"      => "\033[44;1m",
  "magenta_h"   => "\033[45;1m",
  "cyan_h"      => "\033[46;1m",
  "white_h"     => "\033[47;1m",
  "bold"        => "\033[1m",
  "blink"       => "\033[5m",
  "inverse"     => "\033[7m",
  "normal"      => "\033[0m",
  "underscore"  => "\033[4m",
);

sub echo {
  my %args = (
              'MODES' => [ 'normal' ],
              @_
             );

  return if (exists($args{'WHEN'}) and not inside_time_window($args{'WHEN'}));

  if ($args{'MODES'}[0] eq 'random') {
    my @mode_names = keys %text_modes;
    print $text_modes{$mode_names[rand $#mode_names]};
  } else {
    foreach my $mode (@{$args{'MODES'}}) {
      print $text_modes{$mode};
    }
  }
  print $args{'MESSAGE'};
  print $text_modes{'normal'};
  print "\n";
}

#
# ring_bell(args) -- send x number of control-G characters to the output.
#
sub ring_bell {
  my %args = (
              'RINGS' => 1,
              @_
             );
  my $sun_terminal = (`uname -s` eq 'SunOS\n');

  return if exists($args{'WHEN'}) and not inside_time_window($args{'WHEN'});

  my $bells = $args{'RINGS'};
  for ( ; $bells > 0 ; $bells-- ) {
    print $BELL;
    sleep 1 if $sun_terminal; # SunOS needed this. Not sure about Solaris though
  }
}

#
# exec_command(args) -- fork and execute a command
#
sub exec_command {
  my %args = (@_);
  my $exec_pid;
  my $command;

  if (exists $args{'COMMAND'}) {
    $command = $args{'COMMAND'};
  } else {
    warn "$0: No command was specified in exec action.\n";
    return;
  }

  return if exists($args{'WHEN'}) and not inside_time_window($args{'WHEN'});

 EXECFORK: {
    if ($exec_pid = fork) {
      waitpid(-1, WNOHANG);
      return;
    } elsif (defined $exec_pid) {
      exec($command);
      } elsif ($! =~ /No more processes/) {
        # EAGAIN, supposedly recoverable fork error
        sleep 5;
        redo EXECFORK;
      } else {
        warn "$0: Can't fork to exec $command: $!\n";
      }
  }
  return;
}


{
  my $pipe_is_open;
  my $current_command_name;
  #
  # send_message_to_pipe -- send text to a pipe.
  #
  # usage: &send_message_to_pipe($program_to_pipe_to_including_the_vertical_bar_
symbol,
  #               $message_to_send_to_the_pipe);
  #

  sub send_message_to_pipe {
    my %args = (@_);
    my $command;

    if (exists $args{'COMMAND'}) {
      $command = $args{'COMMAND'};
    } else {
      warn "$0: No command was specified in pipe action.\n";
      return;
    }

    return if exists($args{'WHEN'}) and not inside_time_window($args{'WHEN'});

    # open a new pipe if necessary
    if ( !$pipe_is_open or $current_command_name ne $command ) {
      # first close an open pipe
      close(PIPE) if $pipe_is_open;
      $pipe_is_open = 0;
      open(PIPE, "| $command")
        or warn "$0: cannot open pipe to $command: $!\n" && return;
      PIPE->autoflush(1);
      $pipe_is_open = 1;
      $current_command_name = $command;
    }
    # send the text
    print PIPE "$args{'MESSAGE'}";

    if (not exists $args{'KEEP_OPEN'}) {
      close(PIPE) if $pipe_is_open;
      $pipe_is_open = 0;
    }
  }

  #
  # close_pipe_if_open -- used at the end of a script to close a pipe
  #     opened by &pipe_it().
  #
  # usage: &close_pipe_if_open();
  #
  sub close_pipe_if_open {
    if ($pipe_is_open) {
      close(PIPE);
    }
  }
}


#
# send_email -- send some mail using $MAILER.
#
# usage: &send_email($addresses_to_mail_to);
#
sub send_email {
  my $login = (getpwuid($<))[0];
  my %args = (
              'ADDRESSES' => $login,
              'SUBJECT' => 'Message from Swatch',
              @_
             );

  return if exists($args{'WHEN'}) and not inside_time_window($args{'WHEN'});

  (my $addresses = $args{'ADDRESSES'}) =~ s/:/ /g;
  (my $to_line = $args{'ADDRESSES'}) =~ s/:/,/g;


  if ($MAILER eq '') {
    warn "ERROR: $0 cannot find a mail delivery program\n";
    return;
  }

  open(MAIL, "| $MAILER $addresses")
    or warn "$0: cannot open pipe to $MAILER: $!\n" && return;

  print MAIL "To: $to_line\n";
  print MAIL "Subject: $args{SUBJECT}\n\n";
  print MAIL "$args{'MESSAGE'}\n";
  close(MAIL);
}


#
# write_message -- use $WRITE to send a message logged on users.
#
sub write_message {
  my %args = (@_);

  return if exists($args{'WHEN'}) and not inside_time_window($args{'WHEN'});

  if ($WRITE eq '') {
    warn "ERROR: $0 cannot find the write(1) program\n";
    return;
  }

  if (exists($args{'USERS'})) {
    foreach my $user (split(/:/, $args{'USERS'})) {
      send_message_to_pipe(COMMAND => "$WRITE $user 2>/dev/null",
                           MESSAGE => "$args{'MESSAGE'}\n");
    }
  }
}

use FileHandle;
my $Filename = 'kern.log';
my $Fh = new FileHandle "$Filename", 'r';
if (not defined $Fh) {
    die "/usr/local/bin/swatch: cannot read input \"$Filename\": $!\n";
}

LOOP: while (<$Fh>) {

    chomp;
    my $S_ = $_;
    @_ = split;

    # quote all special shell chars
    $S_ =~ s/([;&\(\)\|\^><\$`'\\])/\\$1/g;
    my @S_ = split(/\s+/, $S_);

    if (/file system full/) {
        exec_command('COMMAND' => "echo $sanitized_ | /scripts/send-alert fs_ful
l address\\@e-mail", );
      next;
    }

}
### Watcher Script END ###







------------------------------

_______________________________________________
Swatch-users mailing list
Swatch-users@ucsb.edu
http://ucsb.edu/mailman/listinfo/swatch-users


End of Swatch-users Digest, Vol 11, Issue 3
*******************************************


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic