±à¼ÍƼö: |
±¾ÎÄÖ÷Òª½éÉÜÈçºÎͨ¹ý»·¾³µÄ°²×°ÒÔ¼°²å¼þµÄ°²×°¼°Ê¹ÓÃÀ´´î½¨ELKÈÕÖ¾·ÖÎöϵͳ£¬Ö±ÖÁ×îÖÕµÄÉÏÏߣ¬Ï£Íû¶ÔÄúµÄѧϰÓÐËù°ïÖú¡£
±¾ÎÄÀ´×ÔÓÚ²©¿ÍÔ°£¬ÓÉ»ðÁú¹ûÈí¼þAlice±à¼¡¢ÍƼö¡£ |
|
Ò»¡¢ELK´î½¨Æª
¹ÙÍøµØÖ·
¹ÙÍøÈ¨ÍþÖ¸ÄÏ
°²×°Ö¸ÄÏ
ELKÊÇElasticsearch¡¢Logstash¡¢KibanaµÄ¼ò³Æ£¬ÕâÈýÕßÊǺËÐÄÌ×¼þ£¬µ«²¢·ÇÈ«²¿¡£
ElasticsearchÊÇʵʱȫÎÄËÑË÷ºÍ·ÖÎöÒýÇæ£¬ÌṩËѼ¯¡¢·ÖÎö¡¢´æ´¢Êý¾ÝÈý´ó¹¦ÄÜ£»ÊÇÒ»Ì׿ª·ÅRESTºÍJAVA
APIµÈ½á¹¹Ìṩ¸ßЧËÑË÷¹¦ÄÜ£¬¿ÉÀ©Õ¹µÄ·Ö²¼Ê½ÏµÍ³¡£Ëü¹¹½¨ÓÚApache LuceneËÑË÷ÒýÇæ¿âÖ®ÉÏ¡£
LogstashÊÇÒ»¸öÓÃÀ´ËѼ¯¡¢·ÖÎö¡¢¹ýÂËÈÕÖ¾µÄ¹¤¾ß¡£ËüÖ§³Ö¼¸ºõÈκÎÀàÐ͵ÄÈÕÖ¾£¬°üÀ¨ÏµÍ³ÈÕÖ¾¡¢´íÎóÈÕÖ¾ºÍ×Ô¶¨ÒåÓ¦ÓóÌÐòÈÕÖ¾¡£Ëü¿ÉÒÔ´ÓÐí¶àÀ´Ô´½ÓÊÕÈÕÖ¾£¬ÕâЩÀ´Ô´°üÀ¨
syslog¡¢ÏûÏ¢´«µÝ£¨ÀýÈç RabbitMQ£©ºÍJMX£¬ËüÄܹ»ÒÔ¶àÖÖ·½Ê½Êä³öÊý¾Ý£¬°üÀ¨µç×ÓÓʼþ¡¢websocketsºÍElasticsearch¡£
KibanaÊÇÒ»¸ö»ùÓÚWebµÄͼÐνçÃæ£¬ÓÃÓÚËÑË÷¡¢·ÖÎöºÍ¿ÉÊÓ»¯´æ´¢ÔÚ ElasticsearchÖ¸±êÖеÄÈÕÖ¾Êý¾Ý¡£ËüÀûÓÃElasticsearchµÄREST½Ó¿ÚÀ´¼ìË÷Êý¾Ý£¬²»½öÔÊÐíÓû§´´½¨ËûÃÇ×Ô¼ºµÄÊý¾ÝµÄ¶¨ÖÆÒDZí°åÊÓͼ£¬»¹ÔÊÐíËûÃÇÒÔÌØÊâµÄ·½Ê½²éѯºÍ¹ýÂËÊý¾Ý

