File: 015action.t

package info (click to toggle)
monitoring-plugins-check-logfiles 4.1.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,992 kB
  • sloc: perl: 19,492; sh: 3,422; makefile: 72; awk: 41
file content (246 lines) | stat: -rw-r--r-- 8,045 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
#!/usr/bin/perl -w
#
# ~/check_logfiles/test/005negative.t
#
#  Test that all the Perl modules we require are available.
#

use strict;
use Test::More tests => 14;
use Cwd;
use lib "../plugins-scripts";
use Nagios::CheckLogfiles::Test;
use constant TESTDIR => ".";

my $configfile = <<EOCFG;
\$seekfilesdir = "./var/tmp";
\$protocolsdir = "./var/tmp";
\$scriptpath = "./bin";
\$MACROS = {
  CL_NSCA_HOST_ADDRESS => 'nagios.dierichs.de',
  CL_NSCA_PORT => 5555,
  SNMP_TRAP_SINK_HOST => 'nagios.dierichs.de',
  SNMP_TRAP_SINK_VERSION => 'snmpv1',
  SNMP_TRAP_SINK_COMMUNITY => 'public',
  SNMP_TRAP_SINK_PORT => 162,
  SNMP_TRAP_ENTERPRISE_OID => '1.3.6.1.4.1.20006.1.5.1',
};
\@searches = (
    {
      tag => "action",
      logfile => "./var/adm/messages",
      criticalpatterns => [ 
             '.*connection unexpectedly closed.*', 
             '.*rsync error.*', 
             'rsync:.*', 
             '!.*DEBUT.*', 
             '!.*FIN.*', 
             '!.*building file list.*', 
             '!.*files to consider.*', 
             '!.*sent .* bytes\\s+received .* bytes\\s+.* bytes/sec.*', 
             '!.*total size is .* \\s+speedup is .*' ,
         ], 
         warningpatterns => [ 
             '.*total size is 0 .*', 
             '.*sent 0 bytes.*', 
             '.*received 0 bytes.*' 
         ],
             options => 'script',
             script => "send_snmptrap"
    });
\$postscript = 'send_nsca';
\$postscriptparams = '-H \$CL_NSCA_HOST_ADDRESS\$ -p \$CL_NSCA_PORT\$ -to \$CL_NSCA_TO_SEC\$ -c \$CL_NSCA_CONFIG_FILE\$';
\$postscriptstdin = '\$CL_HOSTNAME\$\\t\$CL_SERVICEDESC\$\\t\$CL_SERVICESTATEID\$\\t\$CL_SERVICEOUTPUT\$\\n';

EOCFG
open CCC, ">./etc/check_action.cfg";
print CCC $configfile;
close CCC;

