当前位置: 移动技术网 > IT编程>开发语言>PHP > PHP+shell脚本操作Memcached和Apache Status的实例分享

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

2017年12月12日  | 移动技术网IT编程  | 我要评论

memcached 进程启动及监控
1.memcached_inc.sh
设置路径,端口等讯息。

#!/bin/sh 
 
#config include 
 
host=$(hostname) 
site="mysite" 
port=11211 
 
memcached_pid_file="/tmp/memcached.pid" 
memcached_daemon_pid_file="/tmp/memcached_daemon.pid" 
 
memcached="memcached -d -m 64 -p $port -u memcache -l 127.0.0.1 -p $memcached_pid_file" 
memcached_daemon_file="memcached_daemon.sh" 
 
error_log_file="${root}/memcached_${site}_${host}_${port}.log" 

2.gm_memcached.sh
控制memcached 启动,停止,重启。

#!/bin/sh 
 
#memcached start and stop 
#$1 action 
 
root=$(cd "$(dirname "$0")"; pwd) 
 
. ${root}/memcached_inc.sh 
 
 
start() { 
 
 if [ -f "$memcached_pid_file" ] && [ -s "$memcached_pid_file" ]; then 
  printf "memcached already running\n" 
 else 
  printf "starting memcached\n" 
  $memcached 
 
  sleep 2 
 
  pid=$(cat $memcached_pid_file) 
  printf "memcached is started pid:$pid\n" 
 
  printf "starting memcached daemon\n" 
  ${root}/${memcached_daemon_file} & 
  daemon_pid=$! 
  echo ${daemon_pid} > ${memcached_daemon_pid_file} 
  printf "memcached daemon is started pid:${daemon_pid}\n" 
 fi 
 
} 
 
 
stop() { 
 
 if [ -f "$memcached_daemon_pid_file" ] && [ -s "$memcached_daemon_pid_file" ]; then 
  daemon_pid=$(cat $memcached_daemon_pid_file) 
  rm -f ${memcached_daemon_pid_file} 
  if [ ! -z ${daemon_pid} ]; then 
   kill -9 ${daemon_pid} 
  fi 
  printf "memcached daemon is stopped\n" 
 else 
  printf "no memcached daemon running\n" 
 fi 
 
 sleep 1 
 
 if [ -f "$memcached_pid_file" ] && [ -s "$memcached_pid_file" ]; then 
  pid=$(cat $memcached_pid_file) 
  rm -f ${memcached_pid_file} 
  if [ ! -z ${pid} ]; then 
   kill -9 ${pid} 
  fi 
  printf "memcached is stopped\n" 
 else 
  printf "no memcached running\n" 
 fi 
 
} 
 
 
case "$1" in 
 
 start) 
  start 
  ;; 
 
 stop) 
  stop 
  ;; 
 
 restart) 
  stop 
  sleep 3 
  start 
  ;; 
 
 *) 
  printf "usage:$0 {start|stop|restart}\n" 
  exit 1 
 
esac 
 
exit 0 

3.memcached_daemon.sh
监控memcached 进程,如进程失效则自动启动。

#!/bin/sh 
 
#memcached daemon 
 
root=$(cd "$(dirname "$0")"; pwd) 
 
. ${root}/memcached_inc.sh 
 
 
while : 
do 
 if [ -f "$memcached_pid_file" ] && [ -s "$memcached_pid_file" ]; then 
  pid=$(cat $memcached_pid_file) 
 else 
  pid="" 
 fi 
  
 if [ -z "$pid" ] || [ -z $(ps aux|awk '{print $2}' | grep "^$pid$") ]; then 
  $memcached 
  sleep 1 
  printf "[$(date +%y-%m-%d' '%h:%m:%s)] ${site} ${host} memcached ${port} is restarted\n" >> $error_log_file 
  echo "subject: ${site} ${host} memcached ${port} is restarted $(date +%y-%m-%d' '%h:%m:%s)" | sendmail me@gmail.com 
 fi 
 
 sleep 5 
 
done 
 
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

#!/bin/bash 
 
#连接数 
site_connects=$(netstat -ant | grep $ip:80 | wc -l) 
#当前连接数 
site_cur_connects=$(netstat -ant | grep $ip:80 | grep est | wc -l) 
 
#apache 
apache_speed=$(netstat -n | awk '/^tcp/ {++s[$nf]} end {for(a in s) print a, s[a]}') 
 
printf "[#start#]\n$(date '+%y-%m-%d %h:%m:%s')\n" 
printf "connects:${site_connects}\n" 
printf "cur connects:${site_cur_connects}\n" 
printf "apache_speed:\n${apache_speed}\n[#end#]\n\n" 
 
exit 0 

在终端设置crontab执行

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

2. 将apache status log 写入数据库
save_monitor_log.php

<?php 
 
$logfile = dirname(__file__).'/monitor.log'; 
 
$dbconfig = array( 
   'host' => '192.168.1.100', 
   'username' => 'username', 
   'password' => 'password', 
   'dbname' => 'mydb', 
   'tabname' => 'monitor_log' 
); 
 