»·¾³
Centos6.5 Á½Ì¨
IP£º192.168.1.202 °²×°£º elasticsearch¡¢logstash¡¢Kibana¡¢Nginx¡¢Http¡¢Redis
192.168.1.201 °²×°: logstash |
°²×°
°²×°elasticsearchµÄyumÔ´µÄÃÜÔ¿£¨Õâ¸öÐèÒªÔÚËùÓзþÎñÆ÷É϶¼ÅäÖã©
# rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
ÅäÖÃelasticsearchµÄyumÔ´
# vim /etc/yum.repos.d/elasticsearch.repo ÔÚelasticsearch.repoÎļþÖÐÌí¼ÓÈçÏÂÄÚÈÝ
[elasticsearch-5.x]
name=Elasticsearch repository for 5.x packages
baseurl=https://artifacts.elastic.co/packages/5.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md |
°²×°elasticsearchµÄ»·¾³
°²×°elasticsearch
# yum install -y elasticsearch
°²×°java»·¾³(java»·¾³±ØÐëÊÇ1.8°æ±¾ÒÔÉϵÄ) wget http://download.oracle.com/otn-pub/java/jdk/8u131-b11/d54c1d3a095b4ff2b6607d096fa80163/jdk-8u131-linux-x64.rpm
rpm -ivh jdk-8u131-linux-x64.rpm
ÑéÖ¤java°²×°³É¹¦
java -version
java version "1.8.0_131"
Java(TM) SE Runtime Environment (build 1.8.0_131-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11,
mixed mode) |
´´½¨elasticsearch dataµÄ´æ·ÅĿ¼£¬²¢Ð޸ĸÃĿ¼µÄÊôÖ÷Êô×é
# mkdir -p /data/es-data
(×Ô¶¨ÒåÓÃÓÚ´æ·ÅdataÊý¾ÝµÄĿ¼)
# chown -R elasticsearch:elasticsearch /data/es-data |
ÐÞ¸ÄelasticsearchµÄÈÕÖ¾ÊôÖ÷Êô×é
# chown -R elasticsearch:elasticsearch
/var/log/elasticsearch/ |
ÐÞ¸ÄelasticsearchµÄÅäÖÃÎļþ
# vim /etc/elasticsearch/elasticsearch.yml
ÕÒµ½ÅäÖÃÎļþÖеÄcluster.name£¬´ò¿ª¸ÃÅäÖò¢ÉèÖÃ
¼¯ÈºÃû³Æcluster.name: demon
ÕÒµ½ÅäÖÃÎļþÖеÄnode.name£¬´ò¿ª¸ÃÅäÖò¢ÉèÖà ½ÚµãÃû³Æ
node.name: elk-1 ÐÞ¸Ädata´æ·ÅµÄ·¾¶
path.data: /data/es-data ÐÞ¸ÄlogsÈÕÖ¾µÄ·¾¶
path.logs: /var/log/elasticsearch/ ÅäÖÃÄÚ´æÊ¹ÓÃÓý»»»·ÖÇø
bootstrap.memory_lock: true ¼àÌýµÄÍøÂçµØÖ·
network.host: 0.0.0.0 ¿ªÆô¼àÌýµÄ¶Ë¿Ú
http.port: 9200 Ôö¼ÓеIJÎÊý£¬ÕâÑùhead²å¼þ¿ÉÒÔ·ÃÎÊes (5.x°æ±¾£¬ Èç¹ûûÓпÉÒÔ×Ô¼ºÊÖ¶¯¼Ó)
http.cors.enabled: true
http.cors.allow-origin: "*"
Æô¶¯elasticsearch·þÎñ |
Æô¶¯·þÎñ
/etc/init.d/elasticsearch
start
Starting elasticsearch: Java HotSpot(TM) 64-Bit
Server VM warning: INFO: os::commit_memory(0x0000000085330000,
2060255232, 0) failed; error='Cannot allocate
memory' (errno=12)
#
# There is insufficient memory for the Java
Runtime Environment to continue.
# Native memory allocation (mmap) failed to
map 2060255232 bytes for committing reserved
memory.
# An error report file with more information
is saved as:
# /tmp/hs_err_pid2616.log
[FAILED]
Õâ¸ö±¨´íÊÇÒòΪĬÈÏʹÓõÄÄÚ´æ´óСΪ2G£¬ÐéÄâ»úû ÓÐÄÇô¶àµÄ¿Õ¼äÐ޸IJÎÊý£º
vim /etc/elasticsearch/jvm.options
-Xms512m
-Xmx512m ÔÙ´ÎÆô¶¯
/etc/init.d/elasticsearch start ²é¿´·þÎñ״̬£¬Èç¹ûÓб¨´í¿ÉÒÔÈ¥¿´´íÎóÈÕÖ¾ less /var/log/elasticsearch/demon.log(ÈÕÖ¾µÄÃû³ÆÊÇ ÒÔ¼¯ÈºÃû³ÆÃüÃûµÄ)
´´½¨¿ª»ú×ÔÆô¶¯·þÎñ
# chkconfig elasticsearch on |
×¢ÒâÊÂÏî
ÐèÒªÐ޸ö²ÎÊý£¬²»È»Æô¶¯»á±¨´í
vim /etc/security/limits.conf
ÔÚĩβ׷¼ÓÒÔÏÂÄÚÈÝ£¨elkΪÆô¶¯Óû§£¬µ±È»Ò²¿ÉÒÔÖ¸¶¨Îª*£©
elk soft nofile 65536
elk hard nofile 65536
elk soft nproc 2048
elk hard nproc 2048
elk soft memlock unlimited
elk hard memlock unlimited ¼ÌÐøÔÙÐÞ¸ÄÒ»¸ö²ÎÊý
vim /etc/security/limits.d/90-nproc.conf
½«ÀïÃæµÄ1024¸ÄΪ2048£¨ES×îÉÙÒªÇóΪ2048£©
* soft nproc 2048 ÁíÍ⻹Ðè×¢ÒâÒ»¸öÎÊÌ⣨ÔÚÈÕÖ¾·¢ÏÖÈçÏÂÄÚÈÝ£¬ ÕâÑùÒ²»áµ¼ÖÂÆô¶¯Ê§°Ü£¬ÕâÒ»ÎÊÌâÀ§ÈÅÁ˺ܾã©
[2017-06-14T19:19:01,641][INFO ][o.e.b.Bootstrap Checks
] [elk-1] bound or publishing to a non-loopback
or non-link-local address, enforcing bootstrap
checks
[2017-06-14T19:19:01,658][ERROR][o.e.b.Bootstrap
] [elk-1] node validation exception
[1] bootstrap checks failed
[1]: system call filters failed to install;
check the logs and fix your configuration or
disable system call filters at your own risk
½â¾ö£ºÐÞ¸ÄÅäÖÃÎļþ£¬ÔÚÅäÖÃÎļþÌí¼ÓÒ»Ïî²ÎÊý £¨Ä¿Ç°»¹Ã»Ã÷°×´Ë²ÎÊýµÄ×÷Óã©
vim /etc/elasticsearch/elasticsearch.yml
bootstrap.system_call_filter: false |
ͨ¹ýä¯ÀÀÆ÷ÇëÇóÏÂ9200µÄ¶Ë¿Ú£¬¿´ÏÂÊÇ·ñ³É¹¦
Ïȼì²é9200¶Ë¿ÚÊÇ·ñÆðÀ´
netstat -antp |grep 9200
tcp 0 0 :::9200 :::* LISTEN 2934/java
ä¯ÀÀÆ÷·ÃÎʲâÊÔÊÇ·ñÕý³££¨ÒÔÏÂΪÕý³££©
# curl http://127.0.0.1:9200/
{
"name" : "linux-node1",
"cluster_name" : "demon",
"cluster_uuid" : "kM0GMFrsQ8K_cl5Fn7BF-g",
"version" : {
"number" : "5.4.0",
"build_hash" : "780f8c4",
"build_date" : "2017-04-28T17:43:27.229Z",
"build_snapshot" : false,
"lucene_version" : "6.5.0"
},
"tagline" : "You Know, for Search"
} |
ÈçºÎºÍelasticsearch½»»¥
JavaAPI
RESTful API
Javascript,.Net,PHP,Perl,Python ÀûÓÃAPI²é¿´×´Ì¬
# curl -i -XGET 'localhost:9200/_count?pretty'
HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8
content-length: 95 {
"count" : 0,
"_shards" : {
"total" : 0,
"successful" : 0,
"failed" : 0
}
} |
°²×°²å¼þ
°²×°elasticsearch-head²å¼þ
°²×°docker¾µÏñ»òÕßͨ¹ýgithubÏÂÔØelasticsearch-headÏîÄ¿¶¼ÊÇ¿ÉÒԵģ¬1»òÕß2Á½ÖÖ·½Ê½Ñ¡ÔñÒ»ÖÖ°²×°Ê¹Óü´¿É 1. ʹÓÃdockerµÄ¼¯³ÉºÃµÄelasticsearch-head
# docker run -p 9100:9100 mobz/elasticsearch-head:5
dockerÈÝÆ÷ÏÂÔØ³É¹¦²¢Æô¶¯ÒÔºó£¬ÔËÐÐä¯ÀÀÆ÷´ò¿ªhttp://localhost:9100/ 2. ʹÓÃgit°²×°elasticsearch-head
# yum install -y npm
# git clone git://github.com/mobz/elasticsearch-head.git
# cd elasticsearch-head
# npm install
# npm run start
¼ì²é¶Ë¿ÚÊÇ·ñÆðÀ´
netstat -antp |grep 9100
ä¯ÀÀÆ÷·ÃÎʲâÊÔÊÇ·ñÕý³£
http://IP:9100/ |

