line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Net::Prober::Probe::Base; |
2
|
|
|
|
|
|
|
$Net::Prober::Probe::Base::VERSION = '0.16'; |
3
|
8
|
|
|
8
|
|
26
|
use strict; |
|
8
|
|
|
|
|
11
|
|
|
8
|
|
|
|
|
225
|
|
4
|
8
|
|
|
8
|
|
27
|
use warnings; |
|
8
|
|
|
|
|
10
|
|
|
8
|
|
|
|
|
208
|
|
5
|
|
|
|
|
|
|
|
6
|
8
|
|
|
8
|
|
26
|
use Carp (); |
|
8
|
|
|
|
|
9
|
|
|
8
|
|
|
|
|
93
|
|
7
|
8
|
|
|
8
|
|
20
|
use Time::HiRes (); |
|
8
|
|
|
|
|
8
|
|
|
8
|
|
|
|
|
81
|
|
8
|
8
|
|
|
8
|
|
4757
|
use Sys::Syslog (); |
|
8
|
|
|
|
|
46141
|
|
|
8
|
|
|
|
|
940
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $USE_SYSLOG = 0; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub new { |
13
|
9
|
|
|
9
|
0
|
39
|
my ($class, $opt) = @_; |
14
|
|
|
|
|
|
|
|
15
|
9
|
|
50
|
|
|
60
|
$opt ||= {}; |
16
|
9
|
|
33
|
|
|
39
|
$class = ref $class || $class; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
my $self = { |
19
|
9
|
|
|
|
|
13
|
%{ $opt }, |
|
9
|
|
|
|
|
20
|
|
20
|
|
|
|
|
|
|
}; |
21
|
|
|
|
|
|
|
|
22
|
9
|
|
|
|
|
32
|
bless $self, $class; |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub name { |
26
|
7
|
|
|
7
|
0
|
13
|
my ($self) = @_; |
27
|
7
|
|
33
|
|
|
22
|
my $class = ref $self || $self; |
28
|
7
|
50
|
|
|
|
52
|
if ($class =~ m{^ .* :: (?.*?) $}x) { |
29
|
8
|
|
|
8
|
|
3555
|
return $+{probename}; |
|
8
|
|
|
|
|
2316
|
|
|
8
|
|
|
|
|
4158
|
|
|
7
|
|
|
|
|
73
|
|
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
|
12
|
my ($self, %info) = @_; |
37
|
|
|
|
|
|
|
|
38
|
2
|
|
|
|
|
5
|
my $name = $self->name(); |
39
|
|
|
|
|
|
|
|
40
|
2
|
50
|
|
|
|
6
|
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
|
|
|
|
3
|
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
|
|
|
|
|
21
|
return { ok => 0, %info }; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub probe_ok { |
60
|
5
|
|
|
5
|
0
|
18
|
my ($self, %info) = @_; |
61
|
|
|
|
|
|
|
|
62
|
5
|
|
|
|
|
30
|
my $name = $self->name(); |
63
|
|
|
|
|
|
|
|
64
|
5
|
50
|
|
|
|
18
|
if (! exists $info{time}) { |
65
|
|
|
|
|
|
|
my $elapsed = exists $self->{_time} |
66
|
5
|
50
|
|
|
|
38
|
? $self->time_elapsed() |
67
|
|
|
|
|
|
|
: 0.0; |
68
|
5
|
|
|
|
|
10
|
$info{time} = $elapsed; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
5
|
50
|
|
|
|
15
|
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
|
5
|
|
|
|
|
341
|
return { ok => 1, %info }; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
sub time_now { |
81
|
7
|
|
|
7
|
0
|
8
|
my ($self) = @_; |
82
|
|
|
|
|
|
|
|
83
|
7
|
|
|
|
|
73
|
return $self->{_time} = [ Time::HiRes::gettimeofday() ]; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
sub time_elapsed { |
87
|
10
|
|
|
10
|
0
|
17
|
my ($self) = @_; |
88
|
|
|
|
|
|
|
|
89
|
10
|
50
|
33
|
|
|
70
|
if (! exists $self->{_time} || ! defined $self->{_time}) { |
90
|
0
|
|
|
|
|
0
|
Carp::croak('time_elapsed() called without a starting time_now()'); |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
10
|
|
|
|
|
13
|
my $last_mark = $self->{_time}; |
94
|
10
|
|
|
|
|
34
|
my $elapsed = Time::HiRes::tv_interval($last_mark); |
95
|
|
|
|
|
|
|
|
96
|
10
|
|
|
|
|
122
|
return $elapsed; |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
sub process_defaults { |
100
|
16
|
|
|
16
|
0
|
19
|
my ($self, $args) = @_; |
101
|
|
|
|
|
|
|
|
102
|
16
|
|
50
|
|
|
43
|
$args ||= {}; |
103
|
16
|
|
|
|
|
11
|
my %args_with_defaults = %{ $args }; |
|
16
|
|
|
|
|
63
|
|
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
# Process and inject defaults if an arg is not supplied |
106
|
16
|
|
|
|
|
45
|
my $defaults = $self->defaults; |
107
|
|
|
|
|
|
|
|
108
|
16
|
50
|
33
|
|
|
79
|
if ($defaults && ref $defaults eq 'HASH') { |
109
|
16
|
|
|
|
|
12
|
for (keys %{ $defaults }) { |
|
16
|
|
|
|
|
51
|
|
110
|
|
|
|
|
|
|
$args_with_defaults{$_} = $defaults->{$_} |
111
|
106
|
100
|
|
|
|
184
|
if ! exists $args_with_defaults{$_}; |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
16
|
|
|
|
|
41
|
return \%args_with_defaults; |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
sub parse_args { |
119
|
16
|
|
|
16
|
0
|
34
|
my ($self, $args, @wanted) = @_; |
120
|
|
|
|
|
|
|
|
121
|
16
|
|
|
|
|
46
|
my $args_with_def = $self->process_defaults($args); |
122
|
|
|
|
|
|
|
|
123
|
16
|
100
|
66
|
|
|
70
|
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
|
|
|
|
|
37
|
); |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
16
|
50
|
|
|
|
36
|
if (! @wanted) { |
130
|
0
|
|
|
|
|
0
|
return $args_with_def; |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
|
133
|
16
|
|
|
|
|
15
|
my @arg_values; |
134
|
16
|
|
|
|
|
65
|
push @arg_values, $args_with_def->{$_} for @wanted; |
135
|
|
|
|
|
|
|
|
136
|
16
|
|
|
|
|
66
|
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__ |