"""
  Return the maximum xmin in the database, having too large of a value leads to
  bad things and eventual database not accepting new writes.  I had to lower
  the default postgresql setting from 200 mil to 180 mil as the database does
  lots of writes and autovac sometimes can not keep up.
"""
from __future__ import print_function
import sys
from pyiem.util import get_dbconn


def check(dbname):
    """Do the database check."""
    pgconn = get_dbconn(dbname, user="nobody")
    icursor = pgconn.cursor()
    icursor.execute(
        """
        SELECT datname, age(datfrozenxid) FROM pg_database
        ORDER by age DESC LIMIT 1
    """
    )
    row = icursor.fetchone()

    return row


def main(argv):
    """Go Main Go."""
    dbname, count = check(argv[1])
    if count < 191000000:
        print(
            "OK - %s %s |count=%s;191000000;195000000;220000000"
            % (count, dbname, count)
        )
        retval = 0
    elif count < 195000000:
        print(
            ("WARNING - %s %s |count=%s;191000000;195000000;220000000")
            % (count, dbname, count)
        )
        retval = 1
    else:
        print(
            ("CRITICAL - %s %s |count=%s;191000000;195000000;220000000")
            % (count, dbname, count)
        )
        retval = 2
    return retval


if __name__ == "__main__":
    sys.exit(main(sys.argv))

@@ -781,18 +781,18 @@
 count %3C 
-19
+20
 1000000:
@@ -837,34 +837,34 @@
 s %7Ccount=%25s;
-19
+20
 1000000;
 195000000;22
@@ -843,34 +843,34 @@
 nt=%25s;201000000;
-19
+20
 5000000;22000000
@@ -955,18 +955,18 @@
 count %3C 
-19
+20
 5000000:
@@ -1017,34 +1017,34 @@
 s %7Ccount=%25s;
-19
+20
 1000000;
 195000000;22
@@ -1023,34 +1023,34 @@
 nt=%25s;201000000;
-19
+20
 5000000;22000000
@@ -1189,18 +1189,18 @@
 =%25s;
-19
+20
 1000000;
 1950
@@ -1199,10 +1199,10 @@
 000;
-19
+20
 5000

increase nagios pgsql xmin check to 200m