2

私のクライアントには、ログ ファイルを集約するように設定された Greylog2 サーバーがあります。いくつかのストリームが定義されています。

毎日の電子メール通知が送信されることを希望します。少なくとも「過去 24 時間にシステムが x エラーを受信しました」、理想的には最も頻繁に発生する上位 10 件のエラーのリストです。

誰かが以前にこのようなことを実装したことがありますか?ヒントや提案を提供できますか? いくつかのフォーラム投稿で REST API についての言及を見ましたが、それ以上の情報を見つけることができませんでした...

4

1 に答える 1

1

私の職場では、rake タスク + crontab に基づいてアラート タスクを構成しました。これは、graylog2-server のアラート API が利用可能になる前 ( plugin directory ) でした。Railsモデルとコントローラーを使用できるようにするため、rakeタスクを引き続き使用します。

ストリーム ID を見つけることができるように、general.yaml に以下が追加されます。

# section in general.yaml

streamalarms:
  error_stream: 50ef145471de3516b900000d

以下は、実際の rake タスクです。

namespace :gl2rake

    # Helper method for recording how long jobs took, which is used to debug / tune the jobs.
    def monitoring_wrapper(task)
    btime = Time.now
    task_name = task.name
    task_starting(task_name)

    if block_given?
      yield
    else 
      puts "No block given to monitoring_wrapper!"
    end

    etime = Time.now
    duration = (etime - btime)
    puts "Time elapsed: #{duration} seconds"
    task_completed(task_name, duration)
  end

    desc "Send an email if a job is written to the error queue. If there are more than 5 errored jobs in the last 6 minutes then send sms"
  task :error_queue => :environment do |task|
    monitoring_wrapper(task) do

      # the streams to check
      # I have customised the configuration class so that all of the stream ids are available. This can be automated.
      streams = Configuration.streamalarm_config('error_stream', '')

      # this method has been added to app/models/configuration.rb as a convenience.
      # def self.streamalarm_config(key, default)
      #   nested_general_config :streamalarms, key, default
      # end

      # get unix epoch time of 6 minutes ago
      six_mins_ago = 6.minutes.ago

      filters = {
        # optionally apply a message filter to the stream
        :message => "\"Writing job to error queue.\"",
        :date => "from #{six_mins_ago}" 
      }

      # get the stream
      stream = Stream.find_by_id(stream_id)

      if !stream
        $stderr.puts "Invalid stream id #{stream_id}"
        next
      end

      messages = MessageGateway.all_by_quickfilter(filters, nil, {:stream_id => stream_id})

      if messages.size > 0

        #alert - jobs written to error queue
        if messages.size > 5
            # send_sms_for_stream is a custom method we wrote that hooks into an sms api.
          send_sms_for_stream("There are #{messages.size} errored job(s) in the last 6 minutes. Check email for details", 'error_queue', stream.title)
        end

        message = "There are #{messages.size} errored job(s) in the last 6 minutes. (Stream #{stream.title})\n"

        messages.each do |m|
          message += "\t#{m.message}\n"
        end

        # sends an email to our designated alerting email
        send_mail("There are  #{messages.size} errored job(s)", message, 'error_queue', stream.title)
      end
    end
  end
end

これは、cron ジョブを介して呼び出すことができるようになりました。

3-59/5 * * * * sudo rake -f /opt/graylog2-web-interface/Rakefile gl2rake:error_queue RAILS_ENV=production
于 2013-05-21T00:48:02.797 に答える