Archive for March, 2013

0154 | script limit bandwidth UDP

Saturday, March 23rd, 2013 Posted in IP Network, Linux | 3 Comments »

เอา script ชาวบ้านมาดัดแปลงนิดหน่อยฮะ ต้นฉบับ shape ตาม IP แต่เอามาแก้ให้บีบเฉพาะ UDP
http://www.iplocation.net/tools/traffic-control.php

ซึ่งโดยการใช้งานปกติมันไม่จำเป็นต้องใช้ UDP เยอะอะไรขนาดนี้อยู่แล้ว ก็พอเอามาช่วยกันการยิง udp ออกได้อยู่หน่อยนึง

#!/bin/bash
#
# udplimiter This script limit the bandwidth for udp traffic
#
# chkconfig: - 13 87
 
### BEGIN INIT INFO
# Provides: $udplimiter
# Required-Start: $network
# Required-Stop: $network
# Default-Start:
# Default-Stop: 0 1 2 3 4 5 6
# Short-Description: start|stop|status|restart Bandwidth Limiter
# Description: This script limit the bandwidth for udp traffic
### END INIT INFO
 
 
#  tc uses the following units when passed as a parameter.
#  kbps: Kilobytes per second
#  mbps: Megabytes per second
#  kbit: Kilobits per second
#  mbit: Megabits per second
#  bps: Bytes per second
#       Amounts of data can be specified in:
#       kb or k: Kilobytes
#       mb or m: Megabytes
#       mbit: Megabits
#       kbit: Kilobits
#  To get the byte figure from bits, divide the number by 8 bit
#
 
#
# Name of the traffic control command.
TC=/sbin/tc
 
# The network interface we're planning on limiting bandwidth.
IF=eth0             # Interface
 
# Bandwidth limit (in mega bits)
BWLIMIT=5mbit          # DOWNLOAD Limit
 
# Filter options for limiting the intended interface.
U32="$TC filter add dev $IF protocol ip parent 1:0 prio 1 u32"
 
start() {
 
# We'll use Hierarchical Token Bucket (HTB) to shape bandwidth.
# For detailed configuration options, please consult Linux man
# page.
 
    $TC qdisc add dev $IF root handle 1: htb default 30
    $TC class add dev $IF parent 1: classid 1:1 htb rate $BWLIMIT
    $U32 match ip protocol 17 0xff flowid 1:1
 
# The first line creates the root qdisc, and the next two lines
# create two child qdisc that are to be used to shape download
# and upload bandwidth.
#
# The 4th and 5th line creates the filter to match the interface.
# The 'dst' IP address is used to limit download speed, and the
# 'src' IP address is used to limit upload speed.
 
}
 
stop() {
 
# Stop the bandwidth shaping.
    $TC qdisc del dev $IF root
 
}
 
restart() {
 
# Self-explanatory.
    stop
    sleep 1
    start
 
}
 
show() {
 
# Display status of traffic control status.
    $TC -s qdisc ls dev $IF
 
}
 
case "$1" in
 
  start)
 
    echo -n "Starting bandwidth shaping: "
    start
    echo "done"
    ;;
 
  stop)
 
    echo -n "Stopping bandwidth shaping: "
    stop
    echo "done"
    ;;
 
  restart)
 
    echo -n "Restarting bandwidth shaping: "
    restart
    echo "done"
    ;;
 
  show)
 
    echo "Bandwidth shaping status for $IF:"
    show
    echo ""
    ;;
 
  *)
 
    pwd=$(pwd)
    echo "Usage: $0 {start|stop|restart|show}"
    ;;
 
esac
 
exit 0

เอาไปเซฟใส่ /etc/init.d/udplimiter แล้วสั่ง /etc/init.d/udplimiter start ได้เลย ถ้าจะให้ทำงานตอนเปิดเครื่องอัตโนมัติด้วยก็ chkconfig udplimiter on

อ้อ อย่าลืมแก้ชื่อ interface ด้วยนะครับ ตรง IF=eth0

Tags: , ,

0153 | ความเชื่อผิดๆ ใน config apache ที่เจอบ่อย

Monday, March 11th, 2013 Posted in Linux, Web Server | No Comments »

เจอบ่อยๆ มาก็ค่อนข้างหงุดหงิดพอควร เรื่องการตั้งค่า “อะไรบางอย่าง” ของ apache ครับ… เวลาไปจูนเครื่องชาวบ้านแล้วเจออะไรแบบนี้ก็จะปวดหัวพอควร

ความจริงมี MySQL ด้วย แต่เขียนไปเขียนมา apache ก็ยาวแล้ว แยกไปเป็นครั้งหน้า (ที่ไม่รู้ว่าจะมาเมื่อไหร่) แล้วกัน 555+

