Advertisement
Guest User

xymon output4logstash

a guest
May 30th, 2012
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.54 KB | None | 0 0
  1. require "logstash/namespace"
  2. require "logstash/outputs/base"
  3.  
  4. # The Xymon output is used for sending check results to xymon server via the
  5. # xymon command line
  6. #
  7. # For this output to work, your event must have the following fields:
  8. #
  9. # * "xymon_host" (the host configured in xymon host.cfg)
  10. # * "xymon_check" (the check name)
  11. # * "xymon_color" (color result of check)
  12. #
  13. # The easiest way to use this output is with the grep filter.
  14. # Presumably, you only want certain events matching a given pattern
  15. # to sends events to xymon server, so use grep to match and also to add the required
  16. # fields.
  17. #
  18. # In this example i use input gelf, the java app use gelfj library, i set
  19. # originHost to fqdn hostname and facility to java app name.
  20. #
  21. # input {
  22. #       gelf {
  23. #               type => "gelf"
  24. #               tags => "gelf"
  25. #       }
  26. # }
  27. #
  28. # filter {
  29. #       grep {
  30. #               tags => "gelf"
  31. #               match => [ "level", "3" ]
  32. #               drop => "false"
  33. #               add_tag => "send-to-xymon"
  34. #               add_field => [
  35. #                       "xymon_host", "%{host}",
  36. #                       "xymon_check", "%{facility}_log}",
  37. #                       "xymon_color", "red"
  38. #               ]
  39. #       }
  40. # }
  41. #
  42. # output {
  43. #       xymon {
  44. #               tags => "send-to-xymon"
  45. #               # specify the hostname or ip of your xymon server
  46. #               # (defaults to localhost)
  47. #               server => "localhost"
  48. #
  49. #               # specify the port to connect to xymon server (default 1984)
  50. #               port => "1984"
  51. #
  52. #               # specify the path to xymon bin
  53. #               xymon_bin => "/home/xymon/client/bin/xymon"
  54. #       }
  55. # }
  56. #
  57.  
  58. class LogStash::Outputs::Xymon < LogStash::Outputs::Base
  59.  
  60.   config_name "xymon"
  61.   plugin_status "experimental"
  62.  
  63.   config :server, :validate => :string, :default => "localhost"
  64.   config :port, :validate => :number, :default => 1984
  65.   config :xymon_bin, :validate => :string, :default => "/home/xymon/client/bin/xymon"
  66.  
  67.   public
  68.   def register
  69.     # nothing to do
  70.   end # def register
  71.  
  72.   public
  73.   def receive(event)
  74.     return unless output?(event)
  75.  
  76.     if !File.exists?(@xymon_bin)
  77.       @logger.warn("Skipping xymon output; xymon_bin file is missing",
  78.                    :xymon_bin => @xymon_bin, :missed_event => event)
  79.       return
  80.     end
  81.  
  82.     host = event.fields["xymon_host"]
  83.     if !host
  84.       @logger.warn("Skipping xymon output; xymon_host field is missing",
  85.                    :missed_event => event)
  86.       return
  87.     end
  88.  
  89.     check = event.fields["xymon_check"]
  90.     if !check
  91.       @logger.warn("Skipping xymon output; xymon_check field is missing",
  92.                    :missed_event => event)
  93.       return
  94.     end
  95.  
  96.     color = event.fields["xymon_color"]
  97.     if !color
  98.       @logger.warn("Skipping xymon output; xymon_color field is missing",
  99.                    :missed_event => event)
  100.       return
  101.     end
  102.  
  103.     host = host[0].gsub(/\./, ",")
  104.     xmsg = event.message
  105.     xmsg = xmsg.gsub("\n", "")
  106.     xmsg = xmsg.gsub(/"/, "\\\"")
  107.  
  108.     cmd = "#{@xymon_bin} #{@server}:#{@port} \"status #{host}.#{check[0]} #{color[0]} #{Time.now}
  109.  
  110.        #{xmsg} \""
  111.  
  112.     @logger.debug("Running xymon command", :command => cmd)
  113.     begin
  114.       system(cmd)
  115.     rescue => e
  116.       @logger.warn("Skipping xymon output; error calling xymon_bin",
  117.                    :command => cmd, :missed_event => event,
  118.                    :exception => e, :backtrace => e.backtrace)
  119.     end
  120.   end # def receive
  121. end # class LogStash::Outputs::xymon
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement