<?php
error_reporting( E_ALL&~E_NOTICE );
define('ELK_QUERY_RR_QUERY', '{
"query": {
"filtered": {
"query": {
"query_string": {
"analyze_wildcard": true,
"query": "type:phptestkv AND report_type:\"passport.login\""
}
},
"filter": {
"bool": {
"must": [
{
"range": {
"@timestamp": {
"gte": "now-15m"
}
}
}
],
"must_not": []
}
}
}
},
"size": 0,
"aggs": {
"1": {
"terms": {
"field": "success",
"size": 5,
"order": {
"_count": "desc"
}
}
}
}
}
}');
define('ELK_QUERY_RR_URL', 'http://localhost:9200/logstash-phptest-*/_search?search_type=count&pretty');
function curl_post($url, $content, $header=''){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $content);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$tmpInfo = curl_exec($curl);
if (curl_errno($curl))
{
print("[error] CURL ERROR: ".curl_error($curl)."\r\n");
}
curl_close($curl);
return $tmpInfo;
}
function Query_Curl(){
$request_params_rr_query = ELK_QUERY_RR_QUERY;
$rr_target =curl_post(ELK_QUERY_RR_URL, $request_params_rr_query, 'Authorization:Basic bmVpZ291OjE5dkpDYzdCUUltbw==');
return get_rr_value($rr_target);
}
function get_rr_value($results){
$results_object = json_decode($results, true);
return $results_object;
}
function query(){
$result=Query_Curl();
$doc_count=$result['aggregations']['1']['buckets'][0]['doc_count'];
$total=$result['hits']['total'];
$ratio=0;
if($total>=10){
if($doc_count && $total){
$ratio=round($doc_count/$total, 2);
}
if($result['aggregations']['1']['buckets'][0]['key']==1){
if($ratio){
$ratio=1-($ratio);
}
}
}
return $ratio;
}
$ratio=query();
echo $ratio;
?>
本文出自 “触手可及” 博客,转载请与作者联系!