Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added TTFB to jetmon #9

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/httpcheck.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ var HttpChecker = {
if ( pointerCurrentMax > arrCheck.length )
pointerCurrentMax = arrCheck.length;
for ( ; pointer < pointerCurrentMax ; pointer++ ) {
logger.debug('checkServers', arrCheck[ pointer ]);
darssen marked this conversation as resolved.
Show resolved Hide resolved
_watcher.http_check( arrCheck[ pointer ].monitor_url, DEFAULT_HTTP_PORT, pointer, HttpChecker.processResultsCallback );
}
}
Expand All @@ -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 ) ) ) )
Expand All @@ -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
Expand Down
17 changes: 6 additions & 11 deletions src/http_checker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
#include "http_checker.h"
#include <cstring>

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;
Expand All @@ -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<microseconds>(high_resolution_clock::now() - m_tstart).count();
}

void HTTP_Checker::check( string p_host_name, int p_port ) {
Expand Down Expand Up @@ -215,6 +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<microseconds>(high_resolution_clock::now() - m_tstart).count();
if ( m_is_ssl )
received = SSL_read( m_ssl, m_buf, MAX_TCP_BUFFER - 1 );
else
Expand Down
10 changes: 7 additions & 3 deletions src/http_checker.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
#include <sys/epoll.h>
#include <sys/types.h>
#include <unistd.h>
#include <chrono>



#include <netinet/tcp.h>
#include <netinet/in_systm.h>
Expand Down Expand Up @@ -56,7 +59,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:
Expand All @@ -66,11 +70,11 @@ 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;
int m_ttfb;

SSL_CTX *m_ctx;
SSL *m_ssl;
Expand Down
8 changes: 5 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <node.h>
#include <uv.h>
#include <chrono>

using namespace v8;
using namespace node;
Expand All @@ -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<HTTP_Check_Baton*>(req->data);
Local<Value> argv[3] = { Number::New( isolate, baton->server_id ),
Local<Value> 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<Function> cb_func = Local<Function>::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;
Expand Down Expand Up @@ -73,7 +75,7 @@ void http_check( const FunctionCallbackInfo<Value>& 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;
Expand Down