I currently have a script that fetches data from my MikroTik router and then inserts it into my database but It creates thousands and thousands of rows.
Is there anyway to combine the data per src and dst IP address and then insert it into the database or is there a better solution to store accounting data.
My server runs a cron job every 5 minutes to run the script.
All my accounting data automatically gets a time stamp by sql when it is inserted into the database (CURRENT_TIMESTAMP).
Here is my current script:
#!/usr/bin/env php
<?php
//Include needed files
require 'config.php';
require ("html/includes/routeros_api.class.php");
require ("html/includes/formatbytes.php");
require ("html/includes/converttobytes.php");
//Set ini
ini_set ("expose_php",$ExposePHP);
ini_set ("display_errors",$DisplayErrors);
ini_set ("max_execution_time","90000");
//UPDATE ACCOUNTING
//Connect to database
$conn = mysqli_connect($SQLserver, $SQLusername, $SQLpassword, $SQLdatabase);
if (!$conn) {
//die("Could not connect: " . mysqli_connect_error());
die("Error Connecting to database!");
}
//Fetch accounting data from router
$routerconnect = "http://$RouterIP:$WebPort/$AccountingLocation";
$orig = file_get_contents($routerconnect);
foreach ( explode( "\n", $orig ) as $line ) {
if ( trim( $line )) {
list( $src, $dst, $bytes, $packets ) = explode( ' ', trim( strip_tags( $line )));
//Create Insert query for accounting data
$sql = "INSERT INTO ip_accounting (src_address, dst_address, bytes, packets) VALUES ('$src', '$dst', '$bytes', '$packets')";
//Execute query
$result = mysqli_query($conn,$sql);
}
}
echo ("Accounting updated!");
?>