bin/check-postgres-query.rb in sensu-plugins-postgres-3.0.0 vs bin/check-postgres-query.rb in sensu-plugins-postgres-4.0.0
- old
+ new
@@ -1,6 +1,8 @@
#! /usr/bin/env ruby
+# frozen_string_literal: true
+
#
# check-postgres-query
#
# DESCRIPTION:
# This plugin queries a PostgreSQL database. It alerts when the numeric
@@ -71,10 +73,15 @@
description: 'Database query to execute',
short: '-q QUERY',
long: '--query QUERY',
required: true
+ option :regex_pattern,
+ description: 'Regex pattern to match on query results and alert on if it does not match',
+ short: '-r REGEX',
+ long: '--regex-pattern REGEX'
+
option :check_tuples,
description: 'Check against the number of tuples (rows) returned by the query',
short: '-t',
long: '--tuples',
boolean: true,
@@ -123,9 +130,11 @@
calc = Dentaku::Calculator.new
if config[:critical] && calc.evaluate(config[:critical], value: value)
critical "Results: #{res.values}"
elsif config[:warning] && calc.evaluate(config[:warning], value: value)
warning "Results: #{res.values}"
+ elsif config[:regex_pattern] && (res.getvalue(0, 0) !~ /#{config[:regex_pattern]}/)
+ critical "Query result #{res.getvalue(0, 0)} doesn't match configured regex #{config[:regex_pattern]}"
else
ok 'Query OK'
end
end
end