my $cl = Nagios::CheckLogfiles::Test->new({ cfgfile => "./etc/check_action.cfg" });
if ($^O =~ /MSWin/) {
  $cl->create_file("./bin/send_snmptrap.bat", 0755, "
echo off
rem echo i am the script
rem echo status %CHECK_LOGFILES_SERVICESTATEID%
echo output \"%CHECK_LOGFILES_SERVICEOUTPUT%\" >> ./var/tmp/scriptcounter
exit /B 3;
");
} else {
  $cl->create_file("./bin/send_snmptrap", 0755, "
echo i am the script \$CHECK_LOGFILES_SERVICESTATEID \$CHECK_LOGFILES_SERVICEOUTPUT
echo status \$CHECK_LOGFILES_SERVICESTATEID
echo output \"\$CHECK_LOGFILES_SERVICEOUTPUT\" >> ./var/tmp/scriptcounter
exit 3;
");
};
if ($^O =~ /MSWin/) {
  $cl->create_file("./bin/send_nsca.bat", 0755, "
echo off
echo i am the postscript 
echo status %CHECK_LOGFILES_SERVICESTATEID%
echo output \"%CHECK_LOGFILES_SERVICEOUTPUT%\"
echo output \"%CHECK_LOGFILES_SERVICEOUTPUT%\" >> ./var/tmp/scriptcounter
exit /B 0;
");
} else {
  $cl->create_file("./bin/send_nsca", 0755, "
echo i am the postscript with \"\$*\"
echo status \$CHECK_LOGFILES_SERVICESTATEID
echo output \$CHECK_LOGFILES_SERVICEOUTPUT
echo output \$CHECK_LOGFILES_SERVICEOUTPUT >> ./var/tmp/scriptcounter
exit 0;
");
}
my $action = $cl->get_search_by_tag("action");
my $postscript = $cl->get_search_by_tag("postscript");
if ($^O =~  /MSWin/) {
  $action->{script} = "send_snmptrap.bat";
  $postscript->{script} = "send_nsca.bat";
  delete $postscript->{scriptstdin};
}
$cl->reset();
$cl->delete_file("./var/tmp/scriptcounter");
$action->delete_logfile();
$action->delete_seekfile();
diag("deleted logfile and seekfile");
$action->trace("deleted logfile and seekfile");
$action->logger(undef, undef, 1, "Failed password for invalid user1...");
diag("wrote 1 message");
sleep 1;
$cl->run();
diag($cl->has_result());
diag($cl->{exitmessage});
ok($cl->expect_result(0, 0, 6, 0, 2));
my $x = $cl->read_file("./var/tmp/scriptcounter");
my @lines = split(/\n/, $x);
diag(sprintf "script was called %d times", scalar(@lines));
ok(@lines == 7);


$cl->reset();
$cl->delete_file("./var/tmp/scriptcounter");
$action->delete_logfile();
$action->delete_seekfile();
$action->loggercrap(undef, undef, 100);
sleep 1;
$cl->run();
$action->dump_protocol();
diag($cl->has_result());
diag($cl->{exitmessage});
ok($cl->expect_result(0, 0, 6, 0, 2));
$x = $cl->read_file("./var/tmp/scriptcounter");
@lines = split(/\n/, $x);
diag(sprintf "script was called %d times", scalar(@lines));
ok(@lines == 7);

$cl->reset();
$cl->delete_file("./var/tmp/scriptcounter");
$action->loggercrap(undef, undef, 100);
$action->logger(undef, undef, 1, "Ici le DEBUT ");
$action->loggercrap(undef, undef, 100);
$action->logger(undef, undef, 4, "there was an rsync error");
$action->logger(undef, undef, 1, "Et le FIN de l'histoire");
sleep 1;
$cl->run();
$action->dump_protocol();
diag($cl->has_result());
diag($cl->{exitmessage});
ok($cl->expect_result(0, 0, 8, 0, 2));
$x = $cl->read_file("./var/tmp/scriptcounter");
@lines = split(/\n/, $x);
diag(sprintf "script was called %d times", scalar(@lines));
ok(@lines == 9);

# backup working perfectly
$cl->reset();
$cl->delete_file("./var/tmp/scriptcounter");
$action->loggercrap(undef, undef, 100);
$action->logger(undef, undef, 1, "DEBUT du backup");
$action->loggercrap(undef, undef, 100);
$action->logger(undef, undef, 1, "building file list ... done");
$action->loggercrap(undef, undef, 100);
$action->logger(undef, undef, 1, "files to consider:...");
$action->logger(undef, undef, 1, "file file file");
$action->logger(undef, undef, 1, "sent 871 bytes  received 26 bytes  163.09 bytes/sec");
$action->loggercrap(undef, undef, 100);
$action->logger(undef, undef, 1, "total size is 30053  speedup is 33.50");
$action->loggercrap(undef, undef, 100);
$action->logger(undef, undef, 1, "FIN du backup");
sleep 1;
$cl->run();
$action->dump_protocol();
diag($cl->has_result());
diag($cl->{exitmessage});
ok($cl->expect_result(0, 0, 0, 0, 0));
$x = $cl->read_file("./var/tmp/scriptcounter");
@lines = split(/\n/, $x);
diag(sprintf "script was called %d times", scalar(@lines));
ok(@lines == 1);

# backup with warning
$cl->reset();
$cl->delete_file("./var/tmp/scriptcounter");
$action->loggercrap(undef, undef, 100);
$action->logger(undef, undef, 1, "DEBUT du backup");
$action->loggercrap(undef, undef, 100);
$action->logger(undef, undef, 1, "building file list ... done");
$action->logger(undef, undef, 1, "files to consider:...");
$action->loggercrap(undef, undef, 100);
$action->logger(undef, undef, 1, "file file file");
$action->logger(undef, undef, 1, "sent 0 bytes  received 0 bytes  0 bytes/sec");
$action->loggercrap(undef, undef, 100);
$action->logger(undef, undef, 1, "total size is 0  speedup is 0");
$action->loggercrap(undef, undef, 100);
$action->logger(undef, undef, 1, "FIN du backup");
$action->loggercrap(undef, undef, 100);
sleep 1;
$cl->run();
$action->dump_protocol();
diag($cl->has_result());
diag($cl->{exitmessage});
ok($cl->expect_result(0, 3, 0, 0, 1));
$x = $cl->read_file("./var/tmp/scriptcounter");
@lines = split(/\n/, $x);
diag(sprintf "script was called %d times", scalar(@lines));
ok(@lines == 4);

#backup did not run at all
$cl->reset();
$cl->delete_file("./var/tmp/scriptcounter");
sleep 1;
$cl->run();
$action->dump_protocol();
diag($cl->has_result());
diag($cl->{exitmessage});
ok($cl->expect_result(0, 0, 6, 0, 2));
$x = $cl->read_file("./var/tmp/scriptcounter");
@lines = split(/\n/, $x);
diag(sprintf "script was called %d times", scalar(@lines));
ok(@lines == 7);

#mixed critical and missing errors. script is a piece of perl now.
$cl->reset();
$cl->delete_file("./var/tmp/scriptcounter");
$action->{script} = sub {
  printf "i am perl\n";
  open OOO, ">>./var/tmp/scriptcounter";
  printf OOO "%s\n", $ENV{CHECK_LOGFILES_SERVICEOUTPUT};
  close OOO;
  return 3;
};
$action->loggercrap(undef, undef, 100);
$action->logger(undef, undef, 1, "Ici le DEBUT ");
$action->loggercrap(undef, undef, 100);
$action->logger(undef, undef, 4, "there was an rsync error");
$action->logger(undef, undef, 1, "Et le FIN de l'histoire");
sleep 1;
$cl->run();
$action->dump_protocol();
diag($cl->has_result());
diag($cl->{exitmessage});
ok($cl->expect_result(0, 0, 8, 0, 2));
$x = $cl->read_file("./var/tmp/scriptcounter");
@lines = split(/\n/, $x);
diag(sprintf "script was called %d times", scalar(@lines));
ok(@lines == 9);