File: swatchdog

package info (click to toggle)
swatch 3.2.4-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 448 kB
  • sloc: perl: 1,181; makefile: 4
file content (1408 lines) | stat: -rwxr-xr-x 36,357 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
#!/usr/bin/perl

eval 'exec /usr/local/bin/perl  -S $0 ${1+"$@"}'
    if 0; # not running under some shell
#
#    swatchdog: The Simple WATCHdog
#    Copyright (C) 1993-2008 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 English;
use FileHandle;
use Getopt::Long;
use IO::Handle;
use POSIX ":sys_wait_h";
use Date::Parse;
use Date::Format;
use Pod::Usage;

use vars qw/
  $commandLineString
  $awk_field_syntax
  $opt_config_file
  $opt_daemon
  $opt_debug_level
  $opt_dump_script
  $opt_examine
  @extra_modules
  @extra_include_dirs
  $opt_help
  $opt_input_record_separator
  $opt_old_style_config
  $opt_pid_file
  $opt_style_config
  $opt_read_pipe
  $opt_restart_time
  $opt_tail_file
  $opt_script_dir
  $opt_version
  @Config
  $Done
  $Restart
  $VERSION
  $Now
  $pid
  $thCounter
  $tail_cmd_args
  $tail_cmd_name
  $use_cpan_file_tail
/;

my @Swatchdog_ARGV = join(' ', $0, @ARGV); # Save just in case we need to restart 
(my $Me = $0) =~ s%.*/%%;	      # Strip the path off of the program name

$SIG{'CHLD'} = 'DEFAULT';

my $DEF_CONFIG_FILE = "$ENV{'HOME'}/.swatchdogrc";
my $DEF_INPUT;
if ( -f '/var/log/messages' ) {
  $DEF_INPUT = '/var/log/messages';
} elsif ( -f '/var/log/syslog' ) {
  $DEF_INPUT = '/var/log/syslog';
}

my $Config_File     = '';
my $Now = 0;  # The current time in Unix seconds. Gets set when set_restart_time is called
my $thCounter = 0;
my $tail_cmd_name = ''; # We'll try to find it in the PATH later
my $tail_cmd_args = '-n 0 -F'; 
$awk_field_syntax = 0;

my $AUTHOR = "E. Todd Atkins <Todd.Atkins\@StanfordAlumni.ORG>";
$VERSION = "3.2.4";
my $BUILD_DATE = "Aug 25, 2008";

my $swID = 0; 		# Main ID used for threshold functionality within 
			# watchfor blocks
my $swIDSub = 0;	# Secondary ID used for threshold functionality 
			# within individule actions (not implemented yet)

my $commandLineString = $0 . ' ' . join(' ', @ARGV);
 
sub print_version {
  print "This is $Me version $VERSION\n";
  print "Built on $BUILD_DATE\n";
  print "Built by $AUTHOR\n";
  exit 0;
}

 
sub parse_command_line {

  use Getopt::Long;
  Getopt::Long::config('bundling');
  pod2usage if not GetOptions(
				 "awk-field-syntax!" => \$awk_field_syntax,
				 "config-file|c=s" => \$opt_config_file,
				 "daemon" => \$opt_daemon,
				 "debug:i" => \$opt_debug_level,
				 "extra-module|M=s@" => \@extra_modules,
				 "extra-include-dir|I=s@" => \@extra_include_dirs,
				 "help|h" => \$opt_help,
                                 "input-record-separator:s" => \$opt_input_record_separator,
				 "old-style-config|O" => \$opt_old_style_config,
				 "pid-file=s" => \$opt_pid_file,
				 "restart-time|r=s" => \$opt_restart_time,
				 "tail-args=s" => \$tail_cmd_args,
				 "tail-program-name=s" => \$tail_cmd_name,
				 "tail-file|t=s" => \$opt_tail_file,
				 "read-pipe|p=s" => \$opt_read_pipe,
				 "examine|f=s" => \$opt_examine,
				 "script-dir=s" => \$opt_script_dir,
				 "use-cpan-file-tail" => \$use_cpan_file_tail,
				 "version|V" => \$opt_version,
				 "dump-script:s" => \$opt_dump_script,
				);

  pod2usage if $opt_help;

  if ($opt_version) {
    print_version;
    exit(0);
  }
  
  $opt_input_record_separator = (defined $opt_input_record_separator) ? $opt_input_record_separator : $/;

  # This is slightly bogus -- we call the set_restart_time function now
  # because if the args aren't properly formatted,  we want to die before the fork
  set_restart_time($opt_restart_time) if defined $opt_restart_time; 
}

 
###
### Routines to help with debugging
###

