<?xml version="1.0" encoding="UTF-8"?>
<plugin-url>
  <approved type="boolean">false</approved>
  <author>Highgroove Studios</author>
  <cached-tag-list>ruby on rails, web requests</cached-tag-list>
  <canonical-name nil="true"></canonical-name>
  <code>require &quot;time&quot;

# This plugin is no longer supported - please use the Scout Rails Instrumentation plugin.
class RailsRequests &lt; Scout::Plugin
  
  #TEST_USAGE = &quot;#{File.basename($0)} log LOG max_request_length MAX_REQUEST_LENGTH last_run LAST_RUN&quot;
  
  def build_report
    begin
      require &quot;elif&quot;
    rescue LoadError
      begin
        require &quot;rubygems&quot;
        require &quot;elif&quot;
      rescue LoadError
        return error(:subject =&gt; &quot;Couldn't load Elif.&quot;,
                     :body    =&gt; &quot;The Elif library is required by &quot; +
                                 &quot;this plugin.&quot;)
      end
    end
    
    if option('log').strip.length == 0
      return { :error =&gt; { :subject =&gt; &quot;A path to the Rails log file wasn't provided.&quot; } }
    end

    report_data = { :slow_request_count =&gt; 0,
               :request_count =&gt; 0,
               :average_request_length =&gt; nil}
    
    last_completed = nil
    slow_requests = ''
    total_request_time = 0.0

    last_run = if mem = memory(:last_run)
                  mem
               else
                  Time.now
               end
    
    
    Elif.foreach(option(&quot;log&quot;)) do |line|
      if line =~ /\ACompleted in (\d+)ms .+ \[(\S+)\]\Z/        # newer Rails
        last_completed = [$1.to_i / 1000.0, $2]
      elsif line =~ /\ACompleted in (\d+\.\d+) .+ \[(\S+)\]\Z/  # older Rails
        last_completed = [$1.to_f, $2]
      elsif last_completed and
            line =~ /\AProcessing .+ at (\d+-\d+-\d+ \d+:\d+:\d+)\)/
        time_of_request = Time.parse($1)
        if time_of_request &lt; last_run
          break
        else
          logger.info 'increment'
          report_data[:request_count] += 1
          total_request_time += last_completed.first.to_f
          if option(&quot;max_request_length&quot;).to_f &gt; 0 and last_completed.first.to_f &gt; option(&quot;max_request_length&quot;).to_f
            report_data[:slow_request_count] += 1
            slow_requests += &quot;#{last_completed.last}\n&quot;
            slow_requests += &quot;Time: &quot; + last_completed.first.to_s + &quot; sec&quot; + &quot;\n\n&quot;
          end
        end # request should be analyzed
      end
    end
    
    # Create a single alert that holds all of the requests that exceeded the +max_request_length+.
    if report_data and (count = report_data[:slow_request_count].to_i and count &gt; 0)
      alert(:subject =&gt; &quot;Maximum Time(#{option(&quot;max_request_length&quot;).to_s} sec) exceeded on #{count} #{count &gt; 1 ? 'requests' : 'request'}&quot;,
            :body =&gt; slow_requests)
    end
    # Calculate the average request time if there are any requests
    if report_data[:request_count] &gt; 0
      avg = total_request_time/report_data[:request_count]
      report_data[:average_request_length] = sprintf(&quot;%.2f&quot;, avg)
    end
    remember(:last_run,Time.now)
    report(report_data)
  rescue
    if $!.message == &quot;undefined method `strip' for nil:NilClass&quot;
      error(:subject =&gt; &quot;Please provide the full path to the log file.&quot;,
            :body =&gt; &quot;A full path to the Rails log file wasn't provided - you can specify the path in the plugin options.&quot;)
    else
      raise
    end
  end
end
</code>
  <created-at type="datetime">2007-12-12T15:57:30-08:00</created-at>
  <default-triggers type="yaml" nil="true"></default-triggers>
  <description>Monitors ALL requests to a Ruby on Rails web application, alerting when slow web requests occur.
&lt;br/&gt;
Reports back the number of total requests, the number of slow requests, the average execution time, and the full URLs of slow requests. 
&lt;br/&gt;
Requires &quot;the elif library&quot;:http://elif.rubyforge.org/
sudo gem install elif
&lt;br/&gt;
Options:

* Location of Ruby on Rails log file
* Maximum request length 
</description>
  <featured type="boolean">false</featured>
  <id type="integer">3</id>
  <metadata type="yaml">--- |
options:
  log:
    name: Log
    notes: The Rails log file to process
  max_request_length:
    name: Max Request Length (sec)
    notes: If any request length is larger than this amount, an alert is generated
    default: 3

</metadata>
  <name>Ruby on Rails Request Monitoring</name>
  <plugins-count type="integer">315</plugins-count>
  <rating-avg type="decimal">5.0</rating-avg>
  <rating-count type="integer">3</rating-count>
  <rating-total type="integer">15</rating-total>
  <readme>Rails Requests Plugin
======================
Created by [Highgroove Studios LLC](http://www.highgroove.com)

Dependencies
------------

Requires the [elif library](http://elif.rubyforge.org/):

	sudo gem install elif
	
Options
-------
Both of the options are required. You must provide both the full path to the Rails log file and a maximum request length (in seconds).

Compatibility 
-------------

Works on Linux and OSX.


</readme>
  <schema type="yaml" nil="true"></schema>
  <scout-version type="integer">2</scout-version>
  <short-description>Monitors ALL requests to a Ruby on Rails web application, alerting when slow web requests occur. Reports the total number of requests, the number of slow requests, and the average execution time.</short-description>
  <tested-platforms>linux macosx solaris</tested-platforms>
  <total-usage-count type="integer">0</total-usage-count>
  <updated-at type="datetime">2009-09-23T23:03:01-07:00</updated-at>
  <url>http://github.com/highgroove/scout-plugins/raw/aae80392b33e5fa84c1fc8f79f2d31557ee79ae2/rails_requests/rails_requests.rb</url>
</plugin-url>
