Skip to content

option to skip CYCLE sequences in action sequence #134

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions check_postgres.pl
Original file line number Diff line number Diff line change
Expand Up @@ -1270,7 +1270,8 @@ package check_postgres;
'filter=s@', ## used by same_schema only
'suffix=s', ## used by same_schema only
'replace', ## used by same_schema only
'lsfunc=s', ## used by wal_files and archive_ready
'lsfunc=s', ## used by wal_fles and archive_ready
'skipcycled', ## used by sequence only
);

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

my $skipcycled = $opt{'skipcycled'} || 0;
my $percsql = 'ROUND(used/slots*100)';
if($skipcycled) {
$percsql = 'CASE WHEN cycle THEN 0 ELSE ' . $percsql . ' END';
}

my ($warning, $critical) = validate_range
({
type => 'percent',
Expand Down Expand Up @@ -7719,13 +7726,14 @@ sub check_sequence {
WHERE nspname !~ '^pg_temp.*'
ORDER BY nspname, seqname, typname
};
my $SQL10 = q{
SELECT seqname, last_value, slots, used, ROUND(used/slots*100) AS percent,
my $SQL10 = qq{
SELECT seqname, last_value, slots, used, $percsql AS percent,
CASE WHEN slots < used THEN 0 ELSE slots - used END AS numleft
FROM (
SELECT quote_ident(schemaname)||'.'||quote_ident(sequencename) AS seqname, COALESCE(last_value,min_value) AS last_value,
CEIL((max_value-min_value::numeric+1)/increment_by::NUMERIC) AS slots,
CEIL((COALESCE(last_value,min_value)-min_value::numeric+1)/increment_by::NUMERIC) AS used
cycle,
CEIL((max_value-min_value::NUMERIC+1)/increment_by::NUMERIC) AS slots,
CEIL((COALESCE(last_value,min_value)-min_value::NUMERIC+1)/increment_by::NUMERIC) AS used
FROM pg_sequences) foo};
## use critic

Expand All @@ -7752,12 +7760,13 @@ sub check_sequence {
my $seqname_l = $seqname;
$seqname_l =~ s/'/''/g; # SQL literal quoting (name is already identifier-quoted)
push @seq_sql, qq{
SELECT '$seqname_l' AS seqname, last_value, slots, used, ROUND(used/slots*100) AS percent,
SELECT '$seqname_l' AS seqname, last_value, slots, used, $percsql AS percent,
CASE WHEN slots < used THEN 0 ELSE slots - used END AS numleft
FROM (
SELECT last_value,
CEIL((LEAST(max_value, $maxValue)-min_value::numeric+1)/increment_by::NUMERIC) AS slots,
CEIL((last_value-min_value::numeric+1)/increment_by::NUMERIC) AS used
is_cycled AS cycle,
CEIL((LEAST(max_value, $maxValue)-min_value::NUMERIC+1)/increment_by::NUMERIC) AS slots,
CEIL((last_value-min_value::NUMERIC+1)/increment_by::NUMERIC) AS used
FROM $seqname) foo
};
}
Expand Down Expand Up @@ -9967,7 +9976,8 @@ =head2 B<sequence>
The I<--warning> and I<--critical> options should be expressed as percentages. The default values
are B<85%> for the warning and B<95%> for the critical. You may use --include and --exclude to
control which sequences are to be checked. Note that this check does account for unusual B<minvalue>
and B<increment by> values, but does not care if the sequence is set to cycle or not.
and B<increment by> values. By default it does not care if the sequence is set to cycle or not,
and by passing I<--skipcycled> sequenced set to cycle are reported with 0% usage.

The output for Nagios gives the name of the sequence, the percentage used, and the number of 'calls'
left, indicating how many more times nextval can be called on that sequence before running into
Expand Down
11 changes: 10 additions & 1 deletion t/02_sequence.t
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use 5.006;
use strict;
use warnings;
use Data::Dumper;
use Test::More tests => 14;
use Test::More tests => 16;
use lib 't','.';
use CP_Testing;

Expand Down Expand Up @@ -47,6 +47,7 @@ my $testtbl = 'sequence_test';
$cp->drop_sequence_if_exists($seqname);
$cp->drop_sequence_if_exists("${seqname}2");
$cp->drop_sequence_if_exists("${seqname}'\"evil");
$cp->drop_sequence_if_exists("${seqname}_cycle");
$cp->drop_table_if_exists("$testtbl");

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

# test CYCLE sequence skipping
$dbh->do(qq{CREATE SEQUENCE ${seqname}_cycle MAXVALUE 5 CYCLE});
$dbh->commit();
$dbh->do("SELECT setval('${seqname}_cycle',5)");
like ($cp->run('--skipcycled'), qr{OK:.+public.cp_test_sequence_cycle=0%;85%;95% }, $t);
like ($cp->run(''), qr{CRITICAL:.+public.cp_test_sequence_cycle=100% \(calls left=0\) }, $t);

$dbh->do("DROP SEQUENCE ${seqname}");
$dbh->do("DROP SEQUENCE ${seqname}2");
$dbh->do("DROP SEQUENCE ${seqname}_cycle");
$dbh->do(qq{DROP SEQUENCE "${seqname}'""evil"});

# test integer column where the datatype range is smaller than the serial range
Expand Down
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