LogStashµÄʹÓÃ
°²×°Logstash»·¾³£º
¹Ù·½°²×°ÊÖ²á:
https://www.elastic.co/guide/en/logstash/current /installing-logstash.html ÏÂÔØyumÔ´µÄÃÜÔ¿ÈÏÖ¤£º
# rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch ÀûÓÃyum°²×°logstash
# yum install -y logstash ²é¿´ÏÂlogstashµÄ°²×°Ä¿Â¼
# rpm -ql logstash ´´½¨Ò»¸öÈíÁ¬½Ó£¬Ã¿´ÎÖ´ÐÐÃüÁîµÄʱºò²»ÓÃÔÚд°²×°Â·¾¢£¨Ä¬Èϰ²×°ÔÚ/usr/shareÏ£©
ln -s /usr/share/logstash/bin/logstash /bin/ Ö´ÐÐlogstashµÄÃüÁî
# logstash -e 'input { stdin { } } output {
stdout {} }' ÔËÐгɹ¦ÒÔºóÊäÈë:
nihao stdout·µ»ØµÄ½á¹û: |

×¢:
-e Ö´ÐвÙ×÷
input ±ê×¼ÊäÈë
{ input } ²å¼þ
output ±ê×¼Êä³ö
{ stdout } ²å¼þ
ͨ¹ýrubydebugÀ´Êä³öϸüÏêϸµÄÐÅÏ¢
# logstash -e 'input { stdin { } } output {
stdout {codec => rubydebug} }' Ö´Ðгɹ¦ÊäÈë:
nihao stdoutÊä³öµÄ½á¹û: |

