炸蝦碎碎念。

[Notes, GNU/Linux, Open Source, Ruby on Rails, Computer Science, Archlinux]

[整理] Linux上常用的server Log監視工具:logwatch

這學期到了CC之後開始使用的log監視工具:logwatch。(每天一過十二點就會被root mail轟炸)。除了看看每天電腦發生什麼事之外,還可以知道每天到底有多少人在try我的sshd….幸好我有裝fail2ban。

logwatch要安裝這幾個套件:ssmtp、syslog-ng、cronie、logwatch (我以archlinux為準)

安裝ssmtp

首先,我們要收到信,就要有辦法寄信。我們要裝的ssmtp是比起postfix、sendmail等複雜的MTA,超簡易型的(只有用別人的smtp server寄信的功能,不知道算不算MTA)。安裝之後在/etc/ssmtp底下有兩個設定檔:revaliasesssmtp.conf

以gmail為例ssmtp.conf:

1
2
3
4
5
6
7
8
9
root=ilove5566@gmail.com
mailhub=smtp.gmail.com:587
rewriteDomain=gmail.com
hostname=yourhost
UseSTARTTLS=Yes
UseTLS=Yes
AuthUser=ilove5566
AuthPass=密碼
FromLineOverride=yes

其中密碼的部份,不想把自己的密碼放在這可以用google app password

revaliases是指定系統裡各個user寄信的地址,設定root的address之後,如果有設定crontab都會自動寄report喔!很方便。

1
2
root:joshua841025@gmail.com:smtp.gmail.com:587
joshua5201:joshua841025@gmail.com:smtp.gmail.com:587

最後記得chmod 600這兩個檔案免得帳密被別人看光光喔~

安裝syslog-ng

因為我們的archlinux是用systemd,所以用journalctl取代了syslog的功能。安裝syslog-ng之後可以有傳統的log,比如說/var/log/messages等等。logwatch是利用正則表達式及perl script等等來分析log。

1
2
$ sudo systemctl start syslog-ng #記得啟動並設定開機啟動
$ sudo systemctl enable syslog-ng

另外,可以到/etc/syslog-ng/syslog-ng.conf設定檔,把不用的log註解掉,比如說我沒有用印表機就把lpr註解掉了。

安裝cronie

同樣的,因為archlinux用了systemd,crontab也被取代了。我們要裝cronie來獲得傳統的crontab(logwatch安裝會自動設定cron,所以這個其實讓他自動裝就好)。

安裝logwatch

主角出場啦,安裝完成後會發現,/etc/logwatch/conf/logwatch.conf什麼東西都沒。原因是預設的範本會在/usr/share/logwatch/default.conf/logwatch.conf

1
$ sudo cp /usr/share/logwatch/default.conf/logwatch.conf /etc/logwatch/conf/logwatch.conf

再來編輯預設的設定:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 在3x行把output改成mail
 32 #Output/Format Options
 33 #By default Logwatch will print to stdout in text with no encoding.
 34 #To make email Default set Output = mail to save to file set Output = file
 35 Output = mail

# 設定分析範圍跟細節等級,我是設成每天凌晨寄昨天的log。
 68 # The default time range for the report...
 69 # The current choices are All, Today, Yesterday
 70 Range = yesterday
 71
 72 # The default detail level for the report.
 73 # This can either be Low, Med, High or a number.
 74 # Low = 0
 75 # Med = 5
 76 # High = 10
 77 Detail = Low

# 這兩個log我不需要(因為沒有mail server)
 92 Service = "-postfix"
 93 Service = "-amavis"

這時候可以測試看看:

1
2
$ logwatch --output stdout --range today #輸出到stdout
$ logwatch #或者是用logwatch.conf 的設定

參考資料

Logwatch - ArchWiki

syslog-ng - ArchWiki

Comments