zabbix-proxy及ELK

1、添加tomcat監(jiān)控模版

yum install java-1.8.0-openjdk tomcat-webapps tomcat-admin-webapps tomcat-docs-webapp -y #在被監(jiān)控節(jié)點(diǎn)安裝tomcat包

與jmx接口通信需要使用特定的客戶端,還需要在其他節(jié)點(diǎn)或者主節(jié)點(diǎn)安裝一個(gè)特定的客戶端組件zabbix-gateway,這里在主節(jié)點(diǎn)直接安裝上

yum install zabbix-java-gateway -y 在主節(jié)點(diǎn)安裝

systemctl start zabbix-java-gateway #啟動(dòng)服務(wù)

修改主節(jié)點(diǎn)配置文件,加入JavaGateway的地址端口及進(jìn)程數(shù)

修改tomcat配置文件支持jmx


CATALINA_OPTS="-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.manageme

nt.jmxremote.port=12345 -Dcom.sun.management.jmxremote.ssh=false -Dcom.sun.management.jmxremote.ssl=false -Djava.rm

i.server.hostname=192.168.1.197"

添加jmx主機(jī)

添加模板





2、搭建zabbix-proxy,并測(cè)試

在代理端安裝zabbix-proxy包,mariadb-server存儲(chǔ)監(jiān)控?cái)?shù)據(jù)

vim /etc/my.cnf#在proxy節(jié)點(diǎn)修改配置數(shù)據(jù)庫(kù)文件

skip_name_resolve=ON

之后創(chuàng)建數(shù)據(jù)庫(kù),授權(quán)賬號(hào),導(dǎo)入表結(jié)構(gòu),

MariaDB [(none)]> create database zabbix character set utf8 collate utf8_bin;

Query OK, 1 row affected (0.01 sec)

MariaDB [(none)]>? grant all privileges on zabbix.* to zabbix@'192.168.1.%' identified by '123456';? ? ? ? ?

Query OK, 0 rows affected (0.00 sec)

zcat /usr/share/doc/zabbix-proxy-mysql-3.0.28/schema.sql.gz | mysql -uzabbix -h 192.168.1.197 -p zabbix


ServerActive=192.168.1.197 #修改agent端配置,更換server地址

Server=192.168.1.196,192.168.1.197

vim /etc/zabbix/zabbix_proxy.conf

Server=192.168.1.196

Hostname=node2

DBName=zabbix

DBUser=zabbix

DBPassword=123456

HeartbeatFrequency=60 #每60秒向主節(jié)點(diǎn)發(fā)送心跳

ConfigFrequency=5 #主節(jié)點(diǎn)每幾秒過(guò)來(lái)proxy拖去配置文件

DataSenderFrequency=1#每1秒向主節(jié)點(diǎn)發(fā)送監(jiān)控項(xiàng)


主節(jié)點(diǎn)添加proxy


創(chuàng)建主機(jī),將node1,和node2自身都加入node2的proxy代理統(tǒng)一來(lái)監(jiān)控

添加模板




3、添加redis監(jiān)控模版(自動(dòng)發(fā)現(xiàn)的方式,一臺(tái)主機(jī),多個(gè)redis端口)

1.安裝Zabbix Server端與數(shù)據(jù)庫(kù)

?腳本內(nèi)容:

?# vim redis.sh

?#!/bin/bash

redis_status(){

R_PORT=$1

R_COMMAND=$2

?(echo -en "INFO \r\n";sleep 1;) | nc 127.0.0.1 "$R_PORT" > /usr/local/zabbix/redis_"$R_PORT".tmp

REDIS_STAT_VALUE=$(grep ""$R_COMMAND":" /usr/local/zabbix/redis_"$R_PORT".tmp | cut -d ':' -f2)

?echo $REDIS_STAT_VALUE

?}

?help(){

?echo "${0} + redis_status + PORT + COMMAND"

?}

?main(){

?case $1 in

?redis_status)

