line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Net::Prober::Probe::Base; |
2
|
|
|
|
|
|
|
{ |
3
|
|
|
|
|
|
|
$Net::Prober::Probe::Base::VERSION = '0.15'; |
4
|
|
|
|
|
|
|
} |
5
|
|
|
|
|
|
|
|
6
|
8
|
|
|
8
|
|
30
|
use strict; |
|
8
|
|
|
|
|
9
|
|
|
8
|
|
|
|
|
242
|
|
7
|
8
|
|
|
8
|
|
30
|
use warnings; |
|
8
|
|
|
|
|
10
|
|
|
8
|
|
|
|
|
223
|
|
8
|
|
|
|
|
|
|
|
9
|
8
|
|
|
8
|
|
29
|
use Carp (); |
|
8
|
|
|
|
|
10
|
|
|
8
|
|
|
|
|
95
|
|
10
|
8
|
|
|
8
|
|
25
|
use Time::HiRes (); |
|
8
|
|
|
|
|
13
|
|
|
8
|
|
|
|
|
105
|
|
11
|
8
|
|
|
8
|
|
5446
|
use Sys::Syslog (); |
|
8
|
|
|
|
|
54054
|
|
|
8
|
|
|
|
|
1192
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
our $USE_SYSLOG = 0; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub new { |
16
|
9
|
|
|
9
|
0
|
48
|
my ($class, $opt) = @_; |
17
|
|
|
|
|
|
|
|
18
|
9
|
|
50
|
|
|
64
|
$opt ||= {}; |
19
|
9
|
|
33
|
|
|
42
|
$class = ref $class || $class; |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
my $self = { |
22
|
9
|
|
|
|
|
14
|
%{ $opt }, |
|
9
|
|
|
|
|
28
|
|
23
|
|
|
|
|
|
|
}; |
24
|
|
|
|
|
|
|
|
25
|
9
|
|
|
|
|
36
|
bless $self, $class; |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub name { |
29
|
7
|
|
|
7
|
0
|
11
|
my ($self) = @_; |
30
|
7
|
|
33
|
|
|
29
|
my $class = ref $self || $self; |
31
|
7
|
50
|
|
|
|
58
|
if ($class =~ m{^ .* :: (?.*?) $}x) { |
32
|
8
|
|
|
8
|
|
4133
|
return $+{probename}; |
|
8
|
|
|
|
|
2639
|
|
|
8
|
|
|
|
|
4900
|
|
|
7
|
|
|
|
|
81
|
|
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
0
|
|
|
|
|
0
|
Carp::croak("Couldn't determine the probe name from class $class"); |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub probe_failed { |
39
|
2
|
|
|
2
|
0
|
6
|
my ($self, %info) = @_; |
40
|
|
|
|
|
|
|
|
41
|
2
|
|
|
|
|
6
|
my $name = $self->name(); |
42
|
|
|
|
|
|
|
|
43
|
2
|
50
|
|
|
|
6
|
if (! exists $info{time}) { |
44
|
|
|
|
|
|
|
my $elapsed = exists $self->{_time} |
45
|
0
|
0
|
|
|
|
0
|
? $self->time_elapsed() |
46
|
|
|
|
|
|
|
: 0.0; |
47
|
0
|
|
|
|
|
0
|
$info{time} = $elapsed; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
2
|
50
|
|
|
|
10
|
if ($USE_SYSLOG) { |
51
|
|
|
|
|
|
|
my $msg = sprintf "Probe %s failed, reason %s, elapsed: %3.2f s", |
52
|
|
|
|
|
|
|
$name, |
53
|
|
|
|
|
|
|
$info{reason} || 'unknown', |
54
|
0
|
|
0
|
|
|
0
|
$info{time}; |
55
|
|
|
|
|
|
|
|
56
|
0
|
|
|
|
|
0
|
Sys::Syslog::syslog('warning', $msg); |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
2
|
|
|
|
|
23
|
return { ok => 0, %info }; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub probe_ok { |
63
|
5
|
|
|
5
|
0
|
23
|
my ($self, %info) = @_; |
64
|
|
|
|
|
|
|
|
65
|
5
|
|
|
|
|
36
|
my $name = $self->name(); |
66
|
|
|
|
|
|
|
|
67
|
5
|
50
|
|
|
|
25
|
if (! exists $info{time}) { |
68
|
|
|
|
|
|
|
my $elapsed = exists $self->{_time} |
69
|
5
|
50
|
|
|
|
62
|
? $self->time_elapsed() |
70
|
|
|
|
|
|
|
: 0.0; |
71
|
5
|
|
|
|
|
16
|
$info{time} = $elapsed; |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
5
|
50
|
|
|
|
23
|
if ($USE_SYSLOG) { |
75
|
|
|
|
|
|
|
my $msg = sprintf "Probe %s ok, elapsed: %3.2f s", |
76
|
0
|
|
|
|
|
0
|
$name, $info{time}; |
77
|
0
|
|
|
|
|
0
|
Sys::Syslog::syslog('info', $msg); |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
5
|
|
|
|
|
423
|
return { ok => 1, %info }; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub time_now { |
84
|
7
|
|
|
7
|
0
|
9
|
my ($self) = @_; |
85
|
|
|
|
|
|
|
|
86
|
7
|
|
|
|
|
75
|
return $self->{_time} = [ Time::HiRes::gettimeofday() ]; |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
sub time_elapsed { |
90
|
10
|
|
|
10
|
0
|
19
|
my ($self) = @_; |
91
|
|
|
|
|
|
|
|
92
|
10
|
50
|
33
|
|
|
63
|
if (! exists $self->{_time} || ! defined $self->{_time}) { |
93
|
0
|
|
|
|
|
0
|
Carp::croak('time_elapsed() called without a starting time_now()'); |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
10
|
|
|
|
|
19
|
my $last_mark = $self->{_time}; |
97
|
10
|
|
|
|
|
43
|
my $elapsed = Time::HiRes::tv_interval($last_mark); |
98
|
|
|
|
|
|
|
|
99
|
10
|
|
|
|
|
178
|
return $elapsed; |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
sub process_defaults { |
103
|
16
|
|
|
16
|
0
|
20
|
my ($self, $args) = @_; |
104
|
|
|
|
|
|
|
|
105
|
16
|
|
50
|
|
|
43
|
$args ||= {}; |
106
|
16
|
|
|
|
|
18
|
my %args_with_defaults = %{ $args }; |
|
16
|
|
|
|
|
87
|
|
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
# Process and inject defaults if an arg is not supplied |
109
|
16
|
|
|
|
|
51
|
my $defaults = $self->defaults; |
110
|
|
|
|
|
|
|
|
111
|
16
|
50
|
33
|
|
|
97
|
if ($defaults && ref $defaults eq 'HASH') { |
112
|
16
|
|
|
|
|
17
|
for (keys %{ $defaults }) { |
|
16
|
|
|
|
|
65
|
|
113
|
|
|
|
|
|
|
$args_with_defaults{$_} = $defaults->{$_} |
114
|
106
|
100
|
|
|
|
201
|
if ! exists $args_with_defaults{$_}; |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
16
|
|
|
|
|
52
|
return \%args_with_defaults; |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
sub parse_args { |
122
|
16
|
|
|
16
|
0
|
40
|
my ($self, $args, @wanted) = @_; |
123
|
|
|
|
|
|
|
|
124
|
16
|
|
|
|
|
53
|
my $args_with_def = $self->process_defaults($args); |
125
|
|
|
|
|
|
|
|
126
|
16
|
100
|
66
|
|
|
77
|
if (exists $args_with_def->{port} && defined $args_with_def->{port}) { |
127
|
|
|
|
|
|
|
$args_with_def->{port} = Net::Prober::port_name_to_num( |
128
|
|
|
|
|
|
|
$args_with_def->{port} |
129
|
13
|
|
|
|
|
42
|
); |
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
|
132
|
16
|
50
|
|
|
|
35
|
if (! @wanted) { |
133
|
0
|
|
|
|
|
0
|
return $args_with_def; |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
|
136
|
16
|
|
|
|
|
15
|
my @arg_values; |
137
|
16
|
|
|
|
|
75
|
push @arg_values, $args_with_def->{$_} for @wanted; |
138
|
|
|
|
|
|
|
|
139
|
16
|
|
|
|
|
76
|
return @arg_values; |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
sub defaults { |
143
|
0
|
|
|
0
|
0
|
|
Carp::croak("defaults() is an abstract method. You must implement it in your class."); |
144
|
|
|
|
|
|
|
} |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
sub probe { |
147
|
0
|
|
|
0
|
0
|
|
Carp::croak("probe() is an abstract method. You must implement it in your class."); |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
1; |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
__END__ |