sub dprint {
    my $msg_lev = shift;
    my $msg = shift;
    print STDERR "DEBUG($msg_lev): $msg\n" if ($msg_lev & $opt_debug_level);
}

 
#
# make_debug_code() - creates the debug code for the watcher script
#
sub make_debug_code {
    my $code = '';

    $code = sprintf("my \$Debug_Mode = %d;\n", defined $opt_debug_level ? $opt_debug_level : 0);
    $code .= q|

sub dprint {
    my $msg_lev = shift;
    my $msg = shift;
    print STDERR "DEBUG($msg_lev): $msg\n" if ($msg_lev & $Debug_Mode);
}

|;
    return $code;
}

 
#
# checks validity of a regular expression. returns 1 if valid.
#
sub is_valid_pattern {
  my $pat = shift;
  return eval { "" =~ /$pat/; 1 } || 0;
}

 
#
# Build a configuration record structure
# 
{
  my @records;
sub read_config {
  my $filename = shift;
  my $rec = ();
  my $i = -1;
  my $keyword;
  my $pattern;
  my $option;
  my $value;
  my $fh;

  if ( not -r $filename ) {
    die "$Me: cannot find $filename. Please create it or specify an alternate configuration file. Exiting.\n";
  }

  $fh = new FileHandle "$filename", "r";
  if (not defined $fh) {
    warn "$Me: cannot open $filename: $!\n";
    exit 1;
  }

  while (<$fh>) {
    my($key, $val);
    chomp;
    s/^\s+//; ## strip off leading blank space
    s/\s+$//; ## strip off trailing blank space

    ### Skip comments and blank lines ###
    next if (/^\#/ or /^$/);

    ### combine lines that end with \ + <cr> ###
    while (/\\$/) {
      my $line;
      s/\\$//;
      if (defined($line = <$fh>)
	  and not $line =~ /^\#/ and not $line =~ /^$/) {
        chomp($line);
        s/^\s+//; ## strip off leading blank space
	s/\s+$//; ## strip off trailing blank space
        $_ .= $line;
      }
    }

    if (/\s*=\s*/) {
      $key = (split(/\s*[= ]\s*/))[0];
      ($val = substr($_, length($key))) =~ s/^\s*=\s*//;
    } else {
      $key = (split())[0];
      ($val = substr($_, length($key))) =~ s/^\s*//;
    }

    s/\#.*$// if ($key ne 'perlcode'); ## strip trailing comments

    if ($key =~ /include/i) {
      @records = read_config($val);
    } elsif ($key =~ /^(watchfor|waitfor|ignore)$/i) {
      $i++;
      if (defined $rec->{pattern}) {
	push @records, $rec;
	$rec = ();
      }

      if (not is_valid_pattern($val)) {
	die "$Me: error in pattern \"$val\" on line $. of $filename\n";
      }

      $rec->{keyword} = lc($key);
      if (length($val)) {
	$rec->{pattern} = $val;
      }
    } elsif ($key =~ /perlcode/i) {
      my $depth = 1;
      if ($val =~ /(\d+)\s+(.*)$/) { # put perlcode at a given depth
	$depth = $1;
	$val = $2;
      }
      if ($depth < 2) {
	$i++;
	if (defined $rec->{pattern}) {
	  push @records, $rec;
	  $rec = ();
	}
	$rec->{keyword} = lc($key);
        $rec->{depth} = $depth;
	$rec->{value} = $val if (length($val));
        push @records, $rec;
	$rec = ();
      } else { # depth is 2
	push(@{$rec->{actions}}, { action => lc($key),
				   depth => $depth,
				   value => $val });
      }
    } elsif ($i < 0) {
      warn "$Me: error in $filename at line ${.}: invalid keyword. Skipping.\n";
    } elsif ($key =~ /^(throttle|threshold)$/i) {
      $rec->{lc($key)}{value} = $val;
    } else {
      push(@{$rec->{actions}}, { action => $key, value => $val });
    }
  }
  undef $fh;
  if (defined $rec->{pattern}) {
    push @records, $rec;
    $rec = ();
  }

  ## Sanity Check: If the config file did not contain anything useful then exit 
  if ($#records < 0) {
    die "$Me: There were no useful entries in the configuration file. Exiting.\n";
  } else {
    return(@records);
  }
}
}

 
sub read_old_config {
  my $filename = shift;
  my $fh = new FileHandle $filename, "r";
  my @records = ();

  if (not defined $fh) {
    die "$Me: cannot read $filename: $!\n";
  }

  while (<$fh>) {
    my $rec = ();
    chomp;
    @_ = split(/\t+/);

    if (/^\s*$/ or /^\s*\#/) {
      next;
    } elsif (/ignore/) {
      $rec->{keyword} = 'ignore';
      $rec->{pattern} = $_[0];
    } else {
      $rec->{keyword} = 'watchfor';
      $rec->{pattern} = $_[0];
      if (defined $_[2] and $_[2] =~ /^[0-9]/) {
	$rec->{'throttle'}->{value} = $_[2];
      }
      foreach my $action (split(/,/, $_[1])) {
	my ($key,$val) = split(/\s*=\s*/, $action);
	push(@{$rec->{actions}}, { action => $key, value => $val });
      }
    }
    push(@records, $rec);
  }
  return (@records);
}

 
#
# make_start_code -- return the start of our swatchdog generated perl script
#
# usage: $script .= make_start_code;
#
sub make_start_code {
  my $code = '';
  my $mail_cmd = '';
  my $extra_includes = '';
  my $extra_modules = '';

  if ($#extra_modules != -1) {
    foreach my $m (@extra_modules) {
      $extra_modules .= "use $m;\n";
    }
  }

  if ($#extra_include_dirs != -1) {
    $extra_includes = join(' ', @extra_include_dirs);
  }

  $code  = qq[
#
#    swatchdog: The Simple WATCHdog
#    Copyright (C) 1993-2006 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";

## User supplied modules and their locations
use lib qw($extra_includes);
$extra_modules

use Swatchdog::Actions;
use Swatchdog::Throttle;
use Swatchdog::Threshold;

use vars qw/
  %Msg_Rec
  \$Fh
/;

\$SIG{'TERM'} = \$SIG{'HUP'} = 'goodbye';
\$SIG{'CHLD'} = 'DEFAULT';

## Constants
(my \$Me = \$0) =~ s%.*/%%;
my \$BELL   = "\007";
\$/ = "$opt_input_record_separator";
my \$swatchdog_flush_interval = 300;
my \$swatchdog_last_flush = time;
my \$tail_pid = -1;

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

sub goodbye {
  \$| = 0;
  if( \$tail_pid != -1 ) {
    kill('TERM', \$tail_pid);
  }
];

  if ($opt_read_pipe) {
    $code .= "  close(SW_PIPE);\n";
  } elsif ($opt_examine) {
    $code .= "  \$Fh->close;\n";
  }

  $code .= q|
  &Swatchdog::Actions::close_pipe_if_open();

  exit(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";
  }
}
  |;

  if ($opt_daemon) {
    $code .= qq[
my \$pid = fork;
exit if \$pid;
die "Couldn't fork: \$!" unless defined(\$pid);
# dissociate from the controlling terminal
POSIX::setsid() or die "Can't start new session: \$!"; 
# set our named to 'swatchdog' so that rc scripts can 
# figure out who we are.
\$0="$commandLineString";
	       ];
  } else {
    $code .= qq[print \"\\n*** ${Me} version ${VERSION} (pid:$$) started at \" . `/bin/date` . \"\\n\";];
  }
  
  $code .= qq[write_pid_file("$opt_pid_file"); \n] if (defined $opt_pid_file);

  return $code;
}

 
sub make_start_loop {
  my $filename = $DEF_INPUT;
  my $code = '';

  if (defined $opt_examine) {
    $filename = $opt_examine;
    $code = qq[
use FileHandle;
my \$Filename = '$filename';
\$Fh = new FileHandle \"\$Filename\", 'r';
if (not defined \$Fh) {
    die "$0: cannot read input \\"\$Filename\\": \$!\\n";
}

LOOP: while (<\$Fh>) {
];

  } elsif (defined $opt_read_pipe) {
    $filename = $opt_read_pipe;
    $code = qq[
use FileHandle;
my \$Filename = '$filename';

if (not open(SW_PIPE, \"$filename|\")) {
    die "$0: cannot read from pipe to program \\"\$Filename\\": \$!\\n";
}

LOOP: while (<SW_PIPE>) {
];

  } else {
    $filename = $opt_tail_file if (defined $opt_tail_file);
    if ($use_cpan_file_tail) {
      $code = qq[
use File::Tail;
my \$Filename = '$filename';
my \$File = File::Tail->new(name=>\$Filename, tail=>1, maxinterval=>0.5, interval=>0.5);
if (not defined \$File) {
    die "$0: cannot read input \\"\$Filename\\": \$!\\n";
}

LOOP: while (defined(\$_=\$File->read)) {
];
    } else {
      if ($tail_cmd_name eq '') {
	foreach my $path (split(/:/,$ENV{'PATH'})) {
	  if (-x "${path}/tail") {
	    $tail_cmd_name = "$path/tail";
	    last;
	  }
	}
	die "$Me: cannot find \"tail\" program in PATH\n" if $tail_cmd_name eq '';
      }
       $code = qq/
my \$filename = '$filename';
\$tail_pid = open(TAIL, \"$tail_cmd_name $tail_cmd_args \$filename|\");
if (not \$tail_pid) {
    die "$0: cannot read run \\"$tail_cmd_name $tail_cmd_args \$filename\\": \$!\\n";
}

LOOP: while (<TAIL>) {
/;
    }
  }

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

!;
}

 
sub make_end_code {
    my $code;
    $code = q[
}
## TODO: Add close !!!
];
    return $code;
} 

sub action_def_to_subroutine_call {
  my $key = shift;  # converts to subroutine name
  my $optstr = shift; # comma separated option string
  my $pattern = shift;
  my $message = shift;

  my $actinfo = { # action subroutine info
                 "continue" => { 'sub_name' => "continue" },
		 "bell" => { 'sub_name' => "&Swatchdog::Actions::ring_bell", 'def_arg' => 'RINGS' },
		 "echo" => { 'sub_name' => "&Swatchdog::Actions::echo", 'def_arg' => 'MODES' },
		 "exec" => { 'sub_name' => "&Swatchdog::Actions::exec_command", 'def_arg' => 'COMMAND' },
		 "pipe" => { 'sub_name' => "&Swatchdog::Actions::send_message_to_pipe", 'def_arg' => 'COMMAND' },
		 "mail" => { 'sub_name' => "&Swatchdog::Actions::send_email", 'def_arg' => 'ADDRESSES' },
		 "quit" => { 'sub_name' => "exit" },
		 "throttle" => { 'sub_name' => '&Swatchdog::Throttle::throttle', 'def_arg' => 'MIN_DELTA' },
		 "write" => { 'sub_name' => "&Swatchdog::Actions::write_message", 'def_arg' => 'USERS' },
		};

  my $opt;
  my %options;
  my $have_opts = 0;

  foreach my $v (split(/,/, $optstr)) {
    if ($v =~ /(\w+)\s*=\s*"?(\S+[^"]*)/) {
      $opt = uc($1);
      $options{$opt} = $2;
      $options{$opt} =~ s/@/\\@/g if ($key eq 'mail');
    } else {
      $opt = $v;
      $opt =~ s/@/\\@/g;
      $opt =~ s/^\s+//o;
      $opt =~ s/^\s+$//o;
      $opt = $1 if ($opt =~ /^['"]\s*(.*)\s*['"]$/o);
      $opt =~ s/"/\\"/go;

      if ($actinfo->{$key}{'def_arg'} eq 'MODES') { 
        ## Modes are processed as an array ##
	push(@{$options{$actinfo->{$key}{'def_arg'}}}, $opt);
      } else {
	$options{$actinfo->{$key}{'def_arg'}} = $opt;
      }
    }
  }

  if ($key =~ /(exec|pipe)/) {
    $options{'COMMAND'} = convert_command('S_', $options{'COMMAND'});
  }

  $options{'MESSAGE'} = $message unless exists $options{'MESSAGE'};

  my $opts = '';
  if (scalar %options) {
    if ($key eq 'threshold') {
      $opts = "\'SWID\' => \'$swID\', ";
    }
    foreach my $k (keys %options) {
      if ($k eq 'MODES') {
	$opts .= "\'$k\' => [ ";
	foreach my $v (@{$options{$k}}) {
	  $opts .= "\"$v\",";
	}
	$opts .= " ], ";
      } elsif ($k eq 'MIN_DELTA') {
        ## convert to new throttle variable name ##
        $opts .= "\'HOLD_DHMS\' => [ ";
        my @dhms = split(/:/,$options{$k});
        for (my $i = $#dhms ; $i < 3 ; $i++) {
          unshift(@dhms, 0);
        }
        foreach my $v (@dhms) {
	  $opts .= "\"$v\",";
	}
	$opts .= " ], ";
      } else {
	$opts .= "\'$k\' => \"$options{$k}\", "; # if (defined $options{$k});
      }

      if ($k eq 'THRESHOLD') {
	$opts .= "\'SWID\' => \'$swID:$swIDSub\', ";
      }
    }
  }

  my $sub_name = (exists $actinfo->{$key}{'sub_name'}) 
  ? $actinfo->{$key}{'sub_name'} : $key;

  return "$sub_name($opts)";
}
 
#
# convert_command -- convert wildcards for fields in command from
#       awk type to perl type.  Also, single quote wildcards
#       for better security.

# usage: &convert_command($Command);

sub convert_command {
  my $varname = shift;
  my $command = shift;
  my @new_cmd = ();
  my $i = my $n = my $m;

  $command =~ s/(?<!\\)\$[0*]/\$$varname/g;

  foreach $i (split(/\s+/, $command)) {
    if ($awk_field_syntax) {
       while ($i =~ /(?<!\\)\$([1-9]+)/) {
          $n = $1;
          $m = $n - 1;
          $i =~ s/\$$n/\$$varname\[$m\]/;
       }
    }
    push(@new_cmd, $i);
  }

  return join(' ', @new_cmd);
}

 
sub make_ignore_block {
  my $ref = shift;
  dprint(4, "ignoring $ref->{pattern}");
  return "\tnext;\n";
}

 
sub make_watchfor_block {
  my $pattern = shift;
  my $ref = shift;
  my $code = "";
  my $do_quit = 0;
  my $do_continue = 0;
  my $message = '$_';

  $swID++; 	# increment internal identifier
  $swIDSub = 0; # reset internal sub identifier

  foreach my $a_ref (@{$ref->{actions}}) {
    if ($a_ref->{action} eq 'perlcode' and $a_ref->{depth} == 2) {
      $code .= "\t$a_ref->{value}\n";
    }
  }

  # Encapsulate the whole thing (even throttle) in a threshold block.  The
  # indenting in the generated code is "wrong", but there is no easy way to
  # fix it.
  if (exists $ref->{"threshold"}) {
    $code .= "     if (";
    $code .=  action_def_to_subroutine_call('threshold', $ref->{'threshold'}{value}, $pattern, $message);
    $code .=  ") {\n";
  }

  if (exists $ref->{"throttle"}) {
    $code .= "      if ((my \$rtn = ";
    $code .=  action_def_to_subroutine_call('throttle', $ref->{'throttle'}{value}, $pattern, $message);
    $code .= ") ne '') {\n";
    $message = '$rtn';
  }
  dprint(4,"watching $ref->{pattern}");

  foreach my $a_ref (@{$ref->{actions}}) {
    $swIDSub++; # increment internal sub identifier

    my $act = $a_ref->{action};
    if ($act eq 'perlcode' and $a_ref->{depth} == 3) {
      $code .= "\t$a_ref->{value}\n";
    } elsif ($act eq 'continue') {
      $do_continue = 1;
    } elsif ($act eq 'quit') {
      $do_quit = 1
    } elsif ($act ne 'perlcode') {
      $code .= "\t";
      $code .= action_def_to_subroutine_call($act, $a_ref->{value}, undef, $message);
      $code .= ";\n";
    }
  }

  if (exists $ref->{"throttle"}) {
    $code .= "      }\n";
  }
  
  if (exists $ref->{"threshold"}) {
    $code .= "     }\n";
  }
  
  if ($do_quit) {
    $code .= "      &goodbye;\n";
  } elsif (not $do_continue) {
    $code .= "      next;\n";
  }
  
  return $code;
}

 
#
# make_script() - The workhorse for creating the script that will do the
# message processing.
#
# returns a string which contains the full script.
#
sub make_script {
  my $key;
  my $block_open = 0;
  my $script = make_start_code();

  for my $rec (@Config) {
    if ($rec->{keyword} eq 'perlcode' and $rec->{depth} == 0) {
      $script .= "$rec->{value}\n";
    }
  }

  $script .= make_start_loop();

  for my $rec (0..$#Config) {
    my $pattern = $Config[$rec]->{pattern};
    my $config = $Config[$rec];
    if ($block_open) {
      $script .= "    }\n\n";
      $block_open = 0;
    }

    $key = $config->{keyword};

    if ($key =~ /^perlcode$/ and $config->{depth} == 1) {
      $script .= "    $config->{value}\n";
      $block_open = 0;
    } elsif ($key !~ /^perlcode$/) {
      $script .= "    if ($pattern) {\n";
      $block_open = 1;
    }

    if ($key =~ /^ignore$/) { 
      $script .= make_ignore_block($config);
    } elsif ($key =~ /^watchfor$/) {
      $script .= make_watchfor_block($pattern, $config);
    }
  }
  $script .= "    }\n";

  $script .= make_end_code;

  return $script;
}
 
#
# terminate
#
# usage: terminate($SIGNAL);
#
sub terminate {
    my($Sig) = shift;
    dprint(16, "terminate($Sig)");
    return if $pid == 0;

    if ($Sig) { 
      print STDERR "Caught a SIG$Sig -- sending a TERM signal to $pid\n" 
    }
    kill('TERM', $pid) unless $opt_dump_script;
    $Restart = 0;
}

 
#
# restart -- kill the child, delete the script, and start over.
#
# usage: &restart($Sig);
#
sub restart {
    my($Sig) = shift;
    dprint(16, "restart($Sig)");
    print STDERR "Caught a SIG$Sig -- sending a TERM signal to $pid\n";
    kill('TERM', $pid);
    $Restart = 1;
}

 
## Courtesy of "Shoshana Abrass" <shoshana@anim.dreamworks.com> ...
##
## USAGE: set_restart_time(timestring)
## WHICH: converts the user-given timestring into the time (in unix 
##        seconds) when the program should next restart
## WHERE: "timestring" is one of the supported command-line arguments, 
##        for example:
##
##       00:01       restart every day at 12:01 AM
##      +24:00       restart every 24 hours
##       +1:00       restart every hour
##
##   There is currently no way to say "restart at the next HH:00 and every
##   hour after that", but it might be a nice feature.
##
## RETURNS: seconds since Jan 1 1970  of the next restart time.
##
sub set_restart_time{
  my ($timestring)=(@_);
  my ($DeltaHrs, $DeltaMins, $RestartTime);
  
  my ($OneMinute, $OneHour, $OneDay) = (60, 3600, 86400); # In seconds
  my ($EndOfTime) = (2147483647);                         # Mon Jan 18 19:14:07 2038

  $Now = time();

  if ( $timestring =~ m/^\+/ ) {
    if ( $timestring =~ m/^\+(\d+):(\d+)$/  ) {
      #
      #
      $DeltaHrs = $1 * $OneHour;
      $DeltaMins = $2 * $OneMinute;

      $RestartTime = $Now + $DeltaHrs + $DeltaMins;
      if ( $RestartTime >= $EndOfTime ) {
	print "ERROR: Restart time delta would put us past the end of\n";
	print "       unix time, ", ctime ($EndOfTime);
	die "       Unacceptable time delta\n";
      }
    }
    else {
      die "Unrecognized delta time format \"$timestring\"\n";
    }
  }
  else {
    if ( ! ($RestartTime = str2time("$timestring")) ) {
      die "Unrecognized time format \"$timestring\"\n";
    }
    while ( $RestartTime <= $Now  ) {
      # if the time of day has already passed, then   
      # the user must mean that time tomorrow
      dprint(32, "set_restart_time(): adding a day to RestartTime $RestartTime (unix seconds)");
      $RestartTime += $OneDay;
    }
  }
  
  return ($RestartTime);
}

 
## Courtesy of "Shoshana Abrass" <shoshana@anim.dreamworks.com> ...
##
## USAGE: set_alarm (seconds)
##
## WHICH: Takes an absolute time value in unix seconds, and sets the alarm 
##        to go off at that time by subtracting $Now seconds.  We want to use 
##        the same value of $Now that was used above in set_restart_time, 
##        because
##            (1) we presume these functions are being called sequentially;
##            (2) to calculate against one $Now and set against another 
##                doesn't make sense.
##
sub set_alarm{
  my ($RestartTime) = @_;

  # carp "Called set_alarm";

  if ( $Now == 0 ) { $Now = time();}  # This should never happen

  if ( $RestartTime <= $Now ) {

    # This should never happen, because the intention is that
    # set_restart_time should be called before set_alarm. 
    # But just in case....
    print "WARNING: setting restart alarm to zero\n";
    alarm(0);
  }
  else {
    alarm ($RestartTime - $Now);
  }

}

 
##
## doit()
##
sub doit {
  $SIG{'INT'} = $SIG{'QUIT'} = $SIG{'TERM'} = $SIG{'ALRM'} = $SIG{'HUP'} = 'default';

  $Config_File = (defined $opt_config_file) ? $opt_config_file : $DEF_CONFIG_FILE;
  
  ## Read in the configuration file ##
  if ($opt_old_style_config) {
    @Config = read_old_config($Config_File);
  } else {
    @Config = read_config($Config_File);
  }

  ## Create a script based on the configuration file and command line options
  my $Watcher_Script = make_script;
  

  if (defined $opt_dump_script) {
    ## Just write the script to STDOUT or the value of $opt_dump_script and exit
    if ($opt_dump_script ne '') {
      open(DS,">$opt_dump_script") or die "$0: cannot write to $opt_dump_script: $!\n";
    } else {
      open(DS,">-") or die "$0: cannot wrote to STDOUT: $!\n";
    }
    print DS "### Watcher Script BEGIN ###\n";
    print DS $Watcher_Script;
    print DS "### Watcher Script END ###\n";
    close(DS);
    $Done = 1;
  } else { ## Write the script to a file and run it ##

    ## Write the script file ##
    my $script_file = defined($opt_script_dir) ? $opt_script_dir : $ENV{'HOME'};
    $script_file .= "/.swatchdog_script.$$";
    my $swatchdog_fh = new FileHandle $script_file, "w";
    if (defined $swatchdog_fh) {
      $swatchdog_fh->print($Watcher_Script);
      $swatchdog_fh->close;

    ## Now fork and start monitoring ##
    FORK: {
	if ($pid = fork) {
	  dprint(8, "doit(): pid = $pid");
	  foreach my $k (sort keys %SIG) {
	    dprint(8, "doit(): a: $k => $SIG{$k}") if defined $SIG{$k};
	  }
	  $SIG{'INT'} = $SIG{'QUIT'} = $SIG{'TERM'} = 'terminate';
	  $SIG{'ALRM'} = $SIG{'HUP'} = 'restart';
	  foreach my $k (sort keys %SIG) {
	    dprint(8, "doit(): b: $k => $SIG{$k}") if defined $SIG{$k};
	  }
	  if ( defined $opt_restart_time ) {
	    my $RestartTime = set_restart_time($opt_restart_time);
	    print "Will restart at ", ctime($RestartTime);
	    set_alarm ($RestartTime);
	  }
	  waitpid($pid, 0);
	  alarm(0);
	  if (defined $opt_daemon) {
	    exit(0);
	  }
	} elsif (defined $pid) {
	  exec("$EXECUTABLE_NAME $script_file");
	} elsif ($! =~ /No more processes/) {
	  # EAGAIN, supposedly recoverable fork error
	  sleep 5;
	  redo FORK;
	} else {
	  die "$Me: Can't fork: $!\n";
	}
      }
      $Done = 1 if (not $Restart); # Restart set to 1 by restart() #
      unlink($script_file);
    }
  }
}

###
### MAIN
###

$Done = 0;
$Restart = 0;

while (!$Done) {
  parse_command_line;
  main::doit();
}

###
### End of main block
###
 
=head1  NAME

swatchdog - simple watcher

=head1  SYNOPSIS

B<swatchdog> 
[ B<--awk-field-syntax> ]
[ B<--config-file|-c> I<file> ] 
[ B<--daemon> ] 
[ B<--extra-include-dir|-I> I<path> ]
[ B<--extra-module|-M> I<module_name> ]
[ B<--help|-h> ]
[ B<--input-record-separator> I<regex> ] 
[ B<--old-style-config|-O> ]
[ B<--pid-file> I<file> ]
[ B<--restart-time|-r> I<time> ] 
[ B<--script-dir> I<path> ]
[ B<--tail-args> I<arguments_for_tail_program> ]
[ B<--tail-program-name> I<filename> ]
[ B<--version|-V> ]
[ B<--use-cpan-file-tail> ]
[ [ B<--examine|-f> I<file_to_examine> ] 
| [ B<--read-pipe|-p> I<program_to_pipe_from> ] 
| [ B<--tail-file|-t> I<file_to_tail> ] ]
[ B<--debug> [ I<level> ] ] 
[ B<--dump-script> I<filename> ]

=head1  DESCRIPTION

B<Swatchdog> is designed to monitor system activity.
In order for B<Swatchdog> to be useful, it requires a configuration file
which contains I<pattern(s)> to look for and I<action(s)>
to perform when each pattern is found.

=head1 COMMAND LINE OPTIONS

=over 4

=item B<--awk-field-syntax>

Use this option only if you want to override regular expression backreferencing
in favor of B<awk(1)> style field referencing. Included for backward 
compatibility.

=item B<--config-file|-c> I<filename>

Tells B<swatchdog> where to find its configuration file. The default
is I<${HOME}/.swatchdogrc>.

=item B<--daemon>

This tells B<swatchdog> to run in the background and disassociate itself from 
any terminal.

=item B<--extra-include-dir|-I> I<path>

This tells B<swatchdog> where to look for custom I<action> modules.

=item B<--extra-module|-M> I<module_name>

This tells B<swatchdog> what custom I<action> modules to load in.

=item B<--help|-h>

Prints usage information and exits.

=item B<--input-record-separator>=I<regular_expression>

Tells B<swatchdog> to use I<regular_expression> to delineate
the boundary of each input record. The default is a carriage return. 

=item B<--old-style-config|-O>

This tells B<swatchdog> that your configuration file is written using the
syntax that was abandoned back in the 1990's.

=item B<--pid-file> I<file>

Writes the process ID to I<file>. Useful when running in daemon mode.

=item B<--restart-time>=I<[+]hh:mm[am|pm]> or B<-r> I<[+]hh:mm[am|pm]>

Restart at the specified time where I<hh> is hours and I<mm> is minutes. 
If the am/pm indicator is omitted, then a 24-hour clock is assumed. 
If the time is preceded by the "+" character, then the restart time 
will be set to the current time plus the specified time and the am/pm
indicator will be ignored.

=item B<--script-dir>=I</path/to/directory>

This switch causes the temporary watcher script to be written to a file
in the specified directory rather than the user's home directory. It is
highly advised that you do B<NOT> use directories that are writable by others
such as /tmp.

=item B<--tail-args> I<arguments_for_tail_program>

Pass specific options to the B<tail(1)> program.

=item B<--tail-program-name> I<filename>

Runs an alternate B<tail(1)> like program instead of the system default.

=item B<--version> or B<-V>

Prints version information and exits.

=item B<--use-cpan-file-tail>

Use CPAN's File::Tail module to read the log file instead of the tail(1) 
command.

=back

You may specify only one of the following options:

=over 4

=item B<--tail-file>=I<filename> or B<-t> I<filename>

Examine lines of text as they are added to filename. 

=item B<--read-pipe>=I<command> or B<-p> I<command>

Examine input piped in from the I<command>. 

=item B<--examine>=I<filename> or B<-f> I<filename>

Use I<filename> as the file to examine. 
B<Swatchdog> will do a single pass through the named file. 

=back

The following options are purely for debugging purposes, but are
documented here for completeness:

=over 4

=item B<--debug>[=I<level>]

Spew out various levels of debugging for swatchdog developers.

=item B<--dump-script>[=I<filename>]

Instead of running the watcher script after it is generated, 
it is written to I<filename> or to STDOUT.

=back 

If swatchdog is called with no options, it is the same as typing the 
command line

	swatchdog --config-file=~/.swatchdogrc --tail-file=/var/log/syslog

or if /var/log/messages exists

	swatchdog --config-file=~/.swatchdogrc --tail-file=/var/log/messages

=head1 THE CONFIGURATION FILE 

The configuration file is used by the B<swatchdog(8)>
program to determine what types of expression patterns to look for
and what type of action(s) should be taken when a pattern is matched.

Each line should contain a keyword and a, sometimes optional,
value for that keyword. The keyword and value are separated by 
a space or an equal (=) sign.

watchfor regex

ignore regex

=over 4

=item B<echo [modes]>

Echo the matched line. The text mode may be I<normal>, I<clear>, I<reset>, I<bold>
I<underline>, I<underscore>, I<blink>, I<reverse>, I<concealed>, I<black>, I<red>
I<green>, I<yellow>, I<blue>, I<magenta>, I<on_black>, I<on_red>, I<on_green>
I<on_yellow>, I<on_blue>, I<on_magenta>, I<on_cyan>, I<on_white>. The <on_> colors
specify a highlighting color. Some modes may not work on some terminals. B<Normal>
is the default.
For B<modes> changes and additions check perl module Term::ANSIColor man page.

=item B<bell [N]>

Echo the matched line, and send a bell I<N> times (default = 1).

=item B<exec command>

Execute I<command>. The I<command> may contain variables which are 
substituted with fields from the matched line. If the B<--awk-field-syntax>
command-line option has been specified, then each I<$N> will be replaced
by the I<Nth> field in the line. If the option has not been specified,
then each I<$N> will refer to a backreference in the regular expression
used to match the line.

A I<$0> or I<$*> will always be replaced by the entire line, unless they
have been escaped, regardless of the B<--awk-field-syntax> option.

An escaped I<$N>, I<$0> or I<$*> may have unwanted effects since the value
will be determined by the shell used to execute the command.

=item B<mail [addresses=address:address:...][,subject=your_text_here]>

Send I<mail> to I<address(es)> containing the matched lines as
they appear (default address is the user who is running the program).

=item B<pipe command[,keep_open]>

Pipe matched lines into I<command>. Use the B<keep_open> option to 
force the pipe to stay open until a different pipe action is run or 
until swatchdog exits.

=item B<write [user:user:...]>

Use B<write(1)> to send matched lines to I<user(s)>.

=item B<throttle hours:minutes:seconds,[key=message|regex|<regexE>]>

This action has been depreciated. Use B<threshold> instead
For example,

=over 4

throttle 15:00,key="foo"

=back

would look like this

=over 4

threshold track_by="foo",type=limit,count=1,seconds=900

=back

=item B<threshold track_by=key, type=<limit|threshold|both>, count=number, seconds=number>

Thresholding can be done for the complete B<watchfor> block and/or for
individual actions. Add "threshold=on" as an option along with the other
threshold options when thresholding an individual action.

=over 4

=item B<track_by> 

The value of this should be something that is unique to the B<watchfor>
regular expression. Tip: enclose unique parts of the regular expression
in parentheses, then use the sub matches as part of the value 
(e.g. track_by="$2:$4").

=item B<type>

There are three types of thresholding. They are as follows:

=over 4

=item B<limit>

Perform action(s) for the first "B<count>" matches during the
time interval specified by "B<seconds>",
then ignore events for the rest of the time interval (kind of like throttle)

=item B<threshold>

Perform action(s) on each match for up to B<count> matches during the 
time interval specified by B<seconds>

=item B<both>

Perform actions(s) once per time interval after "B<count>" matches occur,
then ignore additional matches during the time interval 
specified by "B<seconds>"

=back

=back

=item B<continue>

Use this action to cause B<swatchdog> to continue to try to match other
pattern/action groups after it is done with the current pattern/action
block.

=item B<quit>

Use this action to cause B<swatchdog> to clean up and quit immediately.

=back

=head1 SPECIAL OPTION

The following may be used as an option for any of the above actions except for throttle and threshold.

=over 4

=item B<when=>I<day_of_week:hour_of_day>

Use this option to specify windows of time and days when the action can 
be performed. 
For example:

=over 4

mail=sysad-pager@somehost.somedomain,when=1-6:8-17

=back

=back

=head1 FOR PERL HACKS ONLY

=over 4

=item B<perlcode> [I<depth>] arbitrary_Perl_code
 
This permits you to easily insert random Perl code into your swatchdogrc file.
The optional depth value tells swatchdog how deep into the code to put the perl 
code. (0=outside the main loop, 1=inside the main loop (default), 2=just inside the 
conditional used by the current watchfor statement, and 3=inside the throttle
block).

Its intended use is to permit variable substitution. For example:

=over 4

perlcode $syslog="^\w{3}\s+\d{1,2}\s+\d{2}:\d{2}:\d{2}.*";
 
watchfor /$syslog hostname pppd/>
 
=back

but any valid Perl is permitted.  Remember the semicolon, and make judicious
use of the B<--dump-script> option if you run into trouble.

=back

=head1 CONFIGURATION EXAMPLE

=over 4

perlcode my $fsf_regex = '\d{2}:\d{2}:\d{2}\s+(.* file system full)';

watchfor /$fsf_regex/
    threshold track_by=$1,type=limit,count=1,seconds=60
    echo
    bell

=back

In this example, a line which contains the string "file system full" will
be echoed and the screen bell will sound.  Also, B<threshold> will use what 
is matched within the parentheses as its key rather than trying to use the 
log message with its time stamp cut out. Multiple instances of
the message will not be echoed if they appear within a minute of the 
first one. Instead the following message will be acted upon after 
the time interval has expired.  

=head1 SEE ALSO

B<signal(3)>, B<perl(1)>, B<perlre(1)>

=head1 NOTES

Upon receiving an ALRM or HUP signal swatchdog will re-read the
configuration file and restart, except when used with the I<--daemon> 
command line option where it will simply exit.
Swatchdog will terminate gracefully
when it receives a QUIT, TERM, or INT signal.

=head1 AUTHOR

    E. Todd Atkins
    Todd.Atkins@StanfordAlumni.ORG

=head1 AVAILABILITY

Swatchdog is a SourceForge project whose project page is at 
http://sourceforge.net/projects/swatchdog and homepage is at
http://swatchdog.sourceforge.net

=cut