?redis_status $2 $3

?;;

?*)

?help

?;;

esac

?}

?main $1 $2 $3

Server端驗(yàn)證腳本:

# bash redis.sh redis_status 6379 connected_clients

?# zabbix_get -s 192.168.15.205 -p 10050 -k "redis_status[redis_status,6379,connected_clients]"


加入自動(dòng)發(fā)現(xiàn)


定義條件



4、搭建ELK日志收集系統(tǒng)

yum install java-1.8.0-openjdk-devel -y? #elasticsearch用java開發(fā),依賴于Jvm需要安裝jdk組件

wget?https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.6.12.rpm#下載官網(wǎng)制作好的rpm包

ansible all -m yum -a 'name=elasticsearch-5.6.12.rpm state=installed' #同意安裝包

三臺(tái)主機(jī)搭建集群,全部需要安裝上述elk安裝包

192.168.1.195 node1

192.168.1.196 node2

192.168.1.198 node3


vim /etc/elasticsearch/elasticsearch.yml #編輯配置文件

cluster.name: myels #配置集群名稱

node.name: node1 #節(jié)點(diǎn)名稱

path.data: /els/data #存儲(chǔ)路徑

path.logs: /els/logs

network.host: 192.168.1.196 #對(duì)外通信主機(jī)名

http.port: 9200#監(jiān)聽端口

discovery.zen.ping.unicast.hosts: ["node1","node2", "node3"] #集群節(jié)點(diǎn)發(fā)現(xiàn)可以是ip也可以是主機(jī)名

discovery.zen.minimum_master_nodes: 1 #內(nèi)部協(xié)調(diào)時(shí)的主節(jié)點(diǎn),對(duì)外els沒(méi)有主節(jié)點(diǎn)這一稱呼


#修改文件屬主

ansible all -m shell -a 'mkdir -p /els/{data,logs}'

ansible all -m shell -a 'chown elasticsearch:elasticsearch /els/*'

開啟啟動(dòng)成功

9200位客戶端提供服務(wù),9300位集群內(nèi)部端口

測(cè)試訪問(wèn)


查詢節(jié)點(diǎn)

RESTful API:

curl? -X<VERB> '<PROTOCOL>://<HOST>:<PORT>/<PATH>?<QUERY_STRING>' -d '<BODY>'

<BODY>:json格式的請(qǐng)求主體;

curl -XPUT http://node1:9200/myindex/students/1 -d ' #創(chuàng)建文檔

> {"name":"GUO","age":17,"kf":"ab"}'

