Class: LogStash::Filters::Bytes
- Inherits:
-
Base
- Object
- Base
- LogStash::Filters::Bytes
- Defined in:
- lib/logstash/filters/bytes.rb
Overview
This filter will parse a given string as a computer storage value (e.g. “123 MB” or “5.3GB”) and add a new numeric field with size in bytes
Constant Summary collapse
- PREFIX_POWERS =
{ 'k' => 1, # 1 kilobyte = 1024 ^ 1 bytes 'm' => 2, # 1 megabyte = 1024 ^ 2 bytes 'g' => 3, # 1 gigabyte = 1024 ^ 3 bytes 't' => 4, # 1 terabyte = 1024 ^ 4 bytes 'p' => 5, # 1 petabyte = 1024 ^ 5 bytes 'e' => 6, # 1 exabyte = 1024 ^ 6 bytes }.freeze
- DIGIT_GROUP_SEPARATORS =
" _,."
Instance Method Summary collapse
Instance Method Details
#filter(event) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/logstash/filters/bytes.rb', line 63 def filter(event) source = event.get(@source) if !source or !source.is_a? String @tag_on_failure.each{|tag| event.tag(tag)} return end source.strip! # Parse the source into the number part (e.g. 123), # the unit prefix part (e.g. M), and the unit suffix part (e.g. B) match = source.match(/^([0-9#{DIGIT_GROUP_SEPARATORS}#{@decimal_separator}]*)\s*([kKmMgGtTpPeE]?)([bB]?)$/) if !match @tag_on_failure.each{|tag| event.tag(tag)} return end number, prefix, suffix = match.captures # Flag error if more than one decimal separator is found num_decimals = number.count(@decimal_separator) if num_decimals > 1 @tag_on_failure.each{|tag| event.tag(tag)} return end number = normalize_number(number.strip) if number == '' @tag_on_failure.each{|tag| event.tag(tag)} return end if suffix == '' suffix = 'B' end # Convert the number to bytes result = number.to_f if prefix != '' if @conversion_method == 'binary' result *= (1024 ** PREFIX_POWERS[prefix.downcase]) else result *= (1000 ** PREFIX_POWERS[prefix.downcase]) end end result = result.round event.set(@target, result) # filter_matched should go in the last line of our successful code filter_matched(event) end |
#register ⇒ Object
58 59 60 |
# File 'lib/logstash/filters/bytes.rb', line 58 def register # Add instance variables end |