$obj = new savemonitorlog($dbconfig, 'myweb'); 
$obj->load($logfile); 
 
 
// 讀取monitor log,記錄入db,查看db 
class savemonitorlog{ // class start 
 
 private $_apache_state = array('time_wait', 'close_wait', 'syn_sent', 'syn_recv', 'fin_wait1', 'fin_wait2', 'established', 'last_ack', 'closing'); 
 private $_dbconfig = array(); 
 private $_site = null; 
 
 
 /** init */ 
 public function __construct($dbconfig=array(), $site='web'){ 
  if(!isset($dbconfig['host']) || !isset($dbconfig['username']) || !isset($dbconfig['password']) || !isset($dbconfig['dbname']) || !isset($dbconfig['tabname'])){ 
   $this->debug('dbconfig error'); 
  } 
  $this->_dbconfig = $dbconfig; 
  $this->_site = $site; 
  $this->connectdb(); 
 } 
 
 
 /** load data 
 * @param string $logfile log文件 
 * @return boolean 
 */ 
 public function load($logfile){ 
 
  // 讀取log數據 
  if(file_exists($logfile)){ 
   $logdata = file_get_contents($logfile); 
   // 清空monitor.log 
   file_put_contents($logfile, '', true); 
  }else{ 
   return false; 
  } 
 
  // 正則分析數據 [#start#]*[#end#] 
  preg_match_all('/
#start#
(.*?)
#end#
.*?/si', $logdata, $data); 
 
  if(isset($data[1]) && count($data[1])>0){ 
   $alldata = $data[1]; 
   foreach($alldata as $val){ 
    $indb = $this->parser($val); 
    $newid = $this->addtodb($indb); 
   } 
  } 
 
 } 
 
 
 /** parser data 
 * @param array $data 
 * @return array 
 */ 
 private function parser($data){ 
  $indb = array(); 
  $tmp = explode(chr(10), $data); // 按換行分隔 
 
  $indb['site'] = $this->_site; 
  $indb['addtime'] = $tmp[1]; 
  $indb['connects'] = array_pop(explode(':',$tmp[2])); 
  $indb['cur_connects'] = array_pop(explode(':',$tmp[3])); 
 
  for($i=5, $max=count($tmp)-2; $i<$max; $i++){ 
   list($key, $num) = explode(' ', $tmp[$i]); 
   if(in_array($key, $this->_apache_state)){ 
    $indb[$key] = $num; 
   } 
  } 
 
  return $indb; 
 } 
 
 
 /** connect db */ 
 private function connectdb(){ 
  $conn=@mysql_connect($this->_dbconfig['host'], $this->_dbconfig['username'], $this->_dbconfig['password']) or die(mysql_error()); 
  mysql_select_db($this->_dbconfig['dbname'], $conn) or die(mysql_error()); 
 } 
 
 
 /** add to db */ 
 private function addtodb($indb){ 
  $insertkey = ''; 
  $insertval = ''; 
  if($indb){ 
   foreach($indb as $key=>$val){ 
    $insertkey .= $insertkey? " ,".$key : $key; 
    $insertval .= $insertval? " ,'".mysql_escape_string(trim($val))."'" : "'".mysql_escape_string(trim($val))."'"; 
   } 
   $sqlstr = "insert into ".$this->_dbconfig['tabname']."($insertkey) values($insertval)"; 
   $query = @mysql_query($sqlstr) or die(mysql_error()); 
   $id = mysql_insert_id(); 
   return $id? $id : false; 
  } 
 } 
 
 
 /** debug */ 
 private function debug($msg){ 
  exit($msg."\r\n"); 
 } 
 
 
} // class end 
 
?> 

在终端crontab执行

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

 
table monitor_log struct

create table if not exists `monitor_log` ( 
 `id` int(10) unsigned not null auto_increment, 
 `site` varchar(20) not null, 
 `connects` int(10) unsigned not null default '0', 
 `cur_connects` int(10) unsigned not null default '0', 
 `time_wait` int(10) unsigned not null default '0', 
 `close_wait` int(10) unsigned not null default '0', 
 `syn_sent` int(10) unsigned not null default '0', 
 `syn_recv` int(10) unsigned not null default '0', 
 `fin_wait1` int(10) unsigned not null default '0', 
 `fin_wait2` int(10) unsigned not null default '0', 
 `established` int(10) unsigned not null default '0', 
 `last_ack` int(10) unsigned not null default '0', 
 `closing` int(10) unsigned not null default '0', 
 `addtime` datetime not null, 
 primary key (`id`), 
 key `connects` (`connects`), 
 key `cur_connects` (`cur_connects`), 
 key `time_wait` (`time_wait`), 
 key `close_wait` (`close_wait`), 
 key `syn_sent` (`syn_sent`), 
 key `syn_recv` (`syn_recv`), 
 key `fin_wait1` (`fin_wait1`), 
 key `fin_wait2` (`fin_wait2`), 
 key `established` (`established`), 
 key `last_ack` (`last_ack`), 
 key `closing` (`closing`), 
 key `addtime` (`addtime`) 
) engine=myisam default charset=latin1 ; 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网