Èç¹û±ê×¼Êä³ö»¹ÓÐelasticsearchÖж¼ÐèÒª±£ÁôÓ¦¸ÃÔõÃ´Íæ£¬¿´ÏÂÃæ
Èç¹û±ê×¼Êä³ö»¹ÓÐelasticsearchÖж¼ÐèÒª±£ÁôÓ¦¸ÃÔõÃ´Íæ£¬¿´ÏÂÃæ
# /usr/share/logstash/bin/logstash -e 'input {
stdin { } } output { elasticsearch { hosts =>
["192.168.1.202:9200"] } stdout { codec
=> rubydebug }}'
ÔËÐгɹ¦ÒÔºóÊäÈë:
I am elk ·µ»ØµÄ½á¹û(±ê×¼Êä³öÖеĽá¹û): |

logstashʹÓÃÅäÖÃÎļþ
¹Ù·½Ö¸ÄÏ:
https://www.elastic.co/guide/en/logstash/current/ configuration.html
´´½¨ÅäÖÃÎļþ01-logstash.conf
# vim /etc/logstash/conf.d/elk.conf ÎļþÖÐÌí¼ÓÒÔÏÂÄÚÈÝ
input { stdin { } }
output {
elasticsearch { hosts => ["192.168.1.202:9200"]
}
stdout { codec => rubydebug }
} ʹÓÃÅäÖÃÎļþÔËÐÐlogstash
# logstash -f ./elk.conf ÔËÐгɹ¦ÒÔºóÊäÈëÒÔ¼°±ê×¼Êä³ö½á¹û |

logstashµÄÊý¾Ý¿âÀàÐÍ
1. Input²å¼þ
ȨÍþÖ¸ÄÏ:https://www.elastic.co/guide/en/logstash/ current/input-plugins.html
file²å¼þµÄʹÓÃ
# vim /etc/logstash/conf.d/elk.conf
Ìí¼ÓÈçÏÂÅäÖÃ
input {
file {
path => "/var/log/messages"
type => "system"
start_position => "beginning"
}
}
output {
elasticsearch {
hosts => ["192.168.1.202:9200"]
index => "system-%{+YYYY.MM.dd}"
}
}
ÔËÐÐlogstashÖ¸¶¨elk.confÅäÖÃÎļþ£¬½øÐйýÂËÆ¥Åä
#logstash -f /etc/logstash/conf.d/elk.conf
|

À´Ò»·¢ÅäÖð²È«ÈÕÖ¾µÄ²¢ÇÒ°ÑÈÕÖ¾µÄË÷Òý°´ÀàÐÍ×ö´æ·Å£¬¼ÌÐø±à¼elk.confÎļþ
# vim /etc/logstash/conf.d/elk.conf
Ìí¼ÓsecureÈÕÖ¾µÄ·¾¶
input {
file {
path => "/var/log/messages"
type => "system"
start_position => "beginning"
} file {
path => "/var/log/secure"
type => "secure"
start_position => "beginning"
}
} output { if [type] == "system" { elasticsearch {
hosts => ["192.168.1.202:9200"]
index => "nagios-system-%{+YYYY.MM.dd}"
}
} if [type] == "secure" { elasticsearch {
hosts => ["192.168.1.202:9200"]
index => "nagios-secure-%{+YYYY.MM.dd}"
}
}
} ÔËÐÐlogstashÖ¸¶¨elk.confÅäÖÃÎļþ£¬½øÐйýÂËÆ¥Åä
# logstash -f ./elk.conf
|

