1、添加tomcat監控模版
yum install java-1.8.0-openjdk tomcat-webapps tomcat-admin-webapps tomcat-docs-webapp -y #在被監控節點安裝tomcat包
與jmx接口通信需要使用特定的客戶端,還需要在其他節點或者主節點安裝一個特定的客戶端組件zabbix-gateway,這里在主節點直接安裝上
yum install zabbix-java-gateway -y 在主節點安裝
systemctl start zabbix-java-gateway #啟動服務
修改主節點配置文件,加入JavaGateway的地址端口及進程數
修改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主機
添加模板
2、搭建zabbix-proxy,并測試
在代理端安裝zabbix-proxy包,mariadb-server存儲監控數據
vim /etc/my.cnf#在proxy節點修改配置數據庫文件
skip_name_resolve=ON
之后創建數據庫,授權賬號,導入表結構,
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秒向主節點發送心跳
ConfigFrequency=5 #主節點每幾秒過來proxy拖去配置文件
DataSenderFrequency=1#每1秒向主節點發送監控項
主節點添加proxy
創建主機,將node1,和node2自身都加入node2的proxy代理統一來監控
添加模板
3、添加redis監控模版(自動發現的方式,一臺主機,多個redis端口)
1.安裝Zabbix Server端與數據庫
?腳本內容:
?# 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端驗證腳本:
# 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]"
加入自動發現
定義條件
4、搭建ELK日志收集系統
yum install java-1.8.0-openjdk-devel -y? #elasticsearch用java開發,依賴于Jvm需要安裝jdk組件
wget?https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.6.12.rpm#下載官網制作好的rpm包
ansible all -m yum -a 'name=elasticsearch-5.6.12.rpm state=installed' #同意安裝包
三臺主機搭建集群,全部需要安裝上述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 #節點名稱
path.data: /els/data #存儲路徑
path.logs: /els/logs
network.host: 192.168.1.196 #對外通信主機名
http.port: 9200#監聽端口
discovery.zen.ping.unicast.hosts: ["node1","node2", "node3"] #集群節點發現可以是ip也可以是主機名
discovery.zen.minimum_master_nodes: 1 #內部協調時的主節點,對外els沒有主節點這一稱呼
#修改文件屬主
ansible all -m shell -a 'mkdir -p /els/{data,logs}'
ansible all -m shell -a 'chown elasticsearch:elasticsearch /els/*'
開啟啟動成功
9200位客戶端提供服務,9300位集群內部端口
測試訪問
RESTful API:
curl? -X<VERB> '<PROTOCOL>://<HOST>:<PORT>/<PATH>?<QUERY_STRING>' -d '<BODY>'
<BODY>:json格式的請求主體;
curl -XPUT http://node1:9200/myindex/students/1 -d ' #創建文檔
> {"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 #請求數據
{
? "_index" : "myindex",
? "_type" : "students",
? "_id" : "1",
? "_version" : 1,
? "found" : true,
? "_source" : {
? ? "name" : "GUO",
? ? "age" : 17,
? ? "kf" : "ab"
? }
}
[root@centos7 els]#
/_search:搜索所有的索引和類型;
/INDEX_NAME/_search:搜索指定的單個索引;
/INDEX1,INDEX2/_search:搜索指定的多個索引;
/s*/_search:搜索所有以s開頭的索引;
/INDEX_NAME/TYPE_NAME/_search:搜索指定的單個索引的指定類型;
Logstash組件:
簡歷web服務。收集web服務日志
安裝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 #將服務程序文件寫入path變量中
[root@node2 ~]# vim /etc/profile.d/logstash.sh
export PATH=$PATH:/usr/share/logstash/bin/
###添加配置文件,增加輸入輸出插件配置段,input段,output段,以及fiter段(過濾數據)
https://www.elastic.co/guide/en/logstash/5.6/index.html官方文檔說明
cd /etc/logstash/con.d/
[root@node2 conf.d]# vim ss.conf #添加一個簡單配置,從交互輸入到json格式輸出
input {
? ? ? ? stdin {}
}
output {
? ? ? ? stdout {
? ? ? ? ? ? ? ? codec => rubydebug
? ? ? ? }
}
需要給定配置文件路徑,我這里在當前目錄所以不需要給出
以上格式就可以保存至elasticsearch中
#修改配置文件從web日志中加載信息
input {
? ? ? ? file {
? ? ? ? ? ? ? ? path => '["/var/log/httpd/access_log"]'
? ? ? ? ? ? ? ? start_position => "beginning"
? ? ? ? }
}
ouput {
? ? ? ? stdout {
? ? ? ? ? ? ? ? codec => rubydebug
? ? ? ? }
}
數據獲取成功。接下來使用fiter字段做數據加工?
使用grok插件
使用表達式匹配字段。使用系統自帶的匹配模式,大寫字母為變量。表示證的表達式
%{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系統自帶的表達式文件
配置示例,match能將message字段中使用某個模式將字段分段處理好
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字段切分開,將結果加上一個key
filter {
? ? ? ? grok {
? ? ? ? ? ? ? ? match => { "message" => "%{HTTPD_COMBINEDLOG}" }
? ? ? ? ? ? ? ? remove_field => " message"
? ? ? ? }
加工數據字段后 將message字段刪除掉 在grok中加入remove_field => " message"
geoip插件。GeoIP過濾器根據來自Maxmind GeoLite2數據庫的數據添加有關IP地址地理位置的信息
wget?https://geolite.maxmind.com/download/geoip/database/GeoLite2-City.tar.gz 下載maxmind數據庫在本地,
配置示例。放在filter字段中
geoop {
? ? ? ? ? ? ? ? source => "clientip" #需要匹配的字段
? ? ? ? ? ? ? ? target => "geoip" #將匹配到的字段替換成這里的字段
? ? ? ? ? ? ? ? database => "/etc/logstash/maxmind/geolite-city.mmdb" #mixmind數據庫位置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。并且提供時區。城市。國家等等詳細信息
mutate插件:變異篩選器可讓您對字段執行常規變異。您可以重命名,刪除,替換和修改事件中的字段。
過濾器{ mutate { split => [ “主機名” ,“。] 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"] #添加主機名
? ? ? ? ? ? ? ? index => "logstash-%{+YYYY.MM.dd}" #添加索引信息
? ? ? ? ? ? ? ? document_type => "httpd_access_logs"#添加類型描述
? ? ? ? }
}
查看索引狀態
curl node2:9200/logstash-*/_search?q=157.84.* | jq . #搜索logstash開頭的索引,基于ip地址搜索
5、用filebeat收集系統日志并發送到ELK,展示出來
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主機地址,
修改logstash的配置文件,將輸入替換為beats。端口改為5044,如果不在同一臺機器也可加hosts => "IPADDR"
input {
? ? ? ? beats {
? ? ? ? ? ? ? ? port => 5044
? ? ? ? }
logstash -f beatels.conf #啟動logstash?
啟動filebeat
通過redis做消息隊列服務
vim /etc/redis.conf#修改一下地址及認證口令
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"
? ? ? ? }
}
在我們沒啟動logstash之前filebeat拿到的日志文件會存在redis中,
啟動之后logstash會在redis拿去數據存放至elasticsearch中
elasticsearch的存儲的數據會不斷增加
數據將會被分片至其他節點
表達式判定字段
編輯filebeat配置文件。加入兩個字段
fields:
? ? log_type: access
? ? #- c:\programdata\elasticsearch\logs\*
- paths:
? ? - /var/log/httpd/error_log
? fields:
? ? log_type: errors
修改logstash配置文件,將錯誤日志和訪問日志分開存放
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錯誤日志,并用kibana畫出php錯誤日志圖
wget https://artifacts.elastic.co/downloads/kibana/kibana-5.6.8-x86_64.rpm#下載安裝kibana圖形界面
修改配置文件里的基礎信息
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 日志格式數據樣品
2.1 訪問日志:
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 錯誤日志
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的日志格式,我這里修改問 json 字符串
3. 配置 logstash 的?config, 輸入為 tomcat accesslog 文件,輸出為 redis,logstash 雖然是 java 編寫,但它的這個配置文件格式,感覺是ruby的語法,由filebeat收集,logstash轉換
收集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" ]? #時間,從kibana上顯示每條日志的時間
}
}
output {
? ? ? ? elasticsearch {
? ? ? ? ? ? ? ? hosts => "elasticsearch的IP"
? ? ? ? ? ? ? ? #protocol =>"http"
? ? ? ? ? ? ? ? index=>"api3_json_%{+YYYY.MM.dd}"? #存到es里索引的名稱
? ? ? ? ? ? ? ? document_type=>"api3_json"
? ? ? ? }
? ? ? ? stdout{
? ? ? ? ? codec => rubydebug
}
}