PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/apps/appserv/lib/phpsysinfo/plugins/SMART/class.SMART.inc.php

http://openbiz-cubi.googlecode.com/
PHP | 233 lines | 169 code | 8 blank | 56 comment | 35 complexity | 7c6a3c2619091cc6430e2e5aa3f735a5 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-3.0
  1. <?php
  2. /**
  3. * SMART Plugin
  4. *
  5. * PHP version 5
  6. *
  7. * @category PHP
  8. * @package PSI_Plugin_SMART
  9. * @author Antoine Bertin <diaoulael@users.sourceforge.net>
  10. * @copyright 2009 phpSysInfo
  11. * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
  12. * @version SVN: $Id: class.SMART.inc.php 522 2011-11-08 18:21:09Z jacky672 $
  13. * @link http://phpsysinfo.sourceforge.net
  14. */
  15. /**
  16. * SMART plugin, which displays all SMART informations available
  17. *
  18. * @category PHP
  19. * @package PSI_Plugin_SMART
  20. * @author Antoine Bertin <diaoulael@users.sourceforge.net>
  21. * @copyright 2009 phpSysInfo
  22. * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
  23. * @version Release: 3.0
  24. * @link http://phpsysinfo.sourceforge.net
  25. */
  26. class SMART extends PSI_Plugin
  27. {
  28. /**
  29. * variable, which holds the content of the command
  30. * @var array
  31. */
  32. private $_filecontent = array();
  33. /**
  34. * variable, which holds the result before the xml is generated out of this array
  35. * @var array
  36. */
  37. private $_result = array();
  38. /**
  39. * variable, which holds PSI_PLUGIN_SMART_IDS well formated datas
  40. * @var array
  41. */
  42. private $_ids = array();
  43. /**
  44. * read the data into an internal array and also call the parent constructor
  45. *
  46. * @param String $enc target encoding
  47. */
  48. public function __construct($enc)
  49. {
  50. parent::__construct(__CLASS__, $enc);
  51. switch (PSI_PLUGIN_SMART_ACCESS) {
  52. case 'command':
  53. $disks = preg_split('/([\s]+)?,([\s]+)?/', PSI_PLUGIN_SMART_DEVICES, -1, PREG_SPLIT_NO_EMPTY);
  54. foreach ($disks as $disk) {
  55. $buffer = "";
  56. if (CommonFunctions::executeProgram('smartctl', '--all'.((PSI_PLUGIN_SMART_DEVICE) ? ' --device '.PSI_PLUGIN_SMART_DEVICE : '').' '.$disk, $buffer, PSI_DEBUG)) {
  57. $this->_filecontent[$disk] = $buffer;
  58. }
  59. }
  60. $fullIds = preg_split('/([\s]+)?,([\s]+)?/', PSI_PLUGIN_SMART_IDS, -1, PREG_SPLIT_NO_EMPTY);
  61. foreach ($fullIds as $fullId) {
  62. $arrFullId = preg_split('/-/', $fullId);
  63. $this->_ids[intval($arrFullId[0])] = strtolower($arrFullId[1]);
  64. }
  65. break;
  66. case 'data':
  67. $disks = preg_split('/([\s]+)?,([\s]+)?/', PSI_PLUGIN_SMART_DEVICES, -1, PREG_SPLIT_NO_EMPTY);
  68. $dn=0;
  69. foreach ($disks as $disk) {
  70. $buffer="";
  71. if ((CommonFunctions::rfts(APP_ROOT."/data/SMART{$dn}.txt", $buffer))&&(!empty($buffer))){
  72. $this->_filecontent[$disk] = $buffer;
  73. }
  74. $dn++;
  75. }
  76. $fullIds = preg_split('/([\s]+)?,([\s]+)?/', PSI_PLUGIN_SMART_IDS, -1, PREG_SPLIT_NO_EMPTY);
  77. foreach ($fullIds as $fullId) {
  78. $arrFullId = preg_split('/-/', $fullId);
  79. $this->_ids[intval($arrFullId[0])] = strtolower($arrFullId[1]);
  80. }
  81. break;
  82. default:
  83. $this->global_error->addError("switch(PSI_PLUGIN_SMART_ACCESS)", "Bad SMART configuration in SMART.config.php");
  84. break;
  85. }
  86. }
  87. /**
  88. * doing all tasks to get the required informations that the plugin needs
  89. * result is stored in an internal array
  90. *
  91. * @return void
  92. */
  93. public function execute()
  94. {
  95. if ( empty($this->_filecontent) || empty($this->_ids)) {
  96. return;
  97. }
  98. foreach ($this->_filecontent as $disk=>$result) {
  99. preg_match('/Vendor Specific SMART Attributes with Thresholds\:\n(.*)\n((.|\n)*)\n\nSMART Error Log Version\:/', $result, $vendorInfos);
  100. if (!empty($vendorInfos)) {
  101. $labels = preg_split('/[\s]+/', $vendorInfos[1]);
  102. foreach ($labels as $k=>$v) {
  103. $labels[$k] = str_replace('#', '', strtolower($v));
  104. }
  105. $lines = preg_split('/\n/', $vendorInfos[2]);
  106. $i = 0; // Line number
  107. foreach ($lines as $line) {
  108. $line = preg_replace('/^[\s]+/', '', $line);
  109. $values = preg_split('/[\s]+/', $line);
  110. if (count($values) > count($labels)) {
  111. $values = array_slice($values, 0, count($labels), true);
  112. }
  113. $j = 0;
  114. $found = false;
  115. foreach ($values as $value) {
  116. if (((in_array($value, array_keys($this->_ids)) && $labels[$j] == 'id') || ($found && (in_array($labels[$j], array_values($this->_ids)))) || ($found && $labels[$j] == 'attribute_name'))) {
  117. $this->_result[$disk][$i][$labels[$j]] = $value;
  118. $found = true;
  119. }
  120. $j++;
  121. }
  122. $i++;
  123. }
  124. } else {
  125. //SCSI devices
  126. if ((!empty($this->_ids[1]))&&($this->_ids[1]=="raw_value")) {
  127. preg_match('/read\: (.*)\n/', $result, $lines);
  128. if ((!empty($lines))&&(!empty($lines[0]))) {
  129. $values=preg_split('/[\s,]+/',$lines[0]);
  130. if ((!empty($values))&&($values[7]!=null)){
  131. $this->_result[$disk][0]['id'] = 1;
  132. $this->_result[$disk][0]['attribute_name'] = "Raw_Read_Error_Rate";
  133. $this->_result[$disk][0]['raw_value'] = $values[7];
  134. }
  135. }
  136. }
  137. if ((!empty($this->_ids[5]))&&($this->_ids[5]=="raw_value")) {
  138. preg_match('/Elements in grown defect list\: (.*)\n/', $result, $lines);
  139. if ((!empty($lines))&&(!empty($lines[0]))) {
  140. $values=preg_split('/[\s,]+/',$lines[0]);
  141. if ((!empty($values))&&($values[5]!=null)){
  142. $this->_result[$disk][1]['id'] = 5;
  143. $this->_result[$disk][1]['attribute_name'] = "Reallocated_Sector_Ct";
  144. $this->_result[$disk][1]['raw_value'] = $values[5];
  145. }
  146. }
  147. }
  148. if ((!empty($this->_ids[9]))&&($this->_ids[9]=="raw_value")) {
  149. preg_match('/ number of hours powered up = (.*)\n/', $result, $lines);
  150. if ((!empty($lines))&&(!empty($lines[0]))) {
  151. $values=preg_split('/[\s,.]+/',$lines[0]);
  152. if ((!empty($values))&&($values[7]!=null)){
  153. $this->_result[$disk][2]['id'] = 9;
  154. $this->_result[$disk][2]['attribute_name'] = "Power_On_Hours";
  155. $this->_result[$disk][2]['raw_value'] = $values[7];
  156. }
  157. }
  158. }
  159. if ((!empty($this->_ids[194]))&&($this->_ids[194]=="raw_value")) {
  160. preg_match('/Current Drive Temperature\: (.*)\n/', $result, $lines);
  161. if ((!empty($lines))&&(!empty($lines[0]))) {
  162. $values=preg_split('/[\s,]+/',$lines[0]);
  163. if ((!empty($values))&&($values[3]!=null)){
  164. $this->_result[$disk][3]['id'] = 194;
  165. $this->_result[$disk][3]['attribute_name'] = "Temperature_Celsius";
  166. $this->_result[$disk][3]['raw_value'] = $values[3];
  167. }
  168. }
  169. }
  170. }
  171. }
  172. //Usage test
  173. $newIds = array();
  174. foreach ($this->_ids as $id=>$column_name) {
  175. $found = 0;
  176. foreach ($this->_result as $diskName=>$diskInfos) {
  177. if ($found!=2) foreach ($diskInfos as $lineInfos) {
  178. if ($found!=2) {
  179. $found = 0;
  180. foreach ($lineInfos as $label=>$value) {
  181. if (($found==0)&&($label=="id")&&($value==$id))
  182. $found = 1;
  183. if (($found==1)&&($label==$column_name))
  184. $found = 2;
  185. }
  186. }
  187. }
  188. }
  189. if ($found==2) $newIds[$id] = $this->_ids[$id];
  190. }
  191. $this->_ids = $newIds;
  192. }
  193. /**
  194. * generates the XML content for the plugin
  195. *
  196. * @return SimpleXMLObject entire XML content for the plugin
  197. */
  198. public function xml()
  199. {
  200. if ( empty($this->_result) || empty($this->_ids)) {
  201. return $this->xml->getSimpleXmlElement();
  202. }
  203. $columnsChild = $this->xml->addChild('columns');
  204. // Fill the xml with preferences
  205. foreach ($this->_ids as $id=>$column_name) {
  206. $columnChild = $columnsChild->addChild('column');
  207. $columnChild->addAttribute('id', $id);
  208. $columnChild->addAttribute('name', $column_name);
  209. }
  210. $disksChild = $this->xml->addChild('disks');
  211. // Now fill the xml with S.M.A.R.T datas
  212. foreach ($this->_result as $diskName=>$diskInfos) {
  213. $diskChild = $disksChild->addChild('disk');
  214. $diskChild->addAttribute('name', $diskName);
  215. foreach ($diskInfos as $lineInfos) {
  216. $lineChild = $diskChild->addChild('attribute');
  217. foreach ($lineInfos as $label=>$value) {
  218. $lineChild->addAttribute($label, $value);
  219. }
  220. }
  221. }
  222. return $this->xml->getSimpleXmlElement();
  223. }
  224. }
  225. ?>