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:vlan') if $cmd eq 'vlan'; % $m->comp('SELF:reasons') unless $cmd eq 'err' or $cmd eq 'vlan'; <%args> $debug => 0 $cmd => undef $type => undef $ip => undef $port => undef $log => undef $log_num => 5 $reason => '' $vlan => undef $location => undef <%shared> # scope my ($arg_ip,$arg_port,$log,$arg_cmd,$port,$err,$db_port,$arg_reason,$arg_log_num,$arg_vlan); my $user = $m->session->{user}; my $userip = $r->connection->client_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|vlan)$/ and !$secure) { my $url = url_secure($r->uri); $m->redirect($url); } if ($cmd =~ /^(up|down|switch|vlan)$/ and !$port_ctl) { $m->session->{_login_msg} = "Please log in as a user with port control privileges."; 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; $arg_vlan = $vlan; if ($cmd eq 'switch' and !$reason and ($type ne 'vlan' and $type ne 'location')){ $cmd = 'err'; $err .= "You must select a reason for bringing the port $type
\n"; } my $dbport; if ($cmd =~ /^(up|down|switch|vlan)$/ && $type ne 'location'){ $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|vlan|location)$/) { my $dir = $type; $dir = 'enable' if $type eq 'up'; $dir = 'disable' if $type eq 'down'; my ($action,$subaction); if ($type =~ /(up|down)/) { $action = 'portcontrol'; $subaction = "$type-$reason"; } if ($type eq 'vlan') { $action = 'vlan'; $subaction = $vlan; } if ($type eq 'location') { $action = 'location'; $subaction = $location; } # Add to admin queue insert_or_update('admin',{}, {'device'=>$ip,'action'=>$action,'status'=>'queued','port'=>$port, 'username' => $user, 'userip', $userip, 'debug' => $debug, 'subaction' => $subaction, 'log' => $log} ); my $job_id = sql_scalar('admin',["currval('admin_job_seq')"]); # 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 || !defined($job->{job})){ $err = "Problem entering job $job_id 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'; if ($type eq 'location') { $err = "Location was successfully set.
\n"; } else { $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" unless ($err); } } <%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 $queue = sql_rows('admin', ['extract(epoch from entered) as entered','extract(epoch from started) as started','action','subaction','username','userip'], % {'device'=>$arg_ip, 'port'=>$arg_port || '', 'status'=>'queued'} ); % my $count = 0; % if ($err) {

<%$err%>

% }

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

% if (@$queue) {

Queued Actions for <%$arg_ip%>/<%$arg_port%> (Check admin daemon status)

% foreach my $entry (@$queue) { % }
User Action Queued Started
<%$entry->{username}%> @ <%$entry->{userip}%> <%$entry->{action}%> <%$entry->{subaction}%> <%scalar localtime($entry->{entered})%> <%$entry->{started} ? scalar localtime($entry->{started}) : "No"%>
% }
% foreach my $entry (sort {$b->{creation} <=> $a->{creation} } @$logs) { % if ( $entry->{action} eq 'vlan' || $entry->{action} eq 'location' ) { % } else { % } % $count++; % last unless ($arg_log_num eq 'All' or $count < $arg_log_num); %}
User Action Date Reason / VLAN Log
<%$entry->{username}%> @ <%$entry->{userip}%> <%$entry->{action}%> <%scalar localtime($entry->{creation})%><%$entry->{reason}%><%$Reasons{$entry->{reason}}->[0]%>
Log Entires to Show :
%# %# switch() %# <%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 &> %# %# vlan() %# <%method vlan>
%# TODO - need to list VLANs
Port : <% $arg_ip %>[<% $arg_port %>]
User : <%$user%> at <%$userip%>
<% scalar localtime %>
Log:
Enter a useful description why the VLAN is being changed.
VLAN: Enter new VLAN
Debug:

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

<& SELF:back &> %# %# reasons() %# <%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 =~ s/\n/
\n/g; <% $err %> <& SELF:back &> <%method back>
%# %# title() %# <%method title> - Port Control \ %# $Id$ %# vim:syntax=mason