糖果实验室杂货铺

Candy Lab

用RESTY-HTTP实现Graylog的Widget更新与查询

6 months ago 0

作者:糖果

MoonScript for GrayLog是之前写的一个基于Lapis与Simple HTTP的Graylog日志查询SDK, 支持Stream查询,最近为了做自动化分析,加入了新的接口中调用功能,加入了对Dashboard widgets和更新与查询,通过这个程序,实现一些反扫逻辑。

None
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
@putRequest:(req_url, data) =>
        http = require "resty.http"
        httpc = http.new()
        metadata = {
          method:"PUT",
          body: data,
          headers: self.headers_info
        }

        res, err = httpc\request_uri(req_url, metadata)

        if not res
          ngx.say("failed to request: ", err)
          return
        return res.body


    @updateWidget: (dashboardId, widgetId,jsonBody) =>
        errList = {}
        if type(dashboardId) == 'nil'
            table.insert(errList, "dashboard id is nil\n")

        if type(widgetId) == 'nil'
            table.insert(errList, "widget id is nil\n")

        if type(jsonBody) == 'nil'
            table.insert(errList, "json body is nil\n")

        num = table.getn(errList) 
        if num > 0 
            return errList


        url = "http://"..self.host..":"..self.port
        req_url = url..'/dashboards/'..dashboardId..'/widgets/'..widgetId

        self.headers_info = {
            'Authorization': self.auth, 
            'Accept': '*/*',
            'Content-Type':'application/json'
        }

        self\putRequest req_url, jsonBody
        return 1


    @getRequest:(req_url) =>
        http = require "resty.http"
        httpc = http.new()
        metadata = {
          method:"GET",
          headers: self.headers_info
        }

        res, err = httpc\request_uri(req_url, metadata)

        if not res
          ngx.say("failed to request: ", err)
          return

        ngx.status = res.status
        return res.body


    @getWidgetValue: (dashboardId, widgetId) =>
        errList = {}
        if type(dashboardId) == 'nil'
            table.insert(errList, "dashboard id is nil\n")

        if type(widgetId) == 'nil'
            table.insert(errList, "widget id is nil\n")

        num = table.getn(errList) 
        if num > 0 
            return errList

        url = "http://"..self.host..":"..self.port
        req_url = url..'/dashboards/'..dashboardId..'/widgets/'..widgetId..'/value'

        self.headers_info = {
            'Authorization': self.auth, 
            'Accept': 'application/json',
        }

        ret = self\getRequest req_url
        return ret

这次没有使用过去端末加JSON数据请求的方式,把simple http换成了RESTY-HTTP,项目名改 了,叫“Finder”。

本文请不要用于商业目地,非商业转载请署名原作者与原文链接。 https://www.moonscript.cn/openresty/resty-http-for-graylog/


糖果实验室

Openresty中文编程网
IKBC经典机械键盘
机械键盘领券优惠购买

Write a Comment