เก็บ IP Traffic Log ด้วย ulogd (Debian Admin Style)
จริง ๆ ก็ครบกำหนดการเก็บข้อมูลการจราจร (IP Traffic) ตั้งแต่วันที่ 23 สิงหาคม 2551 (1 ปี ให้หลัง จากประกาศ) แต่ด้วยวุ่น ๆ กับหลายเรื่อง (ลูกสาวก็อายุ 3 เดือนแล้วครับ :P) ก็ลองหาการเก็บ log ในแบบที่เราคุ้นเคย และพยายามไม่ให้กระทบกับ performance ของระบบ ก็เลยได้ลงเอยกับ ulogd กับ ulogd-pcap (ตอนนี้ เป็น version 1.24 ส่วน version 2 ยังเป็นรุ่นทดสอบ ที่พยายาม build แล้ว ยังไม่สำเร็จ :P)
เริ่มเลยละกันครับ
- ติดตั้ง packages ที่จำเป็น
# aptitude install ulogd ulogd-pcap tshark
- เขียน script มาจัดการเกี่ยวกับ log
# vi /usr/local/bin/traffic-ulogd.sh
#!/bin/sh ULOGD_CONF_TPL=/etc/ulogd.conf.tpl ULOGD_CONF=/etc/ulogd.conf LOG_DIR=/home/log/ip-traffic PARENT_DIR=`date +%Y-%m` PARENT_DIR="$LOG_DIR/$PARENT_DIR" DAILY_LOG=`date +%Y-%m-%d` DAILY_LOG="$PARENT_DIR/$DAILY_LOG.pcap" # Copy original debian ulogd.conf file to be # the future config template. if [ ! -f $ULOGD_CONF_TPL ]; then echo "Copying default $ULOGD_CONF to $ULOGD_CONF_TPL" cp $ULOGD_CONF $ULOGD_CONF_TPL fi # Create the directory structure for logging if [ ! -d $PARENT_DIR ]; then echo "Make directory $PARENT_DIR" mkdir -p $PARENT_DIR fi # Edit template config file cat $ULOGD_CONF_TPL | \ sed -e 's:^#\(plugin="/usr/lib/ulogd/ulogd_PCAP.so"\):\1:g' | \ sed -e "s:/var/log/ulog/pcap.log:$DAILY_LOG:g" > /tmp/ulogd.conf mv /tmp/ulogd.conf $ULOGD_CONF # Restarting to reload config file /etc/init.d/ulogd restart
เป็น script ที่จะคอยจัดการเกี่ยวกับ directory สำหรับจัดเก็บ ในรูปแบบ 2008-08, 2008-09 และจัดเก็บ log file ในรูปแบบ 2008-08-26.pcap,...
# chmod u+x /usr/local/bin/traffic-ulogd.sh # /usr/local/bin/traffic-ulogd.sh
สั่งให้ script จัด config ให้ และเริ่มรอการ log
- กำหนด iptables rule ให้ทำการ log
# iptables -I FORWARD -j ULOG --ulog-cprange 100 --ulog-qthreshold 50
- ลองดูคร่าว ๆ ว่ามี packet ผ่านเข้า log ไหม
# iptables -nvL FORWARD Chain FORWARD (policy ACCEPT 211M packets, 106G bytes) pkts bytes target prot opt in out source destination 14M 7502M ULOG all -- * * 0.0.0.0/0 0.0.0.0/0 ULOG copy_range 100 nlgroup 1 queue_threshold 50
จะเห็นว่ามี Packet วิ่งเข้า rule นี้ด้วย :P
- ดูแฟ้มที่จัดเก็บ log (ผมเก็บไว้ที่ /home เพราะแบ่งพื้นที่ไว้เยอะที่นี่)
# cd /home/log/ip-traffic/2008-08 # ls 2008-08-26.pcap # file 2008-08-26.pcap 2008-08-26.pcap: tcpdump capture file (little-endian) - version 2.4 (raw IP, capture length 65535)
อ้า... ใช่แล้วครับ เป็น ลักษณะ tcpdump capture file :P (binary ด้วย ... เห็นเขาบอกให้เก็บในรูปแบบที่น่าเชื่อถือได้ ไม่รู้ตัวนี้น่าเชื่อถือพอหรือเปล่า :P)
- ลองดู log ข้างในแฟ้มนี้ดู
- เป็นอันว่า เก็บได้ ก็น่าจะเรียบร้อย ตอนนี้ก็เหลือให้ script เราทำงานเป็นช่วง ๆ เพื่อจัดการเรื่องชื่อไฟล์ให้ถูกต้อง
# vi /etc/cron.d/traffic-log
0 */1 * * * root /usr/local/bin/traffic-ulogd.sh >/dev/null 2>&1
# /etc/init.d/cron restart
เป็นการกำหนดให้ระบบ run script นี้ ทุก ๆ ชั่วโมง (กันพลาด เดี๋ยว log ในแฟ้ม จะไม่ตรงกับวันเวลาจริง แต่ถ้าเครื่องเปิดตลอดเวลาไม่น่ามีปัญหา) เท่านี้ก็น่าจะเรียบร้อยแล้วครับผม ตอนนี้ก็เหลือดูผลว่าไฟล์ใหญ่แค่ไหน อาจจะต้องมีการมาทำ archive อีกที เดี๋ยวค่อยมาว่ากันต่อคราวหน้าละกันครับ
# tshark -r 2008-08-26.pcap -tad -c 5 Running as user "root" and group "root". This could be dangerous. 1 2008-08-26 03:31:34.464265 172.30.10.18 -> 80.252.110.146 TCP acter > kar2ouche [SYN] Seq=0 Win=16384 Len=0 MSS=1452 2 2008-08-26 03:31:34.537428 172.30.10.102 -> 64.71.134.246 TCP ft-role > https [SYN] Seq=0 Win=16384 Len=0 MSS=1452 3 2008-08-26 03:31:34.565095 172.30.10.18 -> 66.199.250.170 TCP appiq-mgmt > manyone-xml [SYN] Seq=0 Win=16384 Len=0 MSS=1452 4 2008-08-26 03:31:34.568396 172.30.10.18 -> 219.239.90.172 TCP rfa > 28221 [SYN] Seq=0 Win=16384 Len=0 MSS=1452 5 2008-08-26 03:31:34.571477 172.30.10.18 -> 72.51.37.237 TCP cxws > ospf-lite [SYN] Seq=0 Win=16384 Len=0 MSS=145
ขอดูแค่ 5 packets ก่อนละกัน
-r 2008-08-26.pcap (read from file) -tad (Absolute date) -c 5 (5 packets count and exit)













Re: เก็บ IP Traffic Log ด้วย ulogd (Debian Admin Style)
ผมไปประชุมที่ กทม.แล้วเจ้านายให้ทำ logfile troffic log ที่ กทม.ให้แผ่น debian แล้วผมลงไม่ผ่านอ่ะ ขอความกรุณาส่ง
การลง debian แบบ เข้าใจ มือใหม่สุดๆ แต่ละหน้าเลยจะขอบคระคุณอย่างสูง tanakrit_ict@hotmail.com
จาก นายธนกฤต เอกวริษฐ์ สิงห์บุรี
Post new comment