Memcached process startup and monitoring 1. memcached_inc.sh sets the path, port, and other information. #! Binsh # configincludeHOST $ (hostname) Handle
Memcached process startup and monitoring 1. memcached_inc.sh sets the path, port, and other information. #! /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 process initiation and monitoring
1. memcached_inc.sh
Set the path, port, and other information.
#!/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
Control memcached startup, stop, and restart.
#!/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
Monitor memcached processes. If the process fails, it is automatically started.
#!/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
Usage:
./Gm_memcached.sh start # start memcached./gm_memcached.sh stop # stop memcached./gm_memcached.sh restart # restart memcached
The shell records apache status and automatically updates it to the database.
1. Obtain apache status
Monitor_log.sh
#! /Bin/bash # Number of connections site_connects =$ (netstat-ant | grep $ ip: 80 | wc-l) # current number of connections 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, 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 "exit 0
Set crontab on the terminal for execution
* * * * * /home/fdipzone/monitor_log.sh >> /home/fdipzone/monitor.log
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', 'dbname' => 'mydb ', 'tabname' => 'Monitor _ log'); $ obj = new SaveMonitorLog ($ dbconfig, 'myweb'); $ obj-> load ($ logfile ); // retrieve the monitor log from the listener, record the listener into the db, and view the 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 File * @ return boolean */public function load ($ logfile) {// retrieve log data if (file_exists ($ logfile) {$ logdata = file_get_contents ($ logfile); // clear the monitor. log file_put_contents ($ logfile, '', true);} else {return false;} // positive analysis data Else [# 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 ); // separate $ indb ['SITE'] = $ this-> _ site; $ indb ['addtime'] = $ tmp [1]; $ indb ['connects'] = Array_pop (explode (':', $ tmp [2]); $ indb ['cur _ CTS '] = 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-> _ db Config ['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 ". $ 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?>
Run crontab on the terminal.
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 ;