当前位置:首页 > PHP教程 > php高级应用 > 列表

PHP+shell脚本操作Memcached和Apache Status的实例分享

发布:smiling 来源: PHP粉丝网  添加日期:2021-07-14 09:28:41 浏览: 评论:0 

这篇文章主要介绍了PHP环境下使用shell脚本操作Memcached和Apache Status的方法,分别还可以控制Memcached进程的启动以及记录Apache Status数据到数据库,需要的朋友可以参考下。

memcached 进程启动及监控

1.memcached_inc.sh

设置路径,端口等讯息。

  1. #!/bin/sh  
  2.    
  3. #config include  
  4.    
  5. HOST=$(hostname)  
  6. SITE="mysite" 
  7. PORT=11211  
  8.    
  9. MEMCACHED_PID_FILE="/tmp/memcached.pid" 
  10. MEMCACHED_DAEMON_PID_FILE="/tmp/memcached_daemon.pid" 
  11.    
  12. MEMCACHED="memcached -d -m 64 -p $PORT -u memcache -l 127.0.0.1 -P $MEMCACHED_PID_FILE" 
  13. MEMCACHED_DAEMON_FILE="memcached_daemon.sh" 
  14.    
  15. ERROR_LOG_FILE="${ROOT}/memcached_${SITE}_${HOST}_${PORT}.log" 

2.gm_memcached.sh

控制memcached 启动,停止,重启。

  1. #!/bin/sh  
  2.    
  3. #memcached start and stop  
  4. #$1 action  
  5.    
  6. ROOT=$(cd "$(dirname "$0")"; pwd)  
  7.    
  8. . ${ROOT}/memcached_inc.sh  
  9.    
  10.    
  11. start() {  
  12.    
  13.  if [ -f "$MEMCACHED_PID_FILE" ] && [ -s "$MEMCACHED_PID_FILE" ]; then 
  14.   printf "memcached already running\n" 
  15.  else 
  16.   printf "starting memcached\n" 
  17.   $MEMCACHED  
  18.    
  19.   sleep 2  
  20.    
  21.   PID=$(cat $MEMCACHED_PID_FILE)  
  22.   printf "memcached is started PID:$PID\n" 
  23.    
  24.   printf "starting memcached daemon\n" 
  25.   ${ROOT}/${MEMCACHED_DAEMON_FILE} &  
  26.   DAEMON_PID=$!  
  27.   echo ${DAEMON_PID} > ${MEMCACHED_DAEMON_PID_FILE}  
  28.   printf "memcached daemon is started PID:${DAEMON_PID}\n" 
  29.  fi 
  30.    
  31. }  
  32.    
  33.    
  34. stop() {  
  35.    
  36.  if [ -f "$MEMCACHED_DAEMON_PID_FILE" ] && [ -s "$MEMCACHED_DAEMON_PID_FILE" ]; then 
  37.   DAEMON_PID=$(cat $MEMCACHED_DAEMON_PID_FILE)  
  38.   rm -f ${MEMCACHED_DAEMON_PID_FILE}  
  39.   if [ ! -z ${DAEMON_PID} ]; then 
  40.    kill -9 ${DAEMON_PID}  
  41.   fi 
  42.   printf "memcached daemon is stopped\n" 
  43.  else 
  44.   printf "no memcached daemon running\n" 
  45.  fi 
  46.    
  47.  sleep 1  
  48.    
  49.  if [ -f "$MEMCACHED_PID_FILE" ] && [ -s "$MEMCACHED_PID_FILE" ]; then 
  50.   PID=$(cat $MEMCACHED_PID_FILE)  
  51.   rm -f ${MEMCACHED_PID_FILE}  
  52.   if [ ! -z ${PID} ]; then 
  53.    kill -9 ${PID}  
  54.   fi 
  55.   printf "memcached is stopped\n" 
  56.  else 
  57.   printf "no memcached running\n" 
  58.  fi 
  59.    
  60. }  
  61.    
  62.    
  63. case "$1" in 
  64.    
  65.  start)  
  66.   start  
  67.   ;;  
  68.    
  69.  stop)  
  70.   stop  
  71.   ;;  
  72.    
  73.  restart)  
  74.   stop  
  75.   sleep 3  
  76.   start  
  77.   ;;  
  78.    
  79.  *)  
  80.   printf "Usage:$0 {start|stop|restart}\n" 
  81.   exit 1  
  82.    
  83. esac  
  84.    
  85. exit 0  

3.memcached_daemon.sh

