在清洗日志时,有一个应用场景,就是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输出链路的稳定性)的更多相关文章

  1. SQLServerException:通过端口 1433 连接到主机 localhost 的 TCP/IP 连接失败。

    一.问题描述: 1.连接数据库时抛出的异常: com.microsoft.sqlserver.jdbc.SQLServerException: 通过端口 1433 连接到主机 localhost 的 ...

  2. oracle ORA-12545:因目标主机或对象不存在

    解决方法: 1.首先从最基本的入手,这里打开计算机右击,选择管理 2. 找到里面的服务和应用程序,打开服务 3.找到: OracleOraDb11g_home1TNSListener OracleSe ...

  3. 解决 Jumpserver coco 使用登录用户(ldap)进行SSH连接目标主机,忽略系统用户

    前言 Jumpserver 作为国内流行的开源堡垒机,很多公司都在尝试使用,同时 Jumpserver 为了契合众多公司的用户认证,也提供了 LDAP 的用户认证方式,作为 Jumpserver 的用 ...

  4. ORA-12545: 因目标主机或对象不存在, 连接失败

    ORA-12545: 因目标主机或对象不存在, 连接失败 1. 问题描述 XP系统下同时安装了AX1应用程序和升级版AX2,连接同一个在本机Oracle客户端上配置的连接实例,其中AX2显示链接成功, ...

  5. 【转】Java JDBC连接SQL Server2005错误:通过端口 1433 连接到主机 localhost 的 TCP/IP 连接失败

    错误原因如下: Exception in thread "main" org.hibernate.exception.JDBCConnectionException: Cannot ...

  6. Java JDBC连接SQL Server2005错误:通过port 1433 连接到主机 localhost 的 TCP/IP 连接失败

    错误原因例如以下: Exception in thread "main" org.hibernate.exception.JDBCConnectionException: Cann ...

  7. Java JDBC连接SQL Server2005错误:通过端口 1433 连接到主机 localhost 的 TCP/IP 连接失败 及sql2008外围服务器

    转载:Java JDBC连接SQL Server2005错误:通过端口 1433 连接到主机 localhost 的 TCP/IP 连接失败 错误原因如下: Exception in thread & ...

  8. Python_跟随目标主机IP变换

    ''' 为了防止黑客攻击或者负载均衡,会经常变换主机,这样同一个域名在不同时间可能会对应不同的IP地址,在这种情况下可以通过 socket模块的gethostbyname()函数来实时获取目标主机的I ...

  9. UNIX网络编程读书笔记:TCP输出、UDP输出和SCTP输出

    TCP输出 下图展示了应用进程写数据到TCP套接口的过程. 每一个TCP套接口有一个发送缓冲区,我们可以用SO_SNDBUF套接口选项来改变这个缓冲区的大小. 当应用进程调用write时,内核从应用进 ...

随机推荐

  1. bzoj5277: [Usaco2018 Open]Out of Sorts

    被tkj大爷艹爆了5555整套模拟赛都是神仙思路题 那么这题题解 还有一个神仙做法,zory巨神在考场上找规律AC,自己都不会证..我证明了一下(然而这货还是不认可自己的做法) 按照分割点的思路,我们 ...

  2. 杂项:DS(目录服务)

    ylbtech-杂项:DS(目录服务) 1.返回顶部 1. DS(目录服务). 目录服务管理概述: 目录服务是扩展计算机系统中最重要的组件之一.虽然用户和管理通常不知道他们感兴趣对象的确切名称,但他们 ...

  3. [JavaEE] Spring事务配置的五种方式

    前段时间对Spring的事务配置做了比较深入的研究,在此之间对Spring的事务配置虽说也配置过,但是一直没有一个清楚的认识.通过这次的学习发觉Spring的事务配置只要把思路理清,还是比较好掌握的. ...

  4. 【BZOJ1196】【HNOI2006】公路修建问题

    [描述] OI island是一个非常漂亮的岛屿,自开发以来,到这儿来旅游的人很多.然而,由于该岛屿刚刚开发不久,所以那里的交通情况还是很糟糕.所以,OIER Association组织成立了,旨在建 ...

  5. C - Elephant(贪心)

    Problem description An elephant decided to visit his friend. It turned out that the elephant's house ...

  6. Spring《八-一》CGLIB代理和自动代理

    CGLIB代理 配置文档 <bean id="logProxy" class="org.springframework.aop.framework.ProxyFac ...

  7. Ubuntu16.04系统下汉字显示为方框解决办法(图文详解)

    不多说,直接上干货! 问题详情 问题分析 今天一打开Ubuntu系统,竟然不支持中文显示了.开始时以为是搜狗拼音的问题,然后重装了一遍fcitx,然后用重装了一遍搜狗输入法.可是重装后问题根本没有解决 ...

  8. week2 notebook2

    Beginning day2: 1.基础数据类型宏观: 1.1.整型:int:1,2,3 1.2.字符串:str:‘anthony’ 1.2.1: 索引:索引即下标,就是字符串组成的元素从第一个开始, ...

  9. AngularJs轻松入门

    AngularJs轻松入门系列博文:http://blog.csdn.net/column/details/angular.html AngularJs轻松入门(一)创建第一个应用 AngularJs ...

  10. Spring学习笔记之基础、IOC、DI(1)

    0.0 Spring基本特性 Spring是一个开源框架:是基于Core来架构多层JavaEE系统 1.0 IOC 控制反转:把对象的创建过程交给spring容器来做. 1.1 application ...