splunk + docker-compose 实现自定义 index

时间:2023-03-08 21:52:04

splunk是一款非常优秀的运维管理平台。Splunk 是机器数据的引擎。使用 Splunk 可收集、索引和利用所有应用程序、服务器和设备生成的快速移动型计算机数据 。 使用 Splunking 处理计算机数据,可让您在几分钟内解决问题和调查安全事件。监视您的端对端基础结构,避免服务性能降低或中断。以较低成本满足合规性要求。关联并分析跨越多个系统的复杂事件。获取新层次的运营可见性以及 IT 和业务智能。

splunk 有多种方式来收集数据,这里只介绍其中一种—— HTTP事件收集器

splunk默认的可用索引只有三个:history main summary ,如果有多个项目的日志需要记录,那么只有这三个索引明显是不够的。所以我们需要自定义索引。

有三种方式来创建自定义索引:web网页、命令行、配置文件。

web网页方式,根据web上的提示即可成功创建索引。

命令行方式,比较少用,每次只能用一次,且将命令中的参数写入文件,即可变为配置文件。

配置文件方式,可以重复使用,它参数就是命令行中的参数。

通过docker-compose.yaml可以实现splunk index自动创建、配置.

配置文件如下:

docker-compose.yaml

splunk:
image: splunk/splunk:latest # 指定 splunk 镜像
container_name: splunk # docker 容器名
restart: always # 是否重启
volumes:
- ./splunk_dev/data:/opt/splunk/etc # 数据文件
- ./splunk_dev/conf:/opt/splunk/var # 配置文件
- ./splunk_dev/indexes.conf:/opt/splunk/etc/system/local/indexes.conf # 映射 indexes.conf 文件,具体内容见下面
- ./splunk_dev/inputs.conf:/opt/splunk/etc/apps/splunk_httpinput/local/inputs.conf # 映射 inputs.conf 文件,具体内容见下面
environment:
SPLUNK_START_ARGS: --accept-license
SPLUNK_ENABLE_LISTEN: 9997
SPLUNK_ADD: tcp 1514
SPLUNK_CMD_1: "set minfreemb 500 -auth admin:changeme" # 修改磁盘空间,可用磁盘空间不足500M时,会暂停索引。splunk免费版,这个数要小于等于500,不然报错
SPLUNK_CMD_2: "edit user admin -tz Asia/Shanghai -auth admin:changeme" # 修改用户时区,默认使用的零时区,即:当搜索日志时,会发现日志时间要早8个小时
SPLUNK_CMD_3: "restart" # 重启splunk使上述配置生效
ports:
- "8080:8000" # 配置docker 端口映射 访问 localhost:8000即可访问 splunk网页版
- "8088:8088" # 该端口是 HTTP事件收集器 使用的端口
在项目指定目录内创建 indexes.conf 文件,在本例中就是在项目的 splunk_dev 目录下创建的。
该文件的作用是 创建一个自定义的 index [customer_index] # index 的名字
homePath=/opt/splunk/var/lib/splunk/customer_index/db/ # 存储路径
coldPath=/opt/splunk/var/lib/splunk/customer_index/colddb/
thawedPath=/opt/splunk/var/lib/splunk/customer_index/thaweddb/
在项目指定目录内创建 inputs.conf 文件,在本例中就是在项目的 splunk_dev 目录下创建的。
该文件的作用是 配置和关联 上面创建的 index [http]
disabled=0 # 全局启用 HTTP 事件收集器 [http://customer_collecter] # 指定 index 关联的 HTTP事件收集器的名字 ,splunk 会自动创建该HTTP事件收集器
disabled=0 # 启用该 HTTP 事件收集器 可省略
token=4B28FED9-1FA7-4E67-A426-7B4DFA0A0624 # 指定index 的 token
index=customer_index # 关联 index

此时通过 docker-compost up -d即可创建splunk ,通过浏览器访问 localhost:8080即可登陆splunk

通过如下命令即可向splunk 发送一条日志,以验证splunk启用成功

curl -k https://localhost:8088/services/collector -H 'Authorization: Splunk 4B28FED9-1FA7-4E67-A426-7B4DFA0A0624' -d '{"sourcetype": "mysourcetype", "event":"Hello, World!"}'

如果想将 splunkdjango结合使用,需要安装 splunk-handler,然后在django 配置文件中添加如下内容:


# logging
LOGGING = {
'version': 1,
'formatters': {
'simple': {
'format': '%(asctime)s %(levelname)s %(module)s[%(lineno)d] %(message)s'
},
'json': {
'()': 'pythonjsonlogger.jsonlogger.JsonFormatter',
'format': ('%(asctime)s %(created)f %(exc_info)s %(filename)s %(funcName)s %(levelname)s %(levelno)s '
'%(lineno)d %(module)s %(message)s %(pathname)s %(process)s %(processName)s %(relativeCreated)d '
'%(thread)s %(threadName)s')
},
},
'handlers': {
'stdout_console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'stream': sys.stdout,
'formatter': 'simple',
},
'splunk': {
'level': 'DEBUG',
'class': 'splunk_handler.SplunkHandler',
'formatter': 'json',
'host': '127.0.0.1',
'port': 8088,
'token': '4B28FED9-1FA7-4E67-A426-7B4DFA0A0624',
'index': 'customer_index',
'sourcetype': 'json',
'verify': False,
'multiple_process': True,
},
},
'loggers': {
'django.request': {
'handlers': ['stdout_console', 'splunk'],
'propagate': False,
'level': 'DEBUG',
},
'console_logger': {
'level': 'DEBUG',
'handlers': ['stdout_console', 'splunk'],
'propagate': False
}
}
}