summaryrefslogtreecommitdiffstats
path: root/lib/Smokeping/probes/AnotherSSH.pm
blob: e762764121ca8fe7c220ab7f60f8020834c4eb8b (plain)
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
package Smokeping::probes::AnotherSSH;

=head1 301 Moved Permanently

This is a Smokeping probe module. Please use the command 

C<smokeping -man Smokeping::probes::AnotherSSH>

to view the documentation or the command

C<smokeping -makepod Smokeping::probes::AnotherSSH>

to generate the POD document.

=cut

use strict;
use base qw(Smokeping::probes::basefork);
use Carp;
use Time::HiRes qw(sleep ualarm gettimeofday tv_interval);
use IO::Select;
use Socket;
use Fcntl;

sub pod_hash {
	return {
		name => <<DOC,
Smokeping::probes::AnotherSSH - Another SSH probe
DOC
		description => <<DOC,
Latency measurement using SSH. This generates Logfile messages on the other 
Host, so get permission from the owner first! 
DOC
		authors => <<'DOC',
Christoph Heine <Christoph.Heine@HaDiKo.DE>
DOC
	}
}

sub new($$$) {
    my $proto = shift;
    my $class = ref($proto) || $proto;
    my $self  = $class->SUPER::new(@_);
    return $self;
}

sub ProbeDesc($) {
    my $self = shift;
    return "SSH connections";
}

sub pingone ($) {
    my $self   = shift;
    my $target = shift;

    my $host       = $target->{addr};
    
    # Time 
    my $mininterval = $target->{vars}{mininterval};
    
    # Our greeting string.
    my $greeting  = $target->{vars}{greeting};
    
    # Interval to measure
    my $interval = $target->{vars}{interval};

   # Connect to this port.
    my $port = $target->{vars}{port};

    #Timeout for the select() calls.
    my $timeout = $target->{vars}{timeout};
    
    my @times; # Result times
        
    my $t0;
    for ( my $run = 0 ; $run < $self->pings($target) ; $run++ ) {
    	if (defined $t0) {
		my $elapsed = tv_interval($t0, [gettimeofday()]);
		my $timeleft = $mininterval - $elapsed;
		sleep $timeleft if $timeleft > 0;
	}
    	my ($t1,$t2,$t3); # Timestamps.
	
	#Temporary variables to play with.
	my $ready;
	my $buf;
	my $nbytes;
	
    	my $proto = getprotobyname('tcp');
	my $iaddr = gethostbyname($host);
	my $sin = sockaddr_in( $port, $iaddr );
	socket( Socket_Handle, PF_INET, SOCK_STREAM, $proto );
	
	# Make the Socket non-blocking
    	my $flags = fcntl( Socket_Handle, F_GETFL, 0 ) or do {
		$self->do_debug("Can't get flags for socket: $!");
		close(Socket_Handle);
		next;
	 };
	
	fcntl( Socket_Handle, F_SETFL, $flags | O_NONBLOCK ) or do {
		$self->do_debug("Can't make socket nonblocking: $!");
		close(Socket_Handle); next;
	};
	
	my $sel = IO::Select->new( \*Socket_Handle );

	# connect () and measure the Time.
	$t0 = [gettimeofday()];
	connect( Socket_Handle, $sin );
	($ready) = $sel->can_read($timeout);
	$t1 = [gettimeofday()];
	
	if(not defined $ready) {
		$self->do_debug("Timeout!");
		close(Socket_Handle); next;
	 }
	$nbytes = sysread( Socket_Handle, $buf, 1500 );	
	if (not defined $nbytes or $nbytes <= 0) {
		$self->do_debug("Read nothing and Connection closed!");
		close(Socket_Handle); next;
	}
	# $self->do_debug("Got '$buf' from remote Server");
	if (not $buf =~ m/^SSH/) {
		$self->do_debug("Not an SSH Server");
		close(Socket_Handle); next;
	}
	
	($ready) = $sel->can_write($timeout);
	if (not defined($ready)) {
		$self->do_debug("Huh? Can't write.");
		close(Socket_Handle); next;
	}
	$t2 = [gettimeofday()];
	syswrite( Socket_Handle, $greeting . "\n" );
	($ready) = $sel->can_read($timeout);
	$t3 = [gettimeofday()];
	if(not defined $ready) {
		$self->do_debug("Timeout!");
		close(Socket_Handle); next;
	 }
	 close(Socket_Handle);

	 # We made it! Yeah!

	 if( $interval eq "connect") {
	 	push @times, tv_interval( $t0, $t1 );
	 } elsif ( $interval eq "established") {
	 	push @times, tv_interval($t2,$t3);
	} elsif ($interval eq "complete") {
	 	push @times, tv_interval($t0,$t3);
	} else {
		$self->do_debug("You should never see this message.\n The universe will now collapse. Goodbye!\n");
	}

	
    }
    @times =
      map { sprintf "%.10e", $_ } sort { $a <=> $b } grep { $_ ne "-" } @times;

    return @times;
}

sub probevars {
	my $class = shift;
	my $h = $class->SUPER::probevars;
	delete $h->{timeout};
	return $h;
}

sub targetvars {
	my $class = shift;
	my $e = "=";
	return $class->_makevars($class->SUPER::targetvars, {
		greeting => {
			_doc => <<DOC,
Greeting string to send to the SSH Server. This will appear in the Logfile. 
Use this to make clear, who you are and what you are doing to avoid confusion.

Also, don't use something that is a valid version string. This probe assumes
that the connection gets terminated because of protocol mismatch.
DOC
			_default => "SSH-Latency-Measurement-Sorry-for-this-logmessage" ,
		},
		mininterval => {
			_doc => "Minimum interval between the start of two connection attempts in (possibly fractional) seconds.",
			_default => 0.5,
			_re => '(\d*\.)?\d+',
		},
		interval => {
			_doc => <<DOC,
The interval to be measured. One of:

${e}over

${e}item connect

Interval between connect() and the greeting string from the host.

${e}item established

Interval between our greeting message and the end of the connection 
because of Protocol mismatch. This is the default.

${e}item complete

From connect() to the end of the connection.

${e}back

DOC

			_sub => sub {
				my $interval = shift;
    				if(not ( $interval eq "connect" 
				      or $interval eq "established" 
				      or $interval eq "complete")) {
   					return "ERROR: Invalid interval parameter";
				}
				return undef;
			},
			_default => 'established',
		},
		timeout => {
			_doc => 'Timeout for the connection.',
			_re => '\d+',
			_default => 5,
		},
		port => {
			_doc => 'Connect to this port.',
			_re => '\d+',
			_default => 22,
		},
	});
}

1;