监控memcached 进程,如进程失效则自动启动。

  1. #!/bin/sh  
  2.    
  3. #memcached daemon  
  4.    
  5. ROOT=$(cd "$(dirname "$0")"; pwd)  
  6.    
  7. . ${ROOT}/memcached_inc.sh  
  8.    
  9.    
  10. while :  
  11. do 
  12.  if [ -f "$MEMCACHED_PID_FILE" ] && [ -s "$MEMCACHED_PID_FILE" ]; then 
  13.   PID=$(cat $MEMCACHED_PID_FILE)  
  14.  else 
  15.   PID="" 
  16.  fi 
  17.     
  18.  if [ -z "$PID" ] || [ -z $(ps aux|awk '{print $2}' | grep "^$PID$") ]; then 
  19.   $MEMCACHED  
  20.   sleep 1  
  21.   printf "[$(date +%Y-%m-%d' '%H:%M:%S)] ${SITE} ${HOST} memcached ${PORT} is restarted\n" >> $ERROR_LOG_FILE  
  22.   echo "Subject: ${SITE} ${HOST} memcached ${PORT} is restarted $(date +%Y-%m-%d' '%H:%M:%S)" | sendmail me@gmail.com  
  23.  fi 
  24.    
  25.  sleep 5  
  26.    
  27. done 
  28.    
  29. exit 0  

使用方法:

./gm_memcached.sh start #启动memcached

./gm_memcached.sh stop #停止memcached

./gm_memcached.sh restart #重启memcached

shell 记录apache status并自动更新到数据库

1. 获取apache status

monitor_log.sh

  1. #!/bin/bash  
  2.    
  3. #连接数  
  4. site_connects=$(netstat -ant | grep $ip:80 | wc -l)  
  5. #当前连接数  
  6. site_cur_connects=$(netstat -ant | grep $ip:80 | grep EST | wc -l)  
  7.    
  8. #apache  
  9. apache_speed=$(netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}')  
  10.    
  11. printf "[#start#]\n$(date '+%Y-%m-%d %H:%M:%S')\n" 
  12. printf "connects:${site_connects}\n" 
  13. printf "cur connects:${site_cur_connects}\n" 
  14. printf "apache_speed:\n${apache_speed}\n[#end#]\n\n" 
  15.    
  16. exit 0  

在终端设置crontab执行

1。* * * * * /home/fdipzone/monitor_log.sh >> /home/fdipzone/monitor.log

2。将apache status log 写入数据库