{"_index":"myindex","_type":"students","_id":"1","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"created":true}[root@centos7 els]#

[root@centos7 els]# curl http://node1:9200/myindex/students/1?pretty #請(qǐng)求數(shù)據(jù)

{

? "_index" : "myindex",

? "_type" : "students",

? "_id" : "1",

? "_version" : 1,

? "found" : true,

? "_source" : {

? ? "name" : "GUO",

? ? "age" : 17,

? ? "kf" : "ab"

? }

}

[root@centos7 els]#


/_search:搜索所有的索引和類型;

/INDEX_NAME/_search:搜索指定的單個(gè)索引;

/INDEX1,INDEX2/_search:搜索指定的多個(gè)索引;

/s*/_search:搜索所有以s開頭的索引;

/INDEX_NAME/TYPE_NAME/_search:搜索指定的單個(gè)索引的指定類型;


Logstash組件:

簡(jiǎn)歷web服務(wù)。收集web服務(wù)日志

安裝jdk,下載logstash包

wget https://artifacts.elastic.co/downloads/logstash/logstash-6.8.1.rpm #下載logstash包

yum install logstash-6.8.1.rpm #本地安裝

/usr/share/logstash/bin/logstash #將服務(wù)程序文件寫入path變量中

[root@node2 ~]# vim /etc/profile.d/logstash.sh

export PATH=$PATH:/usr/share/logstash/bin/

###添加配置文件,增加輸入輸出插件配置段,input段,output段,以及fiter段(過(guò)濾數(shù)據(jù))

https://www.elastic.co/guide/en/logstash/5.6/index.html官方文檔說(shuō)明

cd /etc/logstash/con.d/

[root@node2 conf.d]# vim ss.conf #添加一個(gè)簡(jiǎn)單配置,從交互輸入到j(luò)son格式輸出

input {

? ? ? ? stdin {}

}

output {

? ? ? ? stdout {

? ? ? ? ? ? ? ? codec => rubydebug

? ? ? ? }

}

需要給定配置文件路徑,我這里在當(dāng)前目錄所以不需要給出

以上格式就可以保存至elasticsearch中

#修改配置文件從web日志中加載信息

input {

? ? ? ? file {

? ? ? ? ? ? ? ? path => '["/var/log/httpd/access_log"]'

? ? ? ? ? ? ? ? start_position => "beginning"

? ? ? ? }

}

ouput {

? ? ? ? stdout {

? ? ? ? ? ? ? ? codec => rubydebug

? ? ? ? }

}


數(shù)據(jù)獲取成功。接下來(lái)使用fiter字段做數(shù)據(jù)加工?

使用grok插件

使用表達(dá)式匹配字段。使用系統(tǒng)自帶的匹配模式,大寫字母為變量。表示證的表達(dá)式

%{IP:client} %{WORD:method} %{URIPATHPARAM:request} %{NUMBER:bytes} %{NUMBER:duration}

less /usr/share/logstash/vendor/bundle/jruby/2.5.0/gems/logstash-patterns-core-4.1.2/patterns/grok-patterns #logstash系統(tǒng)自帶的表達(dá)式文件


配置示例,match能將message字段中使用某個(gè)模式將字段分段處理好

1.input { file { path => "/var/log/http.log" } } filter { grok { match => { "message" => "%{IP:client} %{WORD:method} %{URIPATHPARAM:request} %{NUMBER:bytes} %{NUMBER:duration}" } } }

2.input {

? ? ? ? file {

? ? ? ? ? ? ? ? path => ["/var/log/httpd/access_log"]

? ? ? ? ? ? ? ? start_position => "beginning"

? ? ? ? }

}

filter {

? ? ? ? grok {

? ? ? ? ? ? ? ? match => { "message" => "%{HTTPD_COMBINEDLOG}" }

? ? ? ? }

}

output {

? ? ? ? stdout {

? ? ? ? ? ? ? ? codec => rubydebug

? ? ? ? }

}


將message字段切分開,將結(jié)果加上一個(gè)key

filter {

? ? ? ? grok {

? ? ? ? ? ? ? ? match => { "message" => "%{HTTPD_COMBINEDLOG}" }

? ? ? ? ? ? ? ? remove_field => " message"

? ? ? ? }

加工數(shù)據(jù)字段后 將message字段刪除掉 在grok中加入remove_field => " message"


geoip插件。GeoIP過(guò)濾器根據(jù)來(lái)自Maxmind GeoLite2數(shù)據(jù)庫(kù)的數(shù)據(jù)添加有關(guān)IP地址地理位置的信息

wget?https://geolite.maxmind.com/download/geoip/database/GeoLite2-City.tar.gz 下載maxmind數(shù)據(jù)庫(kù)在本地,

配置示例。放在filter字段中

geoop {

? ? ? ? ? ? ? ? source => "clientip" #需要匹配的字段

? ? ? ? ? ? ? ? target => "geoip" #將匹配到的字段替換成這里的字段

? ? ? ? ? ? ? ? database => "/etc/logstash/maxmind/geolite-city.mmdb" #mixmind數(shù)據(jù)庫(kù)位置mmdb為特定壓縮格式

? ? ? ? }

tar xf GeoLite2-City.tar.gz

mkdir /etc/logstash/maxmind/

[root@centos7 ~]# mv GeoLite2-City_20191015/ /etc/logstash/maxmind/

[root@centos7 ~]# cd /etc/logstash/maxmind/

[root@centos7 maxmind]# ln -s GeoLite2-City_20191015/GeoLite2-City.mmdb ./


將clientip替換成geoip。并且提供時(shí)區(qū)。城市。國(guó)家等等詳細(xì)信息


mutate插件:變異篩選器可讓您對(duì)字段執(zhí)行常規(guī)變異。您可以重命名,刪除,替換和修改事件中的字段。

過(guò)濾器{ mutate { split => [ “主機(jī)名” ,“。] add_field => { “ shortHostname” => “%{hostname [0]}” } } mutate { 重命名=> [ “ shortHostname” ,“ hostname” ] } }

filter { mutate { split => ["hostname", "."] add_field => { "shortHostname" => "%{hostname[0]}" } } mutate { rename => ["shortHostname", "hostname" ] }}


elasticsearch 插件

output { #將輸出從屏幕保存至elasticsearch

? ? ? ? elasticsearch {

? ? ? ? ? ? ? ? hosts => ["http://node1:9200","http://node2:9200"] #添加主機(jī)名

? ? ? ? ? ? ? ? index => "logstash-%{+YYYY.MM.dd}" #添加索引信息

? ? ? ? ? ? ? ? document_type => "httpd_access_logs"#添加類型描述

? ? ? ? }

}

查看索引狀態(tài)

curl node2:9200/logstash-*/_search?q=157.84.* | jq . #搜索logstash開頭的索引,基于ip地址搜索


5、用filebeat收集系統(tǒng)日志并發(fā)送到ELK,展示出來(lái)

wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-5.6.8-x86_64.rpm #下載安裝filebeat組件。


vim filebeat.yml

- /var/log/httpd/access_log

hosts: ["192.168.1.197:5044"] #修改logstash主機(jī)地址,


修改logstash的配置文件,將輸入替換為beats。端口改為5044,如果不在同一臺(tái)機(jī)器也可加hosts => "IPADDR"

input {

? ? ? ? beats {

? ? ? ? ? ? ? ? port => 5044

? ? ? ? }

logstash -f beatels.conf #啟動(dòng)logstash?

啟動(dòng)filebeat


通過(guò)redis做消息隊(duì)列服務(wù)

vim /etc/redis.conf#修改一下地址及認(rèn)證口令

bind 0.0.0.0

requirepass 123456

修改filebeat文件



修改logstash入站插件為redis

input {

? ? ? ? redis {

? ? ? ? ? ? ? ? host => "192.168.1.197"

? ? ? ? ? ? ? ? port => 6379

? ? ? ? ? ? ? ? password => "centos"

? ? ? ? ? ? ? ? db => 0

? ? ? ? ? ? ? ? key => "filebeat"

? ? ? ? ? ? ? ? data_type => "list"

? ? ? ? }

}

在我們沒(méi)啟動(dòng)logstash之前filebeat拿到的日志文件會(huì)存在redis中,

啟動(dòng)之后logstash會(huì)在redis拿去數(shù)據(jù)存放至elasticsearch中

elasticsearch的存儲(chǔ)的數(shù)據(jù)會(huì)不斷增加

數(shù)據(jù)將會(huì)被分片至其他節(jié)點(diǎn)

表達(dá)式判定字段

編輯filebeat配置文件。加入兩個(gè)字段

fields:

? ? log_type: access

? ? #- c:\programdata\elasticsearch\logs\*

- paths:

? ? - /var/log/httpd/error_log

? fields:

? ? log_type: errors

修改logstash配置文件,將錯(cuò)誤日志和訪問(wèn)日志分開存放

input {

? ? ? ? redis {

? ? ? ? ? ? ? ? host => "192.168.1.197"

? ? ? ? ? ? ? ? port => 6379

? ? ? ? ? ? ? ? password => "centos"

? ? ? ? ? ? ? ? db => 0

? ? ? ? ? ? ? ? key => "filebeat"

? ? ? ? ? ? ? ? data_type => "list"

? ? ? ? }

}

filter {

? ? ? ? if [fields][log_type] == "access"{

? ? ? ? grok {

? ? ? ? ? ? ? ? match => { "message" => "%{HTTPD_COMBINEDLOG}" }

? ? ? ? ? ? ? ? remove_field => "message"

? ? ? ? }

? ? ? ? geoip {

? ? ? ? ? ? ? ? source => "clientip"

? ? ? ? ? ? ? ? target => "geoip"

? ? ? ? ? ? ? ? database => "/etc/logstash/maxmind/GeoLite2-City.mmdb"

? ? ? ? }

? ? }

}

output {

? ? ? ? if [fields][log_type] == "access" {

? ? ? ? elasticsearch {

? ? ? ? ? ? ? ? hosts => ["http://node1:9200/","http://node2:9200/","http://node3:9200"]

? ? ? ? ? ? ? ? index => "logstash-%{+YYYY.MM.dd}"

? ? ? ? ? ? ? ? document_type => "httpd_access_logs"

? ? ? ? }

? ? ? ? } else {

? ? ? ? ? ? ? ? elasticsearch {

? ? ? ? ? ? ? ? ? ? ? ? hosts => ["http://node1:9200/","http://node2:9200/","http://node3:9200"]

? ? ? ? ? ? ? ? ? ? ? ? index => "logstash-%{+YYYY.MM.dd}"

? ? ? ? ? ? ? ? ? ? ? ? document_type => "httpd_error_logs"

? ? ? ? ? ? ? ? }

? ? ? ? }? ? ?

}

6、收集nginx,tomcat日志,php錯(cuò)誤日志,并用kibana畫出php錯(cuò)誤日志圖

wget https://artifacts.elastic.co/downloads/kibana/kibana-5.6.8-x86_64.rpm#下載安裝kibana圖形界面

修改配置文件里的基礎(chǔ)信息








1 nginx 日志格式配置

[root@elk-5-10 config]# cd /usr/local/nginx/conf/

[root@elk-5-10 conf]# vi nginx.conf

log_format? access? '$http_host $remote_addr - $remote_user [$time_local] "$request" '

???????????? '$status $body_bytes_sent "$http_referer" '

???????????? '"$http_user_agent" $http_x_forwarded_for';

2 日志格式數(shù)據(jù)樣品

2.1 訪問(wèn)日志:

ss00.xxxxxx.me 150.138.154.157 - - [25/Jul/2017:03:02:35 +0800] "GET /csm/7_527.html HTTP/1.1" 304 0 "http://www.twww.com/tetris/page/64000159042/?ad_id=62928537191&cid=62928889880&req_id=0" "Mozilla/5.0 (Linux; Android 6.0.1; Redmi 4X Build/MMB29M; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/55.0.2883.91 Mobile Safari/537.36" 183.204.183.118

2.2 錯(cuò)誤日志

2017/07/25 15:23:29 [error] 24881#0: *33 open() "/usr/local/nginx-1.12.0/html/favicon.ico" failed (2: No such file or directory), client: 192.168.1.103, server: www.zyb.com, request: "GET /favicon.ico HTTP/1.1", host: "www.zyb.com"

3 logstash 配置文件

input {

??? file {

??????? type => "nginx-access"

??????? path => "/data/weixin.sys.mingyaohui.com.log"

??????? start_position => beginning ?

??? }

??? file {

??????? type => "nginx-error"

??????? path => "/data/nginx_error.log"

??????? start_position => beginning

??? }

}

filter {

??? if [type] == "nginx-access" {

??????? grok {

??????????? match => ["message","%{IPORHOST:clientip} %{NGUSER:ident} %{NGUSER:auth}

"%{WORD:verb} %{URIPATHPARAM:request} HTTP/%{NUMBER:httpversion}" %{NUMBER:response} (?:%{NUMBER:bytes}|-) (?:"(?:%{URI:referrer}|-)"|%{QS:referrer}) %{QS:agent} %{IPORHOST:forwordip}" ]}

??????? }

??? } else if [type] == "nginx-error" {

??????? grok {

??????????? match => [ "message" , "(?<timestamp>%{YEAR}[./-]%{MONTHNUM}[./-]%{MONTHDAY}[- ]%{TIME})

%{POSINT:pid}#%{NUMBER}: %{GREEDYDATA:errormessage}(?:, client: (?<clientip>%{IP}|%{HOSTNAME}))(?:, server: %{IPORHOST:server}?)(?:, request: %{QS:request})?(?:, upstream: (?<upstream>\"%{URI}\"|%{QS}))?(?:, host: %{QS:request_host})?(?:, referrer: \"%{URI:referrer}\")?"]

??????? }

??? }

??? # add geo-location info

??? geoip {

??????? source => "clientip"

??? }

}

output {

??? elasticsearch {

??????? hosts => ["10.0.0.10"]

??????? index => "%{type}-%{+YYYY.MM.dd}"

??? }

}

4 kibana分析效果圖



參考資料

https://grokdebug.herokuapp.com/patterns#

https://github.com/adventure-yunfei/ELK-for-nginx-log/blob/master/logstash.conf

————————————————

原文鏈接:https://blog.csdn.net/zyb378747350/article/details/76084840

tomcat:

. 修改Tomcat accesslog的日志格式,我這里修改問(wèn) json 字符串

3. 配置 logstash 的?config, 輸入為 tomcat accesslog 文件,輸出為 redis,logstash 雖然是 java 編寫,但它的這個(gè)配置文件格式,感覺(jué)是ruby的語(yǔ)法,由filebeat收集,logstash轉(zhuǎn)換

收集php

filebeat.prospectors:

- input_type: log

? paths:

? ? - /alidata1/www/timecash22/api3/application/logs/api3/2017/*/*.php

? document_type: api3_json

? multiline.pattern: '^[0-9]{4}-[0-9]{2}-[0-9]{2}'

? multiline.negate: true

? multiline.match: after

- input_type: log

? paths:

? ? - /alidata1/www/timecash22/api3/application/logs/2017/*/*.php

? document_type: api3_error_log

? multiline.pattern: '^[0-9]{4}-[0-9]{2}-[0-9]{2}'

? multiline.negate: true

? multiline.match: after

- input_type: log

? paths:

? ? - /alidata1/www/timecash22/wx/application/logs/2017/*/*.php

? document_type: wx_error_log? ? ? ? #這是的key是什么,到redis里就是什么

? multiline.pattern: '^[0-9]{4}-[0-9]{2}-[0-9]{2}'

? multiline.negate: true

? multiline.match: after

output.redis:

? ? hosts: ["10.45.40.112"]

? ? password: "timecash2016"

? ? #key: "api3_json"

? ? keys:

? ? ? - key: "%{[type]}"

配置logstash配置文件

input {

? ? redis {

? ? ? ? host => "redis的iP"

? ? ? ? password => "redis的密碼"

? ? ? ? port => 6379

? ? ? ? key => "api3_json"

? ? ? ? type => "api3_json"

? ? ? ? data_type => "list"

? ? }

? ? }

filter {

? ? date {

? ? ? ? match => [ "timestamp" , "YYYY-MM-dd HH:mm:ss" ]? #時(shí)間,從kibana上顯示每條日志的時(shí)間

}

}

output {

? ? ? ? elasticsearch {

? ? ? ? ? ? ? ? hosts => "elasticsearch的IP"

? ? ? ? ? ? ? ? #protocol =>"http"

? ? ? ? ? ? ? ? index=>"api3_json_%{+YYYY.MM.dd}"? #存到es里索引的名稱

? ? ? ? ? ? ? ? document_type=>"api3_json"

? ? ? ? }

? ? ? ? stdout{

? ? ? ? ? codec => rubydebug

}

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容