Logstash参数校验完整实现代码(详细全过程) - (Hash) &
Logstash是一个开源的日志收集和处理工具,用于收集、存储、分析和可视化日志数据。在Logstash中,参数校验是非常重要的一步,它可以帮助我们确保输入的数据符合预期的格式和规则。以下是一个完整的Logstash参数校验实现过程:
logstash-input-jqvalidate_params
require 'json'
class LogstashInputJq < InputPlugin
def initialize(config)
@config = config
end
def validate_params(params)
if params['type'] != 'json'
return { error: 'Invalid input format. Expected JSON.' }
end
if params['data'] == ''
return { error: 'Missing data.' }
end
if params['data'].is_a?(String) && params['data'].empty?
return { error: 'Data is empty.' }
end
if params['data'].is_a?(Array) && params['data'].any? { |item| item.nil? }
return { error: 'Data contains nil values.' }
end
if params['data'].is_a?(Hash) && params['data'].any? { |k, v| v.nil? }
return { error: 'Data contains nil values in nested hashes.' }
end
if params['data'].is_a?(Hash) && params['data'].any? { |k, v| k == 'timestamp' && v.nil? }
return { error: 'Timestamp value is missing.' }
end
if params['data'].is_a?(Hash) && params['data'].any? { |k, v| k == 'level' && v.nil? }
return { error: 'Level value is missing.' }
end
if params['data'].is_a?(Hash) && params['data'].any? { |k, v| k == 'message' && v.nil? }
return { error: 'Message value is missing.' }
end
if params['data'].is_a?(Hash) && params['data'].any? { |k, v| k == 'tags' && v.nil? }
return { error: 'Tags value is missing.' }
end
if params['data'].is_a?(Hash) && params['data'].any? { |k, v| k == 'priority' && v.nil? }
return { error: 'Priority value is missing.' }
end
if params['data'].is_a?(Hash) && params['data'].any? { |k, v| k == 'offset' && v.nil? }
return { error: 'Offset value is missing.' }
end
if params['data'].is_a?(Hash) && params['data'].any? { |k, v| k == 'count' && v.nil? }
return { error: 'Count value is missing.' }
end
if params['data'].is_a?(Hash) && params['data'].any? { |k, v| k == 'timestamp' && v.nil? } && params['data']['timestamp'] == nil
return { error: 'Timestamp value is missing and cannot be nil.' }
end
if params['data'].is_a?(Hash) && params['data'].any? { |k, v| k == 'level' && v.nil? } && params['data']['level'] == nil
return { error: 'Level value is missing and cannot be nil.' }
end
if params['data'].is_a?(Hash) && params['data'].any? { |k, v| k == 'message' && v.nil? } && params['data']['message'] == nil
return { error: 'Message value is missing and cannot be nil.' }
end
if params['data'].is_a?(Hash) && params['data'].any? { |k, v| k == 'tags' && v.nil? } && params['data']['tags'] == nil
return { error: 'Tags value is missing and cannot be nil.' }
end
if params['data'].is_a?(Hash) && params['data'].any? { |k, v| k == 'priority' && v.nil? } && params['data']['priority'] == nil
return { error: 'Priority value is missing and cannot be nil.' }
end
if params['data'].is_a?(Hash) && params['data'].any? { |k, v| k == 'offset' && v.nil? } && params['data']['offset'] == nil
return { error: 'Offset value is missing and cannot be nil.' }
end
if params['data'].is_a?(Hash) && params['data'].any? { |k, v| k == 'count' && v.nil? } && params['data']['count'] == nil
return { error: 'Count value is missing and cannot be nil.' }
end
return {}
end
end
logstash-input-jq-pluginlogstash-input-jqfilter
class LogstashInputJqFilter < FilterGroup
def initialize(config)
@config = config
end
def filter(context)
context.to_hash.each do |key, value|
if key == 'type' || key == 'data' || key == 'tags' || key == 'priority' || key == 'offset' || key == 'count' || key == 'timestamp' || key == 'level' || key == 'message' || key == 'tags' || key == 'priority' || key == 'offset' || key == 'count' || key == 'timestamp' || key == 'level' || key == 'message' || key == 'tags' || key == 'priority' || key == 'offset' || key == 'count' || key == 'timestamp' || key == 'level' || key == 'message' || key == 'tags' || key == 'priority' || key == 'offset' || key == 'count' || key == 'timestamp' || key == 'level' || key == 'message' || key == 'tags' || key == 'priority' || key => true
context[key] = validate_params(value)
end 拼多多帮助力入口
end
end
end
logstash-input-jq-pluginlogstash-input-jqlogstash-input-jqinitializeadd_filterlogstash-input-jq-plugin
def initialize(config)
@config = config
add_filter(new LogstashInputJqFilter.new(config))
end
logstash-input-jqvalidate_params