ÕâЩÉèÖö¼Ã»ÓÐÎÊÌâÖ®ºó£¬½ÓÏÂÀ´°²×°ÏÂkibana£¬¿ÉÒÔÈÃÔÚǰ̨չʾ
KibanaµÄ°²×°¼°Ê¹ÓÃ
°²×°kibana»·¾³
¹Ù·½°²×°ÊÖ²á:https://www.elastic.co/guide/en/kibana /current/install.html ÏÂÔØkibanaµÄtar.gzµÄÈí¼þ°ü
# wget https://artifacts.elastic.co/downloads/kibana /kibana-5.4.0-linux-x86_64.tar.gz ½âѹkibanaµÄtar°ü
# tar -xzf kibana-5.4.0-linux-x86_64.tar.gz ½øÈë½âѹºÃµÄkibana
# mv kibana-5.4.0-linux-x86_64 /usr/local ´´½¨kibanaµÄÈíÁ¬½Ó
# ln -s /usr/local/kibana-5.4.0-linux-x86_64/
/usr/local/kibana ±à¼kibanaµÄÅäÖÃÎļþ
# vim /usr/local/kibana/config/kibana.yml ÐÞ¸ÄÅäÖÃÎļþÈçÏ£¬¿ªÆôÒÔϵÄÅäÖÃ
server.port: 5601 server.host: "0.0.0.0" elasticsearch.url: "http://192.168.1.202:9200" kibana.index: ".kibana" °²×°screen,ÒÔ±ãÓÚkibanaÔÚºǫ́ÔËÐУ¨µ±È»Ò²¿ÉÒÔ²» Óð²×°£¬ÓÃÆäËû·½Ê½½øÐкǫ́Æô¶¯£©
# yum -y install screen # screen # /usr/local/kibana/bin/kibana
netstat -antp |grep 5601
tcp 0 0 0.0.0.0:5601 0.0.0.0:* LISTEN 17007/node
´ò¿ªä¯ÀÀÆ÷²¢ÉèÖöÔÓ¦µÄindex
http://IP:5601 |


¶þ¡¢ELKʵսƪ
ºÃ£¬ÏÖÔÚË÷ÒýÒ²¿ÉÒÔ´´½¨ÁË£¬ÏÖÔÚ¿ÉÒÔÀ´Êä³önginx¡¢apache¡¢message¡¢secrueµÄÈÕÖ¾µ½Ç°Ì¨Õ¹Ê¾£¨NginxÓеϰֱ½ÓÐ޸ģ¬Ã»ÓÐ×ÔÐа²×°£©
±à¼nginxÅäÖÃÎļþ£¬ÐÞ¸ÄÒÔÏÂÄÚÈÝ£¨ÔÚhttpÄ£¿éÏÂÌí¼Ó£©
log_format json '{"@timestamp":"$time_iso8601",'
'"@version":"1",'
'"client":"$remote_addr",'
'"url":"$uri",'
'"status":"$status",'
'"domian":"$host",'
'"host":"$server_addr",'
'"size":"$body_bytes_sent",'
'"responsetime":"$request_time",'
'"referer":"$http_referer",'
'"ua":"$http_user_agent"'
'}'; ÐÞ¸Äaccess_logµÄÊä³ö¸ñʽΪ¸Õ²Å¶¨ÒåµÄjson
access_log logs/elk.access.log json; ¼ÌÐøÐÞ¸ÄapacheµÄÅäÖÃÎļþ LogFormat "{ \
\"@timestamp\": \"%{%Y-%m-%dT%H:%M:%S%z}t\",
\
\"@version\": \"1\", \
\"tags\":[\"apache\"], \
\"message\": \"%h %l %u %t \\\"%r\\\"
%>s %b\", \
\"clientip\": \"%a\", \
\"duration\": %D, \
\"status\": %>s, \
\"request\": \"%U%q\", \
\"urlpath\": \"%U\", \
\"urlquery\": \"%q\", \
\"bytes\": %B, \
\"method\": \"%m\", \
\"site\": \"%{Host}i\",
\
\"referer\": \"%{Referer}i\",
\
\"useragent\": \"%{User-agent}i\"
\
}" ls_apache_json Ò»ÑùÐÞ¸ÄÊä³ö¸ñʽΪÉÏÃæ¶¨ÒåµÄjson¸ñʽ
CustomLog logs/access_log ls_apache_json ±à¼logstashÅäÖÃÎļþ£¬½øÐÐÈÕÖ¾ÊÕ¼¯
vim /etc/logstash/conf.d/full.conf input {
file {
path => "/var/log/messages"
type => "system"
start_position => "beginning"
} file {
path => "/var/log/secure"
type => "secure"
start_position => "beginning"
} file {
path => "/var/log/httpd/access_log"
type => "http"
start_position => "beginning"
} file {
path => "/usr/local/nginx/logs/elk.access.log"
type => "nginx"
start_position => "beginning"
} }
output { if [type] == "system" { elasticsearch {
hosts => ["192.168.1.202:9200"]
index => "nagios-system-%{+YYYY.MM.dd}"
}
} if [type] == "secure" { elasticsearch {
hosts => ["192.168.1.202:9200"]
index => "nagios-secure-%{+YYYY.MM.dd}"
}
} if [type] == "http" { elasticsearch {
hosts => ["192.168.1.202:9200"]
index => "nagios-http-%{+YYYY.MM.dd}"
}
} if [type] == "nginx" { elasticsearch {
hosts => ["192.168.1.202:9200"]
index => "nagios-nginx-%{+YYYY.MM.dd}"
}
} } ÔËÐп´¿´Ð§¹ûÈçºÎ
logstash -f /etc/logstash/conf.d/full.conf |

