服务器报警,出现 blocked for more than 120 seconds.

前几天服务器发生了一次报警,显示系统负载过高,Nagios监控发生超时。等我连上去看的时候,已经收到服务器恢复正常,报警解除的短信了。

 

Jul 16 12:14:11 web3 kernel: INFO: task nfsd:3269 blocked for more than 120 seconds.
Jul 16 12:14:11 web3 kernel: "echo 0 > /proc/sys/kernel/hung_task_timeout_secs" disables this message.

句的意思是nfsd和php-cgi这两个进程出现了超过120秒的阻塞现象。初步怀疑是机房的网络出现了短暂中断或者磁盘写入出现问题造成的,只是日志又正常写进去了,网络的原因可能性比较大。有待后续观察.....

This is a know bug. By default Linux uses up to 40% of the available memory for file system caching. After this mark has been reached the file system flushes all outstanding data to disk causing all following IOs going synchronous. For flushing out this data to disk this there is a time limit of 120 seconds by default. In the case here the IO subsystem is not fast enough to flush the data withing 120 seconds. This especially happens on systems with a lof of memory.

The problem is solved in later kernels and there is not “fix” from Oracle. I fixed this by lowering the mark for flushing the cache from 40% to 10% by setting “vm.dirty_ratio=10″ in /etc/sysctl.conf. This setting does not influence overall database performance since you hopefully use Direct IO and bypass the file system cache completely.

原理:linux会设置40%的可用内存用来做系统cache,当flush数据时这40%内存中的数据由于和IO同步问题导致超时(120s),所将40%减小到10%,避免超时。

简单讲就是设置在文件 /etc/sysctl.conf中加入 “vm.dirty_ratio=10″ 。

INFO: task httpd:23402 blocked for more than 120 seconds.
2011-07-17 2:34

Jul 17 00:43:24 bogon kernel: INFO: task httpd:23402 blocked for more than 120 seconds.

Jul 17 00:44:29 bogon kernel: "echo 0 > /proc/sys/kernel/hung_task_timeout_secs" disables this message.

修改了:MaxClients 不知道能解决问题不
===================
问题解决了:time_wait连接过多的问题

利用sysctl工具调整linux核心参数

sysctl配置与显示在/proc/sys目录中的内核参数.可以用sysctl来设置或重新设置联网功能,如IP转发、IP碎片去除以及源路由检查等。用户只需要编辑/etc/sysctl.conf文件,即可手工或自动执行由sysctl控制的功能。
    命令格式:
    sysctl [-n] [-e] -w variable=value
    sysctl [-n] [-e] -p  (default /etc/sysctl.conf)
    sysctl [-n] [-e] -a
    常用参数的意义:
    -w   临时改变某个指定参数的值,如
         sysctl -w net.ipv4.ip_forward=1
    -a   显示所有的系统参数
    -p   从指定的文件加载系统参数,如不指定即从/etc/sysctl.conf中加载
    如果仅仅是想临时改变某个系统参数的值,可以用两种方法来实现,例如想启用IP路由转发功能:
    1) #echo 1 > /proc/sys/net/ipv4/ip_forward
    2) #sysctl -w net.ipv4.ip_forward=1
    以上两种方法都可能立即开启路由功能,但如果系统重启,或执行了
    # service network restart
