顯示具有 graylog 4 標籤的文章。 顯示所有文章
顯示具有 graylog 4 標籤的文章。 顯示所有文章

2021/10/24

最近有個朋友問到如何用python走rest api 撈graylog的資料

因為一直以來都是用curl

沒用python

今天朋友說他試出來了 感謝他的分享

做個記錄


程式碼如下


import requests


user = 'admin'

pw = 'pwd'

send_format_date_from = '2021-10-21T16:00:00.000Z'

send_format_date_to = '2021-10-21T17:00:00.000Z'

str = 'search key word'

url='http://graylog_ip:9000/api/views/search/messages'

header = {'Accept':'text/csv,application/json', 'Content-Type':'application/json', 'X-Requested-By':'cli'}


#以下是使用絶對時間的語法 上方已定義區間

graylog_send_data={ "streams":["000000000000000000000001"], "timerange":[ "absolute",{ "from":send_format_date_from, "to":send_format_date_to } ], "query_string":{ "type":"elasticsearch", "query_string":str } }


#以下是使用相對時間的語法 range 是以秒為單位

graylog_send_data={ "streams":["000000000000000000000001"], "timerange":{ "type":"relative","range":60 }, "query_string":{ "type":"elasticsearch", "query_string":str } }


r = requests.post(url, auth=(user, pw), headers=header, json=graylog_send_data)


print(r.text)


相對時間或絶對時間擇一使用

2020/12/14

之前升級graylog rest 碰到的問題

http://adminkk.blogspot.com/2020/11/graylog-4-ova-ubuntu-18.html

官方文件上說明此種方法會停止支援

必須使用新方法

但官方文件上並沒有很詳細的說明

去forum上問了

感謝回答

語法如下

直接匯出txt

絕對時間的語法

curl -u admin:passwd -H 'Accept: text/csv' -H "Content-Type:application/json" -H "Accept:application/json" -H 'X-Requested-By: cli' -d '{"streams":["000000000000000000000001"],"timerange":["absolute",{"from":"2020-12-11T00:00:00.000Z","to":"2020-12-11T01:00:00.000Z"}],"query_string":{"type":"elasticsearch","query_string":"keyword" }}' "http://10.0.0.1:9000/api/views/search/messages"


相對時間的語法

curl -u admin:passwd -H 'Accept: text/csv' -H "Content-Type:application/json" -H "Accept:application/json" -H 'X-Requested-By: cli' -d '{"streams":["000000000000000000000001"],"timerange":{"type": "relative","range": 300},"query_string":{"type":"elasticsearch","query_string":"keyword" }}' "http://10.0.0.1:9000/api/views/search/messages"


https://community.graylog.org/t/how-to-search-messages-using-rest-api/17943

2020/11/24

今天升graylog 4

稍微記一下

如果還有其碰到其他問題

之後再補上

官方提供的ova是 ubuntu 18.04

這個很怪 要用就只能用二年多到 2023

為什麼不直接用2004

所以決定不用ova 直接在centos 8裝

照官方文件 安裝沒什麼問題

https://docs.graylog.org/en/4.0/pages/installation/os/centos.html#centosguide


裝完後要調整 graylog  和 elasticsearch的 記憶体參數


/etc/elasticsearch/jvm.options

-Xms4g

-Xmx4g


 /etc/sysconfig/graylog-server

GRAYLOG_SERVER_JAVA_OPTS="-Xms4g -Xmx4g -XX:NewRatio=1 -server -XX:+ResizeTLAB -XX:+UseConcMarkSweepGC -XX:+CMSConcurrentMTEnabled -XX:+CMSClassUnloadingEnabled -XX:-OmitStackTraceInFastThrow"

我用16G的ram
es和graylog各占4G


再來要讓其他sever可以從這台撈資料

/etc/elasticsearch/elasticsearch.yml

在之前的版本只要加上以下這行就可以

network.host: 0.0.0.0


但這個版本加了之後 ES一直起不來
看了一下log


[1]: the default discovery settings are unsuitable for production use; at least one of [discovery.seed_hosts, discovery.seed_providers, cluster.initial_master_nodes] must be configured

還要再加上以下這行

discovery.seed_hosts: ["127.0.0.1"] 

加完ES啟動就沒問題了

graylog預設只會listen在localhost 9000
要加上

http_bind_address = 0.0.0.0:9000

再來就是透過rest撈資料的問題了

legacy 的search 只剩json輸出的沒問題 另外二個都撈不出資料

這個不知道是bug還是什麼
反正目前就先撈出來再用jq來處理了