¿ÉÒÔ·¢ÏÖËùÓд´½¨ÈÕÖ¾µÄË÷Òý¶¼ÒÑ´æÔÚ£¬½ÓÏÂÀ´¾ÍÈ¥Kibana´´½¨ÈÕÖ¾Ë÷Òý£¬½øÐÐչʾ£¨°´ÕÕÉÏÃæµÄ·½·¨½øÐд´½¨Ë÷Òý¼´¿É£©£¬¿´ÏÂչʾµÄЧ¹û

½ÓÏÂÀ´ÔÙÀ´Ò»·¢MySQLÂýÈÕÖ¾µÄչʾ
ÓÉÓÚMySQLµÄÂýÈÕÖ¾²éѯ¸ñʽ±È½ÏÌØÊ⣬ËùÒÔÐèÒªÓÃÕýÔò½øÐÐÆ¥Å䣬²¢Ê¹ÓÃmultilineÄܹ»½øÐжàÐÐÆ¥Å䣨¿´¾ßÌåÅäÖã©
input {
file {
path => "/var/log/messages"
type => "system"
start_position => "beginning"
}
file {
path => "/var/log/secure"
type => "secure"
start_position => "beginning"
} file {
path => "/var/log/httpd/access_log"
type => "http"
start_position => "beginning"
} file {
path => "/usr/local/nginx/logs/elk.access.log"
type => "nginx"
start_position => "beginning"
}
file {
path => "/var/log/mysql/mysql.slow.log"
type => "mysql"
start_position => "beginning"
codec => multiline {
pattern => "^# User@Host:"
negate => true
what => "previous"
}
}
} filter {
grok {
match => { "message" => "SELECT
SLEEP" }
add_tag => [ "sleep_drop" ]
tag_on_failure => []
}
if "sleep_drop" in [tags] {
drop {}
}
grok {
match => { "message" => "(?m)^#
User@Host: %{USER:User} \[[^\]]+\] @ (?:(?<clienthost>\S*)
)?\[(?:%{IP: Client_IP})?\]\s.*# Query_time:
%{NUMBER:Query_Time:float}\s+Lock_time: %{NUMBER:Lock_Time:float}\s+Rows_sent:
%{NUMBER:Rows_Sent:int}\s+Rows_examined: %{NUMBER:Rows_Examined:int}\s*(?:use
%{DATA:Database} ;\s*)?SET timestamp=%{NUMBER:timestamp};\s*(?<Query> (?<Action> \w+)\s+.*)\n#
Time:.*$" }
}
date {
match => [ "timestamp", "UNIX"
]
remove_field => [ "timestamp" ]
}
}
output { if [type] == "system" { elasticsearch {
hosts => ["192.168.1.202:9200"]
index => "nagios-system-%{+YYYY.MM.dd}"
}
} if [type] == "secure" { elasticsearch {
hosts => ["192.168.1.202:9200"]
index => "nagios-secure-%{+YYYY.MM.dd}"
}
} if [type] == "http" { elasticsearch {
hosts => ["192.168.1.202:9200"]
index => "nagios-http-%{+YYYY.MM.dd}"
}
} if [type] == "nginx" { elasticsearch {
hosts => ["192.168.1.202:9200"]
index => "nagios-nginx-%{+YYYY.MM.dd}"
}
}
if [type] == "mysql" { elasticsearch {
hosts => ["192.168.1.202:9200"]
index => "nagios-mysql-slow-%{+YYYY.MM.dd}"
}
}
} |
²é¿´Ð§¹û(Ò»ÌõÂýÈÕÖ¾²éѯ»áÏÔʾһÌõ£¬Èç¹û²»½øÐÐÕýÔòÆ¥Å䣬ÄÇôһÐоͻáÏÔʾһÌõ)

¾ßÌåµÄÈÕÖ¾Êä³öÐèÇ󣬽øÐоßÌåµÄ·ÖÎö
Èý£ºELKÖÕ¼«Æª
°²×°reids
# yum install -y redis
ÐÞ¸ÄredisµÄÅäÖÃÎļþ
# vim /etc/redis.conf ÐÞ¸ÄÄÚÈÝÈçÏÂ
daemonize yes bind 192.168.1.202 Æô¶¯redis·þÎñ
# /etc/init.d/redis restart ²âÊÔredisµÄÊÇ·ñÆôÓóɹ¦
# redis-cli -h 192.168.1.202 ÊäÈëinfoÈç¹ûÓв»±¨´í¼´¿É
redis 192.168.1.202:6379> info redis_version:2.4.10
.... ±à¼ÅäÖÃredis-out.confÅäÖÃÎļþ£¬°Ñ±ê×¼ÊäÈëµÄÊý¾Ý´æ´¢µ½redisÖÐ
# vim /etc/logstash/conf.d/redis-out.conf Ìí¼ÓÈçÏÂÄÚÈÝ input {
stdin {}
} output { redis {
host => "192.168.1.202"
port => "6379"
password => 'test'
db => '1'
data_type => "list"
key => 'elk-test'
}
} ÔËÐÐlogstashÖ¸¶¨redis-out.confµÄÅäÖÃÎļþ
# /usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/redis-out.conf |
ÔËÐгɹ¦ÒÔºó£¬ÔÚlogstashÖÐÊäÈëÄÚÈÝ£¨²é¿´ÏÂЧ¹û£©

±à¼ÅäÖÃredis-in.confÅäÖÃÎļþ£¬°ÑreidsµÄ´æ´¢µÄÊý¾ÝÊä³öµ½elasticsearchÖÐ
# vim /etc/logstash/conf.d/redis-out.conf
Ìí¼ÓÈçÏÂÄÚÈÝ
input{
redis {
host => "192.168.1.202"
port => "6379"
password => 'test'
db => '1'
data_type => "list"
key => 'elk-test'
batch_count => 1 #Õâ¸öÖµÊÇÖ¸´Ó¶ÓÁÐÖжÁÈ¡Êý¾Ýʱ£¬Ò»´ÎÐÔÈ¡³ö¶àÉÙÌõ£¬Ä¬ÈÏ125Ìõ£¨Èç¹ûredisÖÐûÓÐ125Ìõ£¬¾Í»á±¨´í£¬ËùÒÔÔÚ²âÊÔÆÚ¼ä¼ÓÉÏÕâ¸öÖµ£©
} } output {
elasticsearch {
hosts => ['192.168.1.202:9200']
index => 'redis-test-%{+YYYY.MM.dd}'
}
} ÔËÐÐlogstashÖ¸¶¨redis-in.confµÄÅäÖÃÎļþ
# /usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/redis-out.conf |

°Ñ֮ǰµÄÅäÖÃÎļþÐÞ¸Äһϣ¬±ä³ÉËùÓеÄÈÕÖ¾¼à¿ØµÄÀ´Ô´Îļþ¶¼´æ·Åµ½redisÖУ¬È»ºóͨ¹ýredisÔÚÊä³öµ½elasticsearchÖÐ
¸ü¸ÄΪÈçÏ£¬±à¼full.conf
input {
file {
path => "/var/log/httpd/access_log"
type => "http"
start_position => "beginning"
}
file {
path => "/usr/local/nginx/logs/elk.access.log"
type => "nginx"
start_position => "beginning"
} file {
path => "/var/log/secure"
type => "secure"
start_position => "beginning"
} file {
path => "/var/log/messages"
type => "system"
start_position => "beginning"
}
}
output {
if [type] == "http" {
redis {
host => "192.168.1.202"
password => 'test'
port => "6379"
db => "6"
data_type => "list"
key => 'nagios_http'
}
} if [type] == "nginx" {
redis {
host => "192.168.1.202"
password => 'test'
port => "6379"
db => "6"
data_type => "list"
key => 'nagios_nginx'
}
} if [type] == "secure" {
redis {
host => "192.168.1.202"
password => 'test'
port => "6379"
db => "6"
data_type => "list"
key => 'nagios_secure'
}
} if [type] == "system" {
redis {
host => "192.168.1.202"
password => 'test'
port => "6379"
db => "6"
data_type => "list"
key => 'nagios_system'
}
}
}
ÔËÐÐlogstashÖ¸¶¨shipper.confµÄÅäÖÃÎļþ
# /usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/full.conf
|
ÔÚredisÖв鿴ÊÇ·ñÒѾ½«Êý¾Ýдµ½ÀïÃæ(ÓÐʱºòÊäÈëµÄÈÕÖ¾Îļþ²»²úÉúÈÕÖ¾£¬»áµ¼ÖÂredisÀïÃæÒ²Ã»ÓÐдÈëÈÕÖ¾)

°ÑredisÖеÄÊý¾Ý¶ÁÈ¡³öÀ´£¬Ð´Èëµ½elasticsearchÖÐ(ÐèÒªÁíÍâһ̨Ö÷»ú×öʵÑé)
±à¼ÅäÖÃÎļþ
# vim /etc/logstash/conf.d/redis-out.conf
Ìí¼ÓÈçÏÂÄÚÈÝ
input {
redis {
type => "system"
host => "192.168.1.202"
password => 'test'
port => "6379"
db => "6"
data_type => "list"
key => 'nagios_system'
batch_count => 1
}
redis {
type => "http"
host => "192.168.1.202"
password => 'test'
port => "6379"
db => "6"
data_type => "list"
key => 'nagios_http'
batch_count => 1
} redis {
type => "nginx"
host => "192.168.1.202"
password => 'test'
port => "6379"
db => "6"
data_type => "list"
key => 'nagios_nginx'
batch_count => 1
}
redis {
type => "secure"
host => "192.168.1.202"
password => 'test'
port => "6379"
db => "6"
data_type => "list"
key => 'nagios_secure'
batch_count => 1
}
}
output {
if [type] == "system" {
elasticsearch {
hosts => ["192.168.1.202:9200"]
index => "nagios-system-%{+YYYY.MM.dd}"
}
} if [type] == "http" {
elasticsearch {
hosts => ["192.168.1.202:9200"]
index => "nagios-http-%{+YYYY.MM.dd}"
}
} if [type] == "nginx" {
elasticsearch {
hosts => ["192.168.1.202:9200"]
index => "nagios-nginx-%{+YYYY.MM.dd}"
}
} if [type] == "secure" {
elasticsearch {
hosts => ["192.168.1.202:9200"]
index => "nagios-secure-%{+YYYY.MM.dd}"
}
}
} |
×¢Òâ:
inputÊÇ´Ó¿Í»§¶ËÊÕ¼¯µÄ
outputÊÇͬÑùÒ²±£´æµ½192.168.1.202ÖеÄelasticsearchÖУ¬Èç¹ûÒª±£´æµ½µ±Ç°µÄÖ÷»úÉÏ£¬¿ÉÒÔ°ÑoutputÖеÄhostsÐ޸ijÉlocalhost£¬Èç¹û»¹ÐèÒªÔÚkibanaÖÐÏÔʾ£¬ÐèÒªÔÚ±¾»úÉϲ¿Êðkabana£¬ÎªºÎÒªÕâÑù×ö£¬Æðµ½Ò»¸öËÉñîºÏµÄÄ¿µÄ
˵°×ÁË£¬¾ÍÊÇÔÚ¿Í»§¶ËÊÕ¼¯ÈÕÖ¾£¬Ð´µ½·þÎñ¶ËµÄredisÀï»òÊDZ¾µØµÄredisÀïÃæ£¬Êä³öµÄʱºò¶Ô½ÓES·þÎñÆ÷¼´¿É
ÔËÐÐÃüÁî¿´¿´Ð§¹û
# /usr/share/logstash/bin/logstash
-f /etc/logstash/conf.d/redis-out.conf |
Ч¹ûÊǺÍÖ±½ÓÍùES·þÎñÆ÷Êä³öÒ»ÑùµÄ£¨ÕâÑùÊÇÏȽ«ÈÕÖ¾´æµ½redisÊý¾Ý¿â£¬È»ºóÔÙ´ÓredisÊý¾Ý¿âÀïÈ¡³öÈÕÖ¾£©

ÉÏÏßELK

ÒòΪES±£´æÈÕÖ¾ÊÇÓÀ¾Ã±£´æ£¬ËùÒÔÐèÒª¶¨ÆÚɾ³ýÒ»ÏÂÈÕÖ¾£¬ÏÂÃæÃüÁîΪɾ³ýÖ¸¶¨Ê±¼äǰµÄÈÕÖ¾
curl -X DELETE
http://xx.xx.com:9200/ logstash-*-`date +%Y-%m-%d
-d "-$n days"` |
|