PageRenderTime 66ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/Disk Wipe Verifier/CLI/DiskWipeVerify_Old.php

https://bitbucket.org/vsposato/diskwipeverify
PHP | 989 lines | 496 code | 184 blank | 309 comment | 81 complexity | cb9633523b38e95f1d50936f60e33996 MD5 | raw file
  1. !/usr/bin/php -q
  2. <?php
  3. /*
  4. * Define some global variables here
  5. *
  6. * $sortCode - this will hold the sort code of the site this machine was located at
  7. * $vaidationArray - this will be the array that will eventually be sent to the server
  8. *
  9. */
  10. /*
  11. * Implement the PEAR package for XML_RPC for communication with the backend server
  12. */
  13. require_once('XML/RPC.php');
  14. //Set the timezone
  15. date_default_timezone_set("America/New_York");
  16. /* Define STDIN in case it wasn't defined somewhere else */
  17. if (! defined("STDIN")) {
  18. define("STDIN", fopen('php://stdin','r'));
  19. }
  20. /*
  21. * Define the values that are needed for the XML client connection
  22. */
  23. define("xml_path", "dwv_xmlserver");
  24. define("xml_server", "diskwipe.nettechconsultants.com");
  25. define("xml_port", 80);
  26. define("xml_proxy", NULL);
  27. define("xml_proxy_port", NULL);
  28. define("xml_proxy_user", NULL);
  29. define("xml_proxy_pass", NULL);
  30. global $sortCode;
  31. global $validationArray;
  32. global $asteriskArray;
  33. //Set up the data array for the final data
  34. $validation_array = array();
  35. function multi_array_key_exists($needle, $haystack) {
  36. foreach ($haystack as $key=>$value) {
  37. if ($needle===$key) {
  38. return $key;
  39. }
  40. if (is_array($value)) {
  41. if(multi_array_key_exists($needle, $value)) {
  42. return $key . ":" . multi_array_key_exists($needle, $value);
  43. }
  44. }
  45. }
  46. return false;
  47. }
  48. function objectToArray($object) {
  49. if(!is_object( $object ) && !is_array( $object )) {
  50. return $object;
  51. }
  52. if(is_object($object) ) {
  53. $object = get_object_vars( $object );
  54. }
  55. return array_map('objectToArray', $object );
  56. }
  57. function getLogFilePath($sortCode) {
  58. /*
  59. * This funciton will determine if a sortCode directory already exists
  60. * if it doesn't it will create it, if it does it will just return the directory
  61. */
  62. global $sortCode;
  63. $oldWorkingDirectory = getcwd();
  64. //Determine if we are already in the sortCode directory
  65. if( stripos($oldWorkingDirectory, $sortCode) === FALSE) {
  66. //We didn't find the sort code in the current working directory
  67. //Attempt to change directory to the sortCode directory
  68. if(! chdir($sortCode)) {
  69. //It didn't work so the directory needs to be created - with wide open permissions
  70. mkdir($sortCode, 0777);
  71. //Now change directory to the sortCode directory
  72. chdir($sortCode);
  73. }
  74. }
  75. //Get the current working directory and append the /
  76. $currentWorkingDirectory = getcwd() . '/';
  77. //Return the current working path
  78. return $currentWorkingDirectory;
  79. }
  80. function createLogFile() {
  81. /*
  82. * This function will create a file to be used as the log for this process
  83. * and will set the fileHandle to be global
  84. */
  85. global $logFile;
  86. global $sortCode;
  87. //Determine the path that we will be using
  88. $path = getLogFilePath($sortCode);
  89. //Create the name of the logfile based upon the path
  90. //Remember we will rename if from TempLogFile.txt to
  91. //SerialNumber-YYYY-MM-DD-HH-MM-SS.txt later
  92. $logFileName = $path . 'TempLogFile.txt';
  93. //Create the logfile and assign the handle to $logFile
  94. $logFile = fopen($logFileName, 'w');
  95. If (! $logFile ) {
  96. return FALSE;
  97. } else {
  98. return TRUE;
  99. }
  100. }
  101. function writeToLogFile($sectionName, $sectionData, $logFile) {
  102. /*
  103. * This function will write data to the log file
  104. * it will take a section name to identify what section
  105. * of the log file we are currently working, a string or array of data
  106. * to actually write, and the log file handle to work with
  107. */
  108. global $logFile;
  109. //Now determine if sectionData is an array or a string
  110. if( is_array($sectionData) ) {
  111. //This is an array so we will need to run a loop to get all the data
  112. //out to the file
  113. //Write the section name to the logFile
  114. fwrite($logFile, $sectionName);
  115. //Insert carriage return
  116. fwrite($logFile, "\n");
  117. foreach($sectionData as $key => $dataLine) {
  118. //Write the line key
  119. fwrite($logFile, "{$key} = ");
  120. if (is_array($dataLine)) {
  121. foreach($dataLine as $subKey => $subDataLine) {
  122. //Write the key and data line for the sub array
  123. fwrite($logFile, " {$subKey} = ");
  124. fwrite($logFile, "{$subDataLine} \n");
  125. }
  126. } else {
  127. //Write a line from the array
  128. fwrite($logFile, "{$dataLine} \n");
  129. }
  130. }
  131. //We completed the loop so return true
  132. return TRUE;
  133. } elseif( is_object($sectionData)) {
  134. //This is an object so we need to convert it to an array
  135. //We will use a conversion function, and then hand it back into the writeToLogFile function
  136. writeToLogFile($sectionName, objectToArray($sectionData), $logFile);
  137. } else {
  138. //Write the section name to the logFile
  139. fwrite($logFile, $sectionName);
  140. //This is a single string so just write the line to the file
  141. fwrite($logFile, $sectionData);
  142. //Insert carriage return
  143. fwrite($logFile, "\n");
  144. //We wrote the data return true
  145. return TRUE;
  146. }
  147. //Something happened if we go down here, so return an error
  148. return FALSE;
  149. }
  150. function closeLogFile($logFile) {
  151. /*
  152. * This function will close the log file and rename it
  153. * to the appropriate name SerialNumber-YYYY-MM-DD-HH-MM.txt
  154. * from the TempLogFile.txt
  155. */
  156. global $logFile;
  157. global $validation_array;
  158. global $sortCode;
  159. //Determine the path that we will be using
  160. $path = getLogFilePath($sortCode);
  161. $newFileName = $path . $validation_array['machine_serial'] . '-' . date("Y-m-d-H-i", time()) . '.txt';
  162. fclose($logFile);
  163. $oldFileName = $path . 'TempLogFile.txt';
  164. if (! rename($oldFileName, $newFileName) ) {
  165. //Something happened during the rename
  166. return FALSE;
  167. }
  168. }
  169. function createAsteriskArray() {
  170. /*
  171. * This function will open the text file that contains the character to asterisk
  172. * representations and build them into the array $asteriskArray()
  173. */
  174. global $asteriskArray;
  175. global $logFile;
  176. $asteriskFilePath = dirname(__FILE__) . '/alpha.csv';
  177. // Open the file alpha.txt from the current directory
  178. $asteriskFile = fopen($asteriskFilePath, 'r');
  179. // Confirm that the file actually opened
  180. if (! $asteriskFile) {
  181. //File didn't open so return an error
  182. echo "File Open Error - alpha.csv does not exist!";
  183. return FALSE;
  184. }
  185. while (($tempArray = fgetcsv($asteriskFile)) !== FALSE) {
  186. //Create the temporary array to hold the line read from the file
  187. $tempString = '';
  188. foreach ($tempArray as $key=>$value) {
  189. if($key<2) {
  190. //These are the array parameters so skip them
  191. continue;
  192. } else {
  193. //Replace 0 with spaces and 1 with a #
  194. $tempString .= ($value == 0) ? ' ' : '#';
  195. }
  196. }
  197. //Now that we have the entire string create the array entry
  198. //$tempArray[0] is the letter and $tempArray[1] is the line number
  199. //of the letter
  200. $asteriskArray[$tempArray[0]][$tempArray[1]] = $tempString;
  201. }
  202. //writeToLogFile("Display Asterisk Array", $asteriskArray, $logFile);
  203. return $asteriskArray;
  204. }
  205. function displayAsteriskMessage($message = NULL) {
  206. /*
  207. * This function will load the asterisk file, and then process a message
  208. * on the screen using the predefined character definitions
  209. */
  210. global $asteriskArray;
  211. global $logFile;
  212. //writeToLogFile("Display Asterisk Array", $asteriskArray, $logFile);
  213. if(! $message) {
  214. //No message was passed so just return a false
  215. return FALSE;
  216. }
  217. if (count($asteriskArray) === 0 ) {
  218. if(! createAsteriskArray()) {
  219. //If the asterisk handler returned a failure then return false
  220. echo "Asterisk Handler failed to load - error!";
  221. return FALSE;
  222. }
  223. }
  224. $message = trim($message);
  225. //Determine the length of the message
  226. $lengthOfMessage = strlen($message);
  227. //Set the line count
  228. $lineCount = round(($lengthOfMessage / 12), 0, PHP_ROUND_HALF_UP);
  229. //Work to build the lines
  230. $currentLineNumber = 1;
  231. $currentCharacterNumber = 1;
  232. $currentDisplayLineNumber = 1;
  233. $currentCharacter = '';
  234. $displayMessage = Array();
  235. while ($currentLineNumber <= $lineCount && $currentCharacterNumber <= $lengthOfMessage) {
  236. //writeToLogFile("LOOP TO DISPLAY MESSAGE ","{$currentLineNumber} of {$lineCount}", $logFile);
  237. //writeToLogFile("LOOP TO DISPLAY MESSAGE ","{$currentCharacterNumber} of {$lengthOfMessage}", $logFile);
  238. if($currentCharacter % 12 === 1) {
  239. //We have reached the beginning of the next line so increment
  240. $currentLineNumber++;
  241. }
  242. $currentCharacter = strtoupper($message[($currentCharacterNumber - 1)]);
  243. for($i = 1; $i <= 8; $i++) {
  244. //Here we are going to loop through line of the character from
  245. //the asterisk array
  246. if(! isset($displayMessage[$currentLineNumber][$i]) ) {
  247. $displayMessage[$currentLineNumber][$i] = '';
  248. }
  249. //Fix the space to allow for the lowercase s to be the identifier
  250. if($currentCharacter === " ") {
  251. $currentCharacter = "s";
  252. }
  253. //writeToLogFile("{$displayMessage[$currentLineNumber][$i]} =", "{$asteriskArray[$currentCharacter][$i]}", $logFile);
  254. $displayMessage[$currentLineNumber][$i] .= ' ' . $asteriskArray[$currentCharacter][$i];
  255. }
  256. //Increment the Character Number
  257. $currentCharacterNumber++;
  258. }
  259. //Run a loop for each line of the message
  260. foreach($displayMessage as $lineKey => $lineValue) {
  261. //Run a loop for each asterisk line of the line of the message
  262. foreach($lineValue as $displayLine) {
  263. echo $displayLine;
  264. echo "\n";
  265. }
  266. echo "\n";
  267. }
  268. }
  269. function getSystemSerialNumber() {
  270. /*
  271. * Here we will use the exec command to pull the system serial number and then
  272. * hand it back to the calling function
  273. */
  274. exec('sudo dmidecode -s system-serial-number', $tempSerialNumber);
  275. //Shift the first array item off to the return value
  276. return array_shift($tempSerialNumber);
  277. }
  278. function getBaseboardSerialNumber() {
  279. /*
  280. * Here we will use the exec command to pull the baseboard serial number and then
  281. * hand it back to the calling function
  282. */
  283. exec('sudo dmidecode -s baseboard-serial-number', $tempSerialNumber);
  284. //Shift the first array item off to the return value
  285. return array_shift($tempSerialNumber);
  286. }
  287. function getChassisSerialNumber() {
  288. /*
  289. * Here we will use the exec command to pull the chassis serial number and then
  290. * hand it back to the calling function
  291. */
  292. exec('sudo dmidecode -s chassis-serial-number', $tempSerialNumber);
  293. //Shift the first array item off to the return value
  294. return array_shift($tempSerialNumber);
  295. }
  296. function cleanHDSize($HDSize) {
  297. //This function will remove the , from the sizing information returned
  298. //from the fdisk function
  299. //The standard separator for the hard drive size is a comma
  300. $temp_hd_size = explode(',', $HDSize);
  301. //Return the 1st array entry as this will house the friendly size
  302. return $temp_hd_size[0];
  303. }
  304. function cleanHDIdentification($HDIdentification) {
  305. //This function will take the model, firmware and serial and return them without
  306. //the beginning entry
  307. //The standard is an = sign so break out from there
  308. $temp_hd_info = explode('=',$HDIdentification);
  309. //Return the 2nd index of the array returned which will hold the important information
  310. return $temp_hd_info[1];
  311. }
  312. function getPartitionCountForDisk($partition_array, $disk_id) {
  313. /*
  314. * This function will take an array of partitions and parse them to see
  315. * how many partitions belong to this disk
  316. * Returns - integer count of partitions
  317. */
  318. $part_count = 0;
  319. foreach ($partition_array as $partition) {
  320. $partition_entry = stripos($partition, $disk_id);
  321. if ($partition_entry !== FALSE) {
  322. //We found the disk id inside the partition listing increment the partition count
  323. $part_count++;
  324. }
  325. }
  326. return $part_count;
  327. }
  328. function getSortCode() {
  329. global $sortCode;
  330. //Present the user with a prompt to get the sort code of the site we are at
  331. echo "What sort code are you at? (enter below): \n";
  332. $sortCode = fread(STDIN,7);
  333. //Lets confirm that the user really meant that sort code
  334. echo "{$sortCode} - are you sure? (yes / no) \n";
  335. //Here we are going to run an input loop to confirm that the user really meant
  336. //this sort code
  337. do {
  338. //Read 4 characters from the keyboard
  339. $answer = fread(STDIN, 4);
  340. //Trim the response and convert it to upper
  341. $answer = trim(strtoupper($answer));
  342. //Check to see if the user said either yes or no
  343. if (($answer != "YES") AND ($answer != "NO")) {
  344. // You must enter YES or NO, nothing else
  345. // The user said something other than yes or no so loop
  346. echo "Are you at sort code {$sortCode}? \n";
  347. echo "You must enter either yes or no! \n";
  348. }
  349. } while (($answer != "YES") AND ($answer != "NO"));
  350. //The loop exited because the user said yes or no,
  351. //if he said no then take him back into the function
  352. if ($answer == "NO") {
  353. getSortCode();
  354. }
  355. }
  356. function determineValidSerialNumber($chassisSN, $baseboardSN, $systemSN) {
  357. /*
  358. * Here we are going to validate that at least 2 of the 3 serial numbers match
  359. * and assign that to the validation array
  360. */
  361. global $validation_array;
  362. if (($chassisSN == $baseboardSN) || ($chassisSN == $systemSN)) {
  363. //Chassis serial number matches one of the other 2 serial numbers - let's use that
  364. $validation_array['machine_serial'] = $chassisSN;
  365. $validation_array['serial_type'] = 'chassis';
  366. } else {
  367. //Chassis serial number did not match either of the other 2 so test to see if system serial does
  368. if (($systemSN == $baseboardSN)) {
  369. //System serial number matches baseboard so we will use that serial number
  370. $validation_array['machine_serial'] = $systemSN;
  371. $validation_array['serial_type'] = 'system';
  372. } else {
  373. //System serial number didn't match either but it is the most reliable so use it
  374. $validation_array['machine_serial'] = $systemSN;
  375. $validation_array['serial_type'] = 'default';
  376. }
  377. }
  378. }
  379. function getFdiskInformation() {
  380. /*
  381. * We are going to use the fdisk command to allow us to pull all the hard drive
  382. * information from the sytem for later cleanup
  383. */
  384. exec('sudo fdisk -l', $temp_output);
  385. return $temp_output;
  386. }
  387. function getPhysicalDiskBaseInformation($fdisk_output) {
  388. /*
  389. * This function will take the output of the fdisk output and clean it up for the physical harddrive
  390. * information to and create an array for the sizes of each disk along with the information for that disk
  391. */
  392. foreach ($fdisk_output as $data) {
  393. /*
  394. * Let's work through each individual line looking for disk
  395. * entries or the partition runs
  396. */
  397. $disk_entry = stripos($data, "Disk /");
  398. if ($disk_entry === FALSE) {
  399. /*
  400. * Not a physical disk entry so we can simply continue on to the next line
  401. */
  402. continue;
  403. } elseif ($disk_entry !== FALSE) {
  404. //We found the Disk string inside of the data line so we are going to break it down
  405. //Build temporary string to get the
  406. //$temp_disk = substr($data, $disk_entry, 13);
  407. $temp_disk = explode(':', $data);
  408. $disk_array[] = substr($temp_disk[0], 5, 8);
  409. }
  410. }
  411. // Return all of the Physical Disks that were found
  412. return $disk_array;
  413. }
  414. function getPhysicalDiskSizeInformation($fdisk_output) {
  415. /*
  416. * This function will take the output of the fdisk output and clean it up for the physical harddrive
  417. * size information to and create an array for the sizes of each disk along with the information
  418. * for that disk
  419. *
  420. * Parameter - $fdisk_output - array - output the FDisk command
  421. * Returns - $size_arry - array - the separated harddrive size information
  422. */
  423. foreach ($fdisk_output as $data) {
  424. /*
  425. * Let's work through each individual line looking for disk
  426. * entries or the partition runs
  427. */
  428. $disk_entry = stripos($data, "Disk /");
  429. if ($disk_entry === FALSE) {
  430. /*
  431. * Not a physical disk entry so we can simply continue on to the next line
  432. */
  433. continue;
  434. } elseif ($disk_entry !== FALSE) {
  435. //We found the Disk string inside of the data line so we are going to break it down
  436. //Build temporary string to get the
  437. //$temp_disk = substr($data, $disk_entry, 13);
  438. $temp_disk = explode(':', $data);
  439. $disk_array[] = substr($temp_disk[0], 5, 8);
  440. $size_array[] = cleanHDSize($temp_disk[1]);
  441. }
  442. }
  443. //Return all of the size information for the Physical Disks found
  444. return $size_array;
  445. }
  446. function getVirtualDiskInformation($fdisk_output) {
  447. /*
  448. * This function will take the fdisk output and pass through each line looking for partition entries
  449. * and hands it all back in array form
  450. *
  451. * Paramater - $fdisk_output - array - output the FDisk command
  452. * Returns - $partition_array - array - the separated listing of each partition found on all physical disks
  453. */
  454. foreach ($fdisk_output as $data) {
  455. /*
  456. * Let's work through each individual line looking for disk
  457. * entries or the partition runs
  458. */
  459. $disk_entry = stripos($data, "Disk /");
  460. if ($disk_entry === FALSE) {
  461. //We did not find any disk entry so we are going to look for a partition entry
  462. $part_entry = stripos($data, "/dev/sd");
  463. if ($part_entry !== FALSE) {
  464. //We did find a partition entry so we are going to add it to the partition array
  465. $partition_array[] = substr($data, $part_entry, 9);
  466. }
  467. } elseif ($disk_entry !== FALSE) {
  468. //This is a physical hard drive not a partition so just continue
  469. continue;
  470. }
  471. }
  472. //We reached the end of the partition routine so hand back the partitions
  473. return $partition_array;
  474. }
  475. function getPhysicalDiskParamaters($disk_array, $partition_array, $size_array) {
  476. /*
  477. * Now we need to utilize the hdparm -i command to show all drive
  478. * serial numbers to allow for all drives to have been output.
  479. * We will utilize the array we built to run hdparm on up to 3 drives
  480. * any more than 3 drives and they will be ignored
  481. */
  482. global $logFile;
  483. $disk_pass = 1;
  484. if (array_count_values($disk_array) > 0) {
  485. foreach($disk_array as $key=>$disk) {
  486. //Build the actual shell command
  487. $exec_command = "sudo hdparm -i {$disk}";
  488. //Create the disk id variable
  489. $disk_id = substr($disk,(strlen($disk) - 3),3);
  490. //Create the dynamic variable for the hdparm output
  491. $hdparm_output = $disk_id . "_hdparm_output";
  492. //Create the dynamic variable for the hdparm return
  493. $hdparm_return = $disk_id . "_return";
  494. //Make output and return variables global
  495. global $$hdparm_output;
  496. global $$hdparm_return;
  497. //Display the command prior to running
  498. echo "Executing - {$exec_command} {$hdparm_output} {$hdparm_return} \n";
  499. //Execute the command
  500. exec($exec_command, $$hdparm_output, $$hdparm_return);
  501. /*
  502. * Here we are going to process the actual HDPARM output data to be able to determine
  503. * harddrive model and serial number - soon to be function processHdparmOutput
  504. */
  505. $model_id = 'disk_model';
  506. $firmware_id = 'disk_fw';
  507. $serial_id = 'disk_serial';
  508. $count_id = 'disk_part_count';
  509. $size_id = 'disk_size';
  510. writeToLogFile("{$hdparm_output}", " {$$hdparm_return}", $logFile);
  511. if ($$hdparm_return == 0) {
  512. //Return value of 0 means a successful run - so process the output
  513. foreach($$hdparm_output as $data) {
  514. //Is this the line that has the data we are looking for - it should begin with Model
  515. $identification_entry = strpos($data, 'Model=');
  516. if ($identification_entry !== FALSE) {
  517. //This is the line we need
  518. //Separate the model, firmware, and serial number into an array
  519. $identification = explode(',', $data);
  520. $hdparm_array[$disk_pass][$model_id] = cleanHDIdentification($identification[0]);
  521. $hdparm_array[$disk_pass][$firmware_id] = cleanHDIdentification($identification[1]);
  522. $hdparm_array[$disk_pass][$serial_id] = cleanHDIdentification($identification[2]);
  523. $hdparm_array[$disk_pass][$count_id] = getPartitionCountForDisk($partition_array, $disk_id);
  524. $hdparm_array[$disk_pass][$size_id] = $size_array[$key];
  525. }
  526. }
  527. } else {
  528. //A failed return value means the hard drive is most likely our USB drive
  529. //We need to create the values and put unknowns in there
  530. $hdparm_array[$disk_pass][$model_id] = 'Unknown (USB?)';
  531. $hdparm_array[$disk_pass][$firmware_id] = 'Unknown (USB?)';
  532. $hdparm_array[$disk_pass][$serial_id] = 'Unknown (USB?)';
  533. $hdparm_array[$disk_pass][$count_id] = getPartitionCountForDisk($partition_array, $disk_id);
  534. $hdparm_array[$disk_pass][$size_id] = $size_array[$key];
  535. }
  536. //Increment the disk counter
  537. $disk_pass++;
  538. }
  539. }
  540. return $hdparm_array;
  541. }
  542. function buildXMLRPCMessage($validation_array) {
  543. /*
  544. * We are going to take the validation array that has been submitted and
  545. * break it down into a valid XML RPC message to be sent to the web server
  546. * that will then code it and add it to the database
  547. */
  548. $parameters = array(XML_RPC_encode($validation_array));
  549. $message = new XML_RPC_Message('submitDiskWipe', $parameters);
  550. return $message;
  551. }
  552. function verifyDiskWipe() {
  553. /*
  554. * This function will take the information gathered and determine whether or
  555. * not the disk wipe validation passed or failed
  556. */
  557. global $validation_array;
  558. global $disk_array;
  559. global $partition_array;
  560. global $size_array;
  561. global $num_of_disks;
  562. //Set the default disk wipe to be true - only change if it actually fails
  563. $diskWipeVerified = 'PASSED';
  564. //This is the array that will hold just the display information
  565. $display_array = array();
  566. //Set the Sort Code
  567. $display_array['sort_code'] = $validation_array['sort_code'];
  568. //Set the machine serial number
  569. $display_array['machine_serial'] = $validation_array['machine_serial'];
  570. if ($num_of_disks > 0) {
  571. /*
  572. * There are disks available in the machine so let's parse to see
  573. * which ones are physical disks
  574. */
  575. for ($i = 1; $i <= $num_of_disks; $i++) {
  576. //Count through each disk to determine
  577. //Does this disk contain the string for a failed HDParm meaning it is our USB Drive
  578. if ((stripos($validation_array['disks'][$i]['disk_model'],'Unknown (USB?)')) === FALSE) {
  579. //This is a valid disk so we will pass back the information
  580. $diskID = 'DiskNo_' . $i;
  581. //Hand the serial number from the original validation array
  582. $display_array[$diskID]['SerialNumber'] = $validation_array['disks'][$i]['disk_serial'];
  583. //Hand the disk size from the original validation array
  584. $display_array[$diskID]['disk_size'] = $validation_array['disks'][$i]['disk_size'];
  585. //Hand the partition count from the original validation array
  586. $display_array[$diskID]['PartitionCount'] = $validation_array['disks'][$i]['disk_part_count'];
  587. //Validate if there are any partitions on the disk - then G-Disk failed on this disk
  588. $display_array[$diskID]['DiskWipeVerify'] = ($display_array[$diskID]['PartitionCount'] > 0) ? 'FAILED' : 'PASSED';
  589. //If the Gdisk failed on this disk, then update overall verification to FALSE
  590. if ($display_array[$diskID]['DiskWipeVerify'] === "FAILED") {
  591. $diskWipeVerified = 'FAILED';
  592. }
  593. }
  594. }
  595. $display_array['FullDiskWipeVerify'] = ($diskWipeVerified == 'FAILED') ? 'FAILED' : 'PASSED';
  596. } else {
  597. //No disks were found in the machine so we are going to return false so an error can be handled
  598. return FALSE;
  599. }
  600. //Return the array back to the calling function
  601. return $display_array;
  602. }
  603. function displayNormalMessage($display_array) {
  604. /*
  605. * This function will take an array input and display it on the screen using normal characters
  606. * and will not use the asterisk message except for overall failure
  607. */
  608. global $logFile;
  609. //Check to determine whether or not overall verification failed
  610. if ($display_array['FullDiskWipeVerify'] == 'FAILED') {
  611. //Overall verification failed so display the failure in asterisks
  612. displayAsteriskMessage("FAIL FAIL");
  613. //Write to the log file
  614. writeToLogFile("Overall Verification Failed", "1 or more disks had active partitions", $logFile);
  615. echo "\n";
  616. //Let's loop through the array and start displaying values
  617. foreach ($display_array as $key => $value) {
  618. if (stripos($key, "DiskNo_") !== FALSE) {
  619. //Start building the display line with the disk code
  620. $displayLine = "Disk Code: {$key} \n";
  621. //Let's loop through the Disk Sub-Array
  622. foreach ($value as $subKey => $subValue) {
  623. //Add each disk subkey to the current line and display the value
  624. $displayLine .= " {$subKey} = {$subValue} \n";
  625. }
  626. //Display the line on the screen
  627. echo $displayLine;
  628. //Continue the foreach loop
  629. continue;
  630. } else {
  631. //This isn't a disk so just display the key value pair
  632. $displayLine = "{$key} = {$value} \n";
  633. //Display the line on the screen
  634. echo $displayLine;
  635. //Continue the foreach loop
  636. continue;
  637. }
  638. }
  639. } elseif ($display_array['FullDiskWipeVerify'] == 'PASSED') {
  640. //Write to the log file
  641. writeToLogFile("Overall Verification Passed", "No disks had active partitions", $logFile);
  642. //Let's loop through the array and start displaying values
  643. foreach ($display_array as $key => $value) {
  644. if (stripos($key, "DiskNo_") !== FALSE) {
  645. //Start building the display line with the disk code
  646. $displayLine = "Disk Code: {$key} \n";
  647. //Let's loop through the Disk Sub-Array
  648. foreach ($value as $subKey => $subValue) {
  649. //Add each disk subkey to the current line and display the value
  650. $displayLine .= " {$subKey} = {$subValue}\n";
  651. }
  652. //Display the line on the screen
  653. echo $displayLine;
  654. //Continue the foreach loop
  655. continue;
  656. } else {
  657. //This isn't a disk so just display the key value pair
  658. $displayLine = "{$key} = {$value} \n";
  659. //Display the line on the screen
  660. echo $displayLine;
  661. //Continue the foreach loop
  662. continue;
  663. }
  664. }
  665. displayAsteriskMessage($display_array['machine_serial']);
  666. }
  667. }
  668. function fixPermissionsOnLogs() {
  669. /*
  670. * This function will go through and fix the permissions on the log files
  671. * this will allow the standard user to read them
  672. */
  673. global $sortCode;
  674. global $logFile;
  675. $folderToChange = dirname(__FILE__) . "/{$sortCode}";
  676. $commandToExecute = "sudo chown -R diskwipe:users {$folderToChange}";
  677. exec($commandToExecute, $chmodOutput, $chmodReturn);
  678. if ($chmodReturn === 0) {
  679. writeToLogFile("Chown Return \n", $chmodReturn, $logFile);
  680. return TRUE;
  681. } else {
  682. writeToLogFile("Chown Return \n", $chmodReturn, $logFile);
  683. return FALSE;
  684. }
  685. }
  686. //Clear the screen
  687. passthru('clear');
  688. //This is the first run of the get sort code routine just to start the process
  689. getSortCode();
  690. //Create the logfile to capture all the data
  691. if (! createLogFile() ) {
  692. //Log File failed to be created
  693. echo "Log file couldn't be opened - aborting!";
  694. exit;
  695. }
  696. //Show the user that we have started the data gathering process
  697. echo "Parsing data for sort code - {$sortCode} \n";
  698. writeToLogFile("Parsing Begin", "Parsing data for sort code - {$sortCode}", $logFile);
  699. //Set the sort code into the validation array
  700. $validation_array['sort_code'] = $sortCode;
  701. /*
  702. * Get the information from the DMIDECODE FUNCTION
  703. * that will allow us to get asset serial number
  704. */
  705. $chassis_serial_number = getChassisSerialNumber();
  706. $baseboard_serial_number = getBaseboardSerialNumber();
  707. $system_serial_number = getSystemSerialNumber();
  708. /*
  709. * Pass serial numbers to function to determine if any two of them match
  710. * if they do, then we will use that one as the actual serial number. If
  711. * not we will use the system serial number
  712. */
  713. determineValidSerialNumber($chassis_serial_number, $baseboard_serial_number, $system_serial_number);
  714. /*
  715. * Now we need to utilize the fdisk -l command to show me the
  716. * partitions that are available to this system
  717. */
  718. $fdisk_output = getFdiskInformation();
  719. /*
  720. * Here we will gather the physical disk information, physical disk size
  721. * information, and the virtual disk breakdowns by disk
  722. */
  723. $disk_array = getPhysicalDiskBaseInformation($fdisk_output);
  724. $size_array = getPhysicalDiskSizeInformation($fdisk_output);
  725. $partition_array = getVirtualDiskInformation($fdisk_output);
  726. //See how many disks were found
  727. $num_of_disks = count($disk_array);
  728. $validation_array['total_disk_count'] = $num_of_disks;
  729. if ($num_of_disks > 0) {
  730. //Found some available disks so let's tell us about them
  731. writeToLogFile("Disk Array Information", "There are {$num_of_disks} disk(s) in this system", $logFile);
  732. writeToLogFile("", $disk_array, $logFile);
  733. }
  734. //See how many partitions were found
  735. $num_of_parts = count($partition_array);
  736. $validation_array['total_partition_count'] = $num_of_parts;
  737. if ($num_of_parts > 0) {
  738. //Found some partitions so let's tell us about them
  739. writeToLogFile("Partition Array Information", "There are {$num_of_parts} partition(s) in this system", $logFile);
  740. writeToLogFile("", $partition_array, $logFile);
  741. }
  742. //Log the size array information to the file
  743. writeToLogFile("Size Array Information", $size_array, $logFile);
  744. /*
  745. * Let's get the physical disk information and insert it into our array
  746. */
  747. $validation_array['disks'] = getPhysicalDiskParamaters($disk_array, $partition_array, $size_array);
  748. /*
  749. * Initialize our RPC client object pasing in all appropriate variables
  750. */
  751. $client = new XML_RPC_Client(xml_path, xml_server, xml_port, xml_proxy, xml_proxy_port, xml_proxy_user, xml_proxy_pass);
  752. /*
  753. * Turn on debugging
  754. */
  755. //$client->setDebug(1);
  756. /*
  757. * Create a response object to catch the information returning from the XML_RPC
  758. *
  759. */
  760. $response = $client->send(buildXMLRPCMessage($validation_array));
  761. writeToLogFile("Message Object", buildXMLRPCMessage($validation_array), $logFile);
  762. writeToLogFile("Response Object", $response, $logFile);
  763. writeToLogFile("Validation Array", $validation_array, $logFile);
  764. writeToLogFile("Fdisk Output", $fdisk_output, $logFile);
  765. writeToLogFile("Chassis Serial", $chassis_serial_number, $logFile);
  766. writeToLogFile("Baseboard Serial", $baseboard_serial_number, $logFile);
  767. writeToLogFile("System Serial", $system_serial_number, $logFile);
  768. if (isset($sda_return)) {
  769. if ($sda_return == 0) {
  770. writeToLogFile("SDA HDPARM A", $sda_hdparm_output, $logFile);
  771. writeToLogFile("SDA RETURN A", $sda_return, $logFile);
  772. }
  773. }
  774. if (isset($sdb_return)) {
  775. if ($sdb_return == 0) {
  776. writeToLogFile("SDA HDPARM B", $sdb_hdparm_output, $logFile);
  777. writeToLogFile("SDA RETURN B", $sdb_return, $logFile);
  778. }
  779. }
  780. if (isset($sdc_return)) {
  781. if ($sdc_return == 0) {
  782. writeToLogFile("SDA HDPARM C", $sdc_hdparm_output, $logFile);
  783. writeToLogFile("SDA RETURN C", $sdc_return, $logFile);
  784. }
  785. }
  786. //Clear the screen
  787. passthru('clear');
  788. //Get the display message to be sent to the screen
  789. $display_array = verifyDiskWipe();
  790. //Now display the message on the screen
  791. displayNormalMessage($display_array);
  792. fixPermissionsOnLogs();
  793. closeLogFile($logFile);
  794. /*
  795. * End of file: DiskWipeVerify.php
  796. * Location: /DiskWipeVerify.php
  797. */
  798. ?>