Posted By

TheJasonParker on 01/17/11


Tagged

magento


Versions (?)

Magento Log File Contents Report


 / Published in: PHP
 

URL: http://www.magentocommerce.com/boards/viewthread/51142/P90/#t271250

And a now, word on the log.php maintenance script that comes with Magento 1.4 which has one major shortcoming.

It doesn’t maintain your dataflowbatch* tables, said tables have been seen hiding in the wild at over a Gigabyte in size.

php -f ./shell/log.php help gives you a listing of the various options

php -f ./shell/log.php status gives you a listing of the log tables, how many rows and the size of storage space contained in the tables and indexes.

php -f ./shell/log.php clean --days 15 will clear all but the last 15 days of log contents (minimum 1 day)

php -f ./shell/log.php clean clears everthing per your settings in system setups under log maintenance

  1. <?php
  2.  
  3. /***************************************************
  4.  * Magento Log File Contents Monitor. GNU/GPL
  5.  * provided without warranty or support
  6.  ***************************************************/
  7.  
  8. /***********************
  9.  * Scan Magento local.xml file for connection information
  10.  ***********************/
  11.  
  12. if (file_exists('./app/etc/local.xml')) {
  13.  
  14. $xml = simplexml_load_file('./app/etc/local.xml');
  15.  
  16. $tblprefix = $xml->global->resources->db->table_prefix;
  17. $dbhost = $xml->global->resources->default_setup->connection->host;
  18. $dbuser = $xml->global->resources->default_setup->connection->username;
  19. $dbpass = $xml->global->resources->default_setup->connection->password;
  20. $dbname = $xml->global->resources->default_setup->connection->dbname;
  21.  
  22. $tables = array(
  23. 'dataflow_batch_export',
  24. 'dataflow_batch_import',
  25. 'log_customer',
  26. 'log_quote',
  27. 'log_summary',
  28. 'log_summary_type',
  29. 'log_url',
  30. 'log_url_info',
  31. 'log_visitor',
  32. 'log_visitor_info',
  33. 'log_visitor_online',
  34. 'report_event'
  35. );
  36.  
  37. }
  38.  
  39. else {
  40. exit('Failed to open ./app/etc/local.xml');
  41. }
  42.  
  43. /** Get current date, time, UTC and offset **/
  44.  
  45. $date = date("Y-m-d");
  46. $time = date("H:i:s T");
  47. $offset = date("P");
  48. $utc = gmdate("H:i:s");
  49.  
  50. /***********************
  51.  * Start HTML output
  52.  ***********************/
  53.  
  54. echo "<html><head><title>Magento Log File Contents on " . $date . " " . $time . "</title>
  55. <meta http-equiv=\"refresh\" content=\"30\">
  56. <style type=\"text/css\">html {width: 100%; font-family: Arial,Helvetica, sans-serif;}
  57. body {line-height:1.0em; font-size: 100%;}
  58. table {border-spacing: 1px;}
  59. th.stattitle {text-align: left; font-size: 100%; font-weight: bold; color: white; background-color: #101010;}
  60. th {text-align: center; font-size: 90%; font-weight: bold; padding: 5px; border-bottom: 1px solid black; border-left: 1px solid black; }
  61. td {font-size: 90%; padding: 4px; border-bottom: 1px solid black; border-left: 1px solid black;}
  62. </style>
  63. </head><body>";
  64.  
  65. /** Output title, connection info and cron job monitor runtime **/
  66.  
  67. echo "<h2>Magento Log File Contents Report</h2><h3>Connection: ".$dbuser."@".$dbhost."&nbsp;&nbsp;&ndash;&nbsp;&nbsp;Database: " . $dbname . "</h3>";
  68. echo "<h3>Runtime: " . $date . "&nbsp;&nbsp;&nbsp;" .$time . "&nbsp;&nbsp;&nbsp;" . $utc . " UTC</h3>";
  69. echo "<h4>Note: Your timezone offset is " . $offset . " hours from UTC</h4>";
  70.  
  71. /** Connect to database **/
  72.  
  73. $conn = mysql_connect($dbhost,$dbuser,$dbpass);
  74. @mysql_select_db($dbname) or die("Unable to select database");
  75.  
  76. /***********************
  77.  * Get table record counts
  78.  ***********************/
  79.  
  80. echo '<table><tbody><tr><th class="stattitle" colspan="2">Log Tables</th></tr>';
  81. echo '<tr><th>Table</th><th>Row Count</th></tr>';
  82.  
  83. foreach($tables as $tblname) {
  84. $result = mysql_query("SELECT COUNT(*) FROM " . $tblprefix . $tblname) or die(mysql_error());
  85. $numrows = mysql_fetch_array($result);
  86. $num = $numrows[0];
  87.  
  88. /* Table output */
  89. echo '<tr>';
  90. echo '<td>'.$tblprefix.$tblname.'</td>';
  91. echo '<td align="right">'.$num."</td>";
  92. echo '</tr>';
  93. }
  94.  
  95. echo '</tbody></table></body></html>';
  96.  
  97. /***********************
  98.  * End of report
  99.  ***********************/
  100.  
  101. mysql_close($conn);
  102. ?>

Report this snippet  

You need to login to post a comment.