"Fossies" - the Fresh Open Source Software Archive

Member "fogproject-1.5.9/packages/web/lib/fog/storagenode.class.php" (13 Sep 2020, 9281 Bytes) of package /linux/misc/fogproject-1.5.9.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "storagenode.class.php": 1.5.8_vs_1.5.9.

    1 <?php
    2 /**
    3  * Storage node handler class.
    4  *
    5  * PHP version 5
    6  *
    7  * @category StorageNode
    8  * @package  FOGProject
    9  * @author   Tom Elliott <tommygunsster@gmail.com>
   10  * @license  http://opensource.org/licenses/gpl-3.0 GPLv3
   11  * @link     https://fogproject.org
   12  */
   13 /**
   14  * Storage node handler class.
   15  *
   16  * @category StorageNode
   17  * @package  FOGProject
   18  * @author   Tom Elliott <tommygunsster@gmail.com>
   19  * @license  http://opensource.org/licenses/gpl-3.0 GPLv3
   20  * @link     https://fogproject.org
   21  */
   22 class StorageNode extends FOGController
   23 {
   24     /**
   25      * The storage node table.
   26      *
   27      * @var string
   28      */
   29     protected $databaseTable = 'nfsGroupMembers';
   30     /**
   31      * The storage node fields and common names.
   32      *
   33      * @var array
   34      */
   35     protected $databaseFields = array(
   36         'id' => 'ngmID',
   37         'name' => 'ngmMemberName',
   38         'description' => 'ngmMemberDescription',
   39         'isMaster' => 'ngmIsMasterNode',
   40         'storagegroupID' => 'ngmGroupID',
   41         'isEnabled' => 'ngmIsEnabled',
   42         'isGraphEnabled' => 'ngmGraphEnabled',
   43         'path' => 'ngmRootPath',
   44         'ftppath' => 'ngmFTPPath',
   45         'bitrate' => 'ngmMaxBitrate',
   46         'helloInterval' => 'ngmHelloInterval',
   47         'snapinpath' => 'ngmSnapinPath',
   48         'sslpath' => 'ngmSSLPath',
   49         'ip' => 'ngmHostname',
   50         'maxClients' => 'ngmMaxClients',
   51         'user' => 'ngmUser',
   52         'pass' => 'ngmPass',
   53         'key' => 'ngmKey',
   54         'interface' => 'ngmInterface',
   55         'bandwidth' => 'ngmBandwidthLimit',
   56         'webroot' => 'ngmWebroot'
   57     );
   58     /**
   59      * The required fields.
   60      *
   61      * @var array
   62      */
   63     protected $databaseFieldsRequired = array(
   64         'ip',
   65         'path',
   66         'ftppath',
   67         'user',
   68         'pass'
   69     );
   70     /**
   71      * Additional fields.
   72      *
   73      * @var array
   74      */
   75     protected $additionalFields = array(
   76         'images',
   77         'snapinfiles',
   78         'logfiles',
   79         'usedtasks',
   80         'storagegroup',
   81         'online'
   82     );
   83     /**
   84      * Database -> Class field relationships
   85      *
   86      * @var array
   87      */
   88     protected $databaseFieldClassRelationships = array(
   89         'StorageGroup' => array(
   90             'id',
   91             'storagegroupID',
   92             'storagegroup'
   93         )
   94     );
   95     /**
   96      * Gets an item from the key sent, if no key all object data is returned.
   97      *
   98      * @param mixed $key the key to get
   99      *
  100      * @return object
  101      */
  102     public function get($key = '')
  103     {
  104         $pathvars = array(
  105             'path',
  106             'ftppath',
  107             'snapinpath',
  108             'sslpath',
  109             'webroot'
  110         );
  111         if (in_array($key, $pathvars)) {
  112             if (trim(parent::get($key)) === '/') {
  113                 return parent::get($key);
  114             }
  115             return rtrim(parent::get($key), '/');
  116         }
  117         if ($key === 'pass') {
  118             return htmlspecialchars_decode(parent::get($key), ENT_QUOTES | ENT_HTML401);
  119         }
  120         $loaders = array(
  121             'snapinfiles' => 'getSnapinfiles',
  122             'images' => 'getImages',
  123             'logfiles' => 'getLogfiles'
  124         );
  125         if (in_array($key, array_keys($loaders))
  126             && !$this->isLoaded($key)
  127         ) {
  128             $func = $loaders[$key];
  129             $this->{$func}();
  130         }
  131 
  132         return parent::get($key);
  133     }
  134     /**
  135      * Get the storage group of this node.
  136      *
  137      * @return object
  138      */
  139     public function getStorageGroup()
  140     {
  141         return $this->get('storagegroup');
  142     }
  143     /**
  144      * Loads the online status for us.
  145      *
  146      * @return void
  147      */
  148     public function loadOnline()
  149     {
  150         $test = self::$FOGURLRequests->isAvailable($this->get('ip'), 1);
  151         $this->set('online', array_shift($test));
  152     }
  153     /**
  154      * Get the node failure.
  155      *
  156      * @param int $Host the host id
  157      *
  158      * @return object
  159      */
  160     public function getNodeFailure($Host)
  161     {
  162         foreach ((array)self::getClass('NodeFailureManager')
  163             ->find(
  164                 array(
  165                     'storagenodeID' => $this->get('id'),
  166                     'hostID' => $Host,
  167                 )
  168             ) as &$Failed
  169         ) {
  170             $curr = self::niceDate();
  171             $prev = $Failed->get('failureTime');
  172             $prev = self::niceDate($prev);
  173             if ($curr < $prev) {
  174                 return $Failed;
  175             }
  176             unset($Failed);
  177         }
  178 
  179         return $Failed;
  180     }
  181     /**
  182      * Loads the logfiles available on this node.
  183      *
  184      * @return void
  185      */
  186     public function getLogfiles()
  187     {
  188         $paths = array_values(
  189             array_filter(
  190                 $this->_getData('logfiles')
  191             )
  192         );
  193         natcasesort($paths);
  194         $this->set('logfiles', (array)$paths);
  195     }
  196     /**
  197      * Get's the storage node snapins, logfiles, and images
  198      * in a single multi call rather than three individual calls.
  199      *
  200      * @return array
  201      */
  202     private function _getData($item)
  203     {
  204         if (!$this->get('online')) {
  205             return;
  206         }
  207         $logpaths = array(
  208             '/var/log/nginx',
  209             '/var/log/httpd',
  210             '/var/log/apache2',
  211             '/var/log/fog',
  212             '/var/log/php7.0-fpm',
  213             '/var/log/php-fpm',
  214             '/var/log/php5-fpm',
  215             '/var/log/php5.6-fpm',
  216         );
  217         $items = array(
  218             'images' => urlencode($this->get('path')),
  219             'snapinfiles' => urlencode($this->get('snapinpath')),
  220             'logfiles' => urlencode(implode(':', $logpaths))
  221         );
  222         if (!array_key_exists($item, $items)) {
  223             return;
  224         }
  225         $url = sprintf(
  226             '%s://%s/fog/status/getfiles.php?path=%s',
  227             self::$httpproto,
  228             $this->get('ip'),
  229             $items[$item]
  230         );
  231         $response = self::$FOGURLRequests->process($url);
  232         return preg_grep(
  233             '#dev|postdownloadscripts|ssl#',
  234             json_decode($response[0], true),
  235             PREG_GREP_INVERT
  236         );
  237     }
  238     /**
  239      * Loads the snapins available on this node.
  240      *
  241      * @return void
  242      */
  243     public function getSnapinfiles()
  244     {
  245         $response = $this->_getData('snapinfiles');
  246         $values = array_map('basename', (array)$response);
  247         $this->set('snapinfiles', $values);
  248     }
  249     /**
  250      * Loads the images available on this node.
  251      *
  252      * @return void
  253      */
  254     public function getImages()
  255     {
  256         $response = $this->_getData('images');
  257         $values = array_map('basename', (array)$response);
  258         $values = self::getSubObjectIDs(
  259             'Image',
  260             array('path' => $values)
  261         );
  262         $this->set('images', $values);
  263     }
  264     /**
  265      * Gets this node's load of clients.
  266      *
  267      * @return float
  268      */
  269     public function getClientLoad()
  270     {
  271         if ($this->getUsedSlotCount() + $this->getQueuedSlotCount() < 0) {
  272             return 0;
  273         }
  274         if ($this->get('maxClients') < 1) {
  275             return 0;
  276         }
  277         return (float)(
  278             $this->getUsedSlotCount() + $this->getQueuedSlotCount()
  279         ) / $this->get('maxClients');
  280     }
  281     /**
  282      * Load used tasks.
  283      *
  284      * @return void
  285      */
  286     protected function loadUsedtasks()
  287     {
  288         $used = explode(',', self::getSetting('FOG_USED_TASKS'));
  289         if (count($used) < 1) {
  290             $used = array(
  291                 1,
  292                 15,
  293                 17,
  294             );
  295         }
  296         $this->set('usedtasks', (array)$used);
  297     }
  298     /**
  299      * Gets this node's used count.
  300      *
  301      * @return int
  302      */
  303     public function getUsedSlotCount()
  304     {
  305         $countTasks = 0;
  306         $usedtasks = $this->get('usedtasks');
  307         $findTasks = array(
  308             'stateID' => self::getProgressState(),
  309             'storagenodeID' => $this->get('id'),
  310             'typeID' => $usedtasks,
  311         );
  312         $countTasks = self::getClass('TaskManager')->count($findTasks);
  313         $index = array_search(8, $usedtasks);
  314         if ($index === false) {
  315             return $countTasks;
  316         }
  317         $MulticastCount = self::getSubObjectIDs(
  318             'MulticastSession',
  319             array(
  320                         'stateID' => self::getProgressState()
  321                 ),
  322             'msID'
  323         );
  324 
  325         $countTasks += count($MulticastCount);
  326         return $countTasks;
  327     }
  328     /**
  329      * Gets the queued hosts on this node.
  330      *
  331      * @return int
  332      */
  333     public function getQueuedSlotCount()
  334     {
  335         $countTasks = 0;
  336         $usedtasks = $this->get('usedtasks');
  337         $findTasks = array(
  338             'stateID' => self::getQueuedStates(),
  339             'storagenodeID' => $this->get('id'),
  340             'typeID' => $usedtasks,
  341         );
  342         $countTasks = self::getClass('TaskManager')->count($findTasks);
  343         $index = array_search(8, $usedtasks);
  344         if ($index === false) {
  345             return $countTasks;
  346         }
  347         $MulticastCount = self::getSubObjectIDs(
  348             'MulticastSession',
  349             array('stateID' => self::getQueuedStates()),
  350             'msID'
  351         );
  352         $countTasks += count($MulticastCount);
  353         return $countTasks;
  354     }
  355 }