% $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
%args>
<%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->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);
%shared>
<%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);
}
}
%init>
<%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) {