Source for file cron_restart_port.php

Documentation is available at cron_restart_port.php

  1. #!/usr/bin/php 
  2. <?php
  3. /**
  4.  * /opt/nac/bin/cron_program_port.php
  5.  *
  6.  * Long description for file:
  7.  * Go through the port table and check for the program flag, and
  8.  * program the ports via SNMP
  9.  *
  10.  * PHP version 5
  11.  *
  12.  * LICENSE: This program is free software; you can redistribute it and/or
  13.  * modify it under the terms of the GNU General Public License as published
  14.  * by the Free Software Foundation.
  15.  *
  16.  * @package                     FreeNAC
  17.  * @author                      Hector Ortiz (FreeNAC Core Team)
  18.  * @copyright               2007 FreeNAC
  19.  * @license                     http://www.gnu.org/copyleft/gpl.html   GNU Public License Version 2
  20.  * @version                     SVN: $Id$
  21.  * @link                                http://www.freenac.net
  22.  *
  23.  */
  24.  
  25. require_once 'funcs.inc.php';
  26. require_once 'snmp_defs.inc.php';
  27.  
  28. $logger->setDebugLevel(0);
  29. $logger->setLogToStdOut(false);
  30.  
  31. /**
  32. * Restart daemons on master server
  33. */
  34. function restart_daemons()
  35. {
  36.    global $conf$logger;
  37.    if ($conf->restart_daemons)
  38.    {
  39.       # Reset flag
  40.       $query="UPDATE config SET value='false',who='', LastChange=NOW() WHERE name='restart_daemons';";
  41.       $logger->debug($query,3);
  42.       $result mysql_query($query);
  43.       if $result)
  44.       {
  45.          $logger->logit(mysql_error()LOG_ERR);
  46.       }
  47.       # Restart them
  48.       popen('/etc/init.d/vmps restart 2>&1','r');
  49.       popen('/etc/init.d/postconnect restart 2>&1','r');
  50.       # Write to the db log
  51.       log2db('info','Daemons have been restarted');
  52.       # Reload settings?
  53.       $conf=Settings::getInstance();   
  54.    }
  55. }
  56.  
  57. /**
  58. * Delete the pid file
  59. * This function is also called the following signals have been caught:
  60. * SIGTERM, SIGHUP, SIGINT
  61. */
  62. function delete_pid_file()
  63. {
  64.    global $file_name;
  65.    unlink($file_name);
  66. }
  67.  
  68. ## ------------------------------------------- Main stuff ---------------------------------------
  69.  
  70. $file_name='cron_restart_port.pid';
  71. #Check for PID file
  72. if (is_readable($file_name))
  73. {
  74.    $pid file_get_contents($file_name);
  75.    $processes syscall("ps uax | grep $pid | awk '{print $2}'");
  76.    if $processes )
  77.    {
  78.       $logger->logit("An error ocurred when calling syscall."LOG_ERR);
  79.       exit(1);
  80.    }
  81.    $processes explode("\n",$processes);
  82.    if (array_search($pid$processes=== false )
  83.    {
  84.       delete_pid_file();
  85.    }
  86.    else
  87.    {
  88.       $logger->logit("A previous instance of cron_restart_port.php is still running."LOG_ERR);
  89.       exit(1);
  90.    }
  91. }
  92. #Create PID file
  93. $file=fopen($file_name,'w');
  94. if $file )
  95. {
  96.    $logger->logit("Can't write PID file"LOG_ERR);
  97.    exit(1);
  98. $pid posix_getpid();
  99. fprintf($file,'%d',$pid);
  100. fclose($file);
  101.  
  102. #Handle signals
  103. pcntl_signal(SIGTERM"delete_pid_file");
  104. pcntl_signal(SIGHUP"delete_pid_file");
  105. pcntl_signal(SIGINT"delete_pid_file");
  106.  
  107. #Should we restart the daemons?
  108. if ($conf->restart_daemons)
  109.  
  110. $query=<<<EOF
  111. SELECT p.id, 
  112.    p.name AS port, 
  113.    s.ip AS switch, 
  114.    s.name AS switch_name,
  115.    p.auth_profile, 
  116.    p.shutdown,
  117.    p.restart_now,
  118.    v.default_name AS vlan 
  119.    FROM port p 
  120.    LEFT JOIN vlan v
  121.    ON p.staticvlan=v.id
  122.    INNER JOIN switch s 
  123.    ON p.switch=s.id 
  124.    WHERE p.restart_now='1'
  125.    AND (p.last_auth_profile='1' OR p.last_auth_profile='2') 
  126.    ORDER BY s.ip ASC;
  127. EOF;
  128. $logger->debug($query3);
  129. $res=mysql_query($query);
  130. if (!$res)
  131. {
  132.    $logger->logit(mysql_error());
  133.    #Delete PID file
  134.    exit(1);
  135. }
  136.  
  137. while ($row mysql_fetch_array($resMYSQL_ASSOC))
  138. {
  139.    foreach ($row as $k => $v)
  140.       $switch_ports[$row['switch']][$k][]=$v;
  141. }
  142.  
  143. #No ports have restart_now=1;
  144. if isset($switch_ports) )
  145. {
  146.    #Delete PID file
  147.    exit();
  148. }
  149.  
  150. foreach ($switch_ports as $switch => $properties)
  151. {
  152.    #Retrieve the list of ports from the switch. If we don't get it, go to the next switch
  153.    if $ports_on_switch =  ports_on_switch($switch))
  154.       continue;
  155.    #Retrieve vlan membership type for the switch ports
  156.    if $vm_type vm_type($switch))
  157.       continue;
  158.  
  159.    for ($i=0$i<count($properties['port'])$i++)
  160.    {
  161.       $port $properties['port'][$i];
  162.       $dont_restart=0;
  163.       #If we have an empty port name, go to the next one
  164.       if ($port)
  165.          continue;
  166.       #Get the index for this port
  167.       $port_index get_snmp_index($port$ports_on_switch);
  168.       if ($port_index)
  169.          continue;
  170.  
  171.       ## Check if it is not a trunk port
  172.       if ($properties['auth_profile'][$i== '3')
  173.       {
  174.          $logger->logit("Port $port on switch $switch({$properties['switch_name'][$i]}) is a trunk port and cannot be programmed");
  175.          continue;
  176.       }
  177.       ## Program port as static or dynamic
  178.       else if (($properties['auth_profile'][$i== '1'&& ($properties['vlan'][$i]))
  179.       {
  180.          set_port_as_static($switch$port$properties['vlan'][$i]$port_index);
  181.          $dont_restart++;
  182.       }
  183.       else if ($properties['auth_profile'][$i== '2')
  184.       {
  185.          #Check if the port is static. If it is, program it, otherwise, don't do anything (CatOS issues)?
  186.          # Look for a SNMP OID key [OID.x.y.1]=value that ends in .1 and get the value after it
  187.          if (array_find_key($port_index$vm_type'.'1== '1')
  188.          {
  189.             set_port_as_dynamic($switch$port$port_index);
  190.             $dont_restart++;
  191.          }
  192.          else {
  193.            $logger->debug('Port is already dynamic, do not reprogram'2);
  194.          }
  195.       }
  196.  
  197.       # Shut down the port
  198.       if ($properties['shutdown'][$i])
  199.       {
  200.          #Try to turn it off
  201.          if (turn_off_port($switch$port$port_index))
  202.          {
  203.             $string="Port $port on switch $switch({$properties['switch_name'][$i]}) was successfully shutdown";
  204.             $dont_restart++;
  205.          }
  206.          else
  207.          {
  208.             $string="Port $port on switch $switch({$properties['switch_name'][$i]}) could not be shutdown";
  209.          }
  210.          $logger->logit($string);
  211.          log2db('info'$string);
  212.       }
  213.  
  214.       #Restart port
  215.       if $dont_restart)
  216.       {
  217.          if (turn_off_port($switch$port$port_index))
  218.          {
  219.             turn_off_port($switch$port$port_index);     #CatOS issues?
  220.             if (turn_on_port($switch$port$port_index))
  221.             {
  222.                $string="Port $port successfully restarted on switch $switch({$properties['switch_name'][$i]})";
  223.             }
  224.             else
  225.             {
  226.                $string="Port $port on switch $switch({$properties['switch_name'][$i]}) couldn't be restarted";
  227.             }
  228.          }
  229.          else
  230.          {
  231.             $string="Port $port on switch $switch({$properties['switch_name'][$i]}) couldn't be restarted";
  232.          }
  233.          $logger->logit($string);
  234.          log2db('info',$string);
  235.       }
  236.    
  237.    }
  238. }
  239.  
  240. if mysql_num_rows($res) )
  241. {
  242.    # Ok, we are done, reset the restart_now flag, for ALL ports
  243.    $query "UPDATE port SET restart_now=0;";
  244.    $logger->debug($query3);
  245.    $result mysql_query($query);
  246.    if $result)
  247.    {
  248.       $logger->logit(mysql_error(),LOG_ERR);
  249.    }
  250. }
  251. #Delete PID file
  252. delete_pid_file($file_name);
  253. ?>

Documentation generated on Mon, 01 Dec 2008 01:10:25 +0100 by phpDocumentor 1.4.0