logstash 入門

1. 安裝

https://www.elastic.co/cn/downloads/logstash
當(dāng)前版本6.5.4斟或,下載.tar.gz

2. 測試是否可用(需要基礎(chǔ)環(huán)境jdk1.7+)

bin/logstash -e 'input { stdin {} } output { stdout {} }'
# 輸入 hello艳悔,看到返回包含hello的json

如果遇到Unrecognized VM option 'UseParNewGC'錯誤,且可用通過java --version查看到j(luò)dk信息讥电,
則需要修改config/jvm.options

#Remove or comment line: 
-XX:+UseParNewGC
#Remove or comment line: 
-XX:+UseConcMarkSweepGC
#Add this line instead: 
-XX:+UseG1GC

查看已安裝插件

./logstash-plugin list

3. 解析mongodb中的數(shù)據(jù)

安裝mongodb的讀取插件

bin/logstash-plugin install logstash-input-mongodb
#Validating logstash-input-mongodb
#Installing logstash-input-mongodb
#Installation successful

創(chuàng)建配置文件my.config:

input {
    mongodb {
    uri => 'mongodb://172.16.1.10:27017/test'
    placeholder_db_dir => '/opt/mongodb/'
    placeholder_db_name =>'BIZ_CATEGORY.db'
    collection => 'BIZ_CATEGORY'
    }
}
filter
{
# 把mongodb的_id替換掉蹂窖,因?yàn)開id是跟es中的_id相沖突
    mutate { 
        rename => ["_id", "uid"]
    }
} 

output {

    file {
        path => "/opt/mongons.log"
    }

    stdout {
       codec => json_lines
    }

#    elasticsearch {
#        hosts => ["192.168.1.171:9200"]
#        index => "testData"
#        manage_template=>true
#        document_type => "judicial"
#    }
}

執(zhí)行命令:

../bin/logstash -f ./my.config

可獲取mongons.log文件和控制臺輸出(處理后logstach會進(jìn)入持續(xù)監(jiān)視)
mongons.log內(nèi)容:

  {
    "@timestamp": "2019-01-29T03:19:20.765Z",
    "@version": "1",
    "host": "fireflyMacBook-Pro.local",
    "label": "xxxxx",
    "log_entry": "{\"_id\"=>BSON::ObjectId('5c2c800a868cde4a10000c61'), \"value\"=>\"NationalProjects\", \"label\"=>\"xxxxx\"}",
    "logdate": "2019-01-02T09:10:34+00:00",
    "mongo_id": "5c2c800a868cde4a10000c61",
    "uid": "5c2c800a868cde4a10000c61",
    "value": "NationalProjects"
  }
  {
    "@timestamp": "2019-01-29T03:19:20.682Z",
    "@version": "1",
    "host": "fireflyMacBook-Pro.local",
    "label": "yyyyyy",
    "log_entry": "{\"_id\"=>BSON::ObjectId('5c2c7e6c868cde4a10000c55'), \"value\"=>\"985School\", \"label\"=>\"yyyyyy\"}",
    "logdate": "2019-01-02T09:03:40+00:00",
    "mongo_id": "5c2c7e6c868cde4a10000c55",
    "uid": "5c2c7e6c868cde4a10000c55",
    "value": "985School"
  }

@timestamp、@version恩敌、host瞬测、log_entry、logdate纠炮、mongo_id為附加信息

API解釋:

Name                 Type          Description
uri                  [String]      A MongoDB URI for your database or cluster (check the MongoDB documentation for further info on this) [No Default, Required]
placeholder_db_dir   [String]      Path where the place holder database will be stored locally to disk [No Default, Required]
  This gets created by the plugin so the directory needs to be writeable by the user that logstash is running as
placeholder_db_name  [String]      Name of the database file that will be created [Default: logstash_sqlite.db]
collection           [String]      A regex that will be used to find desired collecitons. [No Default, Required]
generateId           [Boolean]     If true, this will add a field '_id' that contains the MongoDB Document id
batch_size           [Int]         Size of the batch of mongo documents to pull at a time [Default: 30]
parse_method         [String]      Built in parsing of the mongodb document object [Default: 'flatten']
dig_fields           [Array]       An array of fields that should employ the dig method
dig_dig_fields       [Array]       This provides a second level of hash flattening after the initial dig has been done

4.輸出到mongodb

安裝mongodb的寫入插件

