How can create index name automatically by filebeat?

My filebeat is working now want to know how can config it to create it index automatically with hostname ? my configuration is such as folllow :

###################### Filebeat Configuration Example #########################

# This file is an example configuration file highlighting only the most common
# options. The filebeat.reference.yml file from the same directory contains all the
# supported options with more comments. You can use it as a reference.
#
# You can find the full configuration reference here:
# https://www.elastic.co/guide/en/beats/filebeat/index.html

# For more available modules and options, please see the filebeat.reference.yml sample
# configuration file.

# ============================== Filebeat inputs ===============================

filebeat.inputs:

# Each - is an input. Most options can be set at the input level, so
# you can use different inputs for various configurations.
# Below are the input specific configurations.

# filestream is an input for collecting log messages from files.
- type: filestream

  # Unique ID among all inputs, an ID is required.
  id: my-filestream-id

  # Change to true to enable this input configuration.
  enabled: true

  # Paths that should be crawled and fetched. Glob based paths.
  paths:
    - /usr/home/*.txt
    #- c:\programdata\elasticsearch\logs\*

  # Exclude lines. A list of regular expressions to match. It drops the lines that are
  # matching any regular expression from the list.
  # Line filtering happens after the parsers pipeline. If you would like to filter lines
  # before parsers, use include_message parser.
  #exclude_lines: ['^DBG']

  # Include lines. A list of regular expressions to match. It exports the lines that are
  # matching any regular expression from the list.
  # Line filtering happens after the parsers pipeline. If you would like to filter lines
  # before parsers, use include_message parser.
  #include_lines: ['^ERR', '^WARN']

  # Exclude files. A list of regular expressions to match. Filebeat drops the files that
  # are matching any regular expression from the list. By default, no files are dropped.
  #prospector.scanner.exclude_files: ['.gz$']

  # Optional additional fields. These fields can be freely picked
  # to add additional information to the crawled log files for filtering
  #fields:
  #  level: debug
  #  review: 1

# ============================== Filebeat modules ==============================

filebeat.config.modules:
  # Glob pattern for configuration loading
  path: ${path.config}/modules.d/*.yml

  # Set to true to enable config reloading
  reload.enabled: false

  # Period on which files under path should be checked for changes
  #reload.period: 10s

# ======================= Elasticsearch template setting =======================

setup.template.settings:
  index.number_of_shards: 1
  #index.codec: best_compression
  #_source.enabled: false


# ================================== General ===================================

# The name of the shipper that publishes the network data. It can be used to group
# all the transactions sent by a single shipper in the web interface.
#name:

# The tags of the shipper are included in their own field with each
# transaction published.
#tags: ["service-X", "web-tier"]

# Optional fields that you can specify to add additional information to the
# output.
#fields:
#  env: staging

# ================================= Dashboards =================================
# These settings control loading the sample dashboards to the Kibana index. Loading
# the dashboards is disabled by default and can be enabled either by setting the
# options here or by using the `setup` command.
#setup.dashboards.enabled: false

# The URL from where to download the dashboards archive. By default this URL
# has a value which is computed based on the Beat name and version. For released
# versions, this URL points to the dashboard archive on the artifacts.elastic.co
# website.
#setup.dashboards.url:

# =================================== Kibana ===================================

# Starting with Beats version 6.0.0, the dashboards are loaded via the Kibana API.
# This requires a Kibana endpoint configuration.
setup.kibana:

  # Kibana Host
  # Scheme and port can be left out and will be set to the default (http and 5601)
  # In case you specify and additional path, the scheme is required: http://localhost:5601/path
  # IPv6 addresses should always be defined as: https://[2001:db8::1]:5601
  host: "10.20.30.40:5601"

  # Kibana Space ID
  # ID of the Kibana Space into which the dashboards should be loaded. By default,
  # the Default Space will be used.
  #space.id:

# =============================== Elastic Cloud ================================

# These settings simplify using Filebeat with the Elastic Cloud (https://cloud.elastic.co/).

# The cloud.id setting overwrites the `output.elasticsearch.hosts` and
# `setup.kibana.host` options.
# You can find the `cloud.id` in the Elastic Cloud web UI.
#cloud.id:

# The cloud.auth setting overwrites the `output.elasticsearch.username` and
# `output.elasticsearch.password` settings. The format is `<user>:<pass>`.
#cloud.auth:

# ================================== Outputs ===================================

# Configure what output to use when sending the data collected by the beat.

# ---------------------------- Elasticsearch Output ----------------------------
##output.elasticsearch:
  # Array of hosts to connect to.
##   hosts: ["https://10.20.30.40:9200"]
##   protocol: "https"


  # Protocol - either `http` (default) or `https`.
  #protocol: "https"

  # Authentication credentials - either API key or username/password.
  #api_key: "id:api_key"
##   username: "elastic"
##   password: "mypass"
##   ssl:
##    enabled: true
##    ca_trusted_fingerprint: "23E9D473CE99BB2C6C331F43C94DA35B3096ABA83C844AB5CB0445321458700"
##output.elasticsearch.index: "NB-%{[agent.version]}"
##setup.template.name: "NB"
##setup.template.pattern: "NB-%{[agent.version]}"

# ------------------------------ Logstash Output -------------------------------
#output.logstash:
  # The Logstash hosts
#  hosts: ["1.1.1.1:5072"]

  # Optional SSL. By default is off.
  # List of root certificates for HTTPS server verifications
  #ssl.certificate_authorities: ["/etc/pki/root/ca.pem"]

  # Certificate for SSL client authentication
  #ssl.certificate: "/etc/pki/client/cert.pem"

  # Client Certificate Key
  #ssl.key: "/etc/pki/client/cert.key"

# ================================= Processors =================================
processors:
  - add_host_metadata:
      when.not.contains.tags: forwarded
  - add_cloud_metadata: ~
  - add_docker_metadata: ~
  - add_kubernetes_metadata: ~

# ================================== Logging ===================================

# Sets log level. The default log level is info.
# Available log levels are: error, warning, info, debug
#logging.level: debug

# At debug level, you can selectively enable logging only for some components.
# To enable all selectors use ["*"]. Examples of other selectors are "beat",
# "publisher", "service".
#logging.selectors: ["*"]

# ============================= X-Pack Monitoring ==============================
# Filebeat can export internal metrics to a central Elasticsearch monitoring
# cluster.  This requires xpack monitoring to be enabled in Elasticsearch.  The
# reporting is disabled by default.

# Set to true to enable the monitoring reporter.
#monitoring.enabled: false

# Sets the UUID of the Elasticsearch cluster under which monitoring data for this
# Filebeat instance will appear in the Stack Monitoring UI. If output.elasticsearch
# is enabled, the UUID is derived from the Elasticsearch cluster referenced by output.elasticsearch.
#monitoring.cluster_uuid:

# Uncomment to send the metrics to Elasticsearch. Most settings from the
# Elasticsearch output are accepted here as well.
# Note that the settings should point to your Elasticsearch *monitoring* cluster.
# Any setting that is not set is automatically inherited from the Elasticsearch
# output configuration, so if you have the Elasticsearch output configured such
# that it is pointing to your Elasticsearch monitoring cluster, you can simply
# uncomment the following line.
#monitoring.elasticsearch:

# ============================== Instrumentation ===============================

# Instrumentation support for the filebeat.
#instrumentation:
    # Set to true to enable instrumentation of filebeat.
    #enabled: false

    # Environment in which filebeat is running on (eg: staging, production, etc.)
    #environment: ""

    # APM Server hosts to report instrumentation results to.
    #hosts:
    #  - http://localhost:8200

    # API Key for the APM Server(s).
    # If api_key is set then secret_token will be ignored.
    #api_key:

    # Secret token for the APM Server(s).
    #secret_token:


# ================================= Migration ==================================

# This allows to enable 6.7 migration aliases
#migration.6_to_7.enabled: true

I want it create index with follow example
hostname-yyyy-mm-dd

hello @alex_zolat ,

I see no outputs enabled in the above configuration.

What output you want to use: Elasticsearch or Logstash?

By the way, it's not a good idea split index by hostname if you have a lot of hosts in your environment because it could generates a lot of indexes day each.

best reggards

I want to use elastic as output
What is your idea while I have more than hosts in my environment ?
I think that is easier to find logs while using index with hostname and create new index with that hostname with date for every day.
Would you please help me where and how can configure filebeat to create index automatically ?

This is a very bad idea as it does not scale well at all. Having lot of small shards in Elasticsearch is inefficient and can cause performance and stability problems.

Please have a look at this old blog post for more details. You can also look at the official docs.

Instead make sure you have the hostname in a field in the data and then filter on it. This will make it a lot easier to work with through dashboards as relying on index names and patterns quickly get difficult.

At the firs step .
How can configure filebeat to create index automatically on the elastic . What is pattern ? Where can insert it in filebeat ?

Looks like you do not want to follow the advice given on best practices. As specified in the docs you can create index names based on fields in the data, so make sure hostname is parsed out as a field.

If you have a long retention period and are going to create granular indices, consider using monthly rather than daily indices.

Thanks. for example these are fields for one of my indexes

Does it mean I can use such as follow sample :

index: "%{[host.hostname_keyword]}-%{[agent.version]}-%{+yyyy.MM.dd}" ?

The field need to be available in the JSON being ingested, so likely host.hostname.

Why are you pushing forward with this even though the responses here, as well as the official documentation, recommend against it?

I don't recommend . Just want to know correct syntax to create index name automatically while filebeat service starts . According to sample in doc it mentions

index: "%{[host.hostname_keyword]}-%{[agent.version]}-%{+yyyy.MM.dd}" ?

According to the pic I have a field like host.hostname and type is keyword
so . Does index syntax correct ?

index: "%{[host.hostname_keyword]}-%{[agent.version]}-%{+yyyy.MM.dd}"

No. As per my previous comment the field must exist in the JSON before it is indexed. The keyword subfield is only created in Elasticsearch and is not available in the JSON.

But my log file format is not json . Does it your means that is mandatory to use json log format when we want to create index automatically after start filebeat ?

Elasticsearch only accepts JSON documents so Filebeat will store your log message in a JSON wrapper before indexing it. Filebeat can also parse out fields from your log data, but this may also be done through an ingest pipeline. If you want to reference host name in your Filebeat output config this need to be available as a field. I have not used this so do not know if it is a standard field or not.

Did you try with the field I mentioned as this sems to be part of the JSON payload?

I have confused about some questions:

1 - Does it mandatory to change log file format to JSON if we want after start filebeat service it creates index automatically on elastic ?

This is part of my log

Thu Jun 13 03:17:35 SP 2024|40|1718236053022||DispatchService.execute|RESPONSE IS RECEIVED
Thu Jun 13 03:17:35 SP 2024|40|1718236053022||DispatchService.execute|RESPONSE IS SUCCESSFUL
Thu Jun 13 03:17:35 SP 2024|40|1718236053022||DispatchService.execute|SERVICELOG IS SAVED 20585132
Thu Jun 13 03:17:36 SP 2024|40|1718236053022||DispatchService.execute|SERVICE 20585132 EXCEUTED BY UNIQUE ID:1718236053217-591529
Thu Jun 13 03:17:36 SP 2024|40|1718236053022||DispatchService.execute|SERVICELOGPARAMETERS ARE SAVED
Thu Jun 13 03:18:01 SP 2024|42|1718236081355|10.50.40.30 90.50.60.30 |ServiceController.execute|-CALLED SERVICE-CUSTOMER-TBT-requestStatus-YzYxNTAwZjkzM2JlNTExMzI5N2Q3MGU5YWIxMDBhMThkNmI0NjI2MzVlOTMyZDAxMGI3YzRkNDAyMGE3ODQ4NA==-{"customerRef": "4fe8174c-f458-41e3-9a1a-a6e5d4c2c24f", "requestId": "ded4229e-f2f1-4ff4-8d68-e6ca3f97502c"}
Thu Jun 13 03:18:01 SP 2024 DispatchService.checkKey TBT requestStatus {"customerRef": "4fe8174c-f458-41e3-9a1a-a6e5d4c2c24f", "requestId": "ded4229e-f2f1-4ff4-8d68-e6ca3f97502c"} BEFORE DIGEST
Thu Jun 13 03:18:01 SP 2024 DispatchService.checkKey TBT requestStatus {"customerRef": "4fe8174c-f458-41e3-9a1a-a6e5d4c2c24f", "requestId": "ded4229e-f2f1-4ff4-8d68-e6ca3f97502c"} AFTER DIGEST

2 - I have no interested in make index with name of host I want just use a sample that could find out index name pattern . For example according to that log how can configure filebeat to create index automatically for every day ?