前言
最近老被打,恶心死了;又没钱搞防护,就写个脚本预警一下。。。
又拍云:
计算当天流量达到
老薛主机:判断流量使用百分比
使用短信接口提醒更及时。
计算当天流量达到
1GB
就预警(这个值还不确定,随便写的,要根据实际情况限定);老薛主机:判断流量使用百分比
是否大于
当月时间进度(今天日期/当月总天数);使用短信接口提醒更及时。
代码
细节不表
<?php // 又拍云监控 header("content-type:application/json"); require "../include/functions.php"; // https://api.upyun.com/doc#/api/operation/oauth/POST%20%2Foauth%2Ftokens $token = "*****"; $GB = getUsed(); if($GB >= 1 && !file_exists('temp/upyun.log')){ file_put_contents('temp/upyun.log', $GB); $msg_arr = array( 'task_name' => '又拍云流量监控', 'status' => "当前流量使用量已达到警戒值:{$GB}GB" ); //发送邮件 send_email('*****@qq.com', $msg_arr); }else{ echo $GB; } // print_r($body); function getUsed(){ global $token; $start = date('Y-m-d') . "T00:00:00"; $end = date('Y-m-d') . "T23:59:59"; $http = new EasyHttp(); $response = $http->request("https://api.upyun.com/v2/statistics?start_time={$start}&end_time={$end}", array( 'method' => 'GET', // GET/POST 'timeout' => 10, // 超时的秒数 'redirection' => 5, // 最大重定向次数 'httpversion' => '1.1', // 1.0/1.1 'user-agent' => null, 'blocking' => true, // 是否阻塞 'headers' => array( "Authorization" => "Bearer {$token}", ), // header信息 'cookies' => null, // 关联数组形式的cookie信息 // 'cookies' => $cookies, 'body' => null, 'compress' => false, // 是否压缩 'decompress' => true, // 是否自动解压缩结果 'sslverify' => true, 'stream' => false, 'filename' => null // 如果stream = true,则必须设定一个临时文件名 )); // print_r($response); // echo $response['body']; $body = json_decode($response['body'], true); $data = $body['data']; $bytes = 0; foreach ($data as $value) { // code... $bytes += $value['bytes']; } $GB = $bytes / pow(1024, 3); return $GB; }
$lx = new LXHOST('host', 'username', 'password'); if($lx->login()) $lx->getUsage(); class LXHOST{ private $host; private $uname; private $upass; private $security_token; private $cpsession; function __construct($host, $uname, $upass){ $this->host = $host; $this->uname = $uname; $this->upass = $upass; } function login(){ $http = new EasyHttp(); $login = $http->request("https://{$this->host}/login/?login_only=1", array( 'method' => 'POST', // GET/POST 'timeout' => 10, // 超时的秒数 'redirection' => 5, // 最大重定向次数 'httpversion' => '1.1', // 1.0/1.1 'user-agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36 Edg/88.0.705.56', 'blocking' => true, // 是否阻塞 'headers' => array( "referer" => "https://{$this->host}/logout/?locale=zh", ), // header信息 'cookies' => null, // 关联数组形式的cookie信息 // 'cookies' => $cookies, 'body' => "user={$this->uname}&pass={$this->upass}", 'compress' => false, // 是否压缩 'decompress' => true, // 是否自动解压缩结果 'sslverify' => false, 'stream' => false, 'filename' => null // 如果stream = true,则必须设定一个临时文件名 )); // print_r($login); $body = json_decode($login['body'], true); if($body['status'] == 1) { $this->security_token = $body['security_token']; $this->cpsession = $login['cookies'][1]->value; return true; }else{ return false; } } function getUsage(){ $http = new EasyHttp(); $usage = $http->request("https://{$this->host}{$this->security_token}/execute/ResourceUsage/get_usages", array( 'method' => 'POST', // GET/POST 'timeout' => 10, // 超时的秒数 'redirection' => 5, // 最大重定向次数 'httpversion' => '1.1', // 1.0/1.1 'user-agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36 Edg/88.0.705.56', 'blocking' => true, // 是否阻塞 'headers' => array( "referer" => "https://{$this->host}/logout/?locale=zh", "X-Requested-With" => "https://{$this->host}/", "cookie" => "timezone=Asia/Shanghai; cpsession={$this->cpsession}" ), // header信息 'cookies' => null, // 关联数组形式的cookie信息 // 'cookies' => $cookies, 'body' => null, 'compress' => false, // 是否压缩 'decompress' => true, // 是否自动解压缩结果 'sslverify' => false, 'stream' => false, 'filename' => null // 如果stream = true,则必须设定一个临时文件名 )); // print_r($usage); $body = json_decode($usage['body'], true); if($body['status'] == 1) { $data = $body['data']; $bandwidth = $data[2]; $GB = $bandwidth['usage'] / pow(1024, 3); if($bandwidth['usage'] / $bandwidth['maximum'] >= date('d') / cal_days_in_month(CAL_GREGORIAN, date('m'), date('Y')) && !file_exists("temp/lxHost_{$this->host}.log")) { // 超标 file_put_contents("temp/lxHost_{$this->host}.log", $GB); $msg_arr = array( 'task_name' => '老薛主机流量监控', 'status' => "当前流量使用量已达到警戒值:{$GB}GB" ); //发送邮件 send_email('*****@qq.com', $msg_arr); }else{ echo "{$GB}GB\r\n"; } return true; }else{ return false; } } }