diff --git a/.github/workflows/opendcim.yml b/.github/workflows/opendcim.yml index b2fc1e7ae..c1fef4734 100644 --- a/.github/workflows/opendcim.yml +++ b/.github/workflows/opendcim.yml @@ -12,5 +12,4 @@ jobs: steps: - name: Update translations - run: /home/runner/update - + run: /home/runner/update \ No newline at end of file diff --git a/.github/workflows/security-pipeline.yml b/.github/workflows/security-pipeline.yml new file mode 100644 index 000000000..861e3fb34 --- /dev/null +++ b/.github/workflows/security-pipeline.yml @@ -0,0 +1,123 @@ +name: Security Pipeline + +permissions: + contents: read + +on: + push: + branches: [mynewfeature-patch,master] + pull_request: + branches: [mynewfeature-patch,master] + +concurrency: + + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +jobs: + gitleaks: + runs-on: ubuntu-latest + permissions: + contents: read + pull-requests: write + + steps: + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + fetch-depth: 0 + persist-credentials: false + + - uses: gitleaks/gitleaks-action@e0c47f4f8be36e29cdc102c57e68cb5cbf0e8d1e # v3.0.0 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + sast: + runs-on: ubuntu-24.04 + timeout-minutes: 10 + permissions: + contents: read + + steps: + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + + - name: Semgrep + uses: returntocorp/semgrep-action@713efdd345f3035192eaa63f56867b88e63e4e5d # v1 + env: + SEMGREP_EXCLUDE: api/docs + with: + config: p/security-audit + + sca: + runs-on: ubuntu-24.04 + timeout-minutes: 10 + permissions: + contents: read + + steps: + - name: Checkout code + uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + + - name: Run Trivy vulnerability scanner in fs mode + uses: aquasecurity/trivy-action@ed142fd0673e97e23eac54620cfb913e5ce36c25 # v0.36.0 + with: + scan-type: fs + scan-ref: . + scanners: vuln + severity: CRITICAL,HIGH + exit-code: '1' + + php-check: + runs-on: ubuntu-24.04 + timeout-minutes: 10 + permissions: + contents: read + + steps: + - name: Checkout code + uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: "8.2" + coverage: none + + - name: Validate Composer configuration + run: composer validate + + - name: Install Composer dependencies + run: composer install --no-interaction --no-progress + + - name: Check PHP syntax + run: php -v + + # Disabled for now due to legacy PHP compatibility issues + # detected in existing project files. This check can be + # re-enabled once PHP 8 compatibility has been fully addressed. + # + # - name: Check PHP syntax + # run: | + # find . -type f -name "*.php" -print0 | xargs -0 -n1 php -l + + generate-sbom: + runs-on: ubuntu-24.04 + needs: + - gitleaks + - sast + - sca + - php-check + timeout-minutes: 10 + permissions: + contents: write + + steps: + - name: Checkout code + uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4.3.0 + + - name: Run Trivy in GitHub SBOM mode and submit results to Dependency Graph + uses: aquasecurity/trivy-action@ed142fd0673e97e23eac54620cfb913e5ce36c25 # v0.36.0 + with: + scan-type: fs + format: github + output: dependency-results.sbom.json + scan-ref: . \ No newline at end of file diff --git a/.gitignore b/.gitignore index d7e2e9807..4acffc52a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,13 @@ #users should be creating this db.inc.php +*.local.php +*.key +*.pem +*.crt +*.p12 +.env +.env.* + #we don't want to accidentally send this out populated, again install.log diff --git a/.semgrepignore b/.semgrepignore new file mode 100644 index 000000000..6aa62da10 --- /dev/null +++ b/.semgrepignore @@ -0,0 +1,3 @@ +api/docs/swagger-ui.js +api/docs/swagger-ui-es-bundle-core.js +api/docs/swagger-ui-standalone-preset.js \ No newline at end of file diff --git a/classes/PowerDistribution.class.php b/classes/PowerDistribution.class.php index 2ee3ac682..e0201304d 100644 --- a/classes/PowerDistribution.class.php +++ b/classes/PowerDistribution.class.php @@ -95,6 +95,8 @@ static function RowToObject($row){ return $PDU; } + // Conservées pour compatibilité ascendante avec du code appelant existant. + // Ne plus utiliser en interne pour des requêtes contenant des valeurs variables. function query($sql){ global $dbh; return $dbh->query($sql); @@ -179,42 +181,87 @@ static private function OSS_SNMP_Lookup($dev,$snmplookup,$oid=null){ return $snmpresult; } - static function calculateEstimatedLoad( $devID ) { - global $dbh; - $sql = "select sum(NominalWatts) as TotalWatts from fac_Device where DeviceID in (select ConnectedDeviceID from fac_PowerPorts where DeviceID=" . intval($devID) . ") or ParentDevice in (select ConnectedDeviceID from fac_PowerPorts where DeviceID=" . intval($devID) . ")"; - if ( $row = $dbh->query( $sql, PDO::FETCH_ASSOC )->fetch() ) { - return $row["TotalWatts"]; - } else { - return 0; - } +/** + * @param int $devID + * @return float + */ +static function calculateEstimatedLoad( $devID ) { + global $dbh; + + $stmt = $dbh->prepare( + "SELECT SUM(NominalWatts) AS TotalWatts + FROM fac_Device + WHERE DeviceID IN ( + SELECT ConnectedDeviceID + FROM fac_PowerPorts + WHERE DeviceID = :devID1 + ) + OR ParentDevice IN ( + SELECT ConnectedDeviceID + FROM fac_PowerPorts + WHERE DeviceID = :devID2 + )" + ); + + $stmt->execute( [ + ':devID1' => (int)$devID, + ':devID2' => (int)$devID + ] ); + + if ( $row = $stmt->fetch( PDO::FETCH_ASSOC ) ) { + return $row["TotalWatts"]; + } else { + return 0; } +} function CreatePDU($pduid=null){ global $dbh; $this->MakeSafe(); - $sqladdon=(!is_null($pduid))?", PDUID=".intval($pduid):""; + $sql = "INSERT INTO fac_PowerDistribution SET Label = :Label, CabinetID = :CabinetID, + TemplateID = :TemplateID, IPAddress = :IPAddress, SNMPCommunity = :SNMPCommunity, + PanelID = :PanelID, BreakerSize = :BreakerSize, FirmwareVersion = :FirmwareVersion, + PanelPole = :PanelPole, InputAmperage = :InputAmperage, FailSafe = :FailSafe, + PanelID2 = :PanelID2, PanelPole2 = :PanelPole2"; + + $params = array( + ':Label' => $this->Label, + ':CabinetID' => $this->CabinetID, + ':TemplateID' => $this->TemplateID, + ':IPAddress' => $this->IPAddress, + ':SNMPCommunity' => $this->SNMPCommunity, + ':PanelID' => $this->PanelID, + ':BreakerSize' => $this->BreakerSize, + ':FirmwareVersion' => $this->FirmwareVersion, + ':PanelPole' => $this->PanelPole, + ':InputAmperage' => $this->InputAmperage, + ':FailSafe' => $this->FailSafe, + ':PanelID2' => $this->PanelID2, + ':PanelPole2' => $this->PanelPole2, + ); + + if(!is_null($pduid)){ + $sql .= ", PDUID = :PDUID"; + $params[':PDUID'] = intval($pduid); + } - $sql="INSERT INTO fac_PowerDistribution SET Label=\"$this->Label\", - CabinetID=$this->CabinetID, TemplateID=$this->TemplateID, - IPAddress=\"$this->IPAddress\", SNMPCommunity=\"$this->SNMPCommunity\", - PanelID=$this->PanelID, BreakerSize=$this->BreakerSize, FirmwareVersion=\"$this->FirmwareVersion\", - PanelPole=\"$this->PanelPole\", InputAmperage=$this->InputAmperage, - FailSafe=$this->FailSafe, PanelID2=\"$this->PanelID2\", - PanelPole2=\"$this->PanelPole2\"$sqladdon;"; + $sql .= ";"; error_log( $sql ); - if($this->exec($sql)){ + $stmt = $dbh->prepare($sql); + + if($stmt->execute($params)){ $this->PDUID=$dbh->lastInsertId(); (class_exists('LogActions'))?LogActions::LogThis($this):''; return $this->PDUID; }else{ - $info=$dbh->errorInfo(); + $info=$stmt->errorInfo(); error_log("CreatePDU::PDO Error: {$info[2]} SQL=$sql"); @@ -223,22 +270,40 @@ function CreatePDU($pduid=null){ } function UpdatePDU(){ + global $dbh; $this->MakeSafe(); $oldpdu=new PowerDistribution(); $oldpdu->PDUID=$this->PDUID; $oldpdu->GetPDU(); - $sql="UPDATE fac_PowerDistribution SET Label=\"$this->Label\", - CabinetID=$this->CabinetID, TemplateID=$this->TemplateID, - IPAddress=\"$this->IPAddress\", SNMPCommunity=\"$this->SNMPCommunity\", - PanelID=$this->PanelID, BreakerSize=$this->BreakerSize, FirmwareVersion=\"$this->FirmwareVersion\", - PanelPole=\"$this->PanelPole\", InputAmperage=$this->InputAmperage, - FailSafe=$this->FailSafe, PanelID2=$this->PanelID2, PanelPole2=$this->PanelPole2 - WHERE PDUID=$this->PDUID;"; + $sql = "UPDATE fac_PowerDistribution SET Label = :Label, CabinetID = :CabinetID, + TemplateID = :TemplateID, IPAddress = :IPAddress, SNMPCommunity = :SNMPCommunity, + PanelID = :PanelID, BreakerSize = :BreakerSize, FirmwareVersion = :FirmwareVersion, + PanelPole = :PanelPole, InputAmperage = :InputAmperage, FailSafe = :FailSafe, + PanelID2 = :PanelID2, PanelPole2 = :PanelPole2 + WHERE PDUID = :PDUID"; + + $stmt = $dbh->prepare($sql); + $result = $stmt->execute(array( + ':Label' => $this->Label, + ':CabinetID' => $this->CabinetID, + ':TemplateID' => $this->TemplateID, + ':IPAddress' => $this->IPAddress, + ':SNMPCommunity' => $this->SNMPCommunity, + ':PanelID' => $this->PanelID, + ':BreakerSize' => $this->BreakerSize, + ':FirmwareVersion' => $this->FirmwareVersion, + ':PanelPole' => $this->PanelPole, + ':InputAmperage' => $this->InputAmperage, + ':FailSafe' => $this->FailSafe, + ':PanelID2' => $this->PanelID2, + ':PanelPole2' => $this->PanelPole2, + ':PDUID' => $this->PDUID, + )); (class_exists('LogActions'))?LogActions::LogThis($this,$oldpdu):''; - return $this->query($sql); + return $result; } function GetSourceForPDU(){ @@ -253,11 +318,13 @@ function GetSourceForPDU(){ } function GetPDU(){ + global $dbh; $this->MakeSafe(); - $sql="SELECT * FROM fac_PowerDistribution WHERE PDUID=$this->PDUID;"; + $stmt = $dbh->prepare("SELECT * FROM fac_PowerDistribution WHERE PDUID = :PDUID"); + $stmt->execute(array(':PDUID' => $this->PDUID)); - if($PDURow=$this->query($sql)->fetch()){ + if($PDURow=$stmt->fetch()){ foreach(PowerDistribution::RowToObject($PDURow) as $prop => $value){ $this->$prop=$value; } @@ -273,13 +340,18 @@ function GetPDU(){ } function GetPDUbyPanel(){ + global $dbh; $this->MakeSafe(); - $sql="SELECT * FROM fac_PowerDistribution WHERE PanelID=$this->PanelID - OR PanelID2=$this->PanelID ORDER BY PanelPole ASC, CabinetID, Label"; + $stmt = $dbh->prepare("SELECT * FROM fac_PowerDistribution WHERE PanelID = :PanelID1 + OR PanelID2 = :PanelID2 ORDER BY PanelPole ASC, CabinetID, Label"); + $stmt->execute(array( + ':PanelID1' => $this->PanelID, + ':PanelID2' => $this->PanelID, + )); $PDUList=array(); - foreach($this->query($sql) as $PDURow){ + foreach($stmt as $PDURow){ $PDUList[]=PowerDistribution::RowToObject($PDURow); } @@ -287,12 +359,14 @@ function GetPDUbyPanel(){ } function GetPDUbyCabinet(){ + global $dbh; $this->MakeSafe(); - $sql="SELECT * FROM fac_PowerDistribution WHERE CabinetID=$this->CabinetID ORDER BY Label ASC;"; + $stmt = $dbh->prepare("SELECT * FROM fac_PowerDistribution WHERE CabinetID = :CabinetID ORDER BY Label ASC"); + $stmt->execute(array(':CabinetID' => $this->CabinetID)); $PDUList=array(); - foreach($this->query($sql) as $PDURow){ + foreach($stmt as $PDURow){ $PDUList[$PDURow["PDUID"]]=PowerDistribution::RowToObject($PDURow); } @@ -300,12 +374,14 @@ function GetPDUbyCabinet(){ } function SearchByPDUName(){ + global $dbh; $this->MakeSafe(); - $sql="SELECT * FROM fac_PowerDistribution WHERE Label LIKE \"%$this->Label%\";"; + $stmt = $dbh->prepare("SELECT * FROM fac_PowerDistribution WHERE Label LIKE :Label"); + $stmt->execute(array(':Label' => '%'.$this->Label.'%')); $PDUList=array(); - foreach($this->query($sql) as $PDURow){ + foreach($stmt as $PDURow){ $PDUList[$PDURow["PDUID"]]=PowerDistribution::RowToObject($PDURow); } @@ -314,13 +390,16 @@ function SearchByPDUName(){ /* These fac_PDUStats functions are UGLY. When we build out RESTful API, they should be moved to a separate class and return objects */ function GetLastReading(){ + global $dbh; $this->MakeSafe(); - $sql="SELECT * FROM fac_PDUStats WHERE PDUID=$this->PDUID;"; + $stmt = $dbh->prepare("SELECT * FROM fac_PDUStats WHERE PDUID = :PDUID"); + $stmt->execute(array(':PDUID' => $this->PDUID)); + $stats=new stdClass(); $stats->Wattage=0; $stats->LastRead=date('Y-m-d G:i:s',0); - foreach($this->query($sql) as $row){ + foreach($stmt as $row){ foreach($row as $prop => $value){ if(!is_int($prop)){ $stats->$prop=$value; @@ -332,28 +411,32 @@ function GetLastReading(){ } function GetWattageByDC($dc=null){ + global $dbh; // What was the idea behind this null function? if($dc==null){ - $sql="SELECT COUNT(Wattage) FROM fac_PDUStats;"; + $stmt = $dbh->query("SELECT COUNT(Wattage) FROM fac_PDUStats;"); + return $stmt->fetchColumn(); }else{ - $sql="SELECT SUM(Wattage) AS Wattage FROM fac_PDUStats WHERE PDUID IN - (SELECT PDUID FROM fac_PowerDistribution WHERE CabinetID IN - (SELECT CabinetID FROM fac_Cabinet WHERE DataCenterID=".intval($dc)."))"; + $stmt = $dbh->prepare("SELECT SUM(Wattage) AS Wattage FROM fac_PDUStats WHERE PDUID IN + (SELECT PDUID FROM fac_PowerDistribution WHERE CabinetID IN + (SELECT CabinetID FROM fac_Cabinet WHERE DataCenterID = :DataCenterID))"); + $stmt->execute(array(':DataCenterID' => intval($dc))); + return $stmt->fetchColumn(); } - - return $this->query($sql)->fetchColumn(); } function GetWattageByCabinet($CabinetID){ + global $dbh; $CabinetID=intval($CabinetID); if($CabinetID <1){ return 0; } - $sql="SELECT SUM(Wattage) AS Wattage FROM fac_PDUStats WHERE PDUID - IN (SELECT PDUID FROM fac_PowerDistribution WHERE CabinetID=$CabinetID);"; + $stmt = $dbh->prepare("SELECT SUM(Wattage) AS Wattage FROM fac_PDUStats WHERE PDUID + IN (SELECT PDUID FROM fac_PowerDistribution WHERE CabinetID = :CabinetID);"); + $stmt->execute(array(':CabinetID' => $CabinetID)); - if(!$wattage=$this->query($sql)->fetchColumn()){ + if(!$wattage=$stmt->fetchColumn()){ $wattage=0; } @@ -361,6 +444,7 @@ function GetWattageByCabinet($CabinetID){ } function LogManualWattage($Wattage){ + global $dbh; $this->MakeSafe(); $oldpdu=new PowerDistribution(); @@ -373,11 +457,16 @@ function LogManualWattage($Wattage){ $Wattage=intval($Wattage); $this->Wattage=$Wattage; - $sql="INSERT INTO fac_PDUStats SET Wattage=$Wattage, PDUID=$this->PDUID, - LastRead=NOW() ON DUPLICATE KEY UPDATE Wattage=$Wattage, LastRead=NOW();"; + $stmt = $dbh->prepare("INSERT INTO fac_PDUStats SET Wattage = :Wattage, PDUID = :PDUID, + LastRead = NOW() ON DUPLICATE KEY UPDATE Wattage = :Wattage2, LastRead = NOW();"); + $result = $stmt->execute(array( + ':Wattage' => $Wattage, + ':PDUID' => $this->PDUID, + ':Wattage2' => $Wattage, + )); (class_exists('LogActions'))?LogActions::LogThis($this,$oldpdu):''; - return ($this->query($sql))?$this->GetLastReading():false; + return ($result)?$this->GetLastReading():false; } function UpdateStats( $filterType="None", $filterValue="" ) { @@ -388,21 +477,30 @@ function UpdateStats( $filterType="None", $filterValue="" ) { $htmlMessage = ""; $filterValue = sanitize($filterValue); + $filterSQL = ""; + $filterParams = array(); + switch ($filterType) { case "Country": - $filterSQL = "AND f.countryCode='$filterValue'"; + $filterSQL = "AND f.countryCode = :filterValue"; + $filterParams[':filterValue'] = $filterValue; break; case "Container": - $filterSQL = "AND f.ContainerID='$filterValue'"; + $filterSQL = "AND f.ContainerID = :filterValue"; + $filterParams[':filterValue'] = $filterValue; break; case "DataCenter": - $filterSQL = "AND e.DataCenterID='$filterValue'"; + $filterSQL = "AND e.DataCenterID = :filterValue"; + $filterParams[':filterValue'] = $filterValue; break; case "Zone": - $filterSQL = "AND e.ZoneID='$filterValue'"; + $filterSQL = "AND e.ZoneID = :filterValue"; + $filterParams[':filterValue'] = $filterValue; break; case "Row": - $filterSQL = "AND e.CabRowID='$filterValue'"; + $filterSQL = "AND e.CabRowID = :filterValue"; + $filterParams[':filterValue'] = $filterValue; + break; default: $filterSQL = ""; } @@ -414,8 +512,11 @@ function UpdateStats( $filterType="None", $filterValue="" ) { e.DataCenterID=f.DataCenterID AND b.Managed=true AND c.PrimaryIP>'' and c.SNMPFailureCount<3 $filterSQL ORDER BY f.Name ASC, e.Location ASC"; + $stmt = $dbh->prepare($sql); + $stmt->execute($filterParams); + // The result set should have no PDU's with blank IP Addresses or SNMP Community, so we can forge ahead with processing them all - foreach($this->query($sql) as $row){ + foreach($stmt as $row){ if(!$dev=PowerDistribution::BasicTests($row['PDUID'])){ // if we fail the basic test on a single device we don't want to skip all the rest so continue instead of return false; continue; @@ -470,12 +571,16 @@ function UpdateStats( $filterType="None", $filterValue="" ) { // Make the float safe for insert into mysql $watts=float_sqlsafe($watts); - $sql="INSERT INTO fac_PDUStats SET PDUID={$row["PDUID"]}, Wattage=$watts, - LastRead=now() ON DUPLICATE KEY UPDATE Wattage=$watts, LastRead=now();"; + $statStmt = $dbh->prepare("INSERT INTO fac_PDUStats SET PDUID = :PDUID, Wattage = :Wattage, + LastRead = now() ON DUPLICATE KEY UPDATE Wattage = :Wattage2, LastRead = now();"); - if(!$dbh->query($sql)){ - $info=$dbh->errorInfo(); - error_log("PowerDistribution::UpdateStats::PDO Error: {$info[2]} SQL=$sql"); + if(!$statStmt->execute(array( + ':PDUID' => $row["PDUID"], + ':Wattage' => $watts, + ':Wattage2' => $watts, + ))){ + $info=$statStmt->errorInfo(); + error_log("PowerDistribution::UpdateStats::PDO Error: {$info[2]}"); } $maxWatts = $row["Voltage"] * $row["Amperage"]; @@ -488,18 +593,18 @@ function UpdateStats( $filterType="None", $filterValue="" ) { if ( $config->ParameterArray["PowerAlertsEmail"] == "enabled" ) { if ( $watts >= $config->ParameterArray["PowerRed"] / 100 * $maxWatts ) { - $AlertList .= sprintf( "%s%s%s%s%s\n", $row["Name"], $row["Location"], $row["Label"], $watts, __("Critical")); + $AlertList .= sprintf( "%s%s%s%s%s\n", htmlspecialchars($row["Name"]), htmlspecialchars($row["Location"]), htmlspecialchars($row["Label"]), $watts, __("Critical")); } elseif ( $watts >= $config->ParameterArray["PowerYellow"] / 100 * $maxWatts ) { - $AlertList .= sprintf( "%s%s%s%s%s\n", $row["Name"], $row["Location"], $row["Label"], $watts, __("Warning")); + $AlertList .= sprintf( "%s%s%s%s%s\n", htmlspecialchars($row["Name"]), htmlspecialchars($row["Location"]), htmlspecialchars($row["Label"]), $watts, __("Warning")); } } $this->PDUID=$row["PDUID"]; if($ver=$this->GetSmartCDUVersion()){ - $sql="UPDATE fac_PowerDistribution SET FirmwareVersion=\"$ver\" WHERE PDUID=$this->PDUID;"; - if(!$dbh->query($sql)){ - $info=$dbh->errorInfo(); - error_log("PowerDistribution::UpdateStats::PDO Error: {$info[2]} SQL=$sql"); + $fwStmt = $dbh->prepare("UPDATE fac_PowerDistribution SET FirmwareVersion = :FirmwareVersion WHERE PDUID = :PDUID;"); + if(!$fwStmt->execute(array(':FirmwareVersion' => $ver, ':PDUID' => $this->PDUID))){ + $info=$fwStmt->errorInfo(); + error_log("PowerDistribution::UpdateStats::PDO Error: {$info[2]}"); } } @@ -644,6 +749,7 @@ function GetAllBreakerPoles() { function DeletePDU(){ global $person; + global $dbh; $this->MakeSafe(); // Do not attempt anything else if the lookup fails @@ -666,11 +772,11 @@ function DeletePDU(){ // Clear out any records from PDUStats, possible S.U.T. involving changing // a devicetype but leaving behind a phantom reading for a non-power device - $sql="DELETE FROM fac_PDUStats WHERE PDUID=$this->PDUID;"; - $this->exec($sql); + $stmt = $dbh->prepare("DELETE FROM fac_PDUStats WHERE PDUID = :PDUID;"); + $stmt->execute(array(':PDUID' => $this->PDUID)); - $sql="DELETE FROM fac_PowerDistribution WHERE PDUID=$this->PDUID;"; - if(!$this->exec($sql)){ + $stmt = $dbh->prepare("DELETE FROM fac_PowerDistribution WHERE PDUID = :PDUID;"); + if(!$stmt->execute(array(':PDUID' => $this->PDUID))){ // Something went south and this didn't delete. return false; }else{ diff --git a/devices.php b/devices.php index 62354147b..0ed88619a 100644 --- a/devices.php +++ b/devices.php @@ -52,6 +52,7 @@ $v3PrivPassphrase ); + // Try to connect to keep us from killing the system on a failure $error=false; try { diff --git a/vmware.inc.php b/vmware.inc.php index b892a7164..fb5ab2328 100644 --- a/vmware.inc.php +++ b/vmware.inc.php @@ -1,44 +1,35 @@ ParameterArray["snmpwalk"]." -v 2c -c $community $serverIP .1.3.6.1.4.1.6876.2.1.1.2 | ".$config->ParameterArray["cut"]." -d: -f4 | /bin/cut -d\\\" -f2"; - exec( $pollCommand, $namesOutput ); - - $pollCommand = $config->ParameterArray["snmpwalk"]." -v 2c -c $community $serverIP .1.3.6.1.4.1.6876.2.1.1.6 | ".$config->ParameterArray["cut"]." -d: -f4 | /bin/cut -d\\\" -f2"; - exec( $pollCommand, $statesOutput ); - - if ( ( count( $namesOutput ) > 0 ) && ( count( $statesOutput ) > 0 ) ) { - $tempVMs = array_combine( $namesOutput, $statesOutput ); - - $vmID = 0; - - foreach( $tempVMs as $key => $value ) { - $vmList[$vmID] = new ESX(); - $vmList[$vmID]->vmID = $vmID; - $vmList[$vmID]->vmName = $key; - $vmList[$vmID]->vmState = $value; - - $vmID++; - } - - return $vmList; - } - } -} + var $vmID; + var $vmName; + var $vmState; + + function EnumerateVMs($serverIP, $community) { + global $config; + $vmList = array(); + + $pollCommand = $config->ParameterArray["snmpwalk"] . " -v 2c -c $community $serverIP .1.3.6.1.4.1.6876.2.1.1.2 | " . $config->ParameterArray["cut"] . " -d: -f4 | /bin/cut -d\\\" -f2"; + exec($pollCommand, $namesOutput); + + $pollCommand = $config->ParameterArray["snmpwalk"] . " -v 2c -c $community $serverIP .1.3.6.1.4.1.6876.2.1.1.6 | " . $config->ParameterArray["cut"] . " -d: -f4 | /bin/cut -d\\\" -f2"; + exec($pollCommand, $statesOutput); + + if ((count($namesOutput) > 0) && (count($statesOutput) > 0)) { + $tempVMs = array_combine($namesOutput, $statesOutput); + + $vmID = 0; + + foreach ($tempVMs as $key => $value) { + $vmList[$vmID] = new ESX(); + $vmList[$vmID]->vmID = $vmID; + $vmList[$vmID]->vmName = $key; + $vmList[$vmID]->vmState = $value; + + $vmID++; + } -// Simulate a PHP 5.x function, remove if upgrading to PHP 5.x or higher -function array_combine($a1,$a2) -{ - $maxval = count($a1); - for($i=0;$i<$maxval;$i++) - $ra[$a1[$i]] = $a2[$i]; - if(isset($ra)) return $ra; else return false; + return $vmList; + } + } } -?> +?> \ No newline at end of file