-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNSCATest.pm
112 lines (94 loc) · 2.29 KB
/
NSCATest.pm
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
#
# DESCRIPTION:
# Helper object class for NSCA testing
#
# COPYRIGHT:
# Copyright (C) 2007 Altinity Limited
# Copyright is freely given to Ethan Galstad if included in the NSCA distribution
#
# LICENCE:
# GNU GPLv2
package NSCATest;
use strict;
use Class::Struct;
use IO::File;
struct NSCATest => {
config => '$',
pid => '$',
timeout => '$',
child_spawned => '$',
};
$| = 1; # Autoflush on
sub start {
my ($self, $mode) = @_;
$mode ||= "--server_type=Single";
my $perl = $^X;
my $command = "$perl -I lib bin/nrd --conf_file=t/nrd_".$self->config.".cfg $mode";
printf "Starting nrd with $mode: $command\n";
system($command);
sleep 2; # Let daemon start
open F, "/tmp/nrd.pid" or die "No pid file found";
chop(my $pid = <F>);
close F;
$self->pid($pid);
open(F, "> var/nagios.cmd") or die "Cannot create var/nagios.cmd";
close F;
return $pid;
}
sub DESTROY {
my $self = shift;
if (! $self->child_spawned && $self->pid) {
print "Killing nrd due to not being stopped correctly",$/;
$self->stop;
}
}
sub stop {
my $self = shift;
print "Stopping nrd: ".$self->pid.$/;
kill "TERM", $self->pid;
$self->pid(undef);
unlink "/tmp/nrd.cmd", "/tmp/nrd.dump";
sleep 2; # Let daemon die
}
sub send {
my ($self, $data) = @_;
my $pipe_error=0;
local $SIG{PIPE} = sub { $pipe_error++ };
my @output = map { join("\t", @$_)."\n" } @$data;
open SEND, "| ".$self->send_cmd;
print SEND @output;
close SEND;
#warn("?=$?, pipe_error=$pipe_error\n");
if ($? == 0 && $pipe_error == 0) {
return 1;
} else {
return undef;
}
}
sub send_cmd {
my ($self) = @_;
my $timeout = $self->timeout || 2;
my $perl = $^X;
return "$perl -I lib bin/send_nrd -c t/send_" . $self->config . ".cfg";
}
sub read_cmd {
my ($self, $file) = @_;
$file ||= "/tmp/nrd.dump";
my $fh = IO::File->new($file) or die "Can't open $file";
$self->process_data($fh);
}
sub process_data {
my ($self, $fh) = @_;
my $data = [];
while(<$fh>) {
chop;
my @bits = /\[\d+\] PROCESS_(?:HOST|SERVICE)_CHECK_RESULT;([^;]+);(?:([^;]+);)?([0123]);(.*)$/o;
# Remove the service name if doesn't exist
splice @bits, 1, 1 unless defined $bits[1];
push @$data, [ @bits ];
}
return $data;
}
# Was thinking of calling $self->stop in DESTROY, but with the forking
# going on, this wouldn't work
1;