欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

Memcached调试/服务器日志监视Memcached服务器? 日志 log 有大用

 
活动 8年零3个月前    
已浏览 40k次    
               
1个                    
               

我有一个基于Memcached变量的聊天引擎,将它们放入数组并通过jquery读取它们的另一端,                    

可以在95%的时间内正常运行,但是,当服务器负载很高时,memcached(假定其为memcached)崩溃会导致浏览器卡住。                    

我不认为它的jquery问题,因为这仅在服务器负载很高时才会发生。                    

我需要一种方法来监视内存缓存的服务器或以某种方式将日志文件写入失败/错误发生的位置...                    

关于我该怎么做的任何想法?或任何知道为何memcached服务器发生故障?我按如下方式运行memcached                    

$GLOBALS['MemCached'] = FALSE;
$GLOBALS['MemCached'] = new Memcache;
$GLOBALS['MemCached']->pconnect('localhost', 11211);
                   

我的memcached配置如下                    

#! /bin/sh
#
# chkconfig: - 55 45
# description:  The memcached daemon is a network memory cache service.
# processname: memcached
# config: /etc/sysconfig/memcached
# pidfile: /var/run/memcached/memcached.pid

# Standard LSB functions
#. /lib/lsb/init-functions

# Source function library.
. /etc/init.d/functions

PORT=11211
USER=memcached
MAXCONN=1024
CACHESIZE=128
OPTIONS=""

if [ -f /etc/sysconfig/memcached ];then 
    . /etc/sysconfig/memcached
fi

# Check that networking is up.
. /etc/sysconfig/network

if [ "$NETWORKING" = "no" ]
then
    exit 0
fi

RETVAL=0
prog="memcached"
pidfile=${PIDFILE-/var/run/memcached/memcached.pid}
lockfile=${LOCKFILE-/var/lock/subsys/memcached}

start () {
    echo -n $"Starting $prog: "
    # Ensure that /var/run/memcached has proper permissions
    if [ "`stat -c %U /var/run/memcached`" != "$USER" ]; then
        chown $USER /var/run/memcached
    fi

    daemon --pidfile ${pidfile} memcached -d -p $PORT -u $USER  -m $CACHESIZE -c $MAXCONN -P ${pidfile} $OPTIONS
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] && touch ${lockfile}
}
stop () {
    echo -n $"Stopping $prog: "
    killproc -p ${pidfile} /usr/bin/memcached
    RETVAL=$?
    echo
    if [ $RETVAL -eq 0 ] ; then
        rm -f ${lockfile} ${pidfile}
    fi
}

restart () {
        stop
        start
}


# See how we were called.
case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  status)
    status -p ${pidfile} memcached
    RETVAL=$?
    ;;
  restart|reload|force-reload)
    restart
    ;;
  condrestart|try-restart)
    [ -f ${lockfile} ] && restart || :
    ;;
  *)
    echo $"Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart|try-restart}"
    RETVAL=2
        ;;
esac

exit $RETVAL
               
                       
  改善这个问题                               
2012年8月29日在7:11                                
                                   
                               
  • cat /etc/sysconfig/memcached –量子 12年 8月29日在7:37                                
  • 1个                                
    PORT =“ 11211” USER =“ memcached” MAXCONN =“ 20480” CACHESIZE =“ 4096” OPTIONS =“” –  user1179459 2012年 8月29日8:55                                
添加评论                
       

2个答案                    

活性最老的投票数                    
       
8                        
                   

首先,您没有显示memcached配置,而是显示了启动脚本。您需要寻找一个名为like的文件/etc/memcached.conf要启用日志记录,您需要取消注释该行                        

logfile /var/log/memcached.log
                       

另外,您可以通过启用-v-vv选项来增加详细程度                        

如果memcached崩溃了,您可以查看系统日志/var/log/messages/var/log/syslog检查可能的错误情况。                        

  改善这个答案                                   
2012年8月29日在7:19回答                                    
                                       
                                   
  • 我在我的系统中没有名为memcached.conf的文件,仅拥有一个名为.. / etc / sysconfig / memcached的文件。PORT =“ 11211” USER =“ memcached” MAXCONN =“ 20480” CACHESIZE =“ 4096” OPTIONS =“” –  user1179459 2012年 8月29日8:56                                    
  • 1个                                    
    在这种情况下,如我的情况一样,配置位于init脚本本身中。从源安装memcached时,这就是memcached.sysv初始化脚本的发送方式。 – emhohensee 16年 1月25日在20:38                                    
添加评论                    
               
       
4                        
                   

在中定义一个附加变量/etc/sysconfig/memcached                        

PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="64"
LOGFILE="/var/log/memcached.log"
OPTIONS=""
                       

start()在初始化脚本中编辑函数,如下所示:                        

