วันจันทร์ที่ 22 มีนาคม พ.ศ. 2564

Rsyslog ทำความรู้จักกับ Log และตั้งค่าให้ส่ง Log ไปเก็บยังเครื่อง Server

File Log ในระบบ Linux

เราจะต้องมีไฟล์ที่จะบันทึกการทำงานเพื่อใช้ในการตรวจสอบการทำงานและใช้เพื่อหาความผิดปกติที่เกิดขึ้นจากระบบ
Rsyslog มาจากคำว่า "The rocket-fast system for log processing"
Rsyslog เป็น Open Source ในระบบปฏิบัติการ UNIX และ Unix-like ใช้สำหรับส่งข้อมูล Log ผ่านทาง network

อ้างอิงจาก: https://www.rsyslog.com/, https://en.wikipedia.org/wiki/Rsyslog

Syslog

ใน syslog จะประกอบด้วย 3 ส่วน Facility, Priority และ Action โดยจากเขียนในรูปแบบตามนี้
Facility.Priority;Facility.Priority Action (; แทน และ)
Facility,Facility.Priority Action (, แทน หรือ)

Facility จะระบบที่มาของ log ที่มาจาก subsystem มีตัวเลขกำกับด้วย เช่น
0 - kern (kernel messages)
1 - user (user-level message)
2 - mail (mail system)
3 - daemon (system daemons)
4 - auth (security/authorization message)
5 - syslog (messages generated internally by syslogd)
6 - lpr (line printer subsystem)
7 - news (network news subsystem)
8 - uucp (UUCP subsystem)
9 - cron (clock deamon)
10 - authpriv (security/authorization message)
11 - ftp (FTP daemon)
12 - ntp (NTP subsystem)
13 - logaudit (log audit)
14 - logalert (log alert)
15 - clock (clock daemon)
16 - 23 - local0 - local7 (local user 0 - local user 7)

Priority ระดับความสำคัญของ log
0 - emerg (Emergency)
1 - alert (Alert)
2 - crit (Critical)
3 - error (Error)
4 - warning (Warning)
5 - notice (Notice)
6 - info (Infomation)
7 - debug (Debug)

อ้างอิงจาก: https://wiki.archlinux.org/index.php/rsyslog

การ Select log จะอยู่ในรูปแบบ Facility.Priority และมีเครื่องหมายอื่น ๆ อยู่ด้วย เช่น
auth.* หมายถึงเอาทุก priority ของ facility auth (* หมายถึง any)
auth,mail.* หมายถึงเอา facility auth และ facility mail ทุก priority (, หมายถึง or)
*.info หมายถึงเอาทุก facility ที่ priority info และสูงกว่าจะได้ info ขึ้นไปถึง emerg
*.=info หมายถึงเอาทุก facility ที่ priority info เท่านั้น (= หมายถึง equal)
*.!=info หมายถึงเอาทุก facility ทุก priority ยกเว้น info เท่านั้น (!= หมายถึง not equal)
*.!info หมายถึงเอาทุก facility ที่ priority สูงกว่า info จะได้ notice ขึ้นไปถึง emerg (! หมายถึง not equal and not lower than)

Action ที่จะได้ทำอะไร
/path/of/some/file - เขียนลง File
/dev/console - แสดงยัง console
/dev/ttyX - แสดงยัง tty อื่น
username1[,username2 ...] - ส่งไปยัง user
* - ส่งไปทุก user
remote host - @log.example.com (ส่งไปที่ server ผ่าน UDP)
remote host - @@log.example.com (ส่งไปที่ server ผ่าน TCP)

ตัวอย่างการ Config
auth,authpriv.* /var/log/auth.log - เอาจาก auth หรือ authpriv เอามาทุก priority เก็บที่ File /var/log/auth.log
*.*;auth,authpriv.none -/var/log/syslog เอาทุก facility ยกเว้น auth หรือ authpriv เก็บที่ File /var/log/syslog
- หมายถึง 'Don't sysnc after every write to the file'

การติดตั้ง rsyslog และเช็ค status การทำงาน

# Install Rsyslog and Check Status
sudo apt install rsyslog
sudo systemctl status rsyslog


การ Config ค่า - Server

File ที่ใช้ในการ Config ชื่อ rsyslog.conf จะอยู่ที่ /etc/rsyslog.conf โดยจะทำการตั้งค่าให้สามารถส่งข้อมูล log เข้ามายังเครื่อง server ได้
# Configfile - rsyslog.conf
sudo nano /etc/rsyslog.conf

ทำการเปิดใช้งาน TCP Port 514 ให้สามารถรับ log ได้
โดยทำการลบ # เพื่อเปิดการทำงาน module(load="imtcp") และ input(type="imtcp" port="514")
เมื่อแก้ไขแล้วทำการบันทึกไฟล์ และ สั่ง restart rsylog และ ทำการตรวจสอบการทำงาน
# /etc/rsyslog.conf configuration file for rsyslog
#
# For more information install rsyslog-doc and see
# /usr/share/doc/rsyslog-doc/html/configuration/index.html
#
# Default logging rules can be found in /etc/rsyslog.d/50-default.conf


#################
##### MODULES #####
#################

module(load="imuxsock") # provides support for local system logging
#module(load="immark")  # provides --MARK-- message capability

# provides UDP syslog reception
#module(load="imudp")
#input(type="imudp" port="514")

# provides TCP syslog reception
module(load="imtcp")
input(type="imtcp" port="514")

# provides kernel logging support and enable non-kernel klog messages
module(load="imklog" permitnonkernelfacility="on")
# Restart Rsyslog
sudo systemctl restart rsyslog

