监控国际阿里云流量包并实现自动关机的PHP脚本

我们进程会有担心自己的站点上了阿里云的CDN后呗别人随便DD一下就流量没了,房子姓马的了~~~

监控国际阿里云流量包并实现自动关机的PHP脚本

现在转载一个可以实现监控国际阿里云流量包并实现自动关机的PHP脚本

自行更改 用户AccessKey 地区 机器ID
如果只有1台ecs 且是新加坡的 只需修改上面3个变量值 就能使用
AccessKey 获取方式 登录阿里云-进入控制台-右上角头像-AccessKey
机器ID就是ecs的id

不哔哔,放代码:

error_reporting(0);
$AccessKey_ID=\’xxxxxxxxxxxx\’;//AccessKey_ID
$Access_Key_Secret=\’xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\’;//Access_Key_Secret
$InstanceId=\’i-t4naxxxxxx\’;//需要关机的ecsID

function percentEncode($str){
// 使用urlencode编码后,将” “,”*”,”%7E”做替换即满足ECS API规定的编码规范
$res = urlencode($str);
$res = preg_replace(\’/\ /\’, \’%20\’, $res);
$res = preg_replace(\’/\*/\’, \’%2A\’, $res);
$res = preg_replace(\’/%7E/\’, \’~\’, $res);
return $res;
}
function computeSignature($parameters, $accessKeySecret){
// 将参数Key按字典顺序排序
ksort($parameters);
// 生成规范化请求字符串
$canonicalizedQueryString = \’\’;
foreach($parameters as $key => $value)
{
$canonicalizedQueryString .= \’&\’ . percentEncode($key)
. \’=\’ . percentEncode($value);
}
// 生成用于计算签名的字符串 stringToSign
$stringToSign = \’GET&%2F&\’ . percentencode(substr($canonicalizedQueryString, 1));
// 计算签名,注意accessKeySecret后面要加上字符\’&\’
$signature = base64_encode(hash_hmac(\’sha1\’, $stringToSign, $accessKeySecret . \’&\’, true));
return $signature;
}
function get_action($data,$Version,$host){
date_default_timezone_set(“GMT”);
$dateTimeFormat = \’Y-m-d\TH:i:s\Z\’;
global $AccessKey_ID;
global $Access_Key_Secret;
$publicdata = array(
// 公共参数
\’Format\’ => \’JSON\’,
\’Version\’ => $Version,
\’AccessKeyId\’ => $AccessKey_ID,
\’SignatureVersion\’ => \’1.0\’,
\’SignatureMethod\’ => \’HMAC-SHA1\’,
\’SignatureNonce\’=> uniqid(),
\’Timestamp\’ => date($dateTimeFormat),
\’TimeStamp\’ => date($dateTimeFormat)
);
$data = array_merge($publicdata,$data);
$data[\’Signature\’] = computeSignature($data, $Access_Key_Secret);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $host . http_build_query($data));
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$res = curl_exec($ch);
date_default_timezone_set(\’PRC\’);
//echo $res;
return $res;
}
//流量JSON
function get_(){
$data = array(
\’Action\’ => \’QueryResourcePackageInstances\’,
\’RegionId\’ => \’ap-southeast-1\’
);

$res = (string)get_action($data,\’2017-12-14\’,\’https://business.ap-southeast-1.aliyuncs.com/?\’);
//echo $res;
$jsonarray = json_decode($res,true);
if($jsonarray[\’Message\’]==\’Successful!\’){
echo $jsonarray[\’Data\’][\’Instances\’][\’Instance\’][0][\’RemainingAmount\’];
if($jsonarray[\’Data\’][\’Instances\’][\’Instance\’][0][\’RemainingAmount\’]<=20){//这里判断流量小于20G
StopInstance();
}
}
}
function StopInstance(){
global $InstanceId;
$data = array(
\’Action\’ => \’StopInstance\’,
\’InstanceId\’ => $InstanceId,
\’ForceStop\’ =>”true”,
\’StoppedMode\’ => “keepcharging”,
\’ConfirmStop\’ => “false”
);
$res = (string)get_action($data,\’2014-05-26\’,\’https://ecs.aliyuncs.com/?\’);
echo $res;
}
get_();
?>

 

 

未经允许不得转载:VPS啦 » 监控国际阿里云流量包并实现自动关机的PHP脚本

赞 (1) 打赏

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