Marlb0ro
Marlb0ro

Reputation: 15

logstash index a text file

I'd like to import a text file in Elasticsearch. The text file contains 3 values per line. After spending several hours of struggling, I didn't get it done. Help is greatly appreciated.

Elasticsearch 5.4.0 with Logstash installed.

Sample data:

username email hash
username email hash
username email hash
username email hash
username email hash

also built a python script but its too slow:

import requests
import json
from elasticsearch import Elasticsearch
es = Elasticsearch([{'host': 'localhost', 'port': 9200}])

i = 1
with open("my2") as fileobject:
    for line in fileobject:
        username, email, hash = line.strip('\n').split(' ')
        body = {"username": username, "email": email, "password": hash}
        es.index(index='dbs', doc_type='db1', id=i, body=body)
        i += 1

edit: Thanks its work but i guess my filter is bad because i want it to look like this:

    {
  "_index": "logstash-2017.06.01",
  "_type": "db",
  "_id": "AVxinqK5XRvft8kN7Q6M",
  "_version": 1,
  "_score": null,
  "_source": {
    "username": "Marlb0ro",
    "email": "[email protected]",
    "hash": "123456",
}

and it put the data like this:

    {
  "_index": "logstash-2017.06.01",
  "_type": "logs",
  "_id": "AVxinqK5XRvft8kN7Q6M",
  "_version": 1,
  "_score": null,
  "_source": {
    "path": "C:/Users/user/Desktop/user/log.txt",
    "@timestamp": "2017-06-01T07:46:22.488Z",
    "@version": "1",
    "host": "DESKTOP-FNGSJ6C",
    "message": "username email password",
    "tags": [
      "_grokparsefailure"
    ]
  },
  "fields": {
    "@timestamp": [
      1496303182488
    ]
  },
  "sort": [
    1496303182488
  ]
}

Upvotes: 0

Views: 2874

Answers (1)

Val
Val

Reputation: 217274

Simply put this in a file called grok.conf:

input {
        file {
                path => "/path/to/your/file.log"
                start_position => beginning
                sincedb_path => "/dev/null"
        }
}
filter {
        grok {
                match => {"message" => "%{WORD:username} %{WORD:email} %{WORD:hash}" }
        }
}
output {
        elasticsearch {
                hosts => ["localhost:9200"]
        }
}

Then run Logstash with bin/logstash -f grok.conf and you should be ok.

Upvotes: 2

Related Questions