/nagios/files/default/plugins/db_queues.rb

http://github.com/37signals/37s_cookbooks · Ruby · 102 lines · 73 code · 22 blank · 7 comment · 4 complexity · 841f25ac127d46d8d3a126b84c0b10e7 MD5 · raw file

  1. #!/usr/bin/env ruby
  2. #
  3. # Check the size of a database queue
  4. #
  5. require 'rubygems'
  6. require 'choice'
  7. require 'mysql'
  8. EXIT_OK = 0
  9. EXIT_WARNING = 1
  10. EXIT_CRITICAL = 2
  11. EXIT_UNKNOWN = 3
  12. Choice.options do
  13. header ''
  14. header 'Specific options:'
  15. option :warn do
  16. short '-w'
  17. long '--warning=VALUE'
  18. desc 'Warning threshold'
  19. cast Integer
  20. end
  21. option :crit do
  22. short '-c'
  23. long '--critical=VALUE'
  24. desc 'Critical threshold'
  25. cast Integer
  26. end
  27. option :host do
  28. short '-H'
  29. long '--host=VALUE'
  30. desc 'MySQL DB host'
  31. end
  32. option :port do
  33. short '-P'
  34. long '--port=VALUE'
  35. desc 'MySQL DB port'
  36. end
  37. option :username do
  38. short '-u'
  39. long '--username=VALUE'
  40. desc 'MySQL DB username'
  41. end
  42. option :password do
  43. short '-p'
  44. long '--password=VALUE'
  45. desc 'MySQL DB password'
  46. end
  47. option :database do
  48. short '-d'
  49. long '--database=VALUE'
  50. desc 'MySQL database'
  51. end
  52. option :query do
  53. short '-q'
  54. long '--query=VALUE'
  55. desc 'MySQL DB count query'
  56. end
  57. end
  58. c = Choice.choices
  59. # nagios performance data format: 'label'=value[UOM];[warn];[crit];[min];[max]
  60. # see http://nagiosplug.sourceforge.net/developer-guidelines.html#AEN203
  61. perfdata = "query_count=%d;#{c[:warn]};#{c[:crit]}"
  62. message = "Query '#{c[:query]}' result %d exceeds %d|#{perfdata}"
  63. if c[:warn] && c[:crit]
  64. conn = Mysql::connect(c[:host], c[:username], c[:password], c[:database], c[:port].to_i)
  65. res = conn.query(c[:query])
  66. value = res.fetch_row
  67. value = value.first.to_i
  68. if value >= c[:crit]
  69. puts sprintf(message, value, c[:crit], value)
  70. exit(EXIT_CRITICAL)
  71. end
  72. if value >= c[:warn]
  73. puts sprintf(message, value, c[:warn], value)
  74. exit(EXIT_WARNING)
  75. end
  76. else
  77. puts "Please provide a warning and critical threshold"
  78. end
  79. # if warning nor critical trigger, say OK and return performance data
  80. puts sprintf("Query '#{c[:query]}' result %d OK|#{perfdata}", value, value)