命令,所设置的值即会丢失,如果想永久保留配置,可以修改/etc/sysctl.conf文件
将 net.ipv4.ip_forward=0改为net.ipv4.ip_forward=1

 

 

 

 

 

 

 

 
block devices:块设备,通常是随机的。修改时可用
echo ‘  ’>  /proc/..    大部分不能用vim进行编辑
/proc 虚拟文件系统,实时更新,目录不会变,不完全是纯文本文件,管理员可对其修改。
/proc/cpuinfo  cpu信息
/proc/l/*
/proc/partitions分区信息
/proc/meminfo 内存信息
/proc/apm 高级电源管理
/proc/filesystems 文件系统信息
/proc/dma  DMA信息
  /proc/sys 系统运行的文件
/proc/sys /ip_forward 是否起用本机路由功能。可通过echosysctl对其进行修改。
 
 
 
以下為/etc/sysctl.conf下特定參數意義
pico /etc/sysctl.conf

and replace the contents of the file with the following:


# Kernel sysctl configuration file for Red Hat Linux
#
# For binary values, 0 is disabled, 1 is enabled. See sysctl(8) and
# sysctl.conf(5) for more details.

# Disables packet forwarding
net.ipv4.ip_forward=0

# Disables IP source routing
net.ipv4.conf.all.accept_source_route = 0
net.ipv4.conf.lo.accept_source_route = 0
net.ipv4.conf.eth0.accept_source_route = 0
net.ipv4.conf.default.accept_source_route = 0

# Enable IP spoofing protection, turn on source route verification
net.ipv4.conf.all.rp_filter = 1
net.ipv4.conf.lo.rp_filter = 1
net.ipv4.conf.eth0.rp_filter = 1
net.ipv4.conf.default.rp_filter = 1

# Disable ICMP Redirect Acceptance
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.lo.accept_redirects = 0
net.ipv4.conf.eth0.accept_redirects = 0
net.ipv4.conf.default.accept_redirects = 0

# Enable Log Spoofed Packets, Source Routed Packets, Redirect Packets
net.ipv4.conf.all.log_martians = 0
net.ipv4.conf.lo.log_martians = 0
net.ipv4.conf.eth0.log_martians = 0

# Disables the magic-sysrq key
kernel.sysrq = 0

# Decrease the time default value for tcp_fin_timeout connection
net.ipv4.tcp_fin_timeout = 15

# Decrease the time default value for tcp_keepalive_time connection
net.ipv4.tcp_keepalive_time = 1800

# Turn off the tcp_window_scaling
net.ipv4.tcp_window_scaling = 0

# Turn off the tcp_sack
net.ipv4.tcp_sack = 0

# Turn off the tcp_timestamps
net.ipv4.tcp_timestamps = 0

# Enable TCP SYN Cookie Protection
net.ipv4.tcp_syncookies = 1

# Enable ignoring broadcasts request
net.ipv4.icmp_echo_ignore_broadcasts = 1

# Enable bad error message Protection
net.ipv4.icmp_ignore_bogus_error_responses = 1

# Log Spoofed Packets, Source Routed Packets, Redirect Packets
net.ipv4.conf.all.log_martians = 1

# Increases the size of the socket queue (effectively, q0).
net.ipv4.tcp_max_syn_backlog = 1024

# Increase the tcp-time-wait buckets pool size
net.ipv4.tcp_max_tw_buckets = 1440000

# Allowed local port range
net.ipv4.ip_local_port_range = 16384 65536

CTRL + X to exit and save the file

To make your changes take effect immediately, type this command:
/sbin/sysctl -p

引用自:FreeBSD下构建安全的Web服务器
作者:heiyeluren http://www.unixsky.net
编辑 /etc/sysctl.conf 文件,在里面加入如下内容:(有注释)

#最大的待发送TCP数据缓冲区空间
net.inet.tcp.sendspace=65536

#最大的接受TCP缓冲区空间
net.inet.tcp.recvspace=65536

#最大的接受UDP缓冲区大小
net.inet.udp.sendspace=65535

#最大的发送UDP数据缓冲区大小
net.inet.udp.maxdgram=65535

#本地套接字连接的数据发送空间
net.local.stream.sendspace=65535

#加快网络性能的协议
net.inet.tcp.rfc1323=1
net.inet.tcp.rfc1644=1
net.inet.tcp.rfc3042=1
net.inet.tcp.rfc3390=1

#最大的套接字缓冲区
kern.ipc.maxsockbuf=2097152

#系统中允许的最多文件数量
kern.maxfiles=65536

#每个进程能够同时打开的最大文件数量
kern.maxfilesperproc=32768

#当一台计算机发起TCP连接请求时,系统会回应ACK应答数据包。该选项设置是否延迟ACK应答数据包,把它和包含数据的数据包一起发送,在高速网络 和低负载的情况下会略微提高性能,但在网络连接较差的时候,对方计算机得不到应答会持续发起连接请求,反而会降低性能。
net.inet.tcp.delayed_ack=0

#屏蔽ICMP重定向功能
net.inet.icmp.drop_redirect=1
net.inet.icmp.log_redirect=1
net.inet.ip.redirect=0
net.inet6.ip6.redirect=0

#防止ICMP广播风暴
net.inet.icmp.bmcastecho=0
net.inet.icmp.maskrepl=0

#限制系统发送ICMP速率
net.inet.icmp.icmplim=100

#安全参数,编译内核的时候加了options TCP_DROP_SYNFIN才可以用
net.inet.icmp.icmplim_output=0
net.inet.tcp.drop_synfin=1

#设置为1会帮助系统清除没有正常断开的TCP连接,这增加了一些网络带宽的使用,但是一些死掉的连接最终能被识别并清除。死的TCP连接是被拨号用户存取的系统的一个特别的问题,因为用户经常断开modem而不正确的关闭活动的连接
net.inet.tcp.always_keepalive=1

#若看到net.inet.ip.intr_queue_drops这个在增加,就要调大net.inet.ip.intr_queue_maxlen,为0最好
net.inet.ip.intr_queue_maxlen=1000

#防止DOS***,默认为30000
net.inet.tcp.msl=7500

#接收到一个已经关闭的端口发来的所有包,直接drop,如果设置为1则是只针对TCP包
net.inet.tcp.blackhole=2

#接收到一个已经关闭的端口发来的所有UDP包直接drop
net.inet.udp.blackhole=1

#为网络数据连接时提供缓冲
net.inet.tcp.inflight.enable=1

#如果打开的话每个目标地址一次转发成功以后它的数据都将被记录进路由表和arp数据表,节约路由的计算时间,但会需要大量的内核内存空间来保存路由表
net.inet.ip.fastforwarding=0

#kernel编译打开options POLLING功能,高负载情况下使用低负载不推荐SMP不能和polling一起用
#kern.polling.enable=1

#并发连接数,默认为128,推荐在1024-4096之间,数字越大占用内存也越大
kern.ipc.somaxconn=32768

#禁止用户查看其他用户的进程
security.bsd.see_other_uids=0

#设置kernel安全级别
kern.securelevel=0

#记录下任何TCP连接
net.inet.tcp.log_in_vain=1

#记录下任何UDP连接
net.inet.udp.log_in_vain=1

#防止不正确的udp包的***
net.inet.udp.checksum=1

#防止DOS***
net.inet.tcp.syncookies=1

#仅为线程提供物理内存支持,需要256兆以上内存
kern.ipc.shm_use_phys=1

# 线程可使用的最大共享内存
kern.ipc.shmmax=67108864

# 最大线程数量
kern.ipc.shmall=32768

# 程序崩溃时不记录
kern.coredump=0

# lo本地数据流接收和发送空间
net.local.stream.recvspace=65536
net.local.dgram.maxdgram=16384
net.local.dgram.recvspace=65536

# 数据包数据段大小,ADSL为1452。
net.inet.tcp.mssdflt=1460

# 为网络数据连接时提供缓冲
net.inet.tcp.inflight_enable=1

# 数据包数据段最小值,ADSL为1452
net.inet.tcp.minmss=1460

# 本地数据最大数量
net.inet.raw.maxdgram=65536

# 本地数据流接收空间
net.inet.raw.recvspace=65536

#ipfw防火墙动态规则数量,默认为4096,增大该值可以防止某些病毒发送大量TCP连接,导致不能建立正常连接
net.inet.ip.fw.dyn_max=65535

#设置ipf防火墙TCP连接空闲保留时间,默认8640000(120小时)
net.inet.ipf.fr_tcpidletimeout=864000