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