Created
September 11, 2021 03:24
-
-
Save nickfox-taterli/89c16ed372b4088d8b346ffe177b9e12 to your computer and use it in GitHub Desktop.
PHP + Go 采集器
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers; | |
use DB; | |
use Carbon\Carbon; | |
use Illuminate\Http\Request; | |
class AgentController extends Controller | |
{ | |
public function index(){ | |
return view('welcome'); | |
} | |
public function feed(Request $request){ | |
// 采集主流程 | |
$net_name = ['eth','ens','enp','ven'/*venet*/]; | |
$net_info = $request->net['net_info']; | |
foreach ( $net_info as $ni ) { | |
foreach ( $net_name as $n ) { | |
if(substr($ni['name'],0,3) === $n){ | |
$bytesSent = $ni['bytesSent']; | |
$bytesRecv = $ni['bytesRecv']; | |
$network_interface = $ni['name']; | |
} | |
} | |
} | |
$feeds = DB::table('feeds')->where('ip_address', '=', $request->ip())->orderBy('id', 'desc')->first(); | |
if($feeds == null){ | |
$net_tx_speed = 0; | |
$net_rx_speed = 0; | |
}else{ | |
$diff = Carbon::parse($feeds->created_at)->floatDiffInSeconds(Carbon::now()); | |
$net_tx_speed = ($bytesSent - $feeds->network_sent) / $diff; | |
$net_rx_speed = ($bytesSent - $feeds->network_sent) / $diff; | |
} | |
// 数据入库 | |
DB::table('feeds')->insertGetId( | |
[ | |
'cpu' => $request->cpu['cpu_info'][0]['modelName'], | |
'cpu_precent' => $request->cpu['cpu_precent'], | |
'network_interface' => $network_interface, | |
'network_sent' => $bytesSent, | |
'network_recv' => $bytesRecv, | |
'net_tx_speed' => $net_tx_speed, | |
'net_rx_speed' => $net_rx_speed, | |
'tcp_conn_count' => $request->net['tcp_conn_count'], | |
'udp_conn_count' => $request->net['udp_conn_count'], | |
'disk_total' => $request->disk['disk_total'], | |
'disk_used' => $request->disk['disk_used'], | |
'uptime' => $request->host['uptime'], | |
'load1' => $request->load['load1'], | |
'load5' => $request->load['load5'], | |
'load15' => $request->load['load15'], | |
'mem_total' => $request->mem['mem_info']['total'], | |
'mem_used' => $request->mem['mem_info']['used'], | |
'swap_total' => $request->mem['swap_info']['total'], | |
'swap_used' => $request->mem['swap_info']['used'], | |
'ip_address' => $request->ip(), | |
'created_at' => now(), | |
'updated_at' => now(), | |
'raw' => $request->getContent() | |
] | |
); | |
// 判断最近10次数据是否网速不达标. | |
$feeds = DB::table('feeds')->where('ip_address', '=', $request->ip())->orderBy('id', 'desc')->skip(1)->take(10)->get(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment