Class: LogStash::Outputs::SCACSV

Inherits:
File
  • Object
show all
Defined in:
lib/logstash/outputs/scacsv.rb

Overview

SCACSV - based upon original Logstash CSV output.

Write events to disk in CSV format Write a PI header as the first line in the file Name file per PI convention, based upon first and last timestamps encountered

Instance Method Summary collapse

Instance Method Details

#receive(event) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/logstash/outputs/scacsv.rb', line 121

def receive(event)
  return unless output?(event)

  @logger.debug("in SCACSV receive")

  if (event['SCAWindowMarker']) 
    # just eat the marker - don't output it
    # if we had at least one record output, then close the file and move on 
    if @recordCount >= 1
      closeAndRenameCurrentFile
    end
  else

    # Now see if we need to close file because of a new boundary
    if @closeOnIntervalBoundaries and @recordCount >= 1 and (@currentOutputIntervalStartTime != snapTimestampToInterval(timestampAsEpochSeconds(event),@fileIntervalWidthSeconds))
        closeAndRenameCurrentFile
    end

    @formattedPath = event.sprintf(@path)
    fd = open(@formattedPath)
    @logger.debug("SCACSVreceive - after opening fd=" + fd.to_s)

    if @recordCount == 0
      # output header on first line - note, need a minimum of one record for sensible output
      if @header then 
#         csv_header = @fields.map { |name| name }
        fd.write(@header.to_csv(@csv_options))
      else     
        fd.write(@fields.to_csv(@csv_options))
      end
    end

    csv_values = @fields.map {|name| get_value(name, event)}
    fd.write(csv_values.to_csv(@csv_options))

    flush(fd)
    close_stale_files

    # remember state
    @recordCount = @recordCount + 1
    @lastOutputTime = Time.now

    # capture the earliest - assumption is that records are in order
    if (@recordCount) == 1 
      if !@closeOnIntervalBoundaries
        @startTime = event[@time_field]
      else
        @startTime = snapTimestampToInterval(timestampAsEpochSeconds(event),@fileIntervalWidthSeconds)
      end
    end

    # for every record, update endTime - again, assumption is that records are in order
    if !@closeOnIntervalBoundaries
      @endTime = event[@time_field]
    else
      @endTime = @startTime + @fileIntervalWidthSeconds - 1   # end of interval
    end

#puts("After snapping. timestamp=" + event[@time_field].to_s + " startTime=" + @startTime.to_s + " endTime = " + @endTime.to_s)

    # remember start of boundary for next time
    if @closeOnIntervalBoundaries
       @currentOutputIntervalStartTime = @startTime
    end


    if ((@max_size > 0) and (@recordCount >= max_size))
      # Have enough records, close it out
      closeAndRenameCurrentFile
    end

  end  

end

#registerObject



56
57
58
59
60
61
62
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
# File 'lib/logstash/outputs/scacsv.rb', line 56

def register
  super
  @csv_options = Hash[@csv_options.map{|(k,v)|[k.to_sym, v]}]
  
  # variables to hold the start and end times which we'll use to rename the files to
  @startTime   = "missingStartTime"
  @endTime     = "missingEndTime"
  @recordCount = 0

  @lastOutputTime = 0 #data time
  @flushInterval = @flush_interval.to_i

  @timerThread = Thread.new { flushWatchdog(@flush_interval) }

  @currentOutputIntervalStartTime = 0
  @fileIntervalWidthSeconds = 0
  @closeOnIntervalBoundaries = false
  case @file_interval_width.upcase
  when "MINUTE"
    @fileIntervalWidthSeconds = 60
    @closeOnIntervalBoundaries = true
  when "FIVE"
    @fileIntervalWidthSeconds = 300
    @closeOnIntervalBoundaries = true 
  when "FIFTEEN"
    @fileIntervalWidthSeconds = 900
    @closeOnIntervalBoundaries = true 
  when "HOUR"
    @fileIntervalWidthSeconds = 3600
    @closeOnIntervalBoundaries = true 
  when "DAY"
    @fileIntervalWidthSeconds = 86400
    @closeOnIntervalBoundaries = true
  else
    @fileIntervalWidthSeconds = 0 #not used  
    @closeOnIntervalBoundaries = false
  end

  @df = nil 
  if (@time_field_format != "epoch")
    @df = java.text.SimpleDateFormat.new(@time_field_format)
  end


end