php實現清除RabbitMQ裡面過期沒有資料的連線
阿新 • • 發佈:2018-12-17
<?php /** * Created by PhpStorm. * User: lichanghua * Date: 2018/6/15 * Time: 9:27 */ $server_ip = '192.168.33.10'; $data['user'] = 'admin'; $data['password'] = 'admin'; //$data['user'] = 'test'; //$data['password'] = 'test123'; $authorization = "Basic YWRtaW46YWRtaW4="; $list = getAllRequest($server_ip, $data, $authorization); //echo "<pre>"; //print_r($list); if(!$list){ exit("list empty nothing to do.\n"); } $connections = []; foreach($list as $item){ $connections[] = $item['name']; } //print_r($connections); if(!$connections){ exit("connections empty nothing to do.\n"); } $list_url = []; foreach($connections as $item2){ $send_ip = trim(explode(':',(explode("->",$item2)[0]))[0]); $send_port = trim(explode(':',(explode("->",$item2)[0]))[1]); $receive_ip = trim(explode(':',(explode("->",$item2)[1]))[0]); $url = 'http://'.$server_ip.':15672/api/connections/'.$send_ip.'%3A'.$send_port.'%20-%3E%20'.$receive_ip.'%3A5672?data_rates_age=3600&data_rates_incr=60'; $list_url[] = $url; } //print_r($list_url); if(!$list_url){ exit("list_url empty nothing to do.\n"); } $url_list1 = []; foreach($list_url as $item3){ $result = curl_get_http_auth($item3, $authorization); //print_r($result); if($result){ echo deleteRequest($result, $server_ip, $authorization); } } echo "done..."."\r\n"; function getAllRequest($server_ip, $data){ $url = 'http://'.$server_ip.':15672/api/connections?user='.$data['user'].'&password='.$data['password']; return curl_get_http($url, $data); } function deleteRequest($receive, $server_ip, $authorization){ $n = 2; $recv_avg_rate = $receive['recv_oct_details']['avg_rate']; $n+= $recv_avg_rate; $send_avg_rate = $receive['send_oct_details']['avg_rate']; $n+= $send_avg_rate; //echo "n == ".$n."\r\n"; if($n == 2) { $name = $receive['name']; echo "name == ".$name."\r\n"; $send_ip = trim(explode(':',(explode("->",$name)[0]))[0]); $send_port = trim(explode(':',(explode("->",$name)[0]))[1]); $receive_ip = trim(explode(':',(explode("->",$name)[1]))[0]); $api = 'http://'.$server_ip.':15672/api/connections/'.$send_ip.'%3A'.$send_port.'%20-%3E%20'.$receive_ip.'%3A5672'; //echo "api == ".$api."\r\n"; $code = curl_get_http_delete($api, $authorization); //print_r($code); if($code['status_code'] == 204){ return "{0}刪除成功".$send_port."\r\n"; } else { return "{0}刪除失敗".$send_port."\r\n"; } } } function curl_get_http($url, $data){ $header = array(); $header[] = "Content-Type:application/json"; $header[] = "Authorization:Basic ".base64_encode($data['user'].":".$data['password']); print_r($header); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); $output = curl_exec($ch); if (curl_errno($ch)) { print_r(curl_error($ch)); } curl_close($ch); return json_decode($output, true); } function curl_get_http_auth($url, $authorization){ $header = array(); $header[] = "Content-Type:application/json"; $header[] = "Authorization:".$authorization; //print_r($header); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); $output = curl_exec($ch); if (curl_errno($ch)) { print_r(curl_error($ch)); } curl_close($ch); return json_decode($output, true); } function curl_get_http_delete($url,$authorization){ $header = array(); $header[] = "Content-Type:application/json"; $header[] = "Authorization: ".$authorization; //print_r($header); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); //curl_setopt($ch, CURLOPT_POST, TRUE); $output = curl_exec($ch); //echo "output == "."\r\n"; //print_r($output); //echo "status == "."\r\n"; //$status = curl_getinfo($ch); //print_r($status); if (curl_errno($ch)) { print_r(curl_error($ch)); } curl_close($ch); return json_decode($output, true); }