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时,内核从应用进 ...
随机推荐
- 自定义django的Template context processors
简要步骤: 1.编辑一个函数: def media_url(request): from django.conf import settings return {'media_url': settin ...
- Java设计模式-策略模式详解
前言 在软件领域中,设计模式作为一种经典的开发实践常常需要我们去深入的理解,而策略模式作为设计模式的一种,使用频率也是相对来说比较高的,在Java中,当我们学习TreeSet集合的时候,就采用了经典的 ...
- POJ 2513 trie树+并查集判断无向图的欧拉路
生无可恋 查RE查了一个多小时.. 原因是我N define的是250500 应该是500500!!!!!!!!! 身败名裂,已无颜面对众人.. 吐槽完了 我们来说思路... 思路: 判有向图能否形成 ...
- 2015 多校赛 第一场 1002 (hdu 5289)
Description Tom owns a company and he is the boss. There are n staffs which are numbered from 1 to n ...
- Hadoop MapReduce编程 API入门系列之网页排序(二十八)
不多说,直接上代码. Map output bytes=247 Map output materialized bytes=275 Input split bytes=139 Combine inpu ...
- WinForm导出DataSet到Excel
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- css relative设置top为百分比值
前言: 最近在学习HTML.CSS的过程中,想模仿一下百度首页.发现搜索框这一部分与上下其它元素的空白距离可以随着窗口大小变化(效果如下图所示),于是自己研究了一下并记录下来. 效果实现 <!D ...
- css 中font属性知识点总结
一. font属性值可以继承.例如子元素可以继承父元素的行高,字体大小等等. 二.font属性可以进行连写:font: font-sytle font-weight font-size/line- ...
- hdu2883 最大流,判断满流 优化的SAP算法
这是09年的多校联赛题目,比10年的难度要大.如果没做过hdu3572,建议先去做.有了解题思维再来做这题. 这题与hdu3572类似.但是1 <= si < ei <= 1,000 ...
- Http状态码大全(200、404、500等)
基本涵盖了所有问题 HTTP 400 – 请求无效HTTP 401.1 – 未授权:登录失败HTTP 401.2 – 未授权:服务器配置问题导致登录失败HTTP 401.3 – ACL 禁止访问资源H ...