1. Logstash
1.1具體內(nèi)容詳解
具體講解大家可以看文檔:https://www.elastic.co/guide/en/logstash/current/index.html
1.2使用整體配置
1.2.1項(xiàng)目工程中的配置
在logback對應(yīng)的配置文件logback-spring.xml中配置如下內(nèi)容:
????????注意encoder標(biāo)簽中對應(yīng)的三個(gè)字段:requestUrl进苍,traceId和clientIp,這三個(gè)是在要求在輸出日志中增加這三個(gè)標(biāo)簽姑裂,和logback對應(yīng)的默認(rèn)輸出合并帖烘,對應(yīng)的輸出結(jié)果為:
1.2.2 日志發(fā)送到elasticsearch配置
具體對應(yīng)conf配置如下:
input {
?file {
?type =>"XXX_kccf_pc_info"??------對應(yīng)的elasticsearch索引type值
path => "/data/log/kccf_pc_info.*.log"??------輸入日志
???start_position => "beginning"
???codec => json {
????????????charset => "UTF-8"
???????}
??? }
}
filter {
?? if[type] == "XXX_kccf_pc_info"
????{
if "method=HEAD" in [requestUrl] {?? ----------如果是心跳檢查(head)在不會將日志拉取到elasticSearch
??????????????? drop {}
??????}
??????????mutate {
??????????????? split =>["[requestUrl]",","]
?????????}
????}
???geoip {
???????????source => "ip"
???????????target => "geoip"
?????}
}
filter {
?? if[type] == "XXX_kccf_pc_info"
????{
????????if [level] == "ERROR" and "PARAM_NO_PRIVILEGE" notin [message] and "ERROR-START" not in [message]
?????????{
??????????mutate {
????????????? add_tag => "email"}
?????????}
?????}
???????? geoip{
??? ???????????????? source=> "ip"
???? ??????????????? target => "geoip"
??? }
}
output {
? if[type] == "XXX_kccf_pc_info"{
?elasticsearch {
???????hosts => "XXXXX"
index => "XXXX-%{+YYYY.MM.dd}"? ------elasticsearch對應(yīng)的索引名稱
???????sniffing => false
???????manage_template => false
???????flush_size => 3000
???????idle_flush_time => 5
???????user => "logstash"
???????password => "logstash"
#???????ssl => true
#???????ssl_certificate_verification => false
#???????truststore => "/etc/logstash/truststore.jks"
#???????truststore_password => changeit
?????}
? }
?if"email" in [tags] and [type] =="XXX_kccf_pc_info"
? {
?email{---------------------發(fā)送錯誤日志郵件
???????port?????????? =>??? "587"
???????address??????? =>??? "smtp.XXXX.com"
???????username?????? =>??? "devops@XXXX.com"
???????password?????? =>??? "xxxxxx"
???????authentication =>???"login"
???????use_tls??????? =>??? true
???????from?????????? =>??? "devops@XXX.com"
???????subject??????? =>??? "Waring: you have an error on host101.201.118.236(TYJ1)"
???????to???????????? =>??? "yjk@XXX.com"
???????via??????????? =>??? "smtp"
???????body?????????? =>??? "you hava an error ofkccf_pc_info!? server_ip:XXX\n ERROR time: %{@timestamp}+08:00Hours ; \n ERROR message: %{message} ; \nLogger Name: %{logger_name}; \n Level:%{level} ; \n level_value:%{level_value}; \n Stack_trace : \n %{stack_trace}\n "
???????}
? }
}
2. Elastic search
2.1具體內(nèi)容詳解
詳細(xì)內(nèi)容參見:https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html
Elasticsearch集群可以包含多個(gè)索引(數(shù)據(jù)庫)滑黔,這些索引又包含多個(gè)類型(表)趁蕊。這些類型包含多個(gè)文檔(行)目派,每個(gè)文檔都有多個(gè)字段(列)。
2.2 elasticsearch head介紹
下載和詳細(xì)內(nèi)容參見:https://github.com/mobz/elasticsearch-head
可以查看elasticsearch服務(wù)器上各種信息
2.3 searchguard 插件
下載和詳細(xì)內(nèi)容參見:https://github.com/floragunncom/search-guard
從對應(yīng)配置文件名稱就可以看出來英遭,就主要有以下主要關(guān)系:
2.4 script field
詳細(xì)說明參見:https://www.elastic.co/guide/en/elasticsearch/reference/5.6/search-request-script-fields.html
就是可以根據(jù)腳本動態(tài)的在ElasticSearch中生成對應(yīng)的field间护,而不是一開始就在mapping中定義的好的,后面在kibana配置大盤的時(shí)候會用到挖诸。
2.5 painless 腳本
腳本語言詳解:https://www.elastic.co/guide/en/elasticsearch/reference/5.4/modules-scripting-painless.html
3.Kibana
3.1 具體內(nèi)容詳解
下載和詳細(xì)內(nèi)容詳解:https://www.elastic.co/guide/en/kibana/current/index.html
3.2 searchguard 插件
插件下載地址和詳細(xì)說明參見:https://github.com/floragunncom/search-guard-kibana-plugin
3.3 sentinl 郵件報(bào)警插件
插件下載和詳細(xì)說明地址:https://github.com/sirensolutions/sentinl/issues/137
配置地址可以參考:http://blog.51cto.com/10546390/2051676
注意:郵件配置的時(shí)候一定是授權(quán)碼汁尺,不是密碼,否則報(bào)授權(quán)失敗
4.日志監(jiān)控大盤配置
4.1 kibana日志搜索基本用法
詳細(xì)內(nèi)容查看:https://www.elastic.co/guide/en/kibana/5.4/search.html
AND多律,OR痴突,NOT,范圍查詢語法支持
其實(shí)對于kibana查詢狼荞,還是可以使用filter進(jìn)行查詢的(高版本已經(jīng)支持了辽装,但是我們使用的5.4需要經(jīng)過下面的步驟來增加filter):
4.2 增加索引scriptfield
之所以要增加scriptfields是因?yàn)楸热鐝膶?yīng)的下面日志中抽取對應(yīng)的耗時(shí):
method=com.kccf.pc.controller.article.ArticleController.getArticleListcost 22 milliseconds
要從對應(yīng)的日志中截取對應(yīng)的耗時(shí)內(nèi)容,所以要使用腳本來進(jìn)行截取粘秆,并將截取的值存放在對應(yīng)的script fields字段中如迟,供后面的Visualize使用。
4.3 Visualize配置
單擊“加號”新增一個(gè)視圖:
并選擇對應(yīng)的視圖類型攻走,然后選擇要從哪個(gè)索引采集數(shù)據(jù)進(jìn)行配置:
當(dāng)然X殷勘,Y軸都可以配置多個(gè)統(tǒng)計(jì),如下面的:
4.4 Dashboard大盤配置
大盤就是將上面配置的多個(gè)Visualized放在一個(gè)地方進(jìn)行顯示昔搂,如下圖所示:
新增大盤玲销,通過如下操作:
將Visualized放置在大盤中:
5. 監(jiān)控郵件報(bào)警sentinl配置
5.1 新增watcher配置
(1)?General:表示多長時(shí)間運(yùn)行一次,定時(shí)遍歷日志
(2)?Input:對elasticSearch中的索引或者索引列表進(jìn)行過濾采集摘符,得到最終的輸入
(3)?Condition:就是過濾之后的日志滿足什么條件才進(jìn)行報(bào)警
(4)?Transform:對結(jié)果進(jìn)行轉(zhuǎn)換
(5)?Actions:到達(dá)報(bào)警條件了贤斜,進(jìn)行什么樣的處理,一般就是發(fā)送郵件通知
5.1.1 Input過濾:
下面是配置的Input過濾:在最近一個(gè)小時(shí)內(nèi)逛裤,耗時(shí)大于20毫秒的記錄進(jìn)行過濾
{
?"search": {
???"request": {
"index":[
???????"indu_kccf_pc_info-*"
????? ],
?????"body": {
???????"query": {
?????????"bool": {
???????????"must": [
????????????? {
???????????????"script": {
?????????????????"script": {
"lang": "painless",
???????????????????"params": {
"costTimeThrehold":20
???????????????????},
"inline":"if(null != doc['message.keyword'].value &&doc['message.keyword'].value.trim().startsWith(\"method=\")&&doc['message.keyword'].value.trim().endsWith(\"milliseconds\")){Number costTimeNum = NumberFormat.getInstance().parse(doc['message.keyword'].value.substring(doc['message.keyword'].value.indexOf('cost')+4,doc['message.keyword'].value.lastIndexOf('milliseconds')-1).trim());if(costTimeNum.longValue() >?params.costTimeThrehold) true; else false;}elsefalse;"
????????????????? }
??????????????? }
????????????? },
?? ???????????{
"range":{
?????????????????"@timestamp": {
???????????????????"gte": "now-1h",
???????????????????"lte": "now",
???????????????????"format": "epoch_millis"
????????????????? }
??????????????? }
????????????? }
??????????? ]
????????? }
??????? }
????? }
??? }
? }
}
注意:inline里面的腳本功能就是截取如下日志中對應(yīng)的耗時(shí)method=com.kccf.pc.controller.article.ArticleController.getArticleListcost?22?milliseconds瘩绒。Range對應(yīng)的是時(shí)間范圍過濾。
5.1.2 Condition配置:
Condition配置就是對input過濾得到結(jié)果進(jìn)行統(tǒng)計(jì)带族,如果統(tǒng)計(jì)的結(jié)果滿足設(shè)置的條件則進(jìn)行觸發(fā)后面的行為:
上面的條件是Input對應(yīng)的過濾結(jié)果的條數(shù)大于1則進(jìn)行報(bào)警锁荔,這個(gè)腳本是可以自己動態(tài)編寫的,因?yàn)閟entinl插件也是在elasticSearch中定義了自己對應(yīng)的索引:watcher和watcher_alarms-時(shí)間蝙砌,如下面在elasticSearch-head中對應(yīng)的截圖所示:
(1)索引watcher類型sentinl-watcher對應(yīng)的內(nèi)容:這個(gè)內(nèi)容就是我們在kibana頁面上配置的watcher內(nèi)容的json化)
{
"_index":"watcher",
"_type":"sentinl-watcher",
??? "_id":"38eljz92mycwqi00ian55ng66r-0tqtq7q46valazkmg8yn20ggb9-im815vceg9gap6akbvjejnhfr",
??? "_version":32,
??? "_score":1,
??? "_source":{
?????? "title":"所有",
?????? "disable":false,
?????? "report":false,
?????? "trigger":{
?????????? "schedule":{
????????????? "later":"every 1 secs"
?????????? }
?????? },
?????? "input":{
?????????? "search":{
????????????? "request":{
????????????????? "index":[
???????????????????? "cpcn-*"
????????????????? ],
????????????????? "body":{}
????????????? }
?????????? }
?????? },
?????? "condition":{
?????????? "script":{
????????????? "script":"payload.hits.total >0"
?????????? }
?????? },
?????? "actions":{
?????????? "error報(bào)警": {
????????????? "throttle_period":"0h0m1s",
????????????? "email":{
????????????????? "to":"yjk@XXX.com",
????????????????? "from":"devops@XXX.com",
????????????????? "subject":"報(bào)警",
????????????????? "body":"event:{{payload.hits.total}} errorMsgConent:{{payload.hits.hits}}"
????????????? }
?????? ??? }
?????? }
??? }
}
(2)索引watcher類型sentinl-script對應(yīng)的內(nèi)容:就是在kibana中配置的Input中的body內(nèi)容
{
"_index":"watcher",
"_type":"sentinl-script",
??? "_id":"8mmkkevvdr0g0sz3mmojn61or-s6hfac0a3y36ier49ju1sjor-keeuakonk0v51cntohp1ll3di",
??? "_version":1,
??? "_score":1,
??? "_source":{
?????? "description":"input",
?????? "title":"error",
?????? "body":"{
?????? "search":{
?????????? "request":{
????????????? "index":["cpcn-*"],
????????????? "body":{
????????????????? "query":{
???????????????????? "bool":{
???????????????????????? "must":[{
??????????????????????????????? "wildcard":{
?????????????????????????????????? "message":"*exception*"
??????????????????????????????? }
??????????????????????????? },
??????????????????????????? {
??????????????????????????????? "wildcard":{
?????????????????????????????????? "message":"*error*"
??????????????????????????????? }
??????????????????????????? },
??????????????????????????? {
??????????????????????????????? "range":{
?????????????????????????????????? "@timestamp":{
?????????????????????????????????????? "gte":"now-1h",
?????????????????????????????????????? "lte":"now",
?????????????????????????????????????? "format":"epoch_millis"
?????????????????????????????????? }
??????????????????????????????? }
??????????????????????????? }
???????????????????????? ],
???????????????????????? "must_not":[]
???????????????????? }
????????????????? }
????????????? }
?????????? }
?????? }
??? }
??? "
}
}
(3)報(bào)警信息內(nèi)容對應(yīng)的是索引watcher_alarms-時(shí)間類型為配置Action名稱的內(nèi)容:
{
"_index":"watcher_alarms-2018.04.09",
"_type":"email_admin",------配置的action名稱
??? "_id":"AWKphaIhjWbDkGCDylsw",
??? "_version":1,
??? "_score":1,
??? "_source":{
?????? "@timestamp":"2018-04-09T08:28:50.080Z",
?????? "watcher":"watcher_title",
?????? "level":"high",
?????? "message":"Found 177 Events",
?????? "action":"email_admin",
"payload":{
?????????? "took":4,
?????????? "timed_out":false,
?????????? "_shards":{
????????????? "total":25,
????????????? "successful":25,
????????????? "skipped":0,
????????????? "failed":0
?????????? },
"hits":{
????????????? "total":177,
????????????? "max_score":1,
"hits":[{
????????????????? "_index":"cpcn-2018.01.17",
????????????????? "_type":"logs",
????????????????? "_id":"AWECHmRX1zYDH4X06oyZ",
????????????????? "_score":1,
????????????????? "_source":{
???????????????????? "@version":"1",
???????????????????? "host":"DESKTOP-7DN8E16",
???????????????????? "@timestamp":"2018-01-17T03:16:41.677Z",
???????????????????? "message":"qwe"
????????????????? }
????????????? }]
?????????? }
?????? },
?????? "report":false
??? }
}
注意上面的報(bào)警內(nèi)容阳堕,正好是我們在Condition中配置的需要的信息,如condition配置所示:
{
?"script": {
"script": "payload.hits.total?> 1"?-----正好對應(yīng)的上面報(bào)警內(nèi)容對應(yīng)的記錄中的內(nèi)容
? }
}
5.1.3 Actions配置:
Actions配置就配置在滿足報(bào)警條件的時(shí)候择克,如何進(jìn)行處理恬总,如下面報(bào)警條件滿足的時(shí)候進(jìn)行郵件通知處理配置如下:
注意:body中的內(nèi)容正是取索引watcher_alarms-時(shí)間類型為“方法耗時(shí)超過閾值報(bào)警”名稱的內(nèi)容