# ?v参数 加Title
[root@localhost ~]# curl -XGET 'http://'${es_ip}'/_cat/indices?v&index=switch_*'
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
# curl 命令默认有统计信息
[root@localhost ~]# curl -XGET 'http://'${es_ip}'/_cat/indices?index=switch_*' | awk '{print $3}'
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 930 100 930 0 0 66428 0 --:--:-- --:--:-- --:--:-- 66428
# 加 -s 参数去除统计信息
curl -s -XGET 'http://'${es_ip}'/_cat/indices?index=switch_*' | awk '{print $3}'
# write to ~/.bashrc
es_ip=localhost:9200
#alias curl='curl -u elastic:xxx'
alias curles='func() { alias curl='"'"'curl -u elastic:xxx'"'"';}; func'
alias curloff='func() { unalias curl;}; func'
#1. 查看集群状态API选项
curl -XGET 'http://'${es_ip}'/_cat'
#2. 查看节点信息
curl -XGET 'http://'${es_ip}'/_cat/nodes?v'
#2. 查看节点信息:总磁盘空间,已用磁盘空间,可用磁盘空间
curl -XGET 'http://'${es_ip}'/_cat/nodes?v&h=ip,diskTotal,diskUsed,diskUsedPercent,diskAvail'
#3. 查看master节点信息
curl -XGET 'http://'${es_ip}'/_cat/master?v'
#4. 查看集群的健康状态
curl -XGET 'http://'${es_ip}'/_cat/health?v'
可以看到我们的集群叫做"es-test",并且状态是绿色。无论何时我们去请求集群的健康状态我们会得到三种:green, yellow, red
#5. 创建索引
# log-test 为索引名
# pretty 参数表示输出格式良好的JSON响应(如果存在)
curl -XPUT 'http://'${es_ip}'/log-test?pretty'
#6. 查看索引列表
curl -XGET 'http://'${es_ip}'/_cat/indices?v'
#6. 查看相关索引
curl -XGET 'http://'${es_ip}'/_cat/indices?v&index=log-test-*'
#7. 删除索引
# log-test 为索引名
curl -XDELETE 'http://'${es_ip}'/log-test?pretty'
# 使用通配符批量删除索引
curl -XDELETE 'http://'${es_ip}'/log-test-*?pretty'
# 或者使用for语句批量删除索引
index=log-test-2023.04
for i in $(curl -s -XGET 'http://'${es_ip}'/_cat/indices' | awk '{print $3}' | grep ${index})
do
echo $i
curl -XDELETE 'http://'${es_ip}'/'$i'?pretty'
done
向es中插入文档(dic)的时候,必须要指定一个类型(type)
#1. 使用PUT来创建文档,需要指定id
# 索引 index:test_one
# 类型 type:test_type
# _id:1
curl -H "Content-Type: application/json" -XPUT 'http://'${es_ip}'/test_one/test_type/1?pretty' -d '{"name": "eson", "age": 35}'
#2. 使用POST来创建文档,可以不指定id(不指定时随机生成id)
curl -H "Content-Type: application/json" -XPOST 'http://'${es_ip}'/test_one/test_type?pretty' -d '{"name": "suzhang"}'
es_ip=192.168.200.96
index=redis_info_0
type=_doc # type=message
id=a2018f17-fbf2-11ea-b155-525400044f66
# 搜索索引中的所有记录: ### 默认返回10条
curl -XGET 'http://'${es_ip}'/'${index}'/_search?pretty'
# 搜索索引中某个type的所有记录:
curl -XGET 'http://'${es_ip}'/'${index}'/'${type}'/_search?pretty'
# 查询具体的某一条记录:
curl -XGET 'http://'${es_ip}'/'${index}'/'${type}'/'${id}'?pretty'
{
"_index" : "redis_info_0",
"_type" : "message",
"_id" : "a2018f17-fbf2-11ea-b155-525400044f66",
"_version" : 1,
"_seq_no" : 63,
"_primary_term" : 1,
"found" : true,
"_source" : {
"filebeat_prospector_type" : "log",
"gl2_remote_ip" : "192.168.200.62",
"gl2_remote_port" : 46346,
"source" : "jrdser03",
"beats_type" : "filebeat",
"gl2_source_input" : "5f18008ae1b595733300f9d6",
"filebeat_@metadata_beat" : "filebeat",
"filebeat_beat_version" : "6.6.0",
"filebeat_@timestamp" : "2020-09-21T10:10:10.171Z",
"filebeat_@metadata_version" : "6.6.0",
"filebeat_host_name" : "jrdser03",
"gl2_source_node" : "c502216e-4b21-41a8-8b6f-4c14f240fa67",
"filebeat_beat_hostname" : "jrdser03",
"timestamp" : "2020-09-21 10:10:10.171",
"gl2_accounted_message_size" : 585,
"gl2_source_collector" : "db792d3a-f6dd-4267-9896-7f8aa02371e5",
"filebeat_offset" : 47,
"filebeat_input_type" : "log",
"streams" : [
"5f68682713034574e5996979"
],
"gl2_message_id" : "01EJR28TM2FHBAEJ0SX8Y8256B",
"filebeat_tags" : [
"redis",
"redis-info"
],
"message" : "used_memory:1217758376",
"filebeat_collector_node_id" : "sidecar_200.62",
"filebeat_beat_name" : "jrdser03",
"filebeat_@metadata_type" : "doc",
"filebeat_source" : "/tmp/redis_info/redis_16002.log",
"filebeat_log_file_path" : "/tmp/redis_info/redis_16002.log"
}
}
# 查询一条索引文档中的具体的字段:
curl -XGET 'http://'${es_ip}'/'${index}'/'${type}'/'${id}'?_source=message&pretty'
# 如果需要查询多个字段,使用逗号进行隔开:
curl -XGET 'http://'${es_ip}'/'${index}'/'${type}'/'${id}'?_source=message,source&pretty'
# 获取source所有数据:(可以通过source指定显示哪些字段,不指定就是显示全部字段)
curl -XGET 'http://'${es_ip}'/'${index}'/'${type}'/'${id}'?_source&pretty'
# 等同于
curl -XGET 'http://'${es_ip}'/'${index}'/'${type}'/'${id}'?pretty'
# 根据条件进行查询:
curl -XGET 'http://'${es_ip}'/'${index}'/'${type}'/_search?q=filebeat_tags:redis&pretty'
# 精确查询
curl -H "Content-Type:application/json" -s -XGET 'http://'${es_ip}'/'${index}'/'${type}'/_search?pretty' -d '
{
"query": {
"term": {
"filebeat_host_name": "Jz-Nginx-01QH"
}
}
}'
# 模糊查询,包含“帐户、登录、失败”中的一个或多个的文档就会被搜索出来。
curl -H "Content-Type:application/json" -s -XGET 'http://'${es_ip}'/'${index}'/'${type}'/_search?pretty' -d '
{
"query": {
"match": {
"message": "帐户登录失败。"
}
},
"size": 0
}'
# 全匹配查询
curl -H "Content-Type:application/json" -s -XGET 'http://'${es_ip}'/'${index}'/'${type}'/_search?pretty' -d '
{
"query": {
"match_phrase": {
"message": "帐户登录失败。"
}
},
"size": 0
}'
# 全匹配查询,英文也适用
curl -H "Content-Type:application/json" -s -XGET 'http://'${es_ip}'/'${index}'/'${type}'/_search?pretty' -d '
{
"query": {
"match_phrase": {
"message": "Failed password for"
}
},
"size": 0
}'
# 全匹配查询,可能比较严,我们会希望有个可调节因子,少匹配一个也满足,那就需要使用到slop。
curl -H "Content-Type:application/json" -s -XGET 'http://'${es_ip}'/'${index}'/'${type}'/_search?pretty' -d '
{
"query": {
"match_phrase": {
"message": {
"query": "帐户登录失败。",
"slop": 1
}
}
},
"size": 0
}'
# 根据多个条件进行查询:(有问题)
curl -XGET 'http://'${es_ip}'/'${index}'/'${type}'/_search?q=filebeat_tags=redis&q=port=16002&_source=message,source,port&pretty'
# 根据多个条件进行查询:
https://www.cnblogs.com/shangwei/p/13530429.html
# 查询 organization_id 为 208 和 event_type 为 007 的数据
curl -X GET 10.44.99.102:9200/situation-event/_search?pretty -d
{
"query": {
"bool": {
"must": [{
"term": {
"organization_id": "208"
}
}, {
"term": {
"event_type": "007"
}
}]
}
}
}
# 查询 organization_id 为 208 和 event_type 为 007 或者 008 的数据
curl -X GET 10.44.99.102:9200/situation-event/_search?pretty -d
{
"query": {
"bool": {
"must": [{
"match_phrase": {
"organization_id": "208"
}
}],
"should": [{
"match_phrase": {
"event_type": "007"
}
},
{
"match_phrase": {
"event_type": "008"
}
}
],
"minimum_should_match": 1
}
}
}
# should 可以使用 []
curl -X GET 10.44.99.102:9200/situation-event/_search?pretty -d
{
"query": {
"bool": {
"must": [{
"term": {
"organization_id": "208"
}
}],
"should": [{
"terms": {
"event_type": ["007", "008"]
}
}
],
"minimum_should_match": 1
}
}
}
curl -H "Content-Type:application/json" -s -XGET 'http://'${es_ip}'/'${index}'/'${type}'/_search?pretty' -d '
{
"query": {
"bool": {
"must": [{
"term": {
"winlogbeat_collector_node_id": "sidecar_203.122"
}
}, {
"term": {
"winlogbeat_level": "错误"
}
}]
}
},
"_source":["winlogbeat_collector_node_id", "winlogbeat_level", "message"],
"size": 1
}'
# 根据条件进行查询多个字段:
curl -XGET 'http://'${es_ip}'/'${index}'/'${type}'/_search?q=filebeat_tags:redis&_source=message,source&pretty'
curl -XGET 'http://192.168.200.96:9200/redis_info_152/message/_search?q=filebeat_tags:redis&_source=message,source&pretty'
#-----------------------------------------
get
&size=0
post
"size": 0
使用PUT并指定id时,es会使用新的文档替换原文档
curl -H "Content-Type: application/json" -XPUT 'http://'${es_ip}'/test_one/test_type/1?pretty' -d '{"name": "su"}'
curl -H "Content-Type: application/json" -XPOST 'http://'${es_ip}'/test_one/test_type/2/_update?pretty' -d '{"doc":{"name": "suxxxnxxg"}}'
es_ip=192.168.200.96
index=redis_info_4
type=message
id=834c73a0-fedb-11ea-b155-525400044f66
curl -XDELETE 'http://'${es_ip}'/'${index}'/'${type}'/'${id}'?pretty'
# 查看设置选项
curl -XGET 'http://'${es_ip}'/'${index}'/_settings?pretty'
{
"redis_info_0" : {
"settings" : {
"index" : {
"number_of_shards" : "1",
"blocks" : {
"write" : "true",
"metadata" : "false",
"read" : "false"
},
"provided_name" : "redis_info_0",
"creation_date" : "1600675483051",
"analysis" : {
"analyzer" : {
"analyzer_keyword" : {
"filter" : "lowercase",
"tokenizer" : "keyword"
}
}
},
"number_of_replicas" : "0",
"uuid" : "J4w5QOhMQsWOMpIWAqAOBw",
"version" : {
"created" : "6081099"
}
}
}
}
}
# 更改索引最大查询窗口,默认值为10000
curl -H "Content-Type: application/json" -XPUT 'http://'${es_ip}'/'${index}'/_settings' -d '{ "index" : { "max_result_window" : 10000}}'
{"acknowledged":true}
ELK日志我们一般都是按天存储,例如索引名为"kafkalog-2022-04-05",因为日志量所占的存储是非常大的,我们不能一直保存,而是要定期清理旧的,这里就以保留7天日志为例。
自动清理7天以前的日志可以用定时任务的方式,这样就需要加入多一个定时任务,可能不同服务记录的索引名又不一样,这样用定时任务配还是没那么方便。
ES给我们提供了一个索引的生命周期策略(lifecycle),就可以对索引指定删除时间,能很好解决这个问题。
索引生命周期分为四个阶段:HOT(热)=>WARM(温)=》COLD(冷)=>DELETE(删除)
curl -H "Content-Type: application/json" -XPUT 'http://'${es_ip}'/_ilm/policy/auto_delete_policy' -d '{
"policy": {
"phases": {
"delete": {
"min_age": "8d",
"actions": {
"delete": {}
}
}
}
}
}'
# 查看创建的策略
curl -XGET 'http://'${es_ip}'/_ilm/policy/auto_delete_policy?pretty'
创建一个自动删除策略(auto_delete_policy)
delete:删除阶段,7天执行删除索引动作
查看策略:GET _ilm/policy/
索引模板可以匹配索引名称,匹配到的索引名称按这个模板创建mapping
curl -H "Content-Type: application/json" -XPUT 'http://'${es_ip}'/_template/elk_template' -d '{
"index_patterns": ["kafka*"],
"settings": {
"index":{
"lifecycle":{
"name":"auto_delete_policy",
"indexing_complete": true
}
}
}
}'
# 查看创建的模板
curl -XGET 'http://'${es_ip}'/_template/elk_template?pretty'
创建索引模板(elk_tempalte),index.lifecycle.name把上面的自动删除策略绑定到elk索引模板
创建 kafka 开头的索引时就会应用这个模板。
indexing_complete:true,必须设为true,跳过HOT阶段的Rollover
查看模板:GET /_template/
这里测试时把DELETE的日期由7天"7d"改为1分钟"1m"。
生命周期策略默认10分钟检测一次,为了方便测试,这里设为30s。
curl -H "Content-Type: application/json" -XPUT 'http://'${es_ip}'/_cluster/settings' -d '{
"transient": {
"indices.lifecycle.poll_interval":"30s"
}
}'
# 查看生命周期策略默认10分钟检测一次
curl -XGET 'http://'${es_ip}'/_cluster/settings?pretty'
把日志写入到es后,查看日志索引的生命周期策略信息。注意:须先创建策略、模板,再创建索引。
# 插入文档
index=kafka
curl -H "Content-Type: application/json" -XPUT 'http://'${es_ip}'/'${index}'/test_type/1?pretty' -d '{"name": "eson", "age": 35}'
curl -H "Content-Type: application/json" -XPUT 'http://'${es_ip}'/'${index}'/test_type/2?pretty' -d '{"name": "eson", "age": 35}'
# 查询文档
curl -XGET 'http://'${es_ip}'/'${index}'/_search?pretty'
# 删除索引
curl -XDELETE 'http://'${es_ip}'/'${index}'?pretty'
# 查看test_one索引的生命周期策略
curl -XGET 'http://'${es_ip}'/'${index}'/_ilm/explain/?pretty'
过一会再点查询,索引已经没有了,说明已经生效。
# 查看生命周期策略
curl -XGET 'http://'${es_ip}'/_ilm/policy/?pretty'
# 管理
curl -XPOST 'http://'${es_ip}'/_ilm/start'
curl -XGET 'http://'${es_ip}'/_ilm/status'
curl -XPOST 'http://'${es_ip}'/_ilm/stop'