Class: LogStash::Filters::CloudFoundry

Inherits:
Base
  • Object
show all
Defined in:
lib/logstash/filters/cloudfoundry.rb

Overview

The Cloud Foundry filter performs a lookup against a Cloud Foundry foundation to provide the following pieces of meta-data to an application log

- Org name
- Space name
- Application name

The conf should look like this:

filter{
  cloudfoundry{
      cf_api      => "https://api.cf-domain.com"
      cf_user     => username
      cf_password => password
      cf_org      => "system"
      cf_space    => "apps_manager"
  }
}

For this plugin to work you will need the CF CLI installed on your system. (github.com/cloudfoundry/cli)

This filter only processes 1 event at a time so the use of this plugin can significantly slow down your pipeline’s throughput if you have a high latency network. In the event of an outage (network or cloud foundry infrastructure), a retry flag can bet set to reduce the number of failed log-in and curl attempts to the Cloud Foundry endpoint. This will allow the pipeline to function without severely impacting throughput. Additionally adjusting the cache flush period and a cache items TTL will reduce slow down to your pipeline’s throughput at the cost of additional resource consumption.

To set up receiving logs from multiple foundations the user executing logstash will need write premissions to it’s home directiory. To achieve connecting to multiple CF endpoints at once the CF_HOME variable needs to be a unique path for each initialization of the plugin. The configuration should look similar to the following:

filter{
  if "zone1" in [tags]
      cloudfoundry{
          cf_api      => "https://api.cf-domain1.com"
          ....
      }
  }
  if "zone2" in [tags]
      cloudfoundry{
          cf_api      => "https://api.cf-domain2.com"
          ....
      }
  }
}

Instance Method Summary collapse

Instance Method Details

#filter(event) ⇒ Object



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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/logstash/filters/cloudfoundry.rb', line 145

def filter(event)

  if @cf_logged_in

    message = event["message"]
    app_guid = message[/loggregator ([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})/, 1]

    unless app_guid.nil?

      event["appguid"] = app_guid
      app_cache_item = nil
      @app_cache_mutex.synchronize { app_cache_item = @app_cache[app_guid] }

      if app_cache_item.nil?

        app_query   = cfcurl("/v2/apps/#{app_guid}")
        validate_query(app_query, app_guid)

        space_query = cfcurl("/v2/spaces/#{app_query[:stdout]["entity"]["space_guid"]}")
        validate_query(space_query)

        org_query   = cfcurl("/v2/organizations/#{space_query[:stdout]["entity"]["organization_guid"]}")
        validate_query(org_query)

        app_info = Hash.new()
        app_info["appname"]   = app_query[:stdout]["entity"]["name"]
        app_info["spacename"] = space_query[:stdout]["entity"]["name"]
        app_info["orgname"]   = org_query[:stdout]["entity"]["name"]

        app_cache_item = Hash.new()
        app_cache_item["info"]      = app_info
        app_cache_item["expire_at"] = Time.now.to_i + @cache_age_time
        @app_cache_mutex.synchronize { @app_cache[app_guid] = app_cache_item }

      else
        app_info = app_cache_item['info']
      end
      app_info.each { |k,v| event[k] = v }
    end

  else

    if @login_next <= Time.now.to_i && @cf_retry_cli
       = cflogin
      raise "CF-login-failed: #{[:stdout]}" unless [:status]
      @cf_logged_in = true
    end

  end

  filter_matched(event)

rescue Exception => e

  if e.inspect.include?("CF-curl-failed") || e.inspect.include?("CF-login-failed")
    @logger.error("Exception: #{e.inspect}.")
    @login_next   = Time.now.to_i + @cf_retry_cli_timeout
    @cf_logged_in = false
  else
    @logger.error("Exception: #{e.inspect}")
  end
  @logger.error("Backtrace: #{e.backtrace}")

end

#registerObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/logstash/filters/cloudfoundry.rb', line 95

def register

  #Check input
  if @cf_api.empty? || @cf_user.empty? || @cf_password.empty? || @cf_org.empty? || @cf_space.empty?
    raise "Required paramters where left blank."
  end
	
  #Set up cache and scheduler
  @app_cache       = Hash.new
  @app_cache_mutex = Mutex.new
  @scheduler       = Rufus::Scheduler.new
  @job = @scheduler.every(@cache_flush_time) do
    begin
      @app_cache_mutex.synchronize {
        @app_cache.delete_if { |key, value| value["expire_at"]<Time.now.to_i }
      }
    rescue Exception => msg
      @logger.error("Error purging app info cache: #{msg}")
    end
  end

  #define CF_HOME path
  @cf_path = "#{ENV['HOME']}/#{@cf_api.gsub(/[^0-9A-Za-z]/, '')}"	
  stdout, stderr, status = Open3.capture3("mkdir #{@cf_path}")	
	
  #Check folder creation status. 
  unless status.success?
    unless stderr.include?("File exists")
      raise "CF-Home-Folder-Creation: #{stderr}"
    end
  end

  #Login to CF endpoint
   = cflogin
  raise "CF-login-failed: #{[:stdout]}" unless [:status]

  @cf_retry_cli_timeout > 0 ? @cf_retry_cli = true : @cf_retry_cli = false
  @login_next = Time.now.to_i
  @cf_logged_in = true
	
rescue Exception => e

  @cf_retry_cli = false
  @cf_logged_in = false
  @logger.error("Exception: #{e.inspect}. Filter won't be applied")
  @logger.error("Backtrace: #{e.backtrace}")

end