summaryrefslogtreecommitdiffstats
path: root/util/lg/lgform.cgi.in
blob: 97a0c71b8b0cc8ecb4ea14080cf10fb8e3c85d3f (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
239
240
241
242
243
244
245
#!@PERLV_PATH@
## Copyright (C) 1997-2001 by Henry Kilmer.
## All rights reserved.
##
## This software may be freely copied, modified and redistributed without
## fee for non-commerical purposes provided that this copyright notice is
## preserved intact on all copies and modified copies.
##
## There is no warranty or other guarantee of fitness of this software.
## It is provided solely "as is". The author(s) disclaim(s) all
## responsibility and liability with respect to this software's usage
## or its effect upon hardware, computer systems, other software, or
## anything else.
#
# lgform.cgi - Looking glass front-end
# produces html form for calling lg.cgi

use CGI qw/:standard/;

my(@rtrlist, %rtrlabels);
my($BASEDIR) = "@prefix@";

# note: the following functions are duplicated between lgform.cgi and lg.cgi
# to avoid the need for module inclusion headaches from within a httpd context.
# it is just easier to be self-contained.
# SO, ANY CHANGES HERE SHOULD BE REFLECTED IN THE OTHER .cgi.

# logging
sub dolog
{
    my($level, $msg) = @_;

    if (defined($LG_LOG) && $LG_LOG !~ /\//) {
	openlog($me, "pid", $LG_LOG);
	syslog($level, "%s", $msg);
	closelog;
    } else {
	local(*LOG);
	my($file);
	if (defined($LG_LOG)) {
	    $file = $LG_LOG;
	} else {
	    $file = "$cache_dir/lg.log";
	}
	#  log date, hostname, query, addr
	if (open(LOG, ">>$file") == 0) {
	    # stderr, if all else fails
	    printf(STDERR "[" . strftime("%a %b %e %H:%M:%S %Y", gmtime) .
			"] could not open log file $file: $!\n");
	    printf(STDERR $msg);
	} else {
	    printf(LOG $msg);
	    close(LOG); 
	}
    }
    return;
}
# read LG configuration file
sub readconf
{
    my($conffile, $cmds);
    local(*CONF);
    if (defined($env{LG_CONF})) {
	$conffile = $env{LG_CONF};
    } elsif (-e "lg.conf") {
	$conffile = "lg.conf";
    } else {
	$conffile = "$BASEDIR/util/lg/lg.conf";
    }

    if (! -f $conffile) {
	return;
    }

    if (open(CONF, "< $conffile")) {
	while (<CONF>) {
	    next if (/^\s*(#|$)/);
	    $cmds .= $_;
	}
	close(CONF);
	eval $cmds;
    } else {
	printf(STDERR "ERROR: couldn\'t open the configuration file: $conffile: $!\n");
	exit(1);
    }

    return;
}
# read router.db file
sub readrouters
{
    my($rtrdb);
    local(*RTR);

    if (defined($LG_ROUTERDB)) {
	$rtrdb = $LG_ROUTERDB;
    } else {
	$rtrdb = "$BASEDIR/util/lg/router.db";
    }

    if (! -f $rtrdb) {
	my(@dirs, $dir);
	# if the router.db file does not exist, try to compile the list from
	# the rancid group router.db files.
	local(*DIR);
	if (! opendir(DIR, $BASEDIR)) {
	    dolog(LOG_ERR, "ERROR: couldn\'t read $BASEDIR: $!\n");
	} else {
	    while ($dir = readdir(DIR)) {
		next if ($dir =~ /^(\.|\.\.|CVS|bin|logs|util)$/);
		push(@dirs, $dir) if (-d "$BASEDIR/$dir");
	    }
	    closedir(DIR);

	    foreach $dir (@dirs) {
		if (! opendir(DIR, "$BASEDIR/$dir")) {
		    dolog(LOG_ERR, "ERROR: couldn\'t read $BASEDIR/$dir: $!\n");
		    next;
		}
		closedir(DIR);
		next if (! -f "$BASEDIR/$dir/router.db");
		if (open(RTR, "< $BASEDIR/$dir/router.db")) {
		    while (<RTR>) {
			next if (/^\s*(#|$)/);
			# fqdn:mfg:state
			@record = split('\:', $_);
			next if ($record[2] !~ /up/i || $record[1] !~ /(cisco|foundry|juniper)/);
			push(@rtrlist, join(':', ($record[0], $record[1])));
			$rtrlabels{join(':', ($record[0], $record[1]))} = $record[0];
		    }
		    close(RTR);
		} else {
		    dolog(LOG_ERR, "ERROR: couldn\'t open the router.db file: $BASEDIR/$dir/router.db: $!\n");
		}
	    }
	}
    } else {
	if (open(RTR, "< $rtrdb")) {
	    while (<RTR>) {
		next if (/^\s*(#|$)/);
		# fqdn:mfg:state
		@record = split('\:', $_);
		next if ($record[2] !~ /up/i || $record[1] !~ /(cisco|foundry|juniper)/);
		push(@rtrlist, join(':', ($record[0], $record[1])));
		$rtrlabels{join(':', ($record[0], $record[1]))} = $record[0];
	    }
	    close(RTR);
	} else {
	    dolog(LOG_ERR, "ERROR: couldn\'t open the router.db file: $rtrdb: $!\n");
	    exit(1);
	}
    }

    return;
}

# Main()
# read the configuration file if it exists.
readconf();

$query = new CGI;

print $query->header;
print $query->start_html("LookingGlass form");

print "<BODY>";

# add the company image, LG_IMAGE
print $LG_IMAGE;

print <<HEAD ;
<br>
<B><FONT SIZE=+2>Looking Glass</FONT></B>
<br>
<hr>
HEAD

# start table, etc here
print <<DOTABLE ;
<form action=lg.cgi method=GET>
<center>
<table border cellspacing=0 width=575 align=center>

DOTABLE

# available query types here
print <<QTYPES ;
<tr valign=top>
        <TD><B>Query:</B>
<dd><input type=radio name="query" value="framerelay">show frame-relay pvc [DLCI]</DD>
<dd><input type=radio name="query" value="interface">show interface &lt;interface&gt;</DD>
<dd><input type=radio name="query" value="prefix">show ip bgp &lt;prefix&gt; [netmask]</DD>
<dd><input type=radio name="query" value="neighbor">show ip bgp neighbor &lt;IP_addr&gt;</DD>
<dd><input type=radio name="query" value="regex">show ip bgp regex &lt;reg_exp&gt;</DD>
<dd><input type=radio name="query" value="summary">show ip bgp summary</DD>
<dd><input type=radio name="query" value="damp">show ip bgp dampened-paths</DD>
<dd><input type=radio name="query" value="prefixlist">show ip prefix-list &lt;list_name&gt;</DD>
<dd><input type=radio name="query" value="route">show ip route &lt;prefix&gt; [netmask]</DD>
<dd><input type=radio name="query" value="routemap">show route-map &lt;map_name&gt;</DD>
<dd><input type=radio name="query" value="mbgp">show ip mbgp &lt;prefix&gt; [netmask]</dd>
<dd><input type=radio name="query" value="mbgpsum">show ip mbgp summary</dd>
<dd><input type=radio name="query" value="log">show logging [ | &lt;match_string&gt;]</DD>
<dd><input type=radio name="query" value="ping">ping &lt;IP_addr | FQDN&gt;</dd>
<dd><input type=radio name="query" value="trace">traceroute &lt;IP_addr | FQDN&gt;</DD>

<P><B>Argument(s):</B> <INPUT name="args" size=30></P>
        </TD>
        <td ALIGH=left VALIGN=top>
        <b>Router:</B>

<dd>
QTYPES

# <dd><input type=radio name="query" value="aspath">sh ip as-path-access-list &lt;list_number&gt;</DD>
# <dd><input type="radio" name="query" value="acl">sh access-list &lt;list_number&gt;</dd>
# <dd><input type=radio name="query" value="communitylist">sh ip community-list &lt;list_number&gt;<DD>
# <dd><input type=radio name="query" value="routemap">sh ip route-map &lt;map_name&gt;</DD>

# read routers table and create the scrolling list
readrouters();
print $query->scrolling_list(-name => 'router',
			-values => \@rtrlist,
			-size => 20,
			-labels => \%rtrlabels);

# end
print <<TAIL ;
</dd>
</td>
</TABLE>
<p><input type=submit value=Submit> <INPUT type=reset value=Reset></FORM>
</center>
<br>
<a href=lgnotes.html>Looking Glass notes</a>
TAIL

print <<TAIL ;
<p>
$LG_INFO
</body>
TAIL

print $query->end_html;

exit(0);