10年 Java程序员,硬核人生!勇往直前,永不退缩!

欢迎围观我的git:https://github.com/R1310328554/spring_security_learn 寻找志同道合的有志于研究技术的朋友,关注本人微信公众号: 觉醒的码农,或Q群 165874185

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

 

 

 

安装

基本上有3种方式,
1 yum安装
2 rpm安装
3 docker安装

yum安装

yum安装,参照官方文档是最好的:
http://docs.graylog.org/en/3.0/pages/installation/os/centos.html

#没有安装jdk,则先安装jdk:
$ sudo yum install java-1.8.0-openjdk-headless.x86_64


#安装MongoDB
#修改yum源 /etc/yum.repos.d/mongodb-org.repo
[mongodb-org-4.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.0.asc

sudo yum install mongodb-org

$ sudo chkconfig --add mongod
$ sudo systemctl daemon-reload
$ sudo systemctl enable mongod.service
$ sudo systemctl start mongod.service


#安装ES
rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch

#修改yum源 /etc/yum.repos.d/elasticsearch.repo
[elasticsearch-6.x]
name=Elasticsearch repository for 6.x packages
baseurl=https://artifacts.elastic.co/packages/oss-6.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md

#安装
sudo yum install elasticsearch-oss

#配置 /etc/elasticsearch/elasticsearch.yml
cluster.name: graylog
action.auto_create_index: false

#启动
$ sudo chkconfig --add elasticsearch
$ sudo systemctl daemon-reload
$ sudo systemctl enable elasticsearch.service
$ sudo systemctl restart elasticsearch.service

#安装Graylog 服务器
$ sudo rpm -Uvh https://packages.graylog2.org/repo/packages/graylog-3.0-repository_latest.rpm
$ sudo yum install graylog-server

#配置,安装好之后需要修改配置,/etc/graylog/server/server.conf 添加: password_secret、root_password_sha2

# root_password_sha2 通过下面的命令生成, 其中输入的密码就是admin的密码,也就是登录时候的密码
echo -n "Enter Password: " && head -1 </dev/stdin | tr -d '\n' | sha256sum | cut -d" " -f1

# Generate one by using for example: pwgen -N 1 -s 96
# password_secret 是可以随便写的,但是要求长度64位,可以通过pwgen 来生成。但是安装pwgen,pwgen需要安装。比较麻烦。

# 参照网上博客,执行 wget http://sourceforge.net/projects/pwgen,可以下载,发现无法解压,执行tar zxvf pwgen,报错说不是正确格式。
# 于是继续尝试通过 yum。 直接 yum install pwgen是不行的。需要先执行 yum  install epel-release 然后 yum install pwgen。 不知道为什么 需要 epel-release

有些值是可以使用默认值的,有些不行。特别是需要设置 ip。 也就是 http_bind_address, 是绝不能使用默认值的, 我开始使用默认值,死活不能通过宿主机访问,改为实际ip就好了。 另外 http_publish_uri 默认是 http_bind_address, 是不需要设置的。但是如果设置则需要设置正确。 elasticsearch_hosts 可以使用默认值:elasticsearch_hosts = http://127.0.0.1:9200; mongodb_uri 也是。Email transport 什么的可以先不管。 后面有需要再改。



#启动。
$ sudo chkconfig --add graylog-server
$ sudo systemctl daemon-reload
$ sudo systemctl enable graylog-server.service
$ sudo systemctl start graylog-server.service

启动好了后, 可以grep一下mongo,elasticsearch,graylog。 特别需要注意的是,  配置graylog的时候, publish_url 需要一个实际的ip,而不能是 127.0.0.1 。

Docker安装

Docker安装的话,其实更简单。


 yum install docker
 systemctl start docker
cat /etc/docker/daemon.json
{
  "registry-mirrors": ["http://hub-mirror.c.163.com"]
}


准备一个文件 docker-compose.yml ,放/opt 目录下


version: '2'
services:
  # MongoDB: https://hub.docker.com/_/mongo/
  mongodb:
    image: mongo:3
  # Elasticsearch: https://www.elastic.co/guide/en/elasticsearch/reference/6.6/docker.html
  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch-oss:6.6.1
    environment:
      - http.host=0.0.0.0
      - transport.host=localhost
      - network.host=0.0.0.0
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
    ulimits:
      memlock:
        soft: -1
        hard: -1
    mem_limit: 1g
  # Graylog: https://hub.docker.com/r/graylog/graylog/
  graylog:
    image: graylog/graylog:3.0
    environment:
      # CHANGE ME (must be at least 16 characters)!
      - GRAYLOG_PASSWORD_SECRET=somepasswordpepper
      # Password: admin
      - GRAYLOG_ROOT_PASSWORD_SHA2=8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918
      - GRAYLOG_HTTP_EXTERNAL_URI=http://127.0.0.1:9000/  ## 需要修改
    links:
      - mongodb:mongo
      - elasticsearch
    depends_on:
      - mongodb
      - elasticsearch
    ports:
      # Graylog web interface and REST API
      - 9000:9000
      # Syslog TCP
      - 1514:1514
      # Syslog UDP
      - 1514:1514/udp
      # GELF TCP
      - 12201:12201
      # GELF UDP
      - 12201:12201/udp
      
然后 docker-compose up  就好了!

特别需要注意的是,上面的 GRAYLOG_HTTP_EXTERNAL_URI 需要一个实际的ip, 否则其他机器是无法访问的!会出现上面情况呢? 就是其他机器可以Telnet graylog, 但是web访问显示一片空白。
通过curl 得到下面的结果:

# curl http://192.168.11.183:9000
<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="robots" content="noindex, nofollow">
    <meta charset="UTF-8">
    <title>Graylog Web Interface</title>
    <link rel="shortcut icon" href="http://127.0.0.1:9000/assets/favicon.png">
    
  </head>
  <body>
    <script src="http://127.0.0.1:9000/config.js"></script>
    
    <script src="http://127.0.0.1:9000/assets/vendor.4024e2a8db732781a971.js"></script>
    
    <script src="http://127.0.0.1:9000/assets/polyfill.a5e2fb591e8fd54ee4ef.js"></script>
    
    <script src="http://127.0.0.1:9000/assets/builtins.a5e2fb591e8fd54ee4ef.js"></script>
    
    <script src="http://127.0.0.1:9000/assets/plugin/org.graylog.plugins.threatintel.ThreatIntelPlugin/plugin.org.graylog.plugins.threatintel.ThreatIntelPlugin.b864ba54b438ac0bdc48.js"></script>
    
    <script src="http://127.0.0.1:9000/assets/plugin/org.graylog.plugins.collector.CollectorPlugin/plugin.org.graylog.plugins.collector.CollectorPlugin.bcc87290018e859a8a9e.js"></script>
    
    <script src="http://127.0.0.1:9000/assets/plugin/org.graylog.aws.AWSPlugin/plugin.org.graylog.aws.AWSPlugin.8ae7cb13983ce33eeb5b.js"></script>
    
    <script src="http://127.0.0.1:9000/assets/app.a5e2fb591e8fd54ee4ef.js"></script>
    
  </body>
</html>

开始没有发现问题,后面才知道 上面的html全部都是通过 js动态加载的, 其中的 script src="http://127.0.0.1:9000/ 其实就是一个问题的根由。 很明显上面的src 是 本地的,是其他机器无法正常访问的!

改了之后,再观察,就正常了!(注意下面的src )
# curl http://192.168.11.183:9000
<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="robots" content="noindex, nofollow">
    <meta charset="UTF-8">
    <title>Graylog Web Interface</title>
    <link rel="shortcut icon" href="http://192.168.11.127:9000/assets/favicon.png">
    
  </head>
  <body>
    <script src="http://192.168.11.127:9000/config.js"></script>
    
    <script src="http://192.168.11.127:9000/assets/vendor.4024e2a8db732781a971.js"></script>
    
    <script src="http://192.168.11.127:9000/assets/polyfill.a5e2fb591e8fd54ee4ef.js"></script>
    
    <script src="http://192.168.11.127:9000/assets/builtins.a5e2fb591e8fd54ee4ef.js"></script>
    
    <script src="http://192.168.11.127:9000/assets/plugin/org.graylog.plugins.threatintel.ThreatIntelPlugin/plugin.org.graylog.plugins.threatintel.ThreatIntelPlugin.b864ba54b438ac0bdc48.js"></script>
    
    <script src="http://192.168.11.127:9000/assets/plugin/org.graylog.plugins.collector.CollectorPlugin/plugin.org.graylog.plugins.collector.CollectorPlugin.bcc87290018e859a8a9e.js"></script>
    
    <script src="http://192.168.11.127:9000/assets/plugin/org.graylog.aws.AWSPlugin/plugin.org.graylog.aws.AWSPlugin.8ae7cb13983ce33eeb5b.js"></script>
    
    <script src="http://192.168.11.127:9000/assets/app.a5e2fb591e8fd54ee4ef.js"></script>
    
  </body>
</html>

实际上,9000 可以访问,但是web显示一片空白, 其实可以通过F12 观察到的。 F12 看到很多的500 404, 那就是在 强烈指明是 服务端的问题了!!


graylog 的9000 web界面可以访问了,但是现在还看不到日志。然后java 也需要配置一下,这个很容易,logback增加一个GELF就好了。不过还是不行的,graylog 还需要手动配置input。 这一点上,和ELK是很大不同的! 刚开始接触的时候就一直卡在了这里,真是坑。

配置GELF的时候,注意旋转 GELF TCP或者GELF UDP,我开始的时候想当然的认为java web,然后http,然后选择了HTTP,结果不行,后面才明白是端口问题。graylog是默认开启了 12201 的tcp和udp端口 。

 

使用

graylog的使用其实是很简单的,经过前面的简单配置,graylog已经可以正式使用了。不同于ELK, ELK配置logstash的grok 要折腾死很多脑细胞。
界面方便跟elk差不多,但是明显,易用多了!

如果 点开Search界面看不到任何的日志,要么就是没有配置input,要么就是时间不对,要么就是没有日志进来。

 

一些注意事项:


The search syntax is very close to the Lucene syntax. By default all message fields are included in the search if you don’t specify a message field to search in.


es6之后,
Elasticsearch 2.x and 5.x split queries on whitespace, so the query type:(ssh login) was equivalent to type:(ssh OR login). This is no longer the case in Elasticsearch 6.0 and you must now include an OR operator between each term.
—— 我在kibana中测试,好像不是这样的。。

Elasticsearch 2.x allows to use _missing_:type instead of NOT _exists_:type. This query syntax has been removed in Elasticsearch 5.0.

多个field之间,可以不用加AND OR,直接空格就好, 默认是OR?

Note that AND, OR, and NOT are case sensitive and must be typed in all upper-case.  必须全大写
—— 如果想查询包含and 关键字的呢?

Note that leading wildcards are disabled to avoid excessive memory consumption! You can enable them in your Graylog configuration file:

allow_leading_wildcard_searches = true

allow_leading_wildcard_searches 默认是false !


Numeric fields support range queries. Ranges in square brackets are inclusive, curly brackets are exclusive and can even be combined:
数字类型field支持range查询



Escaping 转义!

The following characters must be escaped with a backslash:

&& || : \ / + - ! ( ) { } [ ] ^ " ~ * ?


示例:
level_name:ERROR —————— 这里的ERROR 是不能变形的,也就是说不能小写,不能大小写混合! 这点和kibana不同,估计是因为graylog中level_name是enum类型?



参考:
https://www.jianshu.com/p/97fcb10c3556
https://www.cnblogs.com/yuhuLin/p/7018858.html
https://blog.csdn.net/dongshaoshuai/article/details/54583668
https://blog.csdn.net/weixin_41004350/article/details/87253316

posted on 2019-05-29 16:57  CanntBelieve  阅读(2413)  评论(0编辑  收藏  举报