start () {
    #echo -n $"Starting $prog: "
    # insure that /var/run/memcached has proper permissions
    if [ "`stat -c %U /var/run/memcached`" != "$USER" ]; then
        chown $USER /var/run/memcached
    fi

    #daemon --pidfile /var/run/memcached/memcached.pid memcached -d -p $PORT -u $USER  -m $CACHESIZE -c $MAXCONN -P /var/run/memcached/memcached.pid $OPTIONS -vv > $LOGFILE 2>&1
    $prog -d -p $PORT -u $USER  -m $CACHESIZE -c $MAXCONN -P /var/run/memcached/memcached.pid $OPTIONS -vv > $LOGFILE 2>&1
    RETVAL=$?
    #echo
    [ $RETVAL -eq 0 ] && action $"Starting $prog: " /bin/true && touch /var/lock/subsys/memcached
}
                       

然后重新启动memcached,您将在看到以下内容/var/log/memcached.log                        

slab class  40: chunk size    616944 perslab       1
slab class  41: chunk size    771184 perslab       1
slab class  42: chunk size   1048576 perslab       1
<26 server listening (auto-negotiate)
<27 server listening (auto-negotiate)
<28 send buffer was 129024, now 268435456
<29 send buffer was 129024, now 268435456
<28 server listening (udp)
<29 server listening (udp)
                       

不要忘记配置logrotate。                        

  改善这个答案                                   
2012年8月29日在9:23回答                                    
                                       
                                   
添加评论                    
       

你的答案            


           

来自  https://serverfault.com/questions/422350/memcached-debuging-server-logs-monitor-the-memcached-servers/422359            


           


           

How to specify the log file for memcached on RHEL / CentOS                

Ask Question                
Asked 
Active 5 years, 3 months ago                
Viewed 29k times
                           
5
                           

I'm running memcached 1.4.5 on RHEL5.5. I installed this using yum which has installed the standard memcached script in /etc/init.d and the configuration file in /etc/sysconfig/memcached:

PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="256"
OPTIONS=""
                               

Everything is working fine with one exception - I can't figure out how to specify the log file location. I'd like to put the log file in /var/log but the help nor the manual specify how to do this.

Is it possible to achieve this using the base scripts?

                                   
  improve this question                                           
asked Dec 2 '10 at 21:18                                            
                                               
                                           
add a comment                            
                   

4 Answers   正确答案 

ActiveOldestVotes                                
                   
1
                               

A suggestion to create a debugging log for memcached in a previous post:

Assuming user nobody has write permissions to /tmp, try the following, noting the position of 2:


memcached -d -m 3072 -l localhost -p 11211 -u nobody -v 2>>/tmp/memcached.log

If user nobody can write to /var/log then I'd assume it's possible to swap out /tmp with /var/log in the above command.

  improve this answer                                               
answered Dec 2 '10 at 21:44                                                
                                                   
                                               
  • 3                                                
    this is a 'hack' and is not the authentic solution for RHEL/CentOS. Preferred solution is OPTIONS option in /etc/sysconfig/memcached, the official RHEL package settings file location (detailed by @Ben-Johnson below) – Joey T May 1 '13 at 23:55                                                 
  • 1                                                
    This is bad because it doesn't allow the logfile to be closed (rotated) without restarted memcached. You should pipe to the logger(1) command instead, or similar. For systemd-based systems, this advise should likely change. – Cameron Kerr Aug 3 '15 at 11:28                                                
add a comment                                
                           
                   
31
                               

Modify the OPTIONS line in /etc/sysconfig/memcached adding ">> /var/log/memcached 2>&1" on the end. IE

OPTIONS="-vv >> /var/log/memcached 2>&1"
                               
  improve this answer                                               
edited Dec 4 '12 at 0:18                                                
                                                   
                                               
Mark Henderson                                                    
67.4k30 gold badges172 silver badges251 bronze badges                                                    
answered Nov 29 '12 at 20:50                                                
                                                   
                                               
  • 1                                                
    this should be marked as actual solution, as it is the RHEL/CentOS specific solution – Joey T May 1 '13 at 23:53                                                
  • I'd suggest appending ".log" to that log file name to make it memcached.log so logrotate default config will rotate the log. – Larry Silverman Nov 11 '14 at 16:03                                                
  • This is bad because it doesn't allow the logfile to be closed (rotated) without restarted memcached. You should pipe to the logger(1) command instead, or similar. For systemd-based systems, this advise should likely change. – Cameron Kerr Aug 3 '15 at 11:50                                                
add a comment                                
                   
10
                               

Using 2>&1 >> logfile in the OPTIONS or init script is not wise, as the log file will not get reopened when log rotation occurs, assuming of course that you have put something in place for that to occur. The implication of this is that you'll end up with rotated log files (empty, depending on rotation settings) and your actual log data will be written to a file for which no directory entry actually exists, which means that it will get deleted when the file is closed. For the logfile to be closed when intended, and therefore safely rotated, you would have to restart memcached (goodbye cache!).

