From c387586d72276c40b79ad92191dcc3fe80ebc539 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Henrik=20Grubbstr=C3=B6m=20=28Grubba=29?=
 <grubba@grubba.org>
Date: Thu, 16 Nov 2017 11:13:46 +0100
Subject: [PATCH] Tools.Standalone.check_http: Add support for option --expect.

Support matching against the response status line.
---
 .../Standalone.pmod/check_http.pike           | 28 +++++++++++++++++++
 1 file changed, 28 insertions(+)

diff --git a/lib/modules/Tools.pmod/Standalone.pmod/check_http.pike b/lib/modules/Tools.pmod/Standalone.pmod/check_http.pike
index b406e69a45..fe87cf1d1e 100644
--- a/lib/modules/Tools.pmod/Standalone.pmod/check_http.pike
+++ b/lib/modules/Tools.pmod/Standalone.pmod/check_http.pike
@@ -14,6 +14,7 @@ constant description = "Check HTTP/HTTPS connectivity to a host.";
 constant options = ({
   ({ "help", Getopt.NO_ARG, ({ "-h", "--help" }) }),
   ({ "version", Getopt.NO_ARG, ({ "-V", "--version" }) }),
+  ({ "expect", Getopt.HAS_ARG, ({ "-e", "--expect" }) }),
   ({ "min_ssl", Getopt.HAS_ARG, ({ "-S", "--ssl" }) }),
   ({ "min_ttl", Getopt.HAS_ARG, ({ "-C", "--certificate" }) }),
   ({ "method", Getopt.HAS_ARG, ({ "-j", "--method" }) }),
@@ -32,6 +33,8 @@ int start_time;
 
 int cert_min_ttl = -1;	// Disabled by default.
 
+array(string) expect = ({});
+
 void display_version()
 {
   Stdio.stdout.write("Check HTTP/Pike v%d.%d.%d\n",
@@ -119,6 +122,28 @@ void request_ok(Protocols.HTTP.Query q)
     }
   }
 
+  if (sizeof(expect)) {
+    // Re-create the status line, and match against it.
+    string status_line =
+      sprintf("%s %03d %s", q->protocol, q->status, q->status_desc);
+    foreach(expect, string match) {
+      if (has_value(status_line, match)) {
+	Stdio.stdout.write("OK: Success. | %s\n", data);
+	exit(RET_OK);
+      }
+    }
+
+    if ((q->status > 399) && (q->status < 500)) {
+      Stdio.stdout.write("WARNING: Bad status code: %s(%d). | %s\n",
+			 q->status_desc, q->status, data);
+      exit(RET_WARNING);
+    }
+
+    Stdio.stdout.write("FAIL: Bad status code: %s(%d). | %s\n",
+		       q->status_desc, q->status, data);
+    exit(RET_CRITICAL);
+  }
+
   if ((q->status > 499) || (q->status < 100)) {
     Stdio.stdout.write("FAIL: Bad status code: %s(%d). | %s\n",
 		       q->status_desc, q->status, data);
@@ -151,6 +176,9 @@ int main(int argc, array(string) argv)
     case "version":
       display_version();
       exit(0);
+    case "expect":
+      expect += opt[1]/",";
+      break;
     case "min_ssl":
       url->scheme = "https";
       switch(lower_case(opt[1])) {
-- 
GitLab