2013-02-04 3 views
2

클라이언트에 우리의 로그 파일을 집계하도록 설정된 Greylog2 서버가 있습니다. 우리는 여러 개의 스트림을 정의했습니다.Graylog2의 전자 메일 알림

매일 전자 메일 알림을 보내고 싶습니다. "최소한 24 시간 동안 시스템에 x 개의 오류가 있습니다", 가장 자주 발생하는 오류 상위 10 개 목록이 이상적입니다.

누구나 전에 이와 같이 구현 한 사람이 있습니까? 팁이나 제안 사항을 제공해 줄 수 있습니까? 일부 포럼 게시물에서 REST API에 대한 언급을 보았지만 더 많은 정보를 찾을 수 없었습니다 ...

답변

1

내 직장에서는 레이크 작업 + crontab을 기반으로 알림 작업을 구성했습니다. 이것은 graylog2-server의 알림 API가 사용 가능해지기 전입니다 (plugin directory). 레이크 모델과 컨트롤러를 사용하도록 레이크 작업을 계속 사용합니다.

다음은 스트림 ID를 찾을 수 있도록 general.yaml에 추가되었습니다.

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 

# section in general.yaml 

streamalarms: 
    error_stream: 50ef145471de3516b900000d 

다음 실제 레이크 작업입니다