summaryrefslogtreecommitdiffstats
path: root/web/attachments/170107-check_pgsql.patch
blob: b880f8c7f7aeb0424e3390d2881181e96dc77852 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
--- check_pgsql.original	2006-03-07 12:40:11.000000000 -0700
+++ check_pgsql.c	2006-03-07 12:47:48.000000000 -0700
@@ -21,5 +21,5 @@
 const char *progname = "check_pgsql";
 const char *revision = "$Revision: 1.31 $";
-const char *copyright = "1999-2004";
+const char *copyright = "1999-2006";
 const char *email = "nagiosplug-devel@lists.sourceforge.net";
 
@@ -36,5 +36,7 @@
 	DEFAULT_PORT = 5432,
 	DEFAULT_WARN = 2,
-	DEFAULT_CRIT = 8
+	DEFAULT_CRIT = 8,
+	DEFAULT_WARNING_XID_AGE = 500000000,			/* 500M */
+	DEFAULT_CRITICAL_XID_AGE = 1000000000			/* 1B */
 };
 
@@ -58,4 +60,6 @@
 double twarn = (double)DEFAULT_WARN;
 double tcrit = (double)DEFAULT_CRIT;
+int warning_xid_age = DEFAULT_WARNING_XID_AGE;
+int critical_xid_age = DEFAULT_CRITICAL_XID_AGE;
 
 PGconn *conn;
@@ -124,4 +128,9 @@
 	int elapsed_time;
 	int status = STATE_UNKNOWN;
+	int i,j;
+	int nFields;
+	const char *paramValues[1];
+        char param[100];
+	PGresult *res;
 
 	/* begin, by setting the parameters for a backend connection if the
@@ -167,8 +176,43 @@
 	}
 	else {
+		snprintf(param, 100, "%d", warning_xid_age);
+		paramValues[0] = param;
+		res = PQexecParams(conn, 
+			"SELECT datname,age(datvacuumxid) FROM pg_database WHERE age(datvacuumxid) > $1 ORDER BY age(datvacuumxid) DESC",
+			1, NULL, paramValues, NULL, NULL, 0);
+		if (PQresultStatus(res) != PGRES_TUPLES_OK)
+		{
+			printf (_("CRITICAL - error '%s' running query on '%s' (%s).\n"),
+		        	PQerrorMessage(conn), dbName,	PQerrorMessage (conn));
+			PQclear (res);
+			PQfinish (conn);
+			return STATE_CRITICAL;
+		}
+
+		if (PQntuples(res) > 0)
+		{
+			j = atoi(PQgetvalue(res, 0, 1));
+
+			printf (_("%s: Databases in danger of XID wraparound: "), 
+				((j > critical_xid_age) ? "CRITICAL" : "WARNING"));
+
+			for (i = 0; i < PQntuples(res); i++)
+    			{
+				printf (_("%s age %s"), PQgetvalue(res, i, 0), PQgetvalue(res, i, 1));
+				if (i < PQntuples(res) - 1)
+					printf (_(","));
+				else
+					printf (_("\n\n"));
+    			}
+			PQclear (res);
+			PQfinish (conn);
+			return (j > critical_xid_age) ? STATE_CRITICAL : STATE_WARNING;
+		}
+
+		PQclear(res);
 		status = STATE_OK;
 	}
 	PQfinish (conn);
-	printf (_(" %s - database %s (%d sec.)|%s\n"), 
+	printf (_(" %s - database %s (%ds, XIDs OK)|%s\n"), 
 	        state_text(status), dbName, elapsed_time,
 	        fperfdata("time", elapsed_time, "s",
@@ -198,9 +242,11 @@
 		{"port", required_argument, 0, 'P'},
 		{"database", required_argument, 0, 'd'},
+		{"warnxid", required_argument, 0, 'x'},
+		{"critxid", required_argument, 0, 'y'},
 		{0, 0, 0, 0}
 	};
 
 	while (1) {
-		c = getopt_long (argc, argv, "hVt:c:w:H:P:d:l:p:a:",
+		c = getopt_long (argc, argv, "hVt:c:w:H:P:d:l:p:a:x:y:",
 		                 longopts, &option);
 
@@ -259,4 +305,16 @@
 				pguser = optarg;
 			break;
+		case 'x':
+			if (!is_intpos(optarg))
+				usage2 (_("Warning XID age must be a positive integer"), optarg);
+			else
+				warning_xid_age = atoi(optarg);		
+			break;
+		case 'y':
+			if (!is_intpos(optarg))
+				usage2 (_("Critical XID age must be a positive integer"), optarg);
+			else
+				critical_xid_age = atoi(optarg);		
+			break;
 		case 'p':     /* authentication password */
 		case 'a':
@@ -403,11 +461,17 @@
 
 	printf (_("\
-  -d, --database=STRING\n\
+ -d, --database=STRING\n\
     Database to check (default: %s)\n\
-  -l, --logname = STRING\n\
+ -l, --logname = STRING\n\
     Login name of user\n\
-  -p, --password = STRING\n\
+ -p, --password = STRING\n\
     Password (BIG SECURITY ISSUE)\n"), DEFAULT_DB);
 
+	printf (_("\
+ -x, --warnxid=INTEGER\n\
+    Minimum XID age to generate a warning (default: %d)\n\
+ -y, --critxid=INTEGER\n\
+    Minimum XID age to generate a critical (default: %d)\n"), DEFAULT_WARNING_XID_AGE, DEFAULT_CRITICAL_XID_AGE);
+
 	printf (_(UT_WARN_CRIT));
 
@@ -442,4 +506,5 @@
 	printf ("\
 Usage: %s [-H <host>] [-P <port>] [-c <critical time>] [-w <warning time>]\n\
-                  [-t <timeout>] [-d <database>] [-l <logname>] [-p <password>]\n", progname);
+                  [-t <timeout>] [-d <database>] [-l <logname>] [-p <password>]\n\
+		  [-x <warning_xid_age>] [-y <critical_xid_age>]\n", progname);
 }