在清洗日志时,有一个应用场景,就是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. bzoj4887: [Tjoi2017]可乐

    一眼暴力宽搜(最近比赛想暴力想疯了... 很明显的矩乘,然后自爆可以看作走向向一个无出边的点 然后没啥难的了吧. #include<cstdio> #include<iostream ...

  2. Java-Maven:POM百科

    ylbtech-Java-Maven:POM百科 Maven是以项目为中心的设计.POM(project object model)是Maven对一个单一项目的描述.没有POM的话,Maven是毫无用 ...

  3. 关于hexo博客自定义域名后gitment评论系统登陆出现redirect error返回主页的解决办法

    title: 关于hexo博客自定义域名后gitment评论系统登陆出现redirect error返回主页的解决办法 toc: false date: 2018-04-16 22:57:50 cat ...

  4. js与jquery基础知识对比(一)---2017-05-06

    用表格做的,想要对比的内容一目了然,红色部分为重点   js jquery 取元素 id: document.getElementById("aa"); 取到的是dom对象 cla ...

  5. 2015 多校赛 第一场 1001 (hdu 5288)

    Description OO has got a array A of size n ,defined a function f(l,r) represent the number of i (l&l ...

  6. C# 常用代码片段

    一.从控制台读取东西代码片断: using System; class TestReadConsole { public static void Main() { Console.Write(Ente ...

  7. guice基本学习,guice的学习资料(十)

    这个是我前面几篇的参考. guice的学习资料下载:http://pan.baidu.com/s/1bDEPem 路途遥远,但是人确在走.不忘初心,方得始终.

  8. mybatis学习笔记之基础复习(3)

    mybatis学习笔记之基础复习(3) mybatis是什么? mybatis是一个持久层框架,mybatis是一个不完全的ORM框架.sql语句需要程序员自己编写, 但是mybatis也是有映射(输 ...

  9. 使用eclipse,对spring boot 进行springloader或者devtool热部署失败处理方法

    确定配置进行依赖和配置没有错误后. 调整spring boot 的版本,因为新版本对老版本的spring boot 不能使用. 改为: <groupId>org.springframewo ...

  10. Singleton pattern的线程安全问题

    original post from here方法一:同步机制关键词public class Singleton { 2 //利用静态变量来记录Singleton的唯一实例 3 private sta ...