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

Citrix ADC 设备上可用的系统日志协议仅适用于 Citrix ADC 设备上生成的消息。它不会对来自外部节点的消息进行负载平衡。为了平衡这些消息,您需要使用协议扩展功能,并使用 Lua 5.2 编程语言编写 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

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