<IfModule prefork.c>
StartServers       8
MinSpareServers    5
MaxSpareServers   20
ServerLimit      2000
MaxClients       2000
MaxRequestsPerChild  4000
</IfModule>

ตัวอื่นไม่ซีเรียส แต่ตัวที่มีปัญหาคือ ServerLimit กับ MaxClients ครับ สองค่านี้ส่วนมากจะไปคู่กันครับ เพราะ ServerLimit คือค่าสูงสุดที่อนุญาตให้ MaxClients ตั้งได้… (แล้วทำไมมึงต้องแยก! อ่าน docs แล้วยังไม่เข้าใจ) แต่เอาเป็นว่าสองค่านี้ควรจะเท่ากันเสมอครับ ด้านล่างเป็นคำอธิบายจากเว็บของ apache เอง

Special care must be taken when using this directive. If ServerLimit is set to a value much higher than necessary, extra, unused shared memory will be allocated. If both ServerLimit and MaxClients are set to values higher than the system can handle, Apache may not start or the system may become unstable.

With the prefork MPM, use this directive only if you need to set MaxClients higher than 256 (default). Do not set the value of this directive any higher than what you might want to set MaxClients to.

ด้วยความที่หลายคนคงเข้าใจไปว่า ค่าสองตัวนี้คือจำนวน uip หรือจำนวน user online บนหน้าเว็บ หรืออะไรก็มิอาจทราบได้ ทำให้ตั้งค่านี้สูงเกินจริง กะว่าเว็บจะรับคนเข้าได้เยอะๆ แต่หารู้ไม่… คุณเข้าใจผิดครับ

เอาล่ะ ทีนี้ถ้าเซตไอ้บ้านี่ไว้เยอะๆ จะเกิดอะไรขึ้น ขออธิบายเป็นข้อๆ ตามนี้ละกันครับ

  • Apache ปกติ จะใช้วิธีแตก process ย่อย 1 process ต่อ 1 connection
  • รูปแบบการทำงานปกติของเว็บคือ request then response and close คือ เมื่อ client ส่งคำร้องไปแล้ว server ควรจะตอบกลับไปให้เร็วที่สุด แล้วก็ตัดการเชื่อมต่อทันทีที่ส่งข้อมูลเสร็จ
  • หมายความว่า ณ เวลาเดียวกัน (simultaneously) ควรจะมี connection ค้างอยู่บน Apache ไม่เยอะ (ถ้าไม่มีการปล่อยให้ download ไฟล์ใหญ่ๆ)
  • ทีนี้ 1 process ของ apache (ที่มี PHP อยู่ด้วย) เนี่ย มันจะกินแรมประมาณ 20 MB เป็นอย่างน้อย แต่ค่าเฉลี่ยที่เจอจริงๆ คือ 40-50 MB
  • เนื่องจาก เราถือว่า apache ควรจะประมวลผลและตอบกลับไปได้เร็วที่สุด สมมติ 1 request ใช้เวลาตอบกลับได้ภายใน 0.2 วินาที เท่ากับว่า 1 process ของ apache สามารถรับ request ได้ วินาทีละ 5 request หมายความว่า ถ้าตั้งไว้ 100 process ก็สามารถรองรับได้ถึง 500 request ต่อวินาที !!
  • ทีนี้ ถ้าตั้งค่านี้สูงมากๆ กรณีที่เกิดมี script ค้างขึ้นมา… แล้วทำให้ apache มี connection (หรือ process) ค้างเป็นจำนวนมาก ปริมาณแรมที่ apache ใช้ก็จะพุ่งพรวดๆ จนเกินกว่าที่เครื่อง server มีให้ ก็จะไปกิน swap แล้วก็ตายอนาถ ล่มทั้งเครื่องในที่สุด แม้แต่ remote ก็ทำไม่ได้
  • การตั้งค่านี้ไว้ให้พอดี / ใกล้เคียงกับปริมาณแรมที่มี โดยเอาค่าเฉลี่ยการใช้แรมต่อ 1 process มาหารปริมาณแรมที่มี (เช่น มีแรม 8000 MB หารด้วยค่า process ละ 50 MB จะได้ค่าที่เหมาะสมคือราวๆ 160 ) จะช่วยลดผลกระทบเวลา server ทำงานหนักได้ดีมาก เพราะเมื่อ connection เต็ม apache ก็ยังกินแรมไม่หมด หรืออาจเกินมานิดหน่อย ทำให้ process อื่นยังทำงานได้ โดยเฉพาะ remote ทำให้เข้าไปแก้ไขปัญหาได้
  • การตั้งค่า maxclients ไว้สูง จึงไม่ได้หมายความว่า server จะรองรับการใช้งานได้เยอะขึ้นแต่อย่างใด … เอวัง

Tags: ,