From 05d068b55ad2bc4c5c3d00c3aa86909b00e18856 Mon Sep 17 00:00:00 2001 From: Juanma Rodriguez Escriche Date: Sat, 29 Oct 2022 00:01:08 +0200 Subject: [PATCH 1/4] Added TTFB to jetmon --- lib/httpcheck.js | 6 ++++-- src/http_checker.cpp | 18 +++++++----------- src/http_checker.h | 8 +++++--- src/main.cpp | 8 +++++--- 4 files changed, 21 insertions(+), 19 deletions(-) diff --git a/lib/httpcheck.js b/lib/httpcheck.js index 25e17af..640523b 100644 --- a/lib/httpcheck.js +++ b/lib/httpcheck.js @@ -70,6 +70,7 @@ var HttpChecker = { if ( pointerCurrentMax > arrCheck.length ) pointerCurrentMax = arrCheck.length; for ( ; pointer < pointerCurrentMax ; pointer++ ) { + logger.debug('checkServers', arrCheck[ pointer ]); _watcher.http_check( arrCheck[ pointer ].monitor_url, DEFAULT_HTTP_PORT, pointer, HttpChecker.processResultsCallback ); } } @@ -78,12 +79,12 @@ var HttpChecker = { } }, - processResultsCallback: function( serverArrayIndex, rtt, http_code ) { + processResultsCallback: function( serverArrayIndex, rtt, ttfb, http_code ) { var server = arrCheck[ serverArrayIndex ]; server.processed = true; server.lastCheck = new Date().valueOf(); // we use set the value to the milliseconds value - if ( rtt > 0 && 400 > http_code && 0 != http_code ) + if ( rtt > 0 && ttfb > 0 && 400 > http_code && 0 != http_code ) server.site_status = SITE_RUNNING; else if ( ( SITE_RUNNING == server.oldStatus ) || ( SITE_CONFIRMED_DOWN != server.oldStatus ) && ( new Date().valueOf() < ( server.last_status_change + ( config.get( 'TIME_BETWEEN_NOTICES_MIN' ) * MINUTES ) ) ) ) @@ -97,6 +98,7 @@ var HttpChecker = { resO.status = server.site_status; resO.rtt = Math.round( rtt / 1000 ); resO.code = http_code; + resO.ttfb = Math.round( ttfb / 1000 ); server.checks.push( resO ); // if site is down and it has not been confirmed diff --git a/src/http_checker.cpp b/src/http_checker.cpp index 36afb67..76594be 100644 --- a/src/http_checker.cpp +++ b/src/http_checker.cpp @@ -2,11 +2,13 @@ #include "http_checker.h" #include +using namespace std::chrono; using namespace std; + HTTP_Checker::HTTP_Checker() : m_sock( -1 ), m_host_name( "" ), m_host_dir( "" ), m_port( HTTP_DEFAULT_PORT ), m_is_ssl( false ), m_triptime( 0 ), m_response_code( 0 ), m_ctx( NULL ), m_ssl( NULL ), m_sbio( NULL ) { - gettimeofday( &m_tstart, &m_tzone ); + m_tstart = high_resolution_clock::now(); memset( m_buf, 0, MAX_TCP_BUFFER ); m_cutofftime = time( NULL ); m_cutofftime += NET_COMMS_TIMEOUT; @@ -16,16 +18,8 @@ HTTP_Checker::~HTTP_Checker() { this->disconnect(); } -time_t HTTP_Checker::get_rtt() { - struct timeval m_tend; - gettimeofday( &m_tend, &m_tzone ); - - if ( (m_tend.tv_usec -= m_tstart.tv_usec) < 0 ) { - m_tend.tv_sec--; - m_tend.tv_usec += 1000000; - } - m_tend.tv_sec -= m_tstart.tv_sec; - return m_tend.tv_sec * 1000000 + ( m_tend.tv_usec ); +int HTTP_Checker::get_rtt() { + return duration_cast(high_resolution_clock::now() - m_tstart).count(); } void HTTP_Checker::check( string p_host_name, int p_port ) { @@ -188,6 +182,7 @@ string HTTP_Checker::send_http_get() { strcpy( m_buf, s_tmp.c_str() ); if ( send_bytes( m_buf, s_tmp.length() ) ) { + m_tstart_ttfb = high_resolution_clock::now(); s_tmp = get_response(); } else { s_tmp = ""; @@ -215,6 +210,7 @@ string HTTP_Checker::get_response() { } while ( ( FD_ISSET( m_sock, &read_fds ) == 0) && ( m_cutofftime > time( NULL ) ) ); if ( FD_ISSET( m_sock, &read_fds) ) { + m_ttfb = duration_cast(high_resolution_clock::now() - m_tstart_ttfb).count(); if ( m_is_ssl ) received = SSL_read( m_ssl, m_buf, MAX_TCP_BUFFER - 1 ); else diff --git a/src/http_checker.h b/src/http_checker.h index c9e4213..4b9767b 100644 --- a/src/http_checker.h +++ b/src/http_checker.h @@ -56,7 +56,8 @@ class HTTP_Checker { ~HTTP_Checker(); void check( std::string p_host_name, int p_port = HTTP_DEFAULT_PORT ); - time_t get_rtt(); + int get_rtt(); + int get_ttfb() { return m_ttfb; }; int get_response_code() { return m_response_code; } private: @@ -66,11 +67,12 @@ class HTTP_Checker { std::string m_host_dir; int m_port; bool m_is_ssl; - struct timezone m_tzone; - struct timeval m_tstart; + std::chrono::_V2::system_clock::time_point m_tstart; time_t m_triptime; time_t m_cutofftime; int m_response_code; + std::chrono::_V2::system_clock::time_point m_tstart_ttfb; + int m_ttfb; SSL_CTX *m_ctx; SSL *m_ssl; diff --git a/src/main.cpp b/src/main.cpp index 088edba..fbe76e5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -9,6 +9,7 @@ #include #include +#include using namespace v8; using namespace node; @@ -28,12 +29,13 @@ static void http_check_async_fin( uv_work_t *req, int status ) { HandleScope scope( isolate ); HTTP_Check_Baton *baton = static_cast(req->data); - Local argv[3] = { Number::New( isolate, baton->server_id ), + Local argv[4] = { Number::New( isolate, baton->server_id ), Number::New( isolate, baton->http_checker->get_rtt() ), + Number::New( isolate, baton->http_checker->get_ttfb() ), Number::New( isolate, baton->http_checker->get_response_code() ) }; Local cb_func = Local::New( isolate, baton->callback ); - cb_func->Call( isolate->GetCurrentContext(), isolate->GetCurrentContext()->Global(), 3, argv ); + cb_func->Call( isolate->GetCurrentContext(), isolate->GetCurrentContext()->Global(), 4, argv ); baton->callback.Reset(); delete baton->http_checker; delete baton; @@ -73,7 +75,7 @@ void http_check( const FunctionCallbackInfo& args ) { String::NewFromUtf8( isolate, "You have not provided a callback function as the 4th parameter" ).ToLocalChecked() ) ); return; } - + HTTP_Check_Baton *baton = new HTTP_Check_Baton(); HTTP_Checker *checker = new HTTP_Checker(); baton->http_checker = checker; From 620cdc7b7d94bca8b87a867c5d4aba641188afec Mon Sep 17 00:00:00 2001 From: Juanma Rodriguez Escriche Date: Tue, 1 Nov 2022 02:04:28 +0100 Subject: [PATCH 2/4] Added chrono library to headers file. --- src/http_checker.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/http_checker.h b/src/http_checker.h index 4b9767b..61c6b0d 100644 --- a/src/http_checker.h +++ b/src/http_checker.h @@ -16,6 +16,9 @@ #include #include #include +#include + + #include #include From 8c845d526ac2335056cf321bb0927f34409c04dd Mon Sep 17 00:00:00 2001 From: Juanma Rodriguez Escriche Date: Mon, 14 Nov 2022 07:08:20 +0100 Subject: [PATCH 3/4] Changed ttfb calculation so that start time considers Connection negotiation. --- src/http_checker.cpp | 3 +-- src/http_checker.h | 1 - 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/src/http_checker.cpp b/src/http_checker.cpp index 76594be..8c77a78 100644 --- a/src/http_checker.cpp +++ b/src/http_checker.cpp @@ -182,7 +182,6 @@ string HTTP_Checker::send_http_get() { strcpy( m_buf, s_tmp.c_str() ); if ( send_bytes( m_buf, s_tmp.length() ) ) { - m_tstart_ttfb = high_resolution_clock::now(); s_tmp = get_response(); } else { s_tmp = ""; @@ -210,7 +209,7 @@ string HTTP_Checker::get_response() { } while ( ( FD_ISSET( m_sock, &read_fds ) == 0) && ( m_cutofftime > time( NULL ) ) ); if ( FD_ISSET( m_sock, &read_fds) ) { - m_ttfb = duration_cast(high_resolution_clock::now() - m_tstart_ttfb).count(); + m_ttfb = duration_cast(high_resolution_clock::now() - m_tstart).count(); if ( m_is_ssl ) received = SSL_read( m_ssl, m_buf, MAX_TCP_BUFFER - 1 ); else diff --git a/src/http_checker.h b/src/http_checker.h index 61c6b0d..d7f71a6 100644 --- a/src/http_checker.h +++ b/src/http_checker.h @@ -74,7 +74,6 @@ class HTTP_Checker { time_t m_triptime; time_t m_cutofftime; int m_response_code; - std::chrono::_V2::system_clock::time_point m_tstart_ttfb; int m_ttfb; SSL_CTX *m_ctx; From 3644420bef24d4e791a65eb238004d1afef70915 Mon Sep 17 00:00:00 2001 From: Juanma Rodriguez Escriche Date: Mon, 14 Nov 2022 10:16:41 +0100 Subject: [PATCH 4/4] Removed unnecessary debug --- lib/httpcheck.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/httpcheck.js b/lib/httpcheck.js index 9615bf7..b56bda6 100644 --- a/lib/httpcheck.js +++ b/lib/httpcheck.js @@ -70,7 +70,6 @@ var HttpChecker = { if ( pointerCurrentMax > arrCheck.length ) pointerCurrentMax = arrCheck.length; for ( ; pointer < pointerCurrentMax ; pointer++ ) { - logger.debug('checkServers', arrCheck[ pointer ]); _watcher.http_check( arrCheck[ pointer ].monitor_url, DEFAULT_HTTP_PORT, pointer, HttpChecker.processResultsCallback ); } }