Linux日誌管理實驗

實驗

如何將某個服務重新定義日誌

  • 環境
    將ssh服務從新定義日誌文件和日誌級別
實驗

1.首先在rsyslog服務的配置文件定義一個符合自己日誌類別

[root@centos7 ~]# vim /etc/rsyslog.conf    #rsyslog的主配置文件
# Save boot messages also to boot.log
local7.*                                  /var/log/boot.log
local0.*                                  /var/log/sshd.log # 添加一行這個local0-7沒有用的數字,local0.這個裏如果寫*則表示任何信息的記錄,當然也可以選擇記錄那個級別的
  1. 修改sshd服務的配置文件
[root@centos7 ~]# vim /etc/ssh/sshd_config 
# Logging
#SyslogFacility AUTH
SyslogFacility local0   # 這裏修改成剛剛在rsyslog配置文件中定義的名字
#LogLevel INFO    # 這個是日誌級別默認是沒有啓用的
  1. 修改好配置文件後重啓這兩個服務
systemctl restart sshd
systemctl restart rsyslog
  1. 測試
[root@centos7 ~]# cat /var/log/sshd.log 
Feb  3 15:07:18 centos7 sshd[6206]: Server listening on 0.0.0.0 port 22.
Feb  3 15:07:18 centos7 sshd[6206]: Server listening on :: port 22.
Feb  3 15:07:38 centos7 sshd[6221]: Accepted password for root from 192.168.27.1 port 49380 ssh2
  1. 用logger這個命令來測試local0是否能記錄
[root@centos7 ~]# logger -p local0.info "this is a test log"  # 用-p選項
[root@centos7 ~]# cat /var/log/sshd.log                     
Feb  3 15:07:18 centos7 sshd[6206]: Server listening on 0.0.0.0 port 22.
Feb  3 15:07:18 centos7 sshd[6206]: Server listening on :: port 22.
Feb  3 15:07:38 centos7 sshd[6221]: Accepted password for root from 192.168.27.1 port 49380 ssh2
Feb  3 15:28:02 centos7 root: this is a test log

當日志發生時用廣播的方式通知特定的用戶

  1. 修改rsyslog的配置文件
# Save boot messages also to boot.log
local7.*                                                /var/log/boot.log
local0.*                                                /var/log/sshd.log
local1.*                                                guo,root    #可以通知多個用戶,如果是通知多個用戶就寫*
  1. 重啓服務
systemctl  restart rsyslog
  1. 測試
[root@centos7 ~]# logger -p local1.info 'this is test' 
[root@centos7 ~]#  root:this is test    # 這個可以的
  1. 注意用su切換的用戶是收不到廣播通知,需要登錄纔可以

實現將日誌存放到日誌服務器中

  • 將centos7上的日誌存放到centos6的主機中進行管理
操作
  • 在centos6中在rsyslog配置文件在啓用日誌端口監聽模塊
# Provides UDP syslog reception
$ModLoad imudp      # 加載模塊
$UDPServerRun 514   # 監聽udp的514端口,因爲udp不是安全的傳輸,所以我們使用tcp

# Provides TCP syslog reception
$ModLoad imtcp      # 加載模塊
$InputTCPServerRun 514  # 監聽tcp的514端口
  1. 重啓服務並且查看端口是否處於監聽狀態
[root@centos6 ~]# service rsyslog restart 
Shutting down system logger:                               [  OK  ]
Starting system logger:                                    [  OK  ]
[root@centos6 ~]# ss -ntlu |grep 514
udp    UNCONN     0      0                      *:514                   *:*     
udp    UNCONN     0      0                     :::514                  :::*     
tcp    LISTEN     0      25                    :::514                  :::*     
tcp    LISTEN     0      25                     *:514                   *:*    
  1. 配置centos7的rsyslog配置文件文件
[root@centos7 ~]# vim /etc/rsyslog.conf 
# Log anything (except mail) of level info or higher.
# Don't log private authentication messages!
#*.info;mail.none;authpriv.none;cron.none                /var/log/messages  # 當開啓將日誌轉發給日誌服務器時,本地默認是不會存日誌的,所有也可以把這一行註釋去掉,這樣本地也會存放日誌
*.info;mail.none;authpriv.none;cron.none                 @@192.168.27.128  # 如果只是用udp的話只用一個@符號就可以
  1. 重啓服務
systemctl restart rsyslog
  1. 測試
[root@centos7 ~]# logger "this is test 2th"
[root@centos7 ~]# logger "this is test 2th"
[root@centos6 ~]# tailf /var/log/messages 
Feb  3 16:34:37 centos7 root: this is test 2=th
Feb  3 16:34:38 centos7 root: this is test 2=th

將日誌存放到數據庫中

  • 環境
    1. 一臺centos6的主機,和一臺有mariadb的centos7的主機
    2. 將centos6上的日誌存放到centos7中
操作
  1. 用yum或者編譯,二進制安裝mariadb數據庫
[root@centos7 ~]# yum install mariadb-server.x86_64 1:5.5.56-2.el7
[root@centos7 ~]# systemctl enable mariadb 
[root@centos7 ~]# systemctl start mariadb
  1. 在centos6中安裝mysql的模塊
