Device Port Control

% $m->comp('SELF:switch') if $cmd =~ /^(up|down)$/; % $m->comp('SELF:log') if $cmd eq 'log'; % $m->comp('SELF:err') if $cmd eq 'err'; % $m->comp('SELF:reasons') unless $cmd eq 'err'; <%args> $debug => 0 $cmd => undef $type => undef $ip => undef $port => undef $log => undef $log_num => 5 $reason => '' <%shared> # scope my ($arg_ip,$arg_port,$log,$arg_cmd,$port,$err,$db_port,$arg_reason,$arg_log_num); my $user = $m->session->{user}; my $userip = $r->connection->remote_ip; my $port_ctl = $m->session->{user_port_ctl}; my $secure = &is_secure; my $timeout = $netdisco::CONFIG{portctl_timeout} || 60; my $odd = 1 ; my %Reasons = ( '' => ['Select a Reason',''], %netdisco::PORT_CONTROL_REASONS); <%init> # Check Authorization - User in portcontrol list in config file, and connected via https if ($cmd =~ /^(up|down|switch)$/ and !$secure) { my $url = url_secure($r->uri); $m->redirect($url); } if ($cmd =~ /^(up|down|switch)$/ and !$port_ctl) { my $url = $m->interp->apply_escapes( $r->uri, 'u' ); $m->redirect("login.html?done=$url"); } $arg_cmd = $cmd; $arg_ip = $ip; $arg_port = $port; $arg_reason = $reason; $arg_log_num = $log_num; if ($cmd eq 'switch' and (!$reason) ){ $cmd = 'err'; $err .= "You must select a reason for bringing the port $type
\n"; } my $dbport; if ($cmd =~ /^(up|down|switch)$/){ $dbport = sql_hash('device_port',['*'],{'ip'=>$ip, 'port'=>$port}); my $remote_ip = $dbport->{remote_ip}; my $remote_type = $dbport->{remote_type}; my $is_uplink = (defined $remote_ip and !(defined $remote_type and $remote_type =~ /ip.phone/i)) ? 1 : 0; unless (defined $dbport and scalar(keys %$dbport)){ $cmd = 'err'; $err .= "That Port doesn't exist!
\n"; } if ($is_uplink) { $cmd = 'err'; $err .= "You cannot shutoff uplink ports.
\n"; } } if ($cmd eq 'switch' and $type =~ /^(up|down)$/) { my $dir = $type; $dir = 'enable' if $type eq 'up'; $dir = 'disable' if $type eq 'down'; # Add to admin queue my $oid = insert_or_update('admin',{}, {'device'=>$ip,'action'=>'portcontrol','status'=>'queued','port'=>$port, 'username' => $user, 'userip', $userip, 'debug' => $debug, 'subaction' => "$type-$reason", 'log' => $log} ); my $job_id = sql_scalar('admin',['job'],{'oid' => $oid}); # Wait for a response from admin daemon my $now = time; my $job; my $timeout_time = $now + $timeout; my $done = 0; my $status; while (!$done){ # Poll every two seconds sleep(2); # Check to see if daemon done $job = sql_hash('admin',['job','extract(epoch from entered) as entered','extract(epoch from finished) as finished', 'extract(epoch from started) as started','status','log'], {'job'=>$job_id} ); if (! defined $job){ $err = "Problem entering job into admin queue.
\n"; last; } $status = $job->{status}; $done++ if $status =~ /^(done|error)$/; $done++ if $status !~ /^(running|queued)$/; $now = time; if (!$done and $now >= $timeout_time and $status =~ /^(running|queued)$/){ my $url = $r->uri . "?ip=$ip&port=$port&cmd=log"; $err = "Timeout! Job is $status in the admin queue. Check the port log entry in a couple minutes to double-check it worked.
\n"; $cmd = 'err'; last; } elsif (!$done and $now >= $timeout_time){ $err = "Error. There was a problem with entering the job in the admin queue. Check to see the admin daemon is running, then check it's log file for problems.\n"; $cmd = 'err'; last; } } if (!$err and $status eq 'done'){ $cmd = 'log'; $err = "Port was successfully $dir.
\n"; } elsif (!$err and $status eq 'error'){ $cmd = 'err'; $err = "[Failed]

$job->{log}
\n"; } else { $cmd = 'err'; $err = "[Failed] I don't know what happened to the job. Status: $job->{status}.

Log: $job->{log}
.\n"; } } <%method log> % my $logs = sql_rows('device_port_log', ['id','log','reason','username','userip','action','extract(epoch from creation) as creation'] , % {'ip'=>$arg_ip, 'port'=>$arg_port} ); % my $count = 0;

Log Entries for <%$arg_ip%>/<%$arg_port%> <& SELF:back &>

% foreach my $entry (sort {$b->{creation} <=> $a->{creation} } @$logs) { % $count++; % last unless ($arg_log_num eq 'All' or $count < $arg_log_num); %}
User Action Date Reason Log
<%$entry->{username}%> @ <%$entry->{userip}%> <%$entry->{action}%> <%scalar localtime($entry->{creation})%> <%$Reasons{$entry->{reason}}->[0]%>
Log Entires to Show :
<%method switch>
Port : <% $arg_ip %>[<% $arg_port %>]
User : <%$user%> at <%$userip%>
<% scalar localtime %>
Reason :
 

See below for descriptions.
Log:
Enter a useful description why the port is being turned <% $arg_cmd eq 'down' ? 'off' : 'on' %>.
Debug:

Note: This can take up to 1 minute. Do not hit refresh on your browser.

<& SELF:back &> <%method reasons>

Reason Descriptions

% foreach my $this_r (sort keys %Reasons){ % next unless $this_r; % my $long = $Reasons{$this_r}->[1]; % $long =~ s!\s+! !g; % }
Reason Description

<%$this_r%> <%$long%>
<%method err> <% $err %> <& SELF:back &> <%method back>
<%method title> - Port Control \ %# $Id: portcontrol.html,v 1.16 2004/09/30 14:36:35 maxbaker Exp $ %# vim:syntax=mason