summaryrefslogtreecommitdiff
path: root/scripts/keepalived/vyatta-show-vrrp.pl
blob: 86dbef3d0521fb0d4810aa15e0f33dbc79a93cd2 (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
#!/usr/bin/perl
#
# Module: vyatta-show-vrrp.pl
# 
# **** License ****
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
# 
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# This code was originally developed by Vyatta, Inc.
# Portions created by Vyatta are Copyright (C) 2005, 2006, 2007 Vyatta, Inc.
# All Rights Reserved.
# 
# Author: Stig Thormodsrud
# Date: October 2007
# Description: display vrrp info
# 
# **** End License ****
# 
use lib "/opt/vyatta/share/perl5/";
use VyattaKeepalived;

use strict;
use warnings;


sub elapse_time {
    my ($start, $stop) = @_;

    my $seconds   = $stop - $start;
    my $string    = '';
    my $secs_min  = 60;
    my $secs_hour = $secs_min  * 60;
    my $secs_day  = $secs_hour * 24;
    my $secs_week = $secs_day  * 7;
    
    my $weeks = int($seconds / $secs_week);
    if ($weeks > 0 ) {
	$seconds = int($seconds % $secs_week);
	$string .= $weeks . "w";
    }
    my $days = int($seconds / $secs_day);
    if ($days > 0) {
	$seconds = int($seconds % $secs_day);
	$string .= $days . "d";
    }
    my $hours = int($seconds / $secs_hour);
    if ($hours > 0) {
	$seconds = int($seconds % $secs_hour);
	$string .= $hours . "h";
    }
    my $mins = int($seconds / $secs_min);
    if ($mins > 0) {
	$seconds = int($seconds % $secs_min);
	$string .= $mins . "m";
    }
    $string .= $seconds . "s";

    return $string;
}

sub get_state_link {
    my $intf = shift;

    my $IFF_UP = 0x1;
    my ($state, $link);
    my $flags = `cat /sys/class/net/$intf/flags 2> /dev/null`;
    my $carrier = `cat /sys/class/net/$intf/carrier 2> /dev/null`;
    chomp $flags; chomp $carrier;
    my $hex_flags = hex($flags);
    if ($hex_flags & $IFF_UP) {
        $state = "up";
    } else {
        $state = "admin down";
    }
    if ($carrier eq "1") {
        $link = "up";
    } else {
        $link = "down";
    }
    return ($state, $link);
}

sub get_master_info {
    my ($intf, $group) = @_;

    my $file = VyattaKeepalived::get_master_file($intf, $group);
    if ( -f $file) {
	my $master = `grep ip.src $file`;
	chomp $master;
	if (defined $master and $master =~ m/show=\"(\d+\.\d+\.\d+\.\d+)\"/) {
	    $master = $1;
	} else {
	    $master = "unknown";
	}
	my $priority = `grep vrrp.prio $file`;
	chomp $priority;
	if (defined $priority and $priority =~ m/show=\"(\d+)\"/) {
	    $priority = $1;
	} else {
	    $priority = "unknown";
	}
	return ($master, $priority);
    } else {
	return ("unknown", "unknown");
    }
}

sub vrrp_showsummary {
    my ($file) = @_;

    my ($start_time, $intf, $group, $state, $ltime) =
        VyattaKeepalived::vrrp_state_parse($file);
    my ($interface_state, $link) = get_state_link($intf);
    if ($state eq "master" || $state eq "backup" || $state eq "fault") {
        my ($primary_addr, $priority, $preempt, $advert_int, $auth_type,
            @vips) = VyattaKeepalived::vrrp_get_config($intf, $group);
        print "\n$intf\t\t$group\tint\t$primary_addr\t$link\t\t$state";
        foreach my $vip (@vips){
                print "\n\t\t\tvip\t$vip";

        }
    } else {
        print "Physical interface $intf, State: unknown\n";
    }
}


sub vrrp_show {
    my ($file) = @_;

    my $now_time = time;
    my ($start_time, $intf, $group, $state, $ltime) = 
	VyattaKeepalived::vrrp_state_parse($file);
    my ($interface_state, $link) = get_state_link($intf);
    if ($state eq "master" || $state eq "backup" || $state eq "fault") {
	my ($primary_addr, $priority, $preempt, $advert_int, $auth_type, 
	    @vips) = VyattaKeepalived::vrrp_get_config($intf, $group);
	print "Physical interface: $intf, Address $primary_addr\n";
	print "  Interface state: $link, Group $group, State: $state\n";
	print "  Priority: $priority, Advertisement interval: $advert_int, ";
	print "Authentication type: $auth_type\n";
	my $vip_count = scalar(@vips);
	my $string = "  Preempt: $preempt, VIP count: $vip_count, VIP: ";
	my $strlen = length($string);
	print $string;
	foreach my $vip (@vips) {
	    if ($vip_count != scalar(@vips)) {
		print " " x $strlen;
	    }
	    print "$vip\n";
	    $vip_count--;
	}
	if ($state eq "master") {
	    print "  Master router: $primary_addr\n";
	} elsif ($state eq "backup") {
	    my ($master_rtr, $master_prio) = get_master_info($intf, $group);
	    print "  Master router: $master_rtr, ";
            print "Master Priority: $master_prio\n";
	}
    } else {
	print "Physical interface $intf, State: unknown\n";
    }
    my $elapsed = elapse_time($start_time, $now_time);
    print "  Last transition: $elapsed\n\n";
}

#
# main
#    
my $intf  = "eth";
my $group = "all";
my $showsummary = 0;

if ($#ARGV >= 0) {

    if ($ARGV[0] eq "summary") {
        $showsummary = 1;
    } else {
        $intf = $ARGV[0];
    }

}

if ($#ARGV == 1) {
    $group = $ARGV[1];
}

if (!VyattaKeepalived::is_running()) {
    print "VRRP isn't running\n";
    exit 1;
}

if ($showsummary == 1) {

        print "\t\tVRRP\tAddr\t\t\tInterface\tVRRP\n";
        print "Interface\tGroup\tType\tAddress\t\tState\t\tState\n";
        print "---------\t-----\t----\t-------\t\t-----\t\t-----";


}

my @state_files = VyattaKeepalived::get_state_files($intf, $group);
foreach my $state_file (@state_files) {
    if ($showsummary == 1) {
        vrrp_showsummary($state_file);
    } else {
         vrrp_show($state_file);
    }
}

exit 0;

#end of file