教程-使用协议扩展对 syslog 消息进行负载平衡

NetScaler 设备上可用的 Syslog 协议仅适用于 NetScaler 设备上生成的消息。它不会对来自外部节点的消息进行负载平衡。要对此类消息进行负载平衡,您需要使用协议扩展功能并使用 Lua 5.2 编程语言编写 syslog 消息解析逻辑。

用于解析 syslog 消息的代码

该代码仅定义了 TCP 客户端数据回调函数 - client.on_data()。对于服务器数据,它不添加回调函数,服务器到客户端采用快速的本地路径。该代码根据尾部字符识别消息边界。如果 TCP 数据包包含多个 syslog 消息,那么我们根据尾随字符拆分数据包,并平衡每个消息的负载。

--[[

  Syslog event handler for TCP client data

    ctxt - TCP client side App processing context.

    data - TCP Data stream received.

--]]

function client.on_data(ctxt, payload)

                  local message = nil

                  local data_len

                  local data = payload.data

                  local trailing_character = "\n"

                  ::split_message::

                                    -- Get the offset of trailing character

                                    local new_line_character_offset = data:find(trailing_character)

                                    -- If trailing character is not found, then wait for more data.

                                    if (not new_line_character_offset) then

                                                      goto need_more_data

                                    end

                                    -- Get the length of the current message

                                    data_len = data:len()

                                    -- Check whether we have more than one message

                                    -- by comparing trailing character offset and

                                    -- current data length

                                    if (data_len > new_line_character_offset) then

                                                      -- If we have more than one message, then split

                                                      -- the data into two parts such that first part

                                                      -- will contain message upto trailing character

                                                      -- offset and second part will contain

                                                      -- remaining message.

                                                      message, data = data:split(new_line_character_offset)

                                    else

                                                      message = data

                                                      data = nil

                                    end

-- Send the data to the backend server.

                                    ns.send(ctxt.output, "EOM", {data = message})

                                    goto done

                                    ::need_more_data::

                                                      -- Wait for more data

                                                      ctxt:hold(data)

                                                      data = nil

                                                      goto done

                                    ::done::

                                                      -- If we have more data to parse,

                                                      -- then do parsing again.

                                                      if (data) then

                                                                        goto split_message

                                                      end

end
<!--NeedCopy-->
教程-使用协议扩展对 syslog 消息进行负载平衡