51bd1a
commit 3b78790b2575daf0e8b3c2822a7e160273df20bd
51bd1a
Author: Tomas Korbar <tkorbar@redhat.com>
51bd1a
Date:   Tue May 19 08:35:29 2020 +0200
51bd1a
51bd1a
    Include ssl errors in the stats
51bd1a
51bd1a
diff --git a/doc/protocol.txt b/doc/protocol.txt
51bd1a
index abe70b2..55479b7 100644
51bd1a
--- a/doc/protocol.txt
51bd1a
+++ b/doc/protocol.txt
51bd1a
@@ -1509,6 +1509,23 @@ The value of the "state" stat may be one of the following:
51bd1a
 |                | sending back multiple lines of response data).            |
51bd1a
 |----------------+-----------------------------------------------------------|
51bd1a
 
51bd1a
+TLS statistics
51bd1a
+--------------
51bd1a
+
51bd1a
+TLS is a compile-time opt-in feature available in versions 1.5.13 and later.
51bd1a
+When compiled with TLS support and TLS termination is enabled at runtime, the
51bd1a
+following additional statistics are available via the "stats" command.
51bd1a
+
51bd1a
+|--------------------------------+----------+--------------------------------|
51bd1a
+| Name                           | Type     | Meaning                        |
51bd1a
+|--------------------------------+----------+--------------------------------|
51bd1a
+| ssl_handshake_errors           | 64u      | Number of times the server has |
51bd1a
+|                                |          | encountered an OpenSSL error   |
51bd1a
+|                                |          | during handshake (SSL_accept). |
51bd1a
+| time_since_server_cert_refresh | 32u      | Number of seconds that have    |
51bd1a
+|                                |          | elapsed since the last time    |
51bd1a
+|                                |          | certs were reloaded from disk. |
51bd1a
+|--------------------------------+----------+--------------------------------|
51bd1a
 
51bd1a
 
51bd1a
 Other commands
51bd1a
diff --git a/memcached.c b/memcached.c
51bd1a
index d81a71f..d769b4a 100644
51bd1a
--- a/memcached.c
51bd1a
+++ b/memcached.c
51bd1a
@@ -3428,6 +3428,7 @@ static void server_stats(ADD_STAT add_stats, conn *c) {
51bd1a
 #endif
51bd1a
 #ifdef TLS
51bd1a
     if (settings.ssl_enabled) {
51bd1a
+        APPEND_STAT("ssl_handshake_errors", "%llu", (unsigned long long)stats.ssl_handshake_errors);
51bd1a
         APPEND_STAT("time_since_server_cert_refresh", "%u", now - settings.ssl_last_cert_refresh_time);
51bd1a
     }
51bd1a
 #endif
51bd1a
@@ -6779,6 +6780,9 @@ static void drive_machine(conn *c) {
51bd1a
                             }
51bd1a
                             SSL_free(ssl);
51bd1a
                             close(sfd);
51bd1a
+                            STATS_LOCK();
51bd1a
+                            stats.ssl_handshake_errors++;
51bd1a
+                            STATS_UNLOCK();
51bd1a
                             break;
51bd1a
                         }
51bd1a
                     }
51bd1a
diff --git a/memcached.h b/memcached.h
51bd1a
index 795ea8f..6b1fe4a 100644
51bd1a
--- a/memcached.h
51bd1a
+++ b/memcached.h
51bd1a
@@ -357,6 +357,9 @@ struct stats {
51bd1a
     uint64_t      extstore_compact_lost; /* items lost because they were locked */
51bd1a
     uint64_t      extstore_compact_rescues; /* items re-written during compaction */
51bd1a
     uint64_t      extstore_compact_skipped; /* unhit items skipped during compaction */
51bd1a
+#endif
51bd1a
+#ifdef TLS
51bd1a
+    uint64_t      ssl_handshake_errors; /* TLS failures at accept/handshake time */
51bd1a
 #endif
51bd1a
     struct timeval maxconns_entered;  /* last time maxconns entered */
51bd1a
 };
51bd1a
diff --git a/t/stats.t b/t/stats.t
51bd1a
index 028a60a..f1dcd54 100755
51bd1a
--- a/t/stats.t
51bd1a
+++ b/t/stats.t
51bd1a
@@ -26,7 +26,7 @@ my $stats = mem_stats($sock);
51bd1a
 # Test number of keys
51bd1a
 if (MemcachedTest::enabled_tls_testing()) {
51bd1a
     # when TLS is enabled, stats contains time_since_server_cert_refresh
51bd1a
-    is(scalar(keys(%$stats)), 72, "expected count of stats values");
51bd1a
+    is(scalar(keys(%$stats)), 73, "expected count of stats values");
51bd1a
 } else {
51bd1a
     is(scalar(keys(%$stats)), 71, "expected count of stats values");
51bd1a
 }