Chinaunix首页 | 论坛 | 博客
  • 博客访问: 351898
  • 博文数量: 26
  • 博客积分: 495
  • 博客等级: 下士
  • 技术积分: 562
  • 用 户 组: 普通用户
  • 注册时间: 2010-04-26 13:50
文章分类

全部博文(26)

文章存档

2015年(9)

2014年(6)

2013年(7)

2012年(2)

2011年(2)

分类: PHP

2013-10-25 14:50:59

1.本脚本只负责获取各个app对应的域名及ip信息,其他信息如CDB容量,CMEM信息等没有涉及,自行改之
2.本脚本系调用shell 命令,没有用原生的php API(命令行方便)
3.脚本如下,亲测没有问题:

点击(此处)折叠或打开

  1. <?php
  2. /*
  3. @author:Anthoy
  4. @date:2013-10-24
  5. @version:0.1
  6. @命令行模式运行收集腾讯平台所有虚拟机信息
  7. */
  8. class CollectionVM {
  9.     public function __construct($args){
  10.         $this->file = $args['file']; //app 管理映射表
  11.         $this->inifile = $args['inifile'];//API ini file
  12.         $this->csvInfo = self::getAppMapping($this->file);
  13.         self::modifyApiIniFile($this->inifile,$this->csvInfo); //写入ini文件
  14.     }

  15.     public static function modifyApiIniFile($inifile,$csvInfo){
  16.         if(is_file($inifile)){
  17.             $fp = fopen($inifile,'w') or die ('can not open file!'.$inifile);
  18.             foreach ($csvInfo as $k => $v) {
  19.                 if(isset($v['cnm']) &&isset($v['id']) && isset($v['key'])){
  20.                     $str = "[".$v['cnm']."]\n"."secretId=".$v['id']."\n"."secretKey=".$v['key']."\n";
  21.                     fwrite($fp, $str);
  22.                 }
  23.             }
  24.             fclose($fp);
  25.         }
  26.     }
  27.     //读取app 名称映射表
  28.     public static function getAppMapping($file){
  29.         $map = array();
  30.         if(is_file($file)){
  31.             $fp = fopen($file, 'r');
  32.             while(($line = fgets($fp)) !== false){
  33.                 $key = explode(",", trim($line));
  34.                 $map[$key[2]]['name']=$key[0];
  35.                 $map[$key[2]]['cnm']=$key[1];
  36.                 $map[$key[2]]['id']=$key[3];
  37.                 $map[$key[2]]['key']=$key[4];
  38.     
  39.             }
  40.             fclose($fp);
  41.             return $map;
  42.         }
  43.     }

  44.     //
  45.     public function getDomainInfo(){
  46.         $rlt = array();
  47.         foreach ($this->csvInfo as $k=> $v) {
  48.             $cmd = "qc-domain-list --appflag=".$v['cnm']." | awk '/^s/{print $1}'";
  49.             $domains = shell_exec($cmd);
  50.             foreach (explode("\n",$domains) as $key ) {
  51.                 if(!empty($key)){
  52.                     $cmdstr = "qc-domain-bindinfo --appflag=".$v['cnm']." --domain=".$key ." | awk '/^s/{print $1,$2}'";
  53.                     $bindInfo = shell_exec($cmdstr);
  54.                     $excArray = self::getBindInfo($bindInfo);
  55.                     array_push($rlt,$excArray);
  56.                 }
  57.                 
  58.             }
  59.         }
  60.         
  61.         return $rlt;
  62.     }

  63.     public static function getBindInfo($info){
  64.         $rstArray = array();
  65.         if(!empty($info)){
  66.             $infoArray = explode("\n", $info);
  67.             foreach ($infoArray as $key ) {
  68.                 if(!empty($key)){
  69.                     list($app,$ip) = explode(" ", $key);
  70.                 }
  71.                 if(preg_match('/:80$/', $ip)){
  72.                     $rstArray[$app]['web']=$ip;
  73.                 }else{
  74.                     if(!empty($rstArray[$app]['socket'])){
  75.                         $rstArray[$app]['socket']=$rstArray[$app]['socket'].",$ip";
  76.                     }else{
  77.                         $rstArray[$app]['socket']=$ip;
  78.                     }
  79.                 }
  80.             }
  81.         }
  82.         return $rstArray;
  83.     }

  84.     //test fucntion
  85.     public function doTest(){
  86.         print_r($this->csvInfo);
  87.     }
  88. }
  89. //收集数据
  90. $o = new CollectionVM(array('file'=>'/tmp/app.csv','inifile'=>'/opt/app/cmd/app.ini'));
  91. $queryArray = $o->getDomainInfo();
  92. foreach ($queryArray as $key) {
  93.     $y = array_keys($key)[0];
  94.     $x = explode(".",array_keys($key)[0]);
  95.     if(in_array($x[1],array_keys($o->csvInfo))){
  96.         echo $y."\t".$key[$y]['web']."\t".$key[$y]['socket']."\t".$o->csvInfo[$x[1]]['name']."\n";
  97.     }
  98. }

  99. ?>


阅读(1646) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~