1.本脚本只负责获取各个app对应的域名及ip信息,其他信息如CDB容量,CMEM信息等没有涉及,自行改之
2.本脚本系调用shell 命令,没有用原生的php API(命令行方便)
3.脚本如下,亲测没有问题:
-
<?php
-
/*
-
@author:Anthoy
-
@date:2013-10-24
-
@version:0.1
-
@命令行模式运行收集腾讯平台所有虚拟机信息
-
*/
-
class CollectionVM {
-
public function __construct($args){
-
$this->file = $args['file']; //app 管理映射表
-
$this->inifile = $args['inifile'];//API ini file
-
$this->csvInfo = self::getAppMapping($this->file);
-
self::modifyApiIniFile($this->inifile,$this->csvInfo); //写入ini文件
-
}
-
-
public static function modifyApiIniFile($inifile,$csvInfo){
-
if(is_file($inifile)){
-
$fp = fopen($inifile,'w') or die ('can not open file!'.$inifile);
-
foreach ($csvInfo as $k => $v) {
-
if(isset($v['cnm']) &&isset($v['id']) && isset($v['key'])){
-
$str = "[".$v['cnm']."]\n"."secretId=".$v['id']."\n"."secretKey=".$v['key']."\n";
-
fwrite($fp, $str);
-
}
-
}
-
fclose($fp);
-
}
-
}
-
//读取app 名称映射表
-
public static function getAppMapping($file){
-
$map = array();
-
if(is_file($file)){
-
$fp = fopen($file, 'r');
-
while(($line = fgets($fp)) !== false){
-
$key = explode(",", trim($line));
-
$map[$key[2]]['name']=$key[0];
-
$map[$key[2]]['cnm']=$key[1];
-
$map[$key[2]]['id']=$key[3];
-
$map[$key[2]]['key']=$key[4];
-
-
}
-
fclose($fp);
-
return $map;
-
}
-
}
-
-
//
-
public function getDomainInfo(){
-
$rlt = array();
-
foreach ($this->csvInfo as $k=> $v) {
-
$cmd = "qc-domain-list --appflag=".$v['cnm']." | awk '/^s/{print $1}'";
-
$domains = shell_exec($cmd);
-
foreach (explode("\n",$domains) as $key ) {
-
if(!empty($key)){
-
$cmdstr = "qc-domain-bindinfo --appflag=".$v['cnm']." --domain=".$key ." | awk '/^s/{print $1,$2}'";
-
$bindInfo = shell_exec($cmdstr);
-
$excArray = self::getBindInfo($bindInfo);
-
array_push($rlt,$excArray);
-
}
-
-
}
-
}
-
-
return $rlt;
-
}
-
-
public static function getBindInfo($info){
-
$rstArray = array();
-
if(!empty($info)){
-
$infoArray = explode("\n", $info);
-
foreach ($infoArray as $key ) {
-
if(!empty($key)){
-
list($app,$ip) = explode(" ", $key);
-
}
-
if(preg_match('/:80$/', $ip)){
-
$rstArray[$app]['web']=$ip;
-
}else{
-
if(!empty($rstArray[$app]['socket'])){
-
$rstArray[$app]['socket']=$rstArray[$app]['socket'].",$ip";
-
}else{
-
$rstArray[$app]['socket']=$ip;
-
}
-
}
-
}
-
}
-
return $rstArray;
-
}
-
-
//test fucntion
-
public function doTest(){
-
print_r($this->csvInfo);
-
}
-
}
-
//收集数据
-
$o = new CollectionVM(array('file'=>'/tmp/app.csv','inifile'=>'/opt/app/cmd/app.ini'));
-
$queryArray = $o->getDomainInfo();
-
foreach ($queryArray as $key) {
-
$y = array_keys($key)[0];
-
$x = explode(".",array_keys($key)[0]);
-
if(in_array($x[1],array_keys($o->csvInfo))){
-
echo $y."\t".$key[$y]['web']."\t".$key[$y]['socket']."\t".$o->csvInfo[$x[1]]['name']."\n";
-
}
-
}
-
-
?>
阅读(1646) | 评论(0) | 转发(0) |