Commit e390f1b3 authored by shajiaiming's avatar shajiaiming

es limit

parent c63168a3
<?php
namespace common\service\es;
use Elasticsearch\ClientBuilder;
class Elastic
{
protected $host;
protected $client;
public function __construct($host = null)
{
$this->host = $host;
}
public function connect()
{
$this->client = ClientBuilder::create()->setHosts([$this->host])
->build();
}
public function search($params)
{
$response = $this->client->search($params);
return $response;
}
}
\ No newline at end of file
<?php
namespace console\controllers;
use yii\console\Controller;
use common\service\es\Elastic;
class IpExceptionController extends Controller
{
public function actionIndex()
{
$now = time();
$elastic = new Elastic('106.15.39.94:9200');
$params = [
'index' => 'bwallet_access',
'type' => 'doc',
'body' => [
"size" => 0,
"aggs" => [
"ips" => [
"terms" => [
"field" => "clientip.keyword",
"size" => 200,
"order" => [
"_count" => "desc"
]
],
"aggs" => [
"request" => [
"significant_terms" => [
"field" => "request.keyword",
"size" => 10,
]
]
]
]
],
"query" => [
"bool" => [
"must" => [
[
"range" => [
"@timestamp" => [
#"gte" => 1574922900000,
#"lt" => 1574923200000,
'gte' => ($now - 300) * 1000,
'lt' => $now * 1000,
"format" => "epoch_millis"
]
]
]
]
],
]
]
];
$elastic->connect();
$response = $elastic->search($params);
$hits = $response['hits'] ?? null;
$total = $hits['total'];
if(empty($total)){
return false;
}
$buckets = $response['aggregations']['ips']['buckets'] ?? null;
if(empty($buckets)){
return false;
}
$ips = '';
foreach ($buckets as $key => $val){
if ($val['doc_count'] > 100){
$ips.= $val['key'].',';
}
}
$ips = substr($ips, 0, -1);
if (!empty($ips)){
$file = fopen("http://47.100.212.109/api_ip_limit.txt","w");
$ip_arr = explode(',', $ips);
foreach ($ip_arr as $key => $ip){
fwrite($file,$ip."\n");
}
fclose($file);
}
return false;
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment