Showing posts with label imfile. Show all posts
Showing posts with label imfile. Show all posts

Monday, June 7, 2010

Logging normal Text file with rsyslog

สำหรับมือใหม่ทางด้าน Linux และ syslog จะมีคำถามว่า
ทำอย่างไร ถึงจะเก็บ Log ที่ไม่ใช่ standard log file (ตัวอย่างของ Standard log file เช่น message/ auth/ cron เป็นต้น)
เช่น เราอาจจะมีการเขียน Script ตัวนึงเพื่อใช้เก็บข้อมูล โดยให้ script นั้นเขียนไฟล์ต่อท้ายไปเรื่อย ๆ แล้วอยากจะให้เก็บ log ด้วย syslog เพื่อจะไปทำอะไรต่อก็แล้วแต่
ด้วยความสามารถของ rsyslog ทำให้เราสามารถ log text file ธรรมดาได้ โดยการโหลด module ที่ชื่อว่า imfile แล้ว config ค่าบางอย่างเพิ่มเติม ดังตัวอย่างด้านล่าง

ให้เพิ่มค่า config ด้านล่างลงไปใน rsyslog.conf



$ModLoad imfile # needs to be done just once
# File 1
$InputFileName /path/to/file1 #ฟูลพาทที่ไฟล์นั้นอยู่รวมชื่อไฟล์ด้วย
$InputFileTag tag1: # อยากให้ logfile เห็นว่ามาจากไหน ก็ใส่ชื่อลงไป
$InputFileStateFile stat-file1
$InputFileSeverity notice
$InputFileFacility local0
$InputRunFileMonitor
$InputFilePollingInterval 10 # เข้าไปตรวจสอบไฟล์ทุก ๆ 10 นาที
# File 2
$InputFileName /path/to/file2
$InputFileTag tag2:
$InputFileStateFile stat-file1
$InputFileSeverity notice
$InputFileFacility local0
$InputRunFileMonitor
$InputFilePollingInterval 10

ถ้ามีหลาย ๆ ไฟล์ ก็ต่อท้ายลงมาเรื่อย ๆ


หมายเหตุ :
1. module นี้ สามารถ monitor text file ได้แค่ 100 files เท่านั้น ถ้าจะให้ได้มากกว่านี้ ต้องเข้าไปแก้ parameter MAX_INPUT FILES ใน imfile.c
2. ใน syslog-ng ก็ทำแบบนี้ได้เหมือนกัน แต่ไม่รู้ว่า config จะเหมือนอย่างนี้หรือเปล่า ที่ทำผ่าน rsyslog เนื่องจากว่า rsyslog จะติดมากับ OS อยู่แล้ว จึงใช้ rsyslog ไปเลย ง่ายดี
3. แก้ไขค่า config เสร็จแล้ว อย่างลืม restart rsyslog ด้วย ไม่งั้นก็ยังไม่ทำงานนะ

ที่มา : http://www.rsyslog.com/doc-imfile.html