rsyslog 系統日志收集上報(可增加自定義項目日志)


 

Linux 自帶應用,沒有復雜的依賴關系卻有強大的日志采集上報功能

 

本文以上報阿里雲為例

1、基礎配置講解

/etc/rsyslog.conf 為其主配置文件(不用動)
/etc/rsyslog.d/ 為其擴充配置目錄,后面我們的自定義配置要放到這個目錄
/etc/rsyslog.d/50-default.conf  為其默認主擴展配置,不用動但我們的自定義配置文件需要保持格式一致並大於50數值,例如
 
80-xx.conf
以下配置文件中 xxx yyy ****均為代替關鍵字,需依據自己的項目替換,$DefaultNetstreamDriverCAFile /etc/ssl/certs/ca-certificates.crt          
證書需替換成自己機器上的證書,其余不用更改
本例中是上報到阿里雲故需要阿里雲的id和key驗證
參數 說明 示例
STRUCTURED-DATA 固定為Logservice。 Logservice
Project 日志服務Project名稱,請提前在日志服務中創建Project。 test-project-1
Logstore 日志服務Logstore名稱,請提前在日志服務中創建Logstore。 test-logstore-1
access-key-id AccessKey ID。建議使用RAM用戶的AccessKey。 <yourAccessKeyId>
access-key-secret AccessKey Secret。建議使用RAM用戶的AccessKey。 <yourAccessKeySecret>

阿里雲上報相關部分參考:

https://help.aliyun.com/document_detail/112903.html

 1 $FileOwner root                                                                                                                                                                                                                                                                                                        
 2 $FileGroup root                                                                                                                                                    
 3 $FileCreateMode 0640                                                                                                                                               
 4 $DirCreateMode 0755                                                                                                                                                
 5 $Umask 0022                                                                                                                                                        
 6 $PrivDropToUser root                                                                                                                                               
 7                                                                                                                                                                    
 8 $PrivDropToGroup root                                                                                                                                              
 9 *.info;auth.none;mail.none;news.none;authpriv.none;cron.none    /var/log/messages                                                                                  
10 xxx.*                          /var/log/xxx/xxx.log                                                                                     
11                                                                                                                                                                    
12 # 增 加 xxx日 志                                                                                                                                                      
13 $ModLoad imfile # needs to be done just once 引 入 模 板                                                                                                               
14 # logstash - test - remote send file.                                                                                                                                                                                                                                
15                                                                         
16 $InputFileName /var/log/xxx/xxx.log #指 定 監 控 日 志 文 件                                                                                                                                                                                                                 
17 $InputFilePollInterval 10 #指 定 每 10秒 輪 詢 一 次 文 件                                                                                                                   
18 $InputFileTag xxx #指 定 文 件 的 tag                                                                                                                         
19 $InputFileStateFile /var/log/xxx/xxx.log #指 定 狀 態 文 件 存 放 位 置 , 如 不 指 定 會 報 錯 。                                                                         
20 $InputFileSeverity info #設 置 監 聽 日 志 級 別                                                                                                                           
21 $InputFileFacility local5 #指 定 告警級別                                                                                                                              
22 $InputRunFileMonitor #啟 動 此 監 控 , 沒 有 此 項 , 上 述 配 置 不 生 效 。                                                                                                        
23                                                                                                                                                                    
24 # Setup disk assisted queues                                                                                                                                       
25 #$WorkDirectory /var/spool/rsyslog # where to place spool files                                                                                                    
26 $ActionQueueFileName fwdRule1     # unique name prefix for spool files                                                                                             
27 $ActionQueueMaxDiskSpace 1g       # 1gb space limit (use as much as possible)                                                                                      
28 $ActionQueueSaveOnShutdown on     # save messages to disk on shutdown                                                                                              
29 $ActionQueueType LinkedList       # run asynchronously                                                                                                             
30 $ActionResumeRetryCount -1        # infinite retries if host is down                                                                                               
31 $ActionSendTCPRebindInterval 100  # close and re-open the connection to the remote host every 100 of messages sent.                                                
32 #RsyslogGnuTLS set to default ca path                                                                                                                              
33 $DefaultNetstreamDriverCAFile /etc/ssl/certs/ca-certificates.crt                                                                                                   
34 template(name="LogServiceFormat" type="string"                                                                                                                     
35 string="<%pri%>1 %timestamp:::date-rfc3339% %HOSTNAME% %app-name% %procid% %msgid% [logservice project=\"xxx\" logstore=\"yyy\" access-key-id=\"******\" access-key-secret=\"*******\"] %msg%\n"                                                                                
37 )                                                                                                                                                                  
38 # Send messages to Loggly over TCP using the template.                                                                                                             
39 action(type="omfwd" protocol="tcp" target="xxxProject.cn-beijing.log.aliyuncs.com" port="10009" template="LogServiceFormat" StreamDriver="gtls" StreamDriverMode="
40 1" StreamDriverAuthMode="x509/name" StreamDriverPermittedPeers="*.cn-beijing.log.aliyuncs.com")      

 阿里雲控制台日志服務入口

https://help.aliyun.com/document_detail/29008.htm


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM