ulvis.paste.net

Paste Search Dynamic
Recent pastes
csv
  1. <?php
  2.  
  3. $locale_db_host = 'localhost';
  4. $locale_db_name = 'asterisk';
  5. $locale_db_login = 'root';
  6. $locale_db_pass = '';
  7. $logfile = "/var/log/asterisk/cdr-csv/Master.csv";
  8. $linkmb = mysql_connect($locale_db_host, $locale_db_login, $locale_db_pass) or die("Could not connect : " . mysql_error());
  9.  
  10. mysql_select_db($locale_db_name, $linkmb) or die("Could not select database $locale_db_name");
  11.  
  12. $sql="SELECT UNIX_TIMESTAMP(calldate) as calldate FROM cdr ORDER BY calldate DESC LIMIT 1";
  13.  
  14. if(!($result = mysql_query($sql, $linkmb))) {
  15. print("Invalid query: " . mysql_error()."\n");
  16. print("SQL: $sql\n");
  17. die();
  18. }
  19.  
  20. $result_array = mysql_fetch_array($result);
  21. $lasttimestamp = $result_array['calldate'];
  22. $rows = 0;
  23. $handle = fopen($logfile, "r");
  24.  
  25. while ( ($data = fgetcsv($handle, 1000, ",") ) !== false) {
  26. list($accountcode, $src, $dst, $dcontext, $clid, $channel, $dstchannel, $lastapp,
  27. $lastdata, $start, $answer, $end, $duration, $billsec, $disposition,
  28. $amaflags, $uniqueid, $userfield ) = $data;
  29. if (strtotime($end) > $lasttimestamp) {
  30. $sql = "INSERT INTO cdr (calldate, clid, src, dst, dcontext, channel, dstchannel, lastapp, lastdata, duration, billsec, disposition, amaflags, accountcode, uniq
  31. if(!($result2 = mysql_query($sql, $linkmb))) {
  32. print("Invalid query: " . mysql_error()."\n");
  33. print("SQL: $sql\n");
  34. die();
  35. }
  36. $rows++;
  37.   }
  38. }
  39. fclose($handle);
  40. print("$rows imported\n");
  41. ?>
  42.  
Parsed in 0.034 seconds