# Check Status Rsyslog
sudo systemctl status rsyslog
● rsyslog.service - System Logging Service
     Loaded: loaded (/lib/systemd/system/rsyslog.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2021-03-20 16:52:12 +07; 21h ago
TriggeredBy: ● syslog.socket
       Docs: man:rsyslogd(8)
             https://www.rsyslog.com/doc/
   Main PID: 3125 (rsyslogd)
      Tasks: 8 (limit: 19112)
     Memory: 1.7M
     CGroup: /system.slice/rsyslog.service
             └─3125 /usr/sbin/rsyslogd -n -iNONE

Mar 20 16:52:12 rsyslog-server systemd[1]: Starting System Logging Service...
Mar 20 16:52:12 rsyslog-server systemd[1]: Started System Logging Service.
Mar 20 16:52:12 rsyslog-server rsyslogd[3125]: unknown facility name "jumbo" [v8.2001.0]
Mar 20 16:52:12 rsyslog-server rsyslogd[3125]: imuxsock: Acquired UNIX socket '/run/systemd/journal/syslog' (fd 3) from systemd.  [v8.2001.0]
Mar 20 16:52:12 rsyslog-server rsyslogd[3125]: imklog: cannot open kernel log (/proc/kmsg): Permission denied.
Mar 20 16:52:12 rsyslog-server rsyslogd[3125]: activation of module imklog failed [v8.2001.0 try https://www.rsyslog.com/e/2145 ]
Mar 20 16:52:12 rsyslog-server rsyslogd[3125]: rsyslogd's groupid changed to 110
Mar 20 16:52:12 rsyslog-server rsyslogd[3125]: rsyslogd's userid changed to 104
Mar 20 16:52:12 rsyslog-server rsyslogd[3125]: [origin software="rsyslogd" swVersion="8.2001.0" x-pid="3125" x-info="https://www.rsyslog.com"] start

# Check TCP Listening
sudo netstat -tapnu | grep 514
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
tcp        0      0 0.0.0.0:514             0.0.0.0:*               LISTEN      -
tcp6       0      0 :::514                  :::*                    LISTEN      -



การ Config ค่า - Client

ทดสอบ rsyslog สามารถเชื่อมต่อกับเครื่อง server ได้หรือไม่ โดยคำสั่ง telnet 172.168.0.10 514 (เครื่อง rsyslog server)
หากเชื่อมต่อสำเร็จ จะขึ้นคำว่า Connected to 172.168.0.10 เรียบร้อยแล้วให้เรา ออกโดยการกดปุ่ม Ctrl + ] หรือ Ctrl + 5 และพิมพ์ quit เพือทำการออกจาก telnet
# telent [ip] [port]
telnet 172.168.0.10 514
Trying 172.168.0.10...
Connected to 172.168.0.10.
Escape character is '^]'.
^]
telnet> quit
Connection closed.

เปิดไฟล์ /etc/rsyslog.conf ทำการตั้งค่าการส่งเป็น *.debug ไปยังเครื่อง rsyslog-server บันทึกไฟล์และทำการ restart rsyslog
# 
# Include all config files in /etc/rsyslog.d/
#
$IncludeConfig /etc/rsyslog.d/*.conf

*.debug	@@172.168.0.10
sudo systemctl restart rsyslog


ตรวจสอบหา Log ที่ส่งมาจาก Client

เปิดอ่านไฟล์ /var/log/auth.log ที่เครื่อง server เพื่อหา log จากเครื่อง client ที่ส่งมายัง server ว่าสำเร็จหรือไม่
sudo tail -f /var/log/auth.log
Mar 21 15:33:09 rsyslog-server sudo: pam_unix(sudo:session): session opened for user root by sarankon(uid=0)
Mar 21 15:33:09 rsyslog-server sudo: pam_unix(sudo:session): session closed for user root
Mar 21 15:33:35 rsyslog-server sudo: sarankon : TTY=pts/0 ; PWD=/var/log ; USER=root ; COMMAND=/usr/bin/tail auth.log
Mar 21 15:33:35 rsyslog-server sudo: pam_unix(sudo:session): session opened for user root by sarankon(uid=0)
Mar 21 15:33:35 rsyslog-server sudo: pam_unix(sudo:session): session closed for user root
Mar 21 15:33:41 rsyslog-server sudo: sarankon : TTY=pts/0 ; PWD=/var/log ; USER=root ; COMMAND=/usr/bin/tail -f auth.log
Mar 21 15:33:41 rsyslog-server sudo: pam_unix(sudo:session): session opened for user root by sarankon(uid=0)
Mar 21 15:33:45 rsyslog-client sshd[4918]: Received disconnect from 172.168.0.1 port 54046:11: disconnected by user
Mar 21 15:33:45 rsyslog-client sshd[4918]: Disconnected from user sarankon 172.168.0.1 port 54046
Mar 21 15:33:45 rsyslog-client sshd[4853]: pam_unix(sshd:session): session closed for user sarankon
Mar 21 15:33:45 rsyslog-client systemd-logind[198]: Session 4652 logged out. Waiting for processes to exit.
Mar 21 15:33:45 rsyslog-client systemd-logind[198]: Removed session 4652.
Mar 21 15:33:51 rsyslog-client sshd[4971]: Accepted password for sarankon from 172.168.0.1 port 54058 ssh2
Mar 21 15:33:51 rsyslog-client sshd[4971]: pam_unix(sshd:session): session opened for user sarankon by (uid=0)
Mar 21 15:33:51 rsyslog-client systemd-logind[198]: New session 4664 of user sarankon.

ใน auth.log เราจะเห็นว่ามีทั้ง rsyslog-server และ rsyslog-client ในไฟล์เดียวกันแสดงว่า log จากเครื่อง client สามารถส่งเข้ามายัง server ได้สำเร็จ


ไม่มีความคิดเห็น:

แสดงความคิดเห็น