Created
August 6, 2013 22:07
-
-
Save danieljpeter/6169172 to your computer and use it in GitHub Desktop.
Revisions
-
danieljpeter created this gist
Aug 6, 2013 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,187 @@ <?php //this requires the php toolkit: https://github.com/developerforce/Force.com-Toolkit-for-PHP require_once ('settings.php'); require_once ('phpToolkit/SforcePartnerClient.php'); require_once ('phpToolkit/SforceHeaderOptions.php'); $storage = checkStorageUsage($USERNAME, $PASSWORD, $ORG_ID); $api = checkAPI($USERNAME, $PASSWORD, $ORG_ID); header("Content-Type: text/plain"); echo $storage; echo "\n"; echo $api; function checkAPI($USERNAME, $PASSWORD, $ORG_ID) { $sReturn = ''; //get a session ID via the php toolkit API $sessionId = getSession($USERNAME, $PASSWORD); //build the cookie info out of session ID and org ID $sc = 'Cookie='; $sc .= 'oid='.$ORG_ID.'; '; $sc .= 'sid='.$sessionId.'; '; //request the orgstorageusage page in the frontend UI $response = getOverviewPageRaw($sc); //parse the data we want out the HTML $apiLimit = ''; $apiUsed = ''; $apiUsedPercent = ''; $arrTable = explode('API Requests, Last 24 Hours', $response, 2); if (count($arrTable) == 2) { $arrTable = explode(')', $arrTable[1], 2); if (count($arrTable) == 2) { $sBlock = $arrTable[0]; //get the max $arrTable = explode('(maximum', $sBlock, 2); if (count($arrTable) == 2) { $apiLimit = intval(str_replace(',', '', trim($arrTable[1]))); } //get the used $arrTable = explode('class="textOnly">', $sBlock, 2); if (count($arrTable) == 2) { $arrTable = explode('<', $arrTable[1], 2); if (count($arrTable) == 2) { $apiUsed = intval(str_replace(',', '', trim($arrTable[0]))); } } //get the % Used $arrTable = explode('class="desc-num">', $sBlock, 2); if (count($arrTable) == 2) { $arrTable = explode('<', $arrTable[1], 2); if (count($arrTable) == 2) { $apiUsedPercent = intval(str_replace('%', '', trim($arrTable[0]))); } } } } $sReturn .= 'API Usage:'.$apiLimit.':'.$apiUsed.':'.$apiUsedPercent; return $sReturn; } function checkStorageUsage($USERNAME, $PASSWORD, $ORG_ID) { $sReturn = ''; //get a session ID via the php toolkit API $sessionId = getSession($USERNAME, $PASSWORD); //build the cookie info out of session ID and org ID $sc = 'Cookie='; $sc .= 'oid='.$ORG_ID.'; '; $sc .= 'sid='.$sessionId.'; '; //request the orgstorageusage page in the frontend UI $response = getStoragePageRaw($sc, $ORG_ID); //parse the data we want out the HTML $arrTable = explode('Data Storage', $response, 2); if (count($arrTable) == 2) { $arrTable = explode('</table>', $arrTable[1], 2); if (count($arrTable) == 2) { $arrTable = explode('</td>', $arrTable[0]); if (count($arrTable) >= 5 ) { for ($i = 0; $i <= 5; $i++) { //array locations 0-5 contain our 6 data points //[0] => </th><td class=" dataCell numericalColumn">8.3 GB $arrRow = explode('>', $arrTable[$i]); if ($i == 0) { $dataLimit = str_replace(array(' ', 'GB', '%'), '', end($arrRow)); } if ($i == 1) { $dataUsed = str_replace(array(' ', 'GB', '%'), '', end($arrRow)); } if ($i == 2) { $dataUsedPercent = str_replace(array(' ', 'GB', '%'), '', end($arrRow)); } if ($i == 3) { $fileLimit = str_replace(array(' ', 'GB', '%'), '', end($arrRow)); } if ($i == 4) { $fileUsed = str_replace(array(' ', 'GB', '%'), '', end($arrRow)); } if ($i == 5) { $fileUsedPercent = str_replace(array(' ', 'GB', '%'), '', end($arrRow)); } } } } } $sReturn .= 'Data Storage:'.$dataLimit.':'.$dataUsed.':'.$dataUsedPercent."\n"; $sReturn .= 'File Storage:'.$fileLimit.':'.$fileUsed.':'.$fileUsedPercent; return $sReturn; } function getOverviewPageRaw($sc) { $url = 'https://ssl.salesforce.com/setup/systemOverview.apexp'; $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_COOKIE, $sc); $response = curl_exec($ch); curl_close($ch); return $response; } function getStoragePageRaw($sc, $ORG_ID) { $url = 'https://ssl.salesforce.com/setup/org/orgstorageusage.jsp?id='.$ORG_ID; $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_COOKIE, $sc); $response = curl_exec($ch); curl_close($ch); return $response; } function getSession($USERNAME, $PASSWORD) { $sessionId = ''; try { $mySforceConnection = new SforcePartnerClient(); $mySoapClient = $mySforceConnection->createConnection('phpToolkit/partner.wsdl.xml'); $mylogin = $mySforceConnection->login($USERNAME, $PASSWORD); $sessionId = $mylogin->sessionId; } catch (Exception $e) { print_r($mySforceConnection->getLastRequest()); echo $e->faultstring; } return $sessionId; } ?> This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,5 @@ <?php $USERNAME = '[email protected]'; $PASSWORD = 'password'; $ORG_ID = '00D00000000xxxx'; ?>