bin/logstash-plugin install logstash-output-mongodb

API解釋:

Name                 Type          Description
bulk                 [boolean]     Bulk insert flag, set to true to allow bulk insertion, else it will insert events one by one.[Default: false]
bulk_interval        [number]      Bulk interval, Used to insert events periodically if the "bulk" flag is activated.[Default: 2]
bulk_size            [number]      Bulk events number, if the number of events to insert into a collection raise that limit, it will be bulk inserted whatever the bulk interval value (mongodb hard limit is 1000).[Default: 900]
collection           [String]      The collection to use. This value can use %{foo} values to dynamically select a collection based on data in the event. [no default, Required]
database             [String]      The database to use. [Default: 2,Required]
generateId           [boolean]     If true, an "_id" field will be added to the document before insertion. The "_id" field will use the timestamp of the event and overwrite an existing "_id" field in the event.[Default: false]
isodate              [boolean]     If true, store the @timestamp field in MongoDB as an ISODate type instead of an ISO8601 string. For more information about this,see (http://www.mongodb.org/display/DOCS/Dates).[Default: false]
retry_delay          [number]      The number of seconds to wait after failure before retrying.[Default: 3]
uri                  [String]      A MongoDB URI to connect to. [no default,Required]

5.java插件

https://github.com/logstash-plugins/logstash-filter-java_filter_example

grok常量
https://github.com/logstash-plugins/logstash-patterns-core/blob/master/patterns/grok-patterns

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末月趟,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子恢口,更是在濱河造成了極大的恐慌狮斗,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,490評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件弧蝇,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)看疗,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,581評論 3 395
  • 文/潘曉璐 我一進(jìn)店門沙峻,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人两芳,你說我怎么就攤上這事摔寨。” “怎么了怖辆?”我有些...
    開封第一講書人閱讀 165,830評論 0 356
  • 文/不壞的土叔 我叫張陵是复,是天一觀的道長。 經(jīng)常有香客問我竖螃,道長淑廊,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,957評論 1 295
  • 正文 為了忘掉前任特咆,我火速辦了婚禮季惩,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘腻格。我一直安慰自己画拾,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,974評論 6 393
  • 文/花漫 我一把揭開白布菜职。 她就那樣靜靜地躺著青抛,像睡著了一般。 火紅的嫁衣襯著肌膚如雪酬核。 梳的紋絲不亂的頭發(fā)上蜜另,一...
    開封第一講書人閱讀 51,754評論 1 307
  • 那天,我揣著相機(jī)與錄音愁茁,去河邊找鬼蚕钦。 笑死,一個胖子當(dāng)著我的面吹牛鹅很,可吹牛的內(nèi)容都是我干的嘶居。 我是一名探鬼主播,決...
    沈念sama閱讀 40,464評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼促煮,長吁一口氣:“原來是場噩夢啊……” “哼邮屁!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起菠齿,我...
    開封第一講書人閱讀 39,357評論 0 276
  • 序言:老撾萬榮一對情侶失蹤佑吝,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后绳匀,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體芋忿,經(jīng)...
    沈念sama閱讀 45,847評論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡炸客,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,995評論 3 338
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了戈钢。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片痹仙。...
    茶點(diǎn)故事閱讀 40,137評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖殉了,靈堂內(nèi)的尸體忽然破棺而出开仰,到底是詐尸還是另有隱情,我是刑警寧澤薪铜,帶...
    沈念sama閱讀 35,819評論 5 346
  • 正文 年R本政府宣布众弓,位于F島的核電站,受9級特大地震影響隔箍,放射性物質(zhì)發(fā)生泄漏谓娃。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,482評論 3 331
  • 文/蒙蒙 一鞍恢、第九天 我趴在偏房一處隱蔽的房頂上張望傻粘。 院中可真熱鬧,春花似錦帮掉、人聲如沸弦悉。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,023評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽稽莉。三九已至,卻和暖如春涩搓,著一層夾襖步出監(jiān)牢的瞬間污秆,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,149評論 1 272
  • 我被黑心中介騙來泰國打工昧甘, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留良拼,地道東北人。 一個月前我還...
    沈念sama閱讀 48,409評論 3 373
  • 正文 我出身青樓充边,卻偏偏與公主長得像庸推,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子浇冰,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,086評論 2 355

推薦閱讀更多精彩內(nèi)容