Git init
[external/ifupdown.git] / contrib / ifstate-check
1 #!/usr/bin/perl
2 #
3 # Generate a report of the status of interfaces configured
4 # by 'ifupdown'
5 # (c) 2004 Javier Fernandez-Sanguino <jfs@debian.org>
6 #
7 #    This program is free software; you can redistribute it and/or modify
8 #    it under the terms of the GNU General Public License as published by
9 #    the Free Software Foundation; either version 2 of the License, or
10 #    (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU General Public License for more details.
16 #
17 #    You should have received a copy of the GNU General Public License
18 #    along with this program; if not, write to the Free Software
19 #    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20 #
21
22 $statefile="/etc/network/run/ifstate";
23 $configfile="/etc/network/interfaces";
24
25 open (IFACE,"<$configfile") || die ("Could not open $configfile: $!\n");
26 while (<IFACE>) {
27         chomp;
28         if ( /^iface\s+(\w+)\s+/ ) {
29                 $configured{$1}=$_; 
30         }
31 }
32
33 close IFACE;
34
35 open (IPLINK,"ip link show|") || die ("Could not execute ip: $!\n");
36 while (<IPLINK>) {
37         chomp;
38 # FORMAT
39 # #: AAAA: <XXXXX,UP> mtu 16436 qdisc noqueue
40         if ( /^\d+: (\w+):.*?\<.*?,UP.*?\>/ ) {
41                 $iplink{$1}=$_;
42         }
43 }
44 close IPLINK;
45
46
47 open (STATE,"<$statefile") || die ("Could not open $statefile: $!\n");
48 $line = 0;
49 while (<STATE>) {
50         chomp;
51         $line++;
52 # Format is IFACE=IFACE
53         if ( /^(\w+)=(\w+)$/ ) {
54                 $iface = $1;
55                 $ifaces = $2;
56                 if ( $iface ne $ifaces ) {
57                         print STDERR "Error in $statefile (line $line), interface names do not match ('$iface' and '$ifaces')\n";
58                 } else {
59                         check_status($iface);
60                 }
61         } else {
62                 print STDERR "Error in $statefile (line $line), unknown content\n";
63         }
64
65 }
66 close STATE;
67
68 exit 0;
69
70 sub check_status {
71         my ($int) = @_;
72         print "$int: ";
73         my $status = "UP";
74 # Check if it's really up, this is done basicly because ifupdown
75 # might not have configured it properly even if it thinks he has
76 # (sample: ifconfig croaks when wrong parameters are used and 
77 # ifupdown does not detect that the system call went awry)
78         $status = "ERROR_NOT_REALLY_UP" if ! defined ($iplink{$int}) ;
79         if ( defined ( $configured{$int} ) ) {
80                 $status .= ",CONFIGURED";
81         } else {
82                 $status .= ",MANUALLY_CONFIGURED";
83         }
84         print "$status\n";
85         return 0;
86 }