logstash tcp multihost output(多目标主机输出,保证TCP输出链路的稳定性)
在清洗日志时,有一个应用场景,就是TCP输出时,须要在一个主机挂了的情况下,自已切换到下一个可用入口。而原tcp output仅支持单个目标主机设定。故本人在原tcp的基础上,开发出tcp_multihost输出插件,来满足此场景。
插件在一開始的时候会随机选择一个链路,而在链路出错连续超过3(默认)次后会尝试数组中下一个主机
github: http://github.com/xiaohelong2005
Logstash版本号:1.4.2
文件位置:
# encoding: utf-8
require "logstash/outputs/base"
require "logstash/namespace"
require "thread"
#@auhor:xiaohelong
#@date:2014-10-24
#@email:xiaohelong2005@gmail.com
# Write events over a TCP socket.
#Auto select the host from iplist to tolerate the link
# Each event json is separated by a newline.
#
# Can connect to a server,
class LogStash::Outputs::Tcp_Multihost < LogStash::Outputs::Base
config_name "tcp_multihost"
milestone 0
default :codec, "json"
# the address to connect to.
#hosts config example
config :hosts, :validate => :array, :required => true,:default=>{}
config :reconnect_times, :validate => :number,:default=>3
# When connect failed,retry interval in sec.
config :reconnect_interval, :validate => :number, :default => 10
#last available host
@hosts_size=0
#last available host ip
@host="0.0.0.0"
#last available port
@port=9200
#retry action count,if retry_count<=0,we need to update the host and port , also include retry_count itself
@retry_count=0
#get the desgined index data
@initloc=0 public
def register
require "stud/try"
#here we use the recorded host,if recorded host is not available, we need update it
@retry_count=@reconnect_times
@hosts_size=@hosts.length
@logger.info("length:#@hosts_size; hosts:#@hosts")
@initloc=Random.rand(@hosts_size)#generate 0-(hosts_size-1) int
@logger.info("initloc:#@initloc")
icount=0;
@hosts.each do |hosthash|
@logger.info("hosthash info",hosthash)
end#do
@host=@hosts[@initloc].keys[0]
@port=@hosts[@initloc][@host] client_socket = nil
@codec.on_event do |payload|
begin
@retry_count=@reconnect_times#here we need to init retry mark
client_socket = connect unless client_socket
r,w,e = IO.select([client_socket], [client_socket], [client_socket], nil)
# don't expect any reads, but a readable socket might
# mean the remote end closed, so read it and throw it away.
# we'll get an EOFError if it happens.
client_socket.sysread(16384) if r.any?
# Now send the payload
client_socket.syswrite(payload) if w.any?
@logger.info("tcp output info:", :host => @host, :port => @port,
:exception => e, :backtrace => e.backtrace)
rescue => e
@logger.warn("tcp output exception", :host => @host, :port => @port,
:exception => e, :backtrace => e.backtrace)
client_socket.close rescue nil
client_socket = nil
@retry_count-=1
@logger.info("retry_count:#@retry_count")
if @retry_count<=0
@initloc+=1
@initloc=@initloc%@hosts_size #update init location
@host=@hosts[@initloc].keys[0]
@port=@hosts[@initloc][@host]
@retry_count=@reconnect_times #update retry_count
@logger.info("retry_count <=0,initloc:#@initloc,retry_count=#@retry_count:", :host => @host, :port => @port, :exception => e, :backtrace => e.backtrace)
end
sleep @reconnect_interval
retry
end
end
end # def register private
def connect
Stud::try do
return TCPSocket.new(@host,@port)
end
end # def connect
public
def receive(event)
return unless output? (event)
@codec.encode(event)
end # def receive
end # class LogStash::Outputs::Tcp_multihost
配置说明(我放在LOGSTASH_HOME/config):
output{
tcp_multihost{
hosts=>[
{"127.0.0.1"=>"9202"},
{"localhost"=>"9201"},
{"127.0.0.1"=>"9203"},
{"127.0.0.1"=>"9204"}
] #主机列表
workers =>16 #线程,默认1
reconnect_times=>3 # 默认3次, 尝试多少次数后切换
reconnect_interval=>3 #默认10秒,失败重连间隔
}
}
调用运行:
"LOGSTASH_HOME/bin/logstash" agent --debug -f "LOGSTASH_HOME/config/shipper.config" --pluginpath "LOGSTASH_HOME"
NC接收端能够尝试:
nc -lkv 9201 之类的
logstash tcp multihost output(多目标主机输出,保证TCP输出链路的稳定性)的更多相关文章
- SQLServerException:通过端口 1433 连接到主机 localhost 的 TCP/IP 连接失败。
一.问题描述: 1.连接数据库时抛出的异常: com.microsoft.sqlserver.jdbc.SQLServerException: 通过端口 1433 连接到主机 localhost 的 ...
- oracle ORA-12545:因目标主机或对象不存在
解决方法: 1.首先从最基本的入手,这里打开计算机右击,选择管理 2. 找到里面的服务和应用程序,打开服务 3.找到: OracleOraDb11g_home1TNSListener OracleSe ...
- 解决 Jumpserver coco 使用登录用户(ldap)进行SSH连接目标主机,忽略系统用户
前言 Jumpserver 作为国内流行的开源堡垒机,很多公司都在尝试使用,同时 Jumpserver 为了契合众多公司的用户认证,也提供了 LDAP 的用户认证方式,作为 Jumpserver 的用 ...
- ORA-12545: 因目标主机或对象不存在, 连接失败
ORA-12545: 因目标主机或对象不存在, 连接失败 1. 问题描述 XP系统下同时安装了AX1应用程序和升级版AX2,连接同一个在本机Oracle客户端上配置的连接实例,其中AX2显示链接成功, ...
- 【转】Java JDBC连接SQL Server2005错误:通过端口 1433 连接到主机 localhost 的 TCP/IP 连接失败
错误原因如下: Exception in thread "main" org.hibernate.exception.JDBCConnectionException: Cannot ...
- Java JDBC连接SQL Server2005错误:通过port 1433 连接到主机 localhost 的 TCP/IP 连接失败
错误原因例如以下: Exception in thread "main" org.hibernate.exception.JDBCConnectionException: Cann ...
- Java JDBC连接SQL Server2005错误:通过端口 1433 连接到主机 localhost 的 TCP/IP 连接失败 及sql2008外围服务器
转载:Java JDBC连接SQL Server2005错误:通过端口 1433 连接到主机 localhost 的 TCP/IP 连接失败 错误原因如下: Exception in thread & ...
- Python_跟随目标主机IP变换
''' 为了防止黑客攻击或者负载均衡,会经常变换主机,这样同一个域名在不同时间可能会对应不同的IP地址,在这种情况下可以通过 socket模块的gethostbyname()函数来实时获取目标主机的I ...
- UNIX网络编程读书笔记:TCP输出、UDP输出和SCTP输出
TCP输出 下图展示了应用进程写数据到TCP套接口的过程. 每一个TCP套接口有一个发送缓冲区,我们可以用SO_SNDBUF套接口选项来改变这个缓冲区的大小. 当应用进程调用write时,内核从应用进 ...
随机推荐
- hdu 1002(大数)
A + B Problem II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- 【SDOI 2010】 计算器
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2242 [算法] 第一问用快速幂解决 第二问用exgcd解决 第三问用BSGS算法解决 ...
- openssl https证书
今天摸索了下 HTTPS 的证书生成,以及它在 Nginx 上的部署.由于博客托管在 github 上,没办法部署证书,先记录下,后续有需要方便快捷操作.本文的阐述不一定完善,但是可以让一个初学者了解 ...
- SNMP简单概述
一.SNMP简单概述 1.1.什么是Snmp SNMP是英文"Simple Network Management Protocol"的缩写,中文意思是"简单网络管理协议& ...
- E - Dividing Orange
Problem description One day Ms Swan bought an orange in a shop. The orange consisted of n·k segments ...
- SQLServer int转float
例: select 2/4 会得到0 改为 select 2/4.0 则会得到0.500000 也同时达到了int转float的效果
- redis的基本命令
一.String类型的键值对 给一个变量赋值 set varName varVal eg 得到一个变量的值 get varName eg 删除一个变量 del varName eg del nume ...
- SourceInsight使用入门与技巧(转)
1 sourceinsight screen font 的默认字体是Verdana的,它是一直变宽字体.在Document style中可以将字体改为定宽的Courier 2 document o ...
- 创建一个dynamics CRM workflow (二) - Build in Workflows
这里我们不着重讲解build in workflow. 但是, 如果要上手custom workflow, 我们必须要了解 build in workflow. build-in workflow 在 ...
- python tips: for循环的小问题
在python中,用for对列表进行遍历的时候,迭代器中维护的是列表的索引而不是列表的元素.也就是说,for循环是对索引进行迭代,如果在for循环过程中修改了列表,迭代出来的值是新列表的索引位置,如果 ...