当前位置: 首页 > 科技观察

Ubuntu16.04下部署Graylog日志服务器_0

时间:2023-03-16 10:02:29 科技观察

Graylog是一个开源的日志管理系统,集中收集、索引和分析其他服务器发送的日志。用Java语言编写,可以接收TCP、UDP、AMQP协议发送的日志信息,使用Mongodb作为后台数据库。它还具有一个用Ruby编写的Web管理界面,可以轻松管理Graylog和查询日志。Graylog可以收集和监控许多不同应用程序的日志。本文将仅用于演示目的,所有使用的组件都将安装在单独的服务器上。对于大型的生产系统,可以将组件分开安装在不同的服务器上,这样可以提高效率。Graylog2的组件Graylog2有4个基本组件:GraylogServer:该服务负责接收和处理日志/消息,并与其他组件通信。Elasticsearch:存储所有日志,其性能取决于内存和硬盘IO。MongoDB:存储数据。网络界面:用户界面。下图是Graylog2各个组件之间的关系图。安装和配置Graylog2环境依赖性Graylog2需要以下环境依赖性:Ubuntu16.04服务器,至少有2GB内存。Elasticsearch(>=2.x,推荐最稳定的版本。)MongoDB(>=2.4,推荐最稳定的版本。)OracleJavaSEorOpenJDK(>=8,推荐最稳定的版本。)如果你UbuntuServer是最小安装,还需要提前安装以下包:$sudoapt-getinstallapt-transport-httpsuuid-runtimepwgen安装JavaJDKElasticsearch是一个基于Java的应用程序,我们首先需要安装OpenJDK或OracleJDK。这里我们选择安装OpenJDK8:$sudoapt-getupdate&&sudoapt-getinstallopenjdk-8-jdk如果你的系统有多个Java版本,可以使用如下命令设置默认版本。$update-alternatives--configjavainstallElasticsearchElasticsearch是Graylog的主要组件,负责分析和索引日志。Graylog2.3.x开始支持Elasticsearch5.x,这里我们安装Elasticsearch5.x版本。#添加GPG签名密钥$wget-qO-https://artifacts.elastic.co/GPG-KEY-elasticsearch|sudoapt-keyadd-#添加Elasticsearch源$echo"debhttps://artifacts.elastic.co/packages/5.x/aptstablemain"|sudotee-a/etc/apt/sources.list.d/elastic-5.x.list#安装Elasticsearch$sudoapt-getupdate&&sudoapt-getinstallelasticsearch编辑Elasticsearch配置文件:$sudovim/etc/elasticsearch/elasticsearch.yml#将cluster.name设置为graylog。cluster.name:graylog修改配置后需要重启Elasticsearch:$sudosystemctldaemon-reload$sudosystemctlrestartelasticsearch.service如果要添加Elasticsearch服务随系统启动,可以执行以下命令:$sudosystemctlenableelasticsearch.service测试Elastisearch是否正常Elastisearch默认使用9200端口接收http请求,这里使用curl命令进行简单的请求测试。$curl-XGEThttp://localhost:9200{"name":"V8jWSvJ","cluster_name":"graylog","cluster_uuid":"8cnTgvEzRZ2U81LTYq5nEw","version":{"number":"5.6.3","build_hash":"1a2f265","build_date":"2017-10-06T20:33:39.012Z","build_snapshot":false,"lucene_version":"6.6.1"},"tagline":"YouKnow,forSearch"}查看Elasticsearch的健康状态$curl-XGET'http://localhost:9200/_cluster/health?pretty=true'{"cluster_name":"graylog","status":"green","timed_out":false“number_of_nodes”:1,“number_of_data_nodes”:1,“active_primary_shards”:0,“active_shards”:0,“relocating_shards”:0,“initializing_shards”:0,“unassigned_shards”:0,“delayed_unassigned_shards”:0,”number_of_pending_tasks”:0,"number_of_in_flight_fetch":0,"task_max_waiting_in_queue_millis":0,"active_shards_percent_as_number":100.0}安装MongoDB(当前版本为2.6.10)包含在默认安装源MongoDBubuntu16.04支持Graylog2.3.x或更高版本$sudoapt-getinstallmongodb-server启动MongoDB服务$sudosystemctlstartmongodb如果要添加MongoDB服务随系统启动,可以执行以下命令:$sudosystemctlenablemongodb安装GraylogServerGraylogServer负责接收和处理日志。#DownloadandinstallGraylogServerwarehouse$wgethttps://packages.graylog2.org/repo/packages/graylog-2.3-repository_latest.deb$sudodpkg-igraylog-2.3-repository_latest.deb#InstallGraylogServer$sudoapt-getupdate&&sudoapt-getinstallgraylog-serverEditGraylogServer配置文件:设置password_secret,首先使用pwgen命令生成密码:$pwgen-N1-s961jfPjMRn5XRsCdVWArjy1nulgXbUJJ8khuW0xQGrqUvJ1iXefhqSh12xsp1dZgkKVsOwiOuDLArh6TYafQE8QFDjEzUIU1tS修改password_secret参数值:$sudovim/etc/graylog/server/server.confpassword_secret=1jfPjMRn5XRsCdVWArjy1nulgXbUJJ8khuW0xQGrqUvJ1iXefhqSh12xsp1dZgkKVsOwiOuDLArh6TYafQE8QFDjEzUIU1tS设置GraylogServer管理员密码root_password_sha2,这个密码用tologintothewebmanagementpage.假如你要把密码设置为000000,可以使用sha256sum命令来生成:$echo-n000000|sha256sum91b4d142823f7d20c5f08df69122de43f35f057a988d9619f6d3138485c9a203修改root_password_sha2参数值:$sudovim/etc/graylog/server/server.confroot_password_sha2=91b4d142823f7d20c5f08df69122de43f35f057a988d9619f6d3138485c9a203注:password_secret和root_password_sha2参数是必须设mustbespecified,otherwiseGraylogServerwillfailtostart.Ifyouwanttosettheadministratormailboxandtimezone,youcanusethefollowingparametervalue:$sudovim/etc/graylog/server/server.confroot_email="admin@hi-linux.com"root_timezone=UTCSettheElasticsearchnode:$sudovim/etc/graylog/server/server.conf#Default:http://127.0.0.1:9200elasticsearch_hosts=http://192.168.100.212:9200IfyouneedtoconfiguremultipleElasticsearchnodesornodesthatrequireauthentication,youcanconfiguretheminthefollowingformat:elasticsearch_hosts=http://node1:9200,http://user:password@node2:19200Note:Ifnotconfigured,thedefaultistoconnecttothelocalElasticsearchnode.其它一些和Elasticsearch的相关设置:elasticsearch_index_prefix=graylogelasticsearch_connect_timeout=10selasticsearch_max_docs_per_index=20000000elasticsearch_max_total_connections=20elasticsearch_max_number_of_indices=20elasticsearch_shards=1elasticsearch_replicas=0安装GraylogWeb接口从Graylog2.x版本开始,Graylog已经默认集成了Web接口。ConfiguretheGraylogWebinterface$sudovim/etc/graylog/server/server.conf#配置rest的URIApirest_listen_uri=http://your_ip_or_domain:9001/#配置Web界面的URIweb_listen_uri=http://your_ip_or_domain:9000/注意:your_ip_or_domain是给你的实际服务器IP或域名。启动GraylogServer服务:$sudosystemctldaemon-reload$sudosystemctlstartgraylog-server.service如果想添加GraylogServer服务随系统启动,可以执行以下命令:$sudosystemctlenablegraylog-server.service访问GraylogWeb使用浏览器访问http://your_ip_or_doamin:9000,如果能成功看到如下界面,说明安装成功。默认用户名为:admin,密码为:在root_password_sha2参数中设置。添加需要收集日志的服务器CreateSyslogUDPInputUbuntu系统自带的Rsyslog服务不需要安装,配置即可。打开Graylog网页,选择System->Inputs->SyslogUDP->Launchnewinput,添加接收Syslog日志的服务器。在弹出的窗口中输入以下信息:节点:在列表中选择你的GraylogServer服务器标题:LinuxServerLogs端口:8514绑定地址:0.0.0.0点击保存配置完成后,监听8514端口的Syslog是generated服务器,下面可以用来收集其他服务器的日志。本次测试使用同一台服务器进行演示,因此绑定了所有网卡接口。如果仅在特定网络访问,请根据实际情况填写绑定地址的IP地址。现在,我们的GraylogServer服务器已准备好接收来自其他服务器的日志。接下来我们还需要配置需要采集的日志服务器,让这些服务器将日志发送到GraylogServer服务器。要配置服务器以将日志发送到Graylog,请创建rsyslog配置文件/etc/rsyslog.d/90-graylog.conf。如果您的rsyslog版本>5.10,请按以下格式配置:*.*@graylog_server_ip:8514;RSYSLOG_SyslogProtocol23Format如果您的rsyslog版本<5.10,请按以下格式配置:$templateGRAYLOGFC5424,"<%PRI%>%PROTOCOL-VERSION%%TIMESTAMP:::date-rfc3339%%HOSTNAME%%APP-NAME%%PROCID%%MSGID%%STRUCTURED-DATA%%msg%\n"*.*@graylog_server_ip:8514;GRAYLOGRFC5424注意:替换graylog_server_ip与Graylog服务器IP地址。我这里使用的是rsyslog8.16版本,修改类似如下:$sudovim/etc/rsyslog.d/90-graylog.conf*.*@192.168.100.212:8514;RSYSLOG_SyslogProtocol23Format重启rsyslog服务生效$sudosystemctlrestartrsyslog配置完成后,回到GraylogWeb,点击Sources,查看是否有新增的Rsyslog源图。搜索Graylog在GraylogWeb上,单击搜索以访问Graylog搜索页面。这里可以根据条件查询指定的日志。如果要搜索ssh的活动日志,输入关键字sshd,点击搜索图标:一些常用的搜索语法搜索包含关键字ssh的信息ssh搜索包含关键字ssh或login的信息sshlogin搜索包含完整关键字的信息sshlogin"sshlogin"searchfieldtypecontainssshinformationtype:sshsearchfieldtypecontainssshorlogininformationtype:(sshlogin)searchfieldtypecontainscompletekeywordsshlogininformationtype:"sshlogin"更详细的搜索语法请参考官方网站文档:http://docs.graylog.org/en/2.3/pages/queries.html至此,一个可以从其他服务器收集日志的基本Graylog服务器部署已经完成。当然,Graylog还提供了一系列其他丰富的功能,例如仪表板、警报和流。期待我们下次的进一步探索。参考文档http://www.google.comhttp://blog.topspeedsnail.com/archives/6670http://docs.graylog.org/en/2.3/pages/installation/os/ubuntu.htmlhttps://marketplace.graylog.org/addons/a47beb3b-0bd9-4792-a56a-33b27b567856https://www.digitalocean.com/community/tutorials/how-to-manage-logs-with-graylog-2-on-ubuntu-16-04