Logstash——Logstash向Email發送告警郵件

email數據輸出

logstash的輸出插件中提供了email的數據輸出來源。這樣當logstash遇見需要預警的內容時候,可以通過郵件向指定用戶發送警報信件。

可配置參數

關於email的參數有下面幾個

字段 參數類型 說明
address string 郵件的服務器地址
username string 用來驗證服務器的用戶名
password string 用於驗證服務器的密碼
port number 用於與郵件服務器通信的端口
authentication string 服務器識別身份的方法
attachments array 附件地址
body string 電子郵件正文
cc string 郵件的抄送地址
bcc string 郵件的密件抄送地址
contenttype string 設置HTML部分的內容類型和/或字符集。
debug boolean 在調試模式下運行郵件中繼
domain string 連接到遠程SMTP服務器時使用的HELO / EHLO域名
from string 發件人
htmlbody string 電子郵件的HTML正文
replyto string 郵件回覆字段
subject string 郵件主題
to string 電子郵件發送目標
use_tls boolean 與服務器通信時啓用TLS
via string Logstash應如何通過SMTP或通過調用sendmail發送電子郵件。
template_file path 用於電子郵件模板的[Mustache模板](https://mustache.github.io/)文件的路徑。

以騰訊郵箱爲例子

  1. 首先開啓相關服務支持

需要打開設置-賬號

在這裏插入圖片描述

  1. 開啓對應服務

在這裏插入圖片描述

  1. 申請授權碼

需要注意,此後所有配置中有關郵箱密碼的部分都是指的授權碼

根據其文檔的介紹

  • 接收郵件服務器:imap.qq.com,使用SSL,端口號993
  • 發送郵件服務器:smtp.qq.com,使用SSL,端口號465或587

以郵件爲目標的配置

email {
	port => 587
	address => "smtp.qq.com"
	username => "郵箱@qq.com"
	password => "授權碼"
	authentication => "plain"
	contenttype => ""
	from => "郵箱@qq.com"
	subject => "錯誤告警"
	to => "郵箱@qq.com"
	use_tls => true
	via => "smtp"
	domain => "smtp.qq.com"
	body => "錯誤告警:120秒內錯誤日誌超過3條,請注意排查"
	debug => true
}

關於logstash發送告警消息我之前有一篇文章使用throttle過濾器向釘釘發送預警消息

使用裏面的配置,替換成郵件目標最終配置爲下面內容

input {
	redis {
		key => "logstash-email"
		host => "localhost"
		password => "dailearn"
		port => 6379
		db => "0"
		data_type => "list"
		type  => "email"
		codec => plain{
            	charset=>"UTF-8"
       	}
	}
}

filter {

	grok {
		match => { "message" => "%{TIMESTAMP_ISO8601:log_date}  %{LOGLEVEL:log_info} %{DATA:thread} %{NOTSPACE} %{SPACE} %{NOTSPACE} %{JAVACLASS:log_class} %{SPACE}: %{GREEDYDATA:log_message}" }
	}
	if "_grokparsefailure" in [tags] {
		drop {}
	}
	if [log_info] == "INFO" {
		drop {}
	}
	if [log_info] == "ERROR" {
		throttle {
			before_count => -1
			after_count => 3
			period => 120
			max_age => 240
			key => "%{[log_info]}"
			add_tag => "throttled"

		}
	}
	if "throttled" in [tags] {
		aggregate{
			task_id => "%{log_info}"
			code => "event.set('throttled_time',Time.parse(event.get('log_date')).to_f*1000)
					 map['throttled_time'] ||= 0
					 event.set('throttled_time_out', (event.get('throttled_time') - map['throttled_time']) > 10000)"
		}
		if [throttled_time_out] {
			aggregate{
				task_id => "%{log_info}"
				code => "map['throttled_time'] = event.get('throttled_time')
					event.set('throttled_time_test',map['throttled_time'])"
			}
		}
	}
	
}

output {
	if [throttled_time_out] {
        email {
        	port => 587
        	address => "smtp.qq.com"
        	username => "郵箱@qq.com"
        	password => "授權碼"
        	authentication => "plain"
        	contenttype => ""
        	from => "郵箱@qq.com"
        	subject => "錯誤告警"
        	to => "郵箱@qq.com"
        	use_tls => true
        	via => "smtp"
        	domain => "smtp.qq.com"
        	body => "錯誤告警:120秒內錯誤日誌超過3條,請注意排查"
        	debug => true
        }
	}
	stdout {
		codec => rubydebug
	}
}

使用上面配置就可以實現預警消息的郵件發送。

在這裏插入圖片描述

可能遇見的問題

一般出現問題的話首先需要檢測是否開啓了POP3和SMTP服務

  1. Something happen while delivering an email {:exception=>#<Net::OpenTimeout: execution expired>}

此時需要注意配置的address活動端口是否正確。

  1. Something happen while delivering an email {:exception=>#<EOFError: end of file reached>}

根據文字描述應該是郵件內容或者附件出現了問題,而且在GitHub上也有人提過這個問題 ,但是實際上出現上面的情況很大概率是超時問題導致的。這個時候需要嘗試使用非SSL發送郵件。或者使用其他端口(假如有的話,比如騰訊提供的465和587端口)


個人水平有限,上面的內容可能存在沒有描述清楚或者錯誤的地方,假如開發同學發現了,請及時告知,我會第一時間修改相關內容。假如我的這篇內容對你有任何幫助的話,麻煩給我點一個贊。你的點贊就是我前進的動力。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章