Instead, use something like the logger(1) command to log to syslog (with a particular facility such as local1, and have your syslog rules route it appropriately (hello timestamps!),

OPTIONS=" 2>&1 | logger -i -p local1.info -t memcached"
                                   

A rule to route the messages specially in syslog is optional, but if you're using rsyslog, then this would help:

local1.debug  /var/log/memcached/memcached.log
                                   

Don't forget a logrotate rule, and because you're using logger, you don't need to worry about closing any log files, because syslog will do that for you.

# cat /etc/logrotate.d/memcached
/var/log/memcached/memcached.log {
    daily
    rotate 3
    dateext
    missingok
    create 0640 root root
    compress
    delaycompress
    postrotate
        #/bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true
        /bin/kill -HUP `cat /var/run/syslogd.pid`
    endscript
}
                                   

FWIW, the situtation is different for EL7 (systemd) systems

For systemd systems, it will take care of collecting the output of stderr and logging that, so if you just include -v (still very quiet) or -vv (too verbose), then you can query those logs with journalctl:

# journalctl --since '2012-01-01' _SYSTEMD_UNIT=memcached.service
-- Logs begin at Fri 2015-07-10 11:00:21 NZST, end at Mon 2015-08-03 23:36:49 NZST. --
Aug 03 23:36:49 HOSTNAME memcached[4318]: slab class  17: chunk size      3632 perslab     288
Aug 03 23:36:49 HOSTNAME memcached[4318]: slab class  18: chunk size      4544 perslab     230
...
Aug 03 23:36:49 HOSTNAME memcached[4318]: slab class  42: chunk size   1048576 perslab       1
Aug 03 23:36:49 HOSTNAME memcached[4318]: <26 server listening (auto-negotiate)
Aug 03 23:36:49 HOSTNAME memcached[4318]: <27 send buffer was 212992, now 268435456
Aug 03 23:36:49 HOSTNAME memcached[4318]: <27 server listening (udp)
Aug 03 23:36:49 HOSTNAME memcached[4318]: <27 server listening (udp)
Aug 03 23:36:49 HOSTNAME memcached[4318]: <27 server listening (udp)
Aug 03 23:36:49 HOSTNAME memcached[4318]: <27 server listening (udp)
                                   

Although you don't find these logs existing anywhere under /var/log/; the logs are held in a binary format so you can use journalctl --output json ... if you wanted. See /etc/systemd/journald.conf for more information.

From beginning to end (albeit for RHEL7 / systemd servers)

# cat /etc/sysconfig/memcached
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS="-l 127.0.0.1 -vv"


# cat /usr/lib/systemd/system/memcached.service
[Unit]
Description=Memcached
Before=httpd.service
After=network.target

[Service]
Type=simple
EnvironmentFile=-/etc/sysconfig/memcached
ExecStart=/usr/bin/memcached -u $USER -p $PORT -m $CACHESIZE -c $MAXCONN $OPTIONS

[Install]
WantedBy=multi-user.target


# cat /etc/systemd/system/memcached.service.d/local.conf
[Service]
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=memcached
SyslogFacility=local1
SyslogLevel=debug
SyslogLevelPrefix=false


# systemctl daemon-reload
# systemctl restart memcached.service

# systemctl status memcached
memcached.service - Memcached
   Loaded: loaded (/usr/lib/systemd/system/memcached.service; enabled)
  Drop-In: /etc/systemd/system/memcached.service.d
           └─local.conf                                                      <---------- NOTE
   Active: active (running) since Tue 2015-08-04 01:07:50 NZST; 7s ago
 Main PID: 3842 (memcached)
   CGroup: /system.slice/memcached.service
           └─3842 /usr/bin/memcached -u memcached -p 11211 -m 64 -c 1024 -l 127.0.0.1 -vv
...

// Note: rsyslogd uses its imjournal module to read logs from journald;
// make sure you have this configured if you've brought your rsyslog
// config from a previous version of RHEL

# echo "local1.debug  /var/log/memcached/memcached.log" >> /etc/rsyslog.d/memcached.conf
# mkdir /var/log/memcached
# systemctl restart rsyslog.service
# systemctl status rsyslog.service

// Don't forget log rotation

# cat /etc/logrotate.d/memcached
/var/log/memcached/memcached.log {
    daily
    rotate 3
    dateext
    missingok
    create 0640 root root
    compress
    delaycompress
    postrotate
        /bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true
    endscript
}
                               
  improve this answer                                               
edited Aug 3 '15 at 14:16                                                
answered Aug 3 '15 at 11:26                                                
                                                   
                                               
add a comment                                
                   
2
                               

Memcached doesn't provide a commandline option for log file location. On Ubuntu, there's a wrapper script called start-memcached which parses an /etc/memcached.conf which contains a logfile parameter. Unfortunately, this doesn't seem to exist on CentOS/RHEL. I think you'd have to modify the init script to accomplish what you want. It does seem to be a bit of an oversight - a quick googling shows that other people have this problem too, so maybe its something they'll fix soon (or have fixed already and I'm not aware of). Or you could try the startup scripts from Ubuntu.

  improve this answer                                               
answered Dec 2 '10 at 21:39                                                
                                                   
                                               
add a comment                                
                   

Your Answer


                       

来自  https://serverfault.com/questions/208538/how-to-specify-the-log-file-for-memcached-on-rhel-centos


           


普通分类: