Php+shell script Operations memcached and Apache status instance sharing, memcachedapache_php tutorial

Source: Internet
Author: User

Php+shell script Operations memcached and Apache status instance sharing, Memcachedapache


memcached process Start-up and monitoring
1.memcached_inc.sh
Set the path, port and other messages.

#!/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 "  

2.gm_memcached.sh
Control memcached Start, stop, restart.

#!/bin/sh #memcached start and stop #$1 action root=$ (CD "$ (dirname") "; pwd"). ${root}/memcached_inc.sh Start () {if [-f] $MEMCACHED _pid_file "] && [-S" $MEMCACHED _pid_file "]; then PRI ntf "Memcached already running\n" Else printf "starting memcached\n" $MEMCACHED sleep 2 pid=$ (cat $MEMCACHED _pi D_file) printf "memcached is started PID: $PID \ n" printf "starting memcached daemon\n" ${root}/${memcached_daemon_fi   LE} & daemon_pid=$! Echo ${daemon_pid} > ${memcached_daemon_pid_file} printf "MEMCACHED DAEMON is started pid:${daemon_pid}\n" fi} s Top () {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 was stopped\n" else printf "no memcached running\n" fi} case "$" I   n 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
Monitor the memcached process, such as process failure, and start automatically.

#!/bin/sh  #memcached Daemon  root=$ (cd "$ (dirname") "; 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 $} ' | 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  

How to use:


Shell records Apache status and updates to database automatically
1. Get Apache Status
monitor_log.sh

#!/bin/bash  #连接数 site_connects=$ (netstat-ant | grep $ip: | wc-l) #当前连接数 site_cur_connects=$ (netstat-ant | grep $i p:80 | grep EST | wc-l)  #apache apache_speed=$ (netstat-n | awk '/^tcp/{++s[$NF]} END {for (A-in S) print A, S[a]} ')  printf "[#star t#]\n$ (date ' +%y-%m-%d%h:%m:%s ') \ nthe "printf" connects:${site_connects}\n "printf" cur connects:${site_cur_connects}\n "printf" apache_speed:\n${apache_speed}\n[#end #]\n\n "  

Crontab execution in Terminal setup

2. Write Apache status log to the database
save_monitor_log.php

<?php $logfile = dirname (__file__). '  /monitor.log '; $dbconfig = Array (' host ' = ' 192.168.1.100 ', ' username ' = ' username ', ' password ' = ' password ', ' db '  Name ' = ' mydb ', ' tabname ' = ' monitor_log '); $obj = new Savemonitorlog ($dbconfig, ' myweb ');   $obj->load ($logfile); Read monitor log, record in db, view 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 ($dbconf ig[' 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 file * @return Boolean */Public Function load ($logfile) {//read log according to if (File_exists ($logfile)) {$logdata = File_get_co    Ntents ($logfile);   Empty Monitor.log file_put_contents ($logfile, ", true);   }else{return false; }//The analysis of the numbers [#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), $data);   separated by line $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->_dbcon   fig[' 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?>

Execute at Terminal crontab


Table Monitor_log struct

CREATE TABLE IF not EXISTS ' Monitor_log ' (' id ' int (ten) unsigned NOT NULL auto_increment, ' site ' varchar () is not NULL, ' Connects ' int (ten) unsigned NOT null default ' 0 ', ' cur_connects ' int (ten) unsigned NOT null default ' 0 ', ' time_wait ' int ( unsigned NOT NULL default ' 0 ', ' close_wait ' int (ten) unsigned NOT null default ' 0 ', ' syn_sent ' int (ten) unsigned not N ULL default ' 0 ', ' syn_recv ' int (ten) unsigned NOT null default ' 0 ', ' fin_wait1 ' int (ten) unsigned NOT null default ' 0 ', ' Fin_wait2 ' int (unsigned) not null default ' 0 ', ' established ' Int (ten) unsigned NOT null default ' 0 ', ' last_ack ' int (10 ) unsigned NOT NULL default ' 0 ', ' CLOSING ' int (ten) 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 ' CLOS E_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;

Articles you may be interested in:

    • Php+shell Method of implementing multithreading
    • PHP-FPM boot automatically starts shell script
    • Shell, Perl, Python, PHP access MySQL database Code instance
    • Implementing update PHP5 in shell scripts
    • Shell script Implementation launches PHP built-in fastcgi Server
    • PHP and Shell implementations check if Samba and NFS server exist
    • Ruby, PHP, Shell implementations for a prime number within 50
    • Shell Script Federated php script capture site PV and Alexa rankings
    • PHP Management Nginx Virtual Host Shell script instance
    • Monitor server swap and restart PHP shell script

http://www.bkjia.com/PHPjc/1108606.html www.bkjia.com true http://www.bkjia.com/PHPjc/1108606.html techarticle Php+shell script Operations memcached and Apache status instance sharing, Memcachedapache memcached process Start-up and monitoring 1.memcached_inc.sh set the path, port and other messages. #!/bin/sh #conf ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.