[root@centos6 ~]# rpm -ql  rsyslog-mysql.x86_64 0:5.8.10-10.el6_6 
/lib64/rsyslog/ommysql.so
/usr/share/doc/rsyslog-mysql-5.8.10
/usr/share/doc/rsyslog-mysql-5.8.10/createDB.sql # 這是一個mariadb腳本,這裏會創建表等內容。
package 0:5.8.10-10.el6_6 is not installed
  1. 因爲這個腳本在centos6中,所以我們將這個腳本複製掉7中
[root@centos6 ~]# scp /usr/share/doc/rsyslog-mysql-5.8.10/createDB.sql 192.168.27.129:
  1. 將這個腳本導入到mariadb中
MariaDB [(none)]> source createDB.sql
Query OK, 1 row affected (0.00 sec)

Database changed
Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.00 sec)

MariaDB [Syslog]>
#  查看是否生成數據庫和表
ariaDB [Syslog]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| Syslog             |  # 這個就是生成的數據庫
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)

MariaDB [Syslog]> 
  1. 創建一個mysql用戶並且授權有Syslog數據庫的管理權限
MariaDB [Syslog]> flush privileges; # 因爲剛剛對數據庫做了增的操作,所有要刷新一下
MariaDB [Syslog]> grant all on Syslog.* to syslog@'192.168.27.128' identified by 'guo123456';
Query OK, 0 rows affected (0.00 sec)
  1. 測試一下用這個用戶是否可以連接數據庫
[root@centos6 ~]# mysql -usyslog -pguo123456 -h192.168.27.129    
Welcome to the MySQL monitor.  Commands end with ; or \g.
  1. 修改centos6上的rsyslog配置文件
#### MODULES ####

$ModLoad imuxsock # provides support for local system logging (e.g. via logger command)
$ModLoad imklog   # provides kernel logging support (previously done by rklogd)
#$ModLoad immark  # provides --MARK-- message capability
$ModLoad ommysql  # 加載這個模塊,就是剛剛用yum安裝的
#### RULES ####
*.info;mail.none;authpriv.none;cron.none                :ommysql:192.168.27.129,Syslog,syslog,guo123456 #:模塊:數據庫主機IP,數據庫文件,用戶名,密碼
  1. 重啓rsyslog服務
[root@centos6 ~]# service rsyslog restart
Shutting down system logger:                               [  OK  ]
Starting system logger:                                    [  OK  ]
  1. 測試
MariaDB [Syslog]> select * from SystemEvents\G ; 
*************************** 183. row ***************************
                ID: 183
        CustomerID: NULL
        ReceivedAt: 2017-11-11 04:13:15
DeviceReportedTime: 2017-11-11 04:13:15
          Facility: 1
          Priority: 5
          FromHost: centos6
           Message:  test
        NTSeverity: NULL
        Importance: NULL
       EventSource: NULL
         EventUser: NULL
     EventCategory: NULL
           EventID: NULL
   EventBinaryData: NULL
      MaxAvailable: NULL
         CurrUsage: NULL
          MinUsage: NULL
          MaxUsage: NULL
        InfoUnitID: 1
         SysLogTag: root:
      EventLogType: NULL
   GenericFileName: NULL
          SystemID: NULL
183 rows in set (0.00 sec)
  • 當然這樣看非常不方便,我們可以讓它在web頁面顯示,
  • 這裏我們還需要一臺當web服務器的機器,安裝httpd,php等安裝包
  • web服務器的配置,爲了省事這裏都是以yum安裝的,當然如果要定製就要用編譯安裝這些
 yum install -y httpd php-gd php php-mysql
  1. 測試一下http是否可以連接到數據,當然還需要在數據庫中創建一個賬號,因爲syslog這個賬號只能192.168.27.128能連接
MariaDB [(none)]> grant all on Syslog.*  to log@'192.168.27.%' identified by 'guo123456';
Query OK, 0 rows affected (0.00 sec)
[root@localhost html]# vim /var/www/html/info.php      
 <?php
$mysqli=new mysqli("192.168.27.129","log","guo123456");
if(mysqli_connect_errno()){
echo "Failure";
$mysqli=null;
exit;
}
echo "OK";
$mysqli->close();
?>
# 啓動服務
[root@localhost html]# systemctl start httpd
  1. 測試一下
    mark
    • 可以正常連接數據庫
  2. 下載loganalyzer這是一個php寫的程序用來展示數據庫中的日誌
 tar xvf loganalyzer-4.1.6.tar.gz 
 cd loganalyzer-4.1.6
 cp -r src /var/www/html/log    # 因爲解壓出來很多文件,我們只需要src的目錄的文件
 touch  /var/www/html/log/config.php  # 創建這個文件這是嚮導是要寫的配置
chmod 666 /var/www/html/log/config.php  # 改權限
  1. 現在打開瀏覽器進行嚮導
    mark
    mark
    mark
    mark
    mark
    • 這裏一定要注意,數據庫需要到mysql查一下表
      mark
    • 這樣就完成
      mark
  2. 將剛剛的配置文件權限修改一下
chmod 644 /var/www/html/log/config.php
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章