通过SolusVM的API对多VPS进行流量进行监控(2)

166次阅读
没有评论

共计 2376 个字符,预计需要花费 6 分钟才能阅读完成。

本程序对上个主题进行修改,只展示流量,并且可以对对个vps进行设置,只要使用solusvm生成API的都可以,并且将流量超过70%的进行黑色展示,防止流量超了,这样可以比较方便的监控VPS的流量情况,如果需要进行利用API进行多个管理,可以查看solusVMmanager

代码比较简单,如果需要多个功能可以自己写,例如超过发邮件什么的,但是定时执行下就可以

通过SolusVM的API对多VPS进行流量进行监控(2)
<?php

$lists = array(
    array("替换为API Key","替换为API Hash","替换为你的主机供应商SoluSVM地址", "备注1")
//多个可以拷贝上面,最后一个不加逗号,其他加即可
);

 foreach($lists as $key ){
    $result[] = post($key[0] , $key[1] , $key[2], $key[3]);
 }

function humanFileSize($size, $unit="") {
  if( (!$unit && $size >= 1<<30) || $unit == "GB")
    return number_format($size/(1<<30),2)." GB";
  if( (!$unit && $size >= 1<<20) || $unit == "MB")
    return number_format($size/(1<<20),2)." MB";
  if( (!$unit && $size >= 1<<10) || $unit == "KB")
    return number_format($size/(1<<10),2)." KB";
  return number_format($size)." bytes";
}

function post($keys , $hashs, $naserurls ,$beizhu) {
    
   
    $postfields["key"] = $keys;
    $postfields["hash"] = $hashs;
    $postfields["action"] = "info";
	$masterurl = $naserurls;
	$postfields["status"] = "true";
    $postfields["hdd"] = "true";
    $postfields["mem"] = "true";
    $postfields["bw"] = "true";

	$ch = curl_init();

	curl_setopt($ch, CURLOPT_URL, "{$masterurl}/api/client/command.php");
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_HEADER, 0);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
	curl_setopt($ch, CURLOPT_HTTPHEADER, array("Expect:  "));
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
	
	// Execute the request

	$data = curl_exec($ch);
	
	$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
	
	if($code != 200) {
		$return['error'] = 1;
		
		if($code == 405) {
			$return['message'] = "Incorrect API credentials.";
			return $return;
		}
		
		$return['message'] = "Invalid status code.";
		
		return $return;
	}

	// Close the Curl handle

	curl_close($ch);
	
	if(!$data) {
		$return['error'] = 1;
		$return['message'] = "Error connecting to API.";
		
		return $return;
	}

	// Extract the data

	preg_match_all('/<(.*?)>([^<]+)<\/\\1>/i', $data, $match);

	$result = array();
    $result['bz'] = $beizhu;
	foreach ($match[1] as $x => $y) {
		$result[$y] = $match[2][$x];
	}
	//获取出错
	if($result['status'] == "error") {
		$result['error'] = 1;
		$result['message'] = $result['statusmsg'];
		
		return $result;
	}
    //获取正常
	$result['error'] = 0;
    
    $result['hdd'] = explode(",", $result['hdd']);
    $result['mem'] = explode(",", $result['mem']);
    $result['bw'] = explode(",", $result['bw']);
	return $result;
}



?>

            <?php foreach($result as $key ){ 
            //var_dump($key);
            ?>
            <div <?php if($key['bw'][3] >60){ ?>style="background:#000; color:#FFF">  <?php }?>
			<h3>备注:<?php echo $key['bz']; ?>    带宽 <small>  已用 <?php echo humanFileSize($key['bw'][1]); ?> , 总量 <?php echo humanFileSize($key['bw'][0]); ?> ,比例 :<?php echo $key['bw'][3]; ?>%</small></h3>
            <div>
            <?php } ?>    

正文完
 
admin
版权声明:本站原创文章,由 admin 2019-11-13发表,共计2376字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)
验证码