[PHP脚本]又拍云&老薛主机(CPanel)流量监控 | 祭夜博客
  • 欢迎光临,这个博客颜色有点多

[PHP脚本]又拍云&老薛主机(CPanel)流量监控

php msojocs 3年前 (2021-02-02) 2094次浏览 已收录 2个评论 扫描二维码
文章目录[隐藏]

前言

最近老被打,恶心死了;又没钱搞防护,就写个脚本预警一下。。。

又拍云:
计算当天流量达到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;
        }
    }
}

祭夜の咖啡馆 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:[PHP脚本]又拍云&老薛主机(CPanel)流量监控
喜欢 (1)
[1690127128@qq.com]
分享 (0)
发表我的评论
取消评论
OwO表情
贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
(2)个小伙伴在吐槽
  1. 那些打服务器的是真恶心
    coFFee2022-01-28 19:47 回复 Linux | Firefox浏览器 96.0
  2. cqcadmin
    部分细节需要自己实现
    祭夜2021-03-16 15:13 回复 Windows 10 | Chrome 89.0.4389.90