aboutsummaryrefslogtreecommitdiffstats
path: root/collectors/ping.pl
blob: d5acdedd97009de89658dacf495feda65ca4b349 (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
#! /usr/bin/perl
use DBI;
use POSIX;
use Time::HiRes qw(sleep time);
use Net::Oping;
use strict;
use warnings;
use Data::Dumper;

use lib '/opt/gondul/include';
use nms;

$|++;
my $dbh = nms::db_connect();
$dbh->{AutoCommit} = 0;
$dbh->{RaiseError} = 1;

my $q = $dbh->prepare("SELECT switch,host(mgmt_v4_addr) as ip,host(mgmt_v6_addr) as secondary_ip FROM switches WHERE mgmt_v4_addr is not null or mgmt_v6_addr is not null ORDER BY random();");
my $lq = $dbh->prepare("SELECT linknet,addr1,addr2 FROM linknets WHERE addr1 is not null and addr2 is not null;");

my $last = time();
my $target = 0.2;
while (1) {
	my $now = time();
	my $elapsed = ($now - $last);
	if ($elapsed < $target) {
		sleep($target - ($now - $last));
	}
	$last = time();
	# ping loopbacks
	my $ping = Net::Oping->new;
	$ping->timeout(0.2);

	$q->execute;
	my %ip_to_switch = ();
	my %secondary_ip_to_switch = ();
	my $affected = 0;
	while (my $ref = $q->fetchrow_hashref) {
		$affected++;
		my $switch = $ref->{'switch'};

		my $ip = $ref->{'ip'};
		if (defined($ip) ) {
			$ping->host_add($ip);
			$ip_to_switch{$ip} = $switch;
		}

		my $secondary_ip = $ref->{'secondary_ip'};
		if (defined($secondary_ip)) {
			$ping->host_add($secondary_ip);
			$secondary_ip_to_switch{$secondary_ip} = $switch;
		}
	}
	if ($affected == 0) {
		print "Nothing to do... sleeping 1 second...\n";
		sleep(1);
		next;
	}

	my $result = $ping->ping();
	my %dropped = %{$ping->get_dropped()};
	die $ping->get_error if (!defined($result));

	$dbh->do('COPY ping (switch, latency_ms) FROM STDIN');  # date is implicitly now.
	my $drops = 0;
	while (my ($ip, $latency) = each %$result) {
		my $switch = $ip_to_switch{$ip};
		if (!defined($switch)) {
			next;
		}

		if (!defined($latency)) {
			$drops += $dropped{$ip};
		}
		$latency //= "\\N";
		$dbh->pg_putcopydata("$switch\t$latency\n");
	}
	if ($drops > 0) {
		print "$drops ";
	}
	$dbh->pg_putcopyend();

	$dbh->do('COPY ping_secondary_ip (switch, latency_ms) FROM STDIN');  # date is implicitly now.
	while (my ($ip, $latency) = each %$result) {
		my $switch = $secondary_ip_to_switch{$ip};
		next if (!defined($switch));

		$latency //= "\\N";
		$dbh->pg_putcopydata("$switch\t$latency\n");
	}
	$dbh->pg_putcopyend();

	$dbh->commit;
	# ping linknets
	$ping = Net::Oping->new;
	$ping->timeout(0.2);

	$lq->execute;
	my @linknets = ();
	while (my $ref = $lq->fetchrow_hashref) {
		push @linknets, $ref;
		$ping->host_add($ref->{'addr1'});
		$ping->host_add($ref->{'addr2'});
	}
	if (@linknets) { 
		$result = $ping->ping();
		die $ping->get_error if (!defined($result));

		$dbh->do('COPY linknet_ping (linknet, latency1_ms, latency2_ms) FROM STDIN');  # date is implicitly now.
			for my $linknet (@linknets) {
				my $id = $linknet->{'linknet'};
				my $latency1 = $result->{$linknet->{'addr1'}} // '\N';
				my $latency2 = $result->{$linknet->{'addr2'}} // '\N';
				$dbh->pg_putcopydata("$id\t$latency1\t$latency2\n");
			}
		$dbh->pg_putcopyend();
	}
	$dbh->commit;
}