我目前有一个脚本从我的MikroTik路由器获取数据,然后将其插入我的数据库,但它创建了成千上万的行。
无论如何要将每个src和dst IP地址的数据组合起来,然后将其插入到数据库中,或者是否有更好的存储会计数据的解决方案。
我的服务器每5分钟运行一次cron作业来运行脚本。
我的所有会计数据在插入数据库(CURRENT_TIMESTAMP)时会自动获取sql的时间戳。
这是我目前的剧本:
#!/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!");
?>
答案 0 :(得分:0)
我已更新代码以使用其他方法,而不是将IP记帐数据存储在表中,而是先对数据进行记帐,然后在traffic_counters
表中进行更新
$API->write('/ip/accounting/snapshot/take',true);
$READ = $API->read(false);
$ARRAY = $API->parseResponse($READ);
$API->write('/ip/accounting/snapshot/print',true);
$READ = $API->read(false);
$ARRAY = $API->parseResponse($READ);
foreach($ARRAY as $ACCOUNTING) {
$ip_src = $ACCOUNTING['src-address'];
$ip_dst = $ACCOUNTING['dst-address'];
$bytes = $ACCOUNTING['bytes'];
if(isset($config['debug']) AND ($config['debug'] == true)) {
echo "Checking $ip_src -> $ip_dst bytes: $bytes\n";
}
//Check if ip in use UPLOAD
$query = "SELECT service_id, ip_address FROM service_info WHERE ip_address='$ip_src' AND deleted='0'";
$result = mysqli_query($mysql_connection, $query);
$row = mysqli_fetch_array($result);
if(mysqli_num_rows($result) > 0) {
$service_id = $row['service_id'];
//Update Download Traffic
$check_if_exist_query = "SELECT * FROM traffic_counters WHERE service_id='$service_id' AND date=CURRENT_DATE()";
$check_result = mysqli_query($mysql_connection, $check_if_exist_query);
$check_num_rows = mysqli_num_rows($check_result);
if($check_num_rows == 0) {
$add_query = "INSERT INTO traffic_counters (service_id, upload_bytes, date) VALUES ('$service_id', '$bytes', CURRENT_DATE());";
$add_result = mysqli_query($mysql_connection, $add_query);
} else {
$update_query = "UPDATE traffic_counters SET
upload_bytes = upload_bytes + $bytes
WHERE service_id='$service_id' AND date=CURRENT_DATE();
";
$update_result = mysqli_query($mysql_connection, $update_query);
}
}
//Check if ip in use DOWNLOAD
$query = "SELECT service_id, ip_address FROM service_info WHERE ip_address='$ip_dst' AND deleted='0'";
$result = mysqli_query($mysql_connection, $query);
$row = mysqli_fetch_array($result);
if(mysqli_num_rows($result) > 0) {
$service_id = $row['service_id'];
//Update Download Traffic
$check_if_exist_query = "SELECT * FROM traffic_counters WHERE service_id='$service_id' AND date=CURRENT_DATE()";
$check_result = mysqli_query($mysql_connection, $check_if_exist_query);
$check_num_rows = mysqli_num_rows($check_result);
if($check_num_rows == 0) {
$add_query = "INSERT INTO traffic_counters (service_id, download_bytes, date) VALUES ('$service_id', '$bytes', CURRENT_DATE());";
$add_result = mysqli_query($mysql_connection, $add_query);
} else {
$update_query = "UPDATE traffic_counters SET
download_bytes = download_bytes + $bytes
WHERE service_id='$service_id' AND date=CURRENT_DATE();
";
$update_result = mysqli_query($mysql_connection, $update_query);
}
}
}
$API->disconnect();