php又拍云,[PHP脚本]又拍云&老薛主机(CPanel)流量监控 | 祭夜の咖啡馆

前言

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

又拍云:

计算当天流量达到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)流量监控

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值