memcached Process Initiation and monitoring
1.memcached_inc.sh
Sets 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
_file= "${root}/memcached_${site}_${host}_${port}.log"
2.gm_memcached.sh
Control memcached start, stop, reboot.
#!/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 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}/${me
Mcached_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_p id=$ (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 Runni Ng\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 "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, such as failure of processes to 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_fi LE)
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
5 done
exit 0
How to use:
./gm_memcached.sh start #启动memcached
./gm_memcached.sh stop #停止memcached
./gm_memcached.sh Restart # Restart memcached
Shell records Apache status and automatically updates to database
1. Get Apache Status
monitor_log.sh
#!/bin/bash
#连接数
site_connects=$ (netstat-ant | grep $ip: | 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]} '
"[# 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
Set crontab execution at terminal
* * * * * */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); Read monitor log, record DB, View DB Class savemonitorlog{//class start Private $_apache_state = Array (' time_wait ', ' Close_w
AIT ', ' 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 ($DBCO nfig[' 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 data if (file_e
Xists ($logfile)) {$logdata = file_get_contents ($logfile);
Empty Monitor.log file_put_contents ($logfile, ', true ');
}else{return false;
//Analysis data [#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) {$ind
b = Array (); $tmp = Explode (CHR), $data);
Separate $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?>
Execute at Terminal crontab
0 0 * * * php/home/fdipzone/save_monitor_log.php
Table Monitor_log struct
CREATE TABLE IF not EXISTS ' Monitor_log ' (' id ' int (a) unsigned NOT NULL auto_increment, ' site ' varchar (?) NOT NULL , ' connects ' int (a) unsigned NOT null default ' 0 ', ' cur_connects ' int (a) unsigned NOT null default ' 0 ', ' time_wait ' int ' unsigned NOT null default ' 0 ', ' close_wait ' int (a) unsigned NOT null default ' 0 ', ' syn_sent ' int (a) unsign Ed NOT null default ' 0 ', ' syn_recv ' int (a) unsigned NOT null default ' 0 ', ' fin_wait1 ' int (a) unsigned NOT null Defau LT ' 0 ', ' fin_wait2 ' int (a) unsigned NOT null default ' 0 ', ' established ' int (a) unsigned NOT null default ' 0 ', ' LAS T_ack ' int (a) unsigned NOT null default ' 0 ', ' CLOSING ' int (a) 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;