save_monitor_log.php

  1. <?php  
  2.    
  3. $logfile = dirname(__FILE__).'/monitor.log';  
  4.    
  5. $dbconfig = array(  
  6.    'host' => '192.168.1.100',  
  7.    'username' => 'username',  
  8.    'password' => 'password',  
  9.    'dbname' => 'mydb',  
  10.    'tabname' => 'monitor_log' 
  11. );  
  12.    
  13. $obj = new SaveMonitorLog($dbconfig'myweb');  
  14. $obj->load($logfile);  
  15.    
  16.    
  17. // 讀取monitor log,記錄入db,查看db  
  18. class SaveMonitorLog{ // class start  
  19.    
  20.  private $_apache_state = array('TIME_WAIT''CLOSE_WAIT''SYN_SENT''SYN_RECV''FIN_WAIT1''FIN_WAIT2''ESTABLISHED''LAST_ACK''CLOSING');  
  21.  private $_dbconfig = array();  
  22.  private $_site = null;  
  23.    
  24.    
  25.  /** init */ 
  26.  public function __construct($dbconfig=array(), $site='web'){  
  27.   if(!isset($dbconfig['host']) || !isset($dbconfig['username']) || !isset($dbconfig['password']) || !isset($dbconfig['dbname']) || !isset($dbconfig['tabname'])){  
  28.    $this->debug('dbconfig error');  
  29.   }  
  30.   $this->_dbconfig = $dbconfig;  
  31.   $this->_site = $site;  
  32.   $this->connectdb();  
  33.  }  
  34.    
  35.    
  36.  /** load data  
  37.  * @param String $logfile log文件  
  38.  * @return boolean  
  39.  */ 
  40.  public function load($logfile){  
  41.    
  42.   // 讀取log數據  
  43.   if(file_exists($logfile)){  
  44.    $logdata = file_get_contents($logfile);  
  45.    // 清空monitor.log  
  46.    file_put_contents($logfile'', true);  
  47.   }else{  
  48.    return false;  
  49.   }  
  50.    
  51.   // 正則分析數據 [#start#]*[#end#]  
  52.   preg_match_all('/ 
  53. #start# 
  54. (.*?) 
  55. #end
  56. .*?/si', $logdata$data);  
  57.    
  58.   if(isset($data[1]) && count($data[1])>0){  
  59.    $alldata = $data[1];  
  60.    foreach($alldata as $val){  
  61.     $indb = $this->parser($val);  
  62.     $newid = $this->addtodb($indb);  
  63.    }  
  64.   }  
  65.    
  66.  }  
  67.    
  68.    
  69.  /** parser data  
  70.  * @param Array $data  
  71.  * @return Array  
  72.  */ 
  73.  private function parser($data){  
  74.   $indb = array();  
  75.   $tmp = explode(chr(10), $data); // 按換行分隔  
  76.    
  77.   $indb['site'] = $this->_site;  
  78.   $indb['addtime'] = $tmp[1];  
  79.   $indb['connects'] = array_pop(explode(':',$tmp[2]));  
  80.   $indb['cur_connects'] = array_pop(explode(':',$tmp[3]));  
  81.    
  82.   for($i=5, $max=count($tmp)-2; $i<$max$i++){  
  83.    list($key$num) = explode(' '$tmp[$i]);  
  84.    if(in_array($key$this->_apache_state)){  
  85.     $indb[$key] = $num;  
  86.    }  
  87.   }  
  88.    
  89.   return $indb;  
  90.  }  
  91.    
  92.    
  93.  /** connect db */ 
  94.  private function connectdb(){  
  95.   $conn=@mysql_connect($this->_dbconfig['host'], $this->_dbconfig['username'], $this->_dbconfig['password']) or die(mysql_error());  
  96.   mysql_select_db($this->_dbconfig['dbname'], $connor die(mysql_error());  
  97.  }  
  98.    
  99.    
  100.  /** add to db */ 
  101.  private function addtodb($indb){  
  102.   $insertkey = '';  
  103.   $insertval = '';  
  104.   if($indb){  
  105.    foreach($indb as $key=>$val){  
  106.     $insertkey .= $insertkey" ,".$key : $key;  
  107.     $insertval .= $insertval" ,'".mysql_escape_string(trim($val))."'" : "'".mysql_escape_string(trim($val))."'";  
  108.    }  
  109.    $sqlstr = "insert into ".$this->_dbconfig['tabname']."($insertkey) values($insertval)";  
  110.    $query = @mysql_query($sqlstror die(mysql_error());  
  111.    $id = mysql_insert_id();  
  112.    return $id$id : false;  
  113.   }  
  114.  }  
  115.    
  116.    
  117.  /** debug */ 
  118.  private function debug($msg){  
  119.   exit($msg."\r\n");  
  120.  }  
  121.    
  122.    
  123. // class end  
  124.    
  125. ?>  

在终端crontab执行

0 0 * * * php /home/fdipzone/save_monitor_log.php

table monitor_log struct

  1. CREATE TABLE IF NOT EXISTS `monitor_log` (  
  2.  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,  
  3.  `site` varchar(20) NOT NULL,  
  4.  `connects` int(10) unsigned NOT NULL DEFAULT '0',  
  5.  `cur_connects` int(10) unsigned NOT NULL DEFAULT '0',  
  6.  `TIME_WAIT` int(10) unsigned NOT NULL DEFAULT '0',  
  7.  `CLOSE_WAIT` int(10) unsigned NOT NULL DEFAULT '0',  
  8.  `SYN_SENT` int(10) unsigned NOT NULL DEFAULT '0',  
  9.  `SYN_RECV` int(10) unsigned NOT NULL DEFAULT '0',  
  10.  `FIN_WAIT1` int(10) unsigned NOT NULL DEFAULT '0',  
  11.  `FIN_WAIT2` int(10) unsigned NOT NULL DEFAULT '0',  
  12.  `ESTABLISHED` int(10) unsigned NOT NULL DEFAULT '0',  
  13.  `LAST_ACK` int(10) unsigned NOT NULL DEFAULT '0',  
  14.  `CLOSING` int(10) unsigned NOT NULL DEFAULT '0',  
  15.  `addtime` datetime NOT NULL,  
  16.  PRIMARY KEY (`id`),  
  17.  KEY `connects` (`connects`),  
  18.  KEY `cur_connects` (`cur_connects`),  
  19.  KEY `TIME_WAIT` (`TIME_WAIT`),  
  20.  KEY `CLOSE_WAIT` (`CLOSE_WAIT`),  
  21.  KEY `SYN_SENT` (`SYN_SENT`),  
  22.  KEY `SYN_RECV` (`SYN_RECV`),  
  23.  KEY `FIN_WAIT1` (`FIN_WAIT1`),  
  24.  KEY `FIN_WAIT2` (`FIN_WAIT2`),  
  25.  KEY `ESTABLISHED` (`ESTABLISHED`),  
  26.  KEY `LAST_ACK` (`LAST_ACK`),  
  27.  KEY `CLOSING` (`CLOSING`),  
  28.  KEY `addtime` (`addtime`)  
  29. ) ENGINE=MyISAM DEFAULT CHARSET=latin1 ;

Tags: PHP+shell Memcached Apache

分享到: