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

Report this snippet  

You need to login to post a comment.