コード例 #1
0
ファイル: netstats.php プロジェクト: gunesahmet/php-esx
 *
 * You should have received a copy of the GNU General Public License
 * along with PHP-ESX.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
if (count($_SERVER['argv']) != 6) {
    echo 'Usage: php ' . $_SERVER['argv'][0] . ' <host> <username> <password> <vmname> <nic #>
       vmname       the name of the virtual machine
	   nic #        which network interface to query (starting at 1)
';
    exit;
}
require_once dirname(__FILE__) . '/../../class_esx.php';
try {
    $esx = new esx();
    $esx->connect($_SERVER['argv'][1], $_SERVER['argv'][2], $_SERVER['argv'][3]);
    $vm = $esx->find_entity_view('VirtualMachine', null, array('config.name' => $_SERVER['argv'][4]));
    $stats = $esx->get_stats($vm);
    $metrics = array();
    foreach ($stats as $stat) {
        if ($stat->groupInfo->key == 'net' && $stat->instance == $_SERVER['argv'][5] + 3999) {
            $metrics[$stat->nameInfo->key] = $stat->value;
        }
    }
    if (isset($metrics['bytesTx']) && isset($metrics['bytesRx']) && isset($metrics['droppedTx']) && isset($metrics['droppedRx'])) {
        echo 'bytestx:' . $metrics['bytesTx'] . ' bytesrx:' . $metrics['bytesRx'] . ' droppedtx:' . $metrics['droppedTx'] . ' droppedrx:' . $metrics['droppedRx'] . "\n";
    } else {
        die('Unknown NIC #' . $_SERVER['argv'][5] . "\n");
    }
    $esx->disconnect();
} catch (Exception $e) {
コード例 #2
0
 public function __construct($esxman, $database)
 {
     $this->esxman = $esxman;
     $this->database = $database;
     $this->html = array();
     $this->commands = array();
     $this->title = _('Hosts');
     if (request('dialog') == 'addhost') {
         return $this->dialog_addhost();
     } else {
         if (request('dialog') == 'deletehost') {
             return $this->dialog_deletehost(request('host'));
         } else {
             if (request('dialog') == 'editsettings') {
                 $host = $this->database->gethost(request('host'));
                 return $this->dialog_editsettings(request('host'), $host);
             } else {
                 if (request('form_name') == 'addhost') {
                     try {
                         $esx = new esx();
                         $esx->connect(request('hostname'), request('username'), request('password'));
                         if ($esx->ServiceContent->about->apiType != 'HostAgent') {
                             return $this->dialog_addhost(_('Only standalone hosts are allowed.'));
                         }
                         $this->database->addhost(request('hostname'), request('username'), request('password'), $esx->ServiceContent->about->apiType, request('enableconsole') == 'on' ? 1 : 0, request('consoleaddress'));
                     } catch (Exception $e) {
                         return $this->dialog_addhost($e->getMessage());
                     }
                     $this->esxman->populate_inventory(request('hostname'));
                     $this->commands[] = '$(\'#dialog\').remove();';
                     $this->commands[] = js_command('window.location = \'' . url() . '#/page=hosts&host=' . request('hostname') . '\';');
                     return;
                 } else {
                     if (request('form_name') == 'deletehost') {
                         $this->database->deletehost(request('host'));
                         if (isset($_SESSION['hosts'][request('host')])) {
                             unset($_SESSION['hosts'][request('host')]);
                         }
                         $this->commands[] = '$(\'#dialog\').remove();';
                         $this->commands[] = js_command('window.location = \'' . url() . '#/page=hosts\';');
                         return;
                     } else {
                         if (request('form_name') == 'editsettings') {
                             $host = array('hostname' => request('hostname'), 'username' => request('username'), 'password' => request('password'), 'console' => request('enableconsole') == 'on', 'consoleaddress' => request('consoleaddress'));
                             $oldhost = $this->database->gethost(request('oldhostname'));
                             $changes = array();
                             if (request('hostname') != $oldhost['hostname']) {
                                 $changes['hostname'] = request('hostname');
                             }
                             if (request('username') != $oldhost['username']) {
                                 $changes['username'] = request('username');
                             }
                             if (request('password') != '' && request('password') != $oldhost['password']) {
                                 $changes['password'] = request('password');
                             }
                             if (request('enableconsole') == 'on' ^ $oldhost['console']) {
                                 $changes['console'] = request('enableconsole') == 'on' ? 1 : 0;
                             }
                             if (request('consoleaddress', '') != $oldhost['consoleaddress']) {
                                 $changes['consoleaddress'] = request('consoleaddress');
                             }
                             try {
                                 $this->database->edithost($oldhost['hostname'], $changes);
                                 if (isset($changes['hostname']) && isset($_SESSION['hosts'][$oldhost['hostname']])) {
                                     $_SESSION['hosts'][$changes['hostname']] = $_SESSION['hosts'][$oldhost['hostname']];
                                     unset($_SESSION['hosts'][$oldhost['hostname']]);
                                 }
                             } catch (Exception $e) {
                                 return $this->dialog_editsettings(request('oldhostname'), $host, $e->getMessage());
                             }
                             $this->commands[] = '$(\'#dialog\').remove();';
                             $this->commands[] = js_command('window.location = \'' . url() . '#/page=hosts&host=' . request('hostname') . '\';');
                             return;
                         }
                     }
                 }
             }
         }
     }
     /* Main hosts page */
     $hosts = $this->database->gethosts();
     if (!count($hosts)) {
         $this->nohosts();
     } else {
         if (($host = request('host')) && isset($hosts[$host])) {
             $this->singlehost($hosts[$host]);
         } else {
             $this->hostlist($hosts);
         }
     }
 }
コード例 #3
0
 public function get_esx($host)
 {
     $host = $this->database->gethost($host);
     $esx = new esx();
     if (!isset($_SESSION['hosts'])) {
         $_SESSION['hosts'] = array();
     }
     if (!isset($_SESSION['hosts'][$host['hostname']])) {
         $_SESSION['hosts'][$host['hostname']] = array();
     }
     if (isset($_SESSION['hosts'][$host['hostname']]['esxcookie'])) {
         $esx->reconnect($host['hostname'], $_SESSION['hosts'][$host['hostname']]['esxcookie']);
     } else {
         $esx->connect($host['hostname'], $host['username'], $host['password']);
         $_SESSION['hosts'][$host['hostname']]['esxcookie'] = $esx->cookie;
     }
     return $esx;
 }