-
Notifications
You must be signed in to change notification settings - Fork 0
/
bandwidth_feeder
executable file
·118 lines (93 loc) · 3.46 KB
/
bandwidth_feeder
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
#!/usr/bin/perl
# Bandwidth Feeder - Grab bandwidth stats from the firewall and feed them into
# the AMQP system so other things can use them.
# Input: SNMP from firewall for bandwidth statistics
# Output: Exchange 'statistics.bandwidth'
use strict;
use warnings;
use IO::File;
use Config::IniFiles;
use Net::RabbitMQ;
use Net::SNMP qw(:snmp);
use IO::Socket;
my $update_every = 15000;
my $cable_ifdescr;
my $if_descr_oid = "1.3.6.1.2.1.2.2.1.2";
my $if_in_octets_oid = "1.3.6.1.2.1.2.2.1.10";
my $if_out_octets_oid = "1.3.6.1.2.1.2.2.1.16";
my ($cable_in_last, $cable_out_last);
my ($cable_in_bw, $cable_out_bw);
$cable_in_last = $cable_out_last = -1;
sub snmp_get {
my $session = shift;
my $oid = shift;
my $result = $session->get_request(-varbindlist => [$oid]);
return $result->{$oid};
}
sub get_ifindex {
my $session = shift;
my $ifdescr = shift;
my $result = $session->get_entries(-columns => [$if_descr_oid]);
foreach my $oid (keys %{$result}) {
if($result->{$oid} eq $ifdescr) {
my @ifindexes = split(/\./, $oid);
my $ifindex = $ifindexes[$#ifindexes];
return $ifindex;
}
}
}
sub update_bandwidth {
my $session = shift;
print "Updating ifIndexes...\n";
my $cable_int_index = get_ifindex($session, $cable_ifdescr);
print "Updating bandwidth data...\n";
# get cable interface bandwidth
my $cable_in = snmp_get($session, "$if_in_octets_oid.$cable_int_index");
my $cable_out = snmp_get($session, "$if_out_octets_oid.$cable_int_index");
# TODO: handle counter rollovers better than just returning -1 for one interval.
if($cable_in_last == -1 || ($cable_in - $cable_in_last) < 0 || ($cable_out - $cable_out_last) < 0) {
# we're still fetching the first sample, display dashes for now
$cable_in_bw = $cable_out_bw = "-1";
} else {
# figure out how much has been used
$cable_in_bw = sprintf("%1.3f", ($cable_in - $cable_in_last) / ($update_every / 1000) / (1024*1024) * 8);
$cable_out_bw = sprintf("%1.3f", ($cable_out - $cable_out_last) / ($update_every / 1000) / (1024*1024) * 8);
}
# store these values for next time
print "Last Cable: $cable_in_last\n";
print "Now Cable: $cable_in\n";
$cable_in_last = $cable_in;
$cable_out_last = $cable_out;
}
sub publish_bandwidth {
my $mq = shift;
my $bw_msg = "{\"cable\":{\"up\":$cable_out_bw,\"down\":$cable_in_bw}}";
print $bw_msg . "\n";
$mq->publish(1, "statistics.bandwidth", "$bw_msg", { exchange => "statistics.bandwidth" });
}
sub sleep_ms {
my $sleep_time = shift;
select(undef, undef, undef, $sleep_time/1000);
}
my $cfg = Config::IniFiles->new(-file => $ENV{"HOME"} . "/.labamqprc");
my $broker_host = $cfg->val('broker', 'BrokerHost');
my $broker_user = $cfg->val('bandwidth_feeder', 'BrokerUsername');
my $broker_pass = $cfg->val('bandwidth_feeder', 'BrokerPassword');
my $firewall_host = $cfg->val('bandwidth_feeder', 'FirewallHost');
my $firewall_commstr = $cfg->val('bandwidth_feeder', 'FirewallCommunity');
$cable_ifdescr = $cfg->val('bandwidth_feeder', 'CableInterface');
my ($session, $error) = Net::SNMP->session(
-hostname => "$firewall_host",
-community => "$firewall_commstr",
-translate => [-octetstring => 0],
-version => 'snmpv2c',
);
my $mq = Net::RabbitMQ->new();
$mq->connect($broker_host, { user => $broker_user, password => $broker_pass });
$mq->channel_open(1);
while(1) {
update_bandwidth($session);
publish_bandwidth($mq);
sleep_ms($update_every);
}
$mq->disconnect();