Skip to content

Commit d2962de

Browse files
option to skip CYCLE sequences in action sequence
This adds a new option "--skipcycled" which forces sequences with the CYCLE option to be reported at 0% usage, thus not creating WARNINGs and CRITICALs for these sequences. The default is still the old behaviour (no special treatment for CYCLE sequences).
1 parent 59b5bb5 commit d2962de

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

check_postgres.pl

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,7 +1270,8 @@ package check_postgres;
12701270
'filter=s@', ## used by same_schema only
12711271
'suffix=s', ## used by same_schema only
12721272
'replace', ## used by same_schema only
1273-
'lsfunc=s', ## used by wal_files and archive_ready
1273+
'lsfunc=s', ## used by wal_fles and archive_ready
1274+
'skipcycled', ## used by sequence only
12741275
);
12751276

12761277
die $USAGE if ! keys %opt and ! @ARGV;
@@ -7665,6 +7666,12 @@ sub check_sequence {
76657666
## Warning and critical are percentages
76667667
## Can exclude and include sequences
76677668

7669+
my $skipcycled = $opt{'skipcycled'} || 0;
7670+
my $percsql = 'ROUND(used/slots*100)';
7671+
if($skipcycled) {
7672+
$percsql = 'CASE WHEN cycle THEN 0 ELSE ' . $percsql . ' END';
7673+
}
7674+
76687675
my ($warning, $critical) = validate_range
76697676
({
76707677
type => 'percent',
@@ -7719,13 +7726,14 @@ sub check_sequence {
77197726
WHERE nspname !~ '^pg_temp.*'
77207727
ORDER BY nspname, seqname, typname
77217728
};
7722-
my $SQL10 = q{
7723-
SELECT seqname, last_value, slots, used, ROUND(used/slots*100) AS percent,
7729+
my $SQL10 = qq{
7730+
SELECT seqname, last_value, slots, used, $percsql AS percent,
77247731
CASE WHEN slots < used THEN 0 ELSE slots - used END AS numleft
77257732
FROM (
77267733
SELECT quote_ident(schemaname)||'.'||quote_ident(sequencename) AS seqname, COALESCE(last_value,min_value) AS last_value,
7727-
CEIL((max_value-min_value::numeric+1)/increment_by::NUMERIC) AS slots,
7728-
CEIL((COALESCE(last_value,min_value)-min_value::numeric+1)/increment_by::NUMERIC) AS used
7734+
cycle,
7735+
CEIL((max_value-min_value::NUMERIC+1)/increment_by::NUMERIC) AS slots,
7736+
CEIL((COALESCE(last_value,min_value)-min_value::NUMERIC+1)/increment_by::NUMERIC) AS used
77297737
FROM pg_sequences) foo};
77307738
## use critic
77317739

@@ -7752,12 +7760,13 @@ sub check_sequence {
77527760
my $seqname_l = $seqname;
77537761
$seqname_l =~ s/'/''/g; # SQL literal quoting (name is already identifier-quoted)
77547762
push @seq_sql, qq{
7755-
SELECT '$seqname_l' AS seqname, last_value, slots, used, ROUND(used/slots*100) AS percent,
7763+
SELECT '$seqname_l' AS seqname, last_value, slots, used, $percsql AS percent,
77567764
CASE WHEN slots < used THEN 0 ELSE slots - used END AS numleft
77577765
FROM (
77587766
SELECT last_value,
7759-
CEIL((LEAST(max_value, $maxValue)-min_value::numeric+1)/increment_by::NUMERIC) AS slots,
7760-
CEIL((last_value-min_value::numeric+1)/increment_by::NUMERIC) AS used
7767+
is_cycled AS cycle,
7768+
CEIL((LEAST(max_value, $maxValue)-min_value::NUMERIC+1)/increment_by::NUMERIC) AS slots,
7769+
CEIL((last_value-min_value::NUMERIC+1)/increment_by::NUMERIC) AS used
77617770
FROM $seqname) foo
77627771
};
77637772
}
@@ -9967,7 +9976,8 @@ =head2 B<sequence>
99679976
The I<--warning> and I<--critical> options should be expressed as percentages. The default values
99689977
are B<85%> for the warning and B<95%> for the critical. You may use --include and --exclude to
99699978
control which sequences are to be checked. Note that this check does account for unusual B<minvalue>
9970-
and B<increment by> values, but does not care if the sequence is set to cycle or not.
9979+
and B<increment by> values. By default it does not care if the sequence is set to cycle or not,
9980+
and by passing I<--skipcycled> sequenced set to cycle are reported with 0% usage.
99719981
99729982
The output for Nagios gives the name of the sequence, the percentage used, and the number of 'calls'
99739983
left, indicating how many more times nextval can be called on that sequence before running into

t/02_sequence.t

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use 5.006;
66
use strict;
77
use warnings;
88
use Data::Dumper;
9-
use Test::More tests => 14;
9+
use Test::More tests => 16;
1010
use lib 't','.';
1111
use CP_Testing;
1212

@@ -47,6 +47,7 @@ my $testtbl = 'sequence_test';
4747
$cp->drop_sequence_if_exists($seqname);
4848
$cp->drop_sequence_if_exists("${seqname}2");
4949
$cp->drop_sequence_if_exists("${seqname}'\"evil");
50+
$cp->drop_sequence_if_exists("${seqname}_cycle");
5051
$cp->drop_table_if_exists("$testtbl");
5152

5253
$t=qq{$S works when no sequences exist};
@@ -100,8 +101,16 @@ $dbh->commit();
100101
$t=qq{$S handles SQL quoting};
101102
like ($cp->run(''), qr{OK:.+'public."${seqname}''""evil"'}, $t); # extra " and ' because name is both identifier+literal quoted
102103

104+
# test CYCLE sequence skipping
105+
$dbh->do(qq{CREATE SEQUENCE ${seqname}_cycle MAXVALUE 5 CYCLE});
106+
$dbh->commit();
107+
$dbh->do("SELECT setval('${seqname}_cycle',5)");
108+
like ($cp->run('--skipcycled'), qr{OK:.+public.cp_test_sequence_cycle=0%;85%;95% }, $t);
109+
like ($cp->run(''), qr{CRITICAL:.+public.cp_test_sequence_cycle=100% \(calls left=0\) }, $t);
110+
103111
$dbh->do("DROP SEQUENCE ${seqname}");
104112
$dbh->do("DROP SEQUENCE ${seqname}2");
113+
$dbh->do("DROP SEQUENCE ${seqname}_cycle");
105114
$dbh->do(qq{DROP SEQUENCE "${seqname}'""evil"});
106115

107116
# test integer column where the datatype range is smaller than the serial range

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy