From 1c822b9263ea7e74abbd5048d516fea743be8b07 Mon Sep 17 00:00:00 2001 From: Irfan Ghaffar Date: Wed, 7 Nov 2018 15:03:56 +0500 Subject: [PATCH] OMG-4586 - Added support for production server dashboard --- .../101_db_4586_register_prod_servers.sql | 8 +- environments/development.yml | 2 +- lib/ProdDashboard.pm | 2 + lib/daemon/daemon_config.pl | 3 + lib/daemon/otp_dashboard.pl | 37 ++++++++ lib/homepage/homepage_controller.pm | 3 - lib/production/dao/production_db.pm | 85 ++----------------- lib/production/production_controller.pm | 66 ++------------ lib/production/production_helper.pm | 61 +------------ 9 files changed, 62 insertions(+), 205 deletions(-) create mode 100644 lib/daemon/daemon_config.pl create mode 100644 lib/daemon/otp_dashboard.pl diff --git a/dbscripts/s1/deploy/101_db_4586_register_prod_servers.sql b/dbscripts/s1/deploy/101_db_4586_register_prod_servers.sql index 1fb7681..6ce87ab 100644 --- a/dbscripts/s1/deploy/101_db_4586_register_prod_servers.sql +++ b/dbscripts/s1/deploy/101_db_4586_register_prod_servers.sql @@ -5,17 +5,17 @@ * Jira Ticket : https://oliveruk.atlassian.net/browse/OMG-4586 *********************************************************************************************/ --- Function: register_prod_servers(character varying, integer, integer) +-- Function: register_prod_servers(character varying, integer) -DROP FUNCTION IF EXISTS register_prod_servers(character varying, integer, integer); +DROP FUNCTION IF EXISTS register_prod_servers(character varying, integer); CREATE OR REPLACE FUNCTION register_prod_servers( server_num character varying, server_status integer) - RETURNS integer AS + RETURNS character varying AS $BODY$ DECLARE - lastid int; + lastid character varying; BEGIN -- insert a new record into prod_servers WITH upsert AS (UPDATE "public"."prod_servers" diff --git a/environments/development.yml b/environments/development.yml index d370f29..fab2371 100644 --- a/environments/development.yml +++ b/environments/development.yml @@ -38,7 +38,7 @@ plugins: AutoCommit: 1 log_queries: 1 -domain: 'http://192.168.154.138:5000/' +domain: 'http://192.168.10.66:5000/' admin_email: "devops@oliver.agency" sender_email: "devops@oliver.agency" \ No newline at end of file diff --git a/lib/ProdDashboard.pm b/lib/ProdDashboard.pm index 740ab04..cf9223f 100644 --- a/lib/ProdDashboard.pm +++ b/lib/ProdDashboard.pm @@ -15,5 +15,7 @@ get '/' => \&homepage_controller::showHomepage; ########## API ROUTES ########################################### post '/api/registerServer' => \&production_controller::registerServers; +post '/api/updateServer' => \&production_controller::updateServers; +get '/api/ragupdate' => \&production_controller::ragUpdate; true; diff --git a/lib/daemon/daemon_config.pl b/lib/daemon/daemon_config.pl new file mode 100644 index 0000000..c980f5a --- /dev/null +++ b/lib/daemon/daemon_config.pl @@ -0,0 +1,3 @@ +#!/usr/bin/perl + +destination_ip => 'http://192.168.10.66:5000', \ No newline at end of file diff --git a/lib/daemon/otp_dashboard.pl b/lib/daemon/otp_dashboard.pl new file mode 100644 index 0000000..18237ee --- /dev/null +++ b/lib/daemon/otp_dashboard.pl @@ -0,0 +1,37 @@ +#!/usr/bin/perl + +use Dancer2; +use strict; +use LWP::UserAgent; +use Data::Dumper; +use JSON qw(decode_json encode_json); + +my %daemon_config; + +if ( -e 'daemon_config.pl'){ + %daemon_config = do 'daemon_config.pl'; +} + +sub main{ + my $dashboard_url; + + if(%daemon_config) { + $dashboard_url = $daemon_config{destination_ip}; + }else{ + $dashboard_url = 'localhost'; + } + + my $http_client = LWP::UserAgent->new; + my $http_response = $http_client->get("$dashboard_url/api/ragupdate"); + + if ($http_response->is_success) { + my $response = decode_json($http_response->decoded_content) ; + if(defined $response->{error}){ + print Dumper $response->{error}->{message}; + } + } +} + +main(); + +1; diff --git a/lib/homepage/homepage_controller.pm b/lib/homepage/homepage_controller.pm index 2f96c36..426d289 100644 --- a/lib/homepage/homepage_controller.pm +++ b/lib/homepage/homepage_controller.pm @@ -16,10 +16,7 @@ use POSIX (); sub showHomepage { my $error; try { - print "\n\n Before server list print \n\n"; my $servers_list = production_helper::getAllProdServers(); - print "\n\n Before server list print \n\n"; - print Dumper $servers_list; my $local_domain = config->{domain}; return template 'homepage/homepage', { diff --git a/lib/production/dao/production_db.pm b/lib/production/dao/production_db.pm index 61c8fc2..0dae79a 100644 --- a/lib/production/dao/production_db.pm +++ b/lib/production/dao/production_db.pm @@ -14,7 +14,7 @@ sub registerServer { my $ser_id; try { - $sth = database->prepare("select * from register_prod_server (?, ?)"); + $sth = database->prepare("select * from register_prod_servers (?, ?)"); $sth->execute( $server_id, $status ); $ser_id = $sth->fetchrow_array; @@ -33,7 +33,7 @@ sub updateServer { my $ser_id; try { - $sth = database->prepare("select * from register_prod_server (?, ?)"); + $sth = database->prepare("select * from register_prod_servers (?, ?)"); $sth->execute( $server_id, $status ); $ser_id = $sth->fetchrow_array; @@ -63,14 +63,14 @@ sub getAllProdServers{ } sub addLogEntry{ - my ($hub_number, $activity, $message, $channel_id, $file_name, $file_size) = @_; + my ($server_num, $activity, $message) = @_; my $sth; my $log_record_id; try { - $sth = database->prepare("select * from create_otp_log_entry (?, ?, ?, ?, ?, ?)"); - $sth->execute( $hub_number, $activity, $message, $channel_id, $file_name, $file_size ); + $sth = database->prepare("select * from create_prod_log_entry (?, ?, ?)"); + $sth->execute( $server_num, $activity, $message ); $log_record_id = $sth->fetchrow_array; @@ -81,80 +81,5 @@ sub addLogEntry{ return $log_record_id; } -# Deletes all channels for single HUB ID passed -sub deleteAllChannels{ - my $hub_id = shift; - - my $sth; - my $node_id; - - try { - $sth = database->prepare("select * from delete_node_channels (?)"); - $sth->execute( $hub_id ); - - } catch { - errors::dbError->from_handle($sth, 'deleteAllChannels')->throw(); - } - - return; -} - -# Adds a channel record in otp_channels table -sub addNodeChannel{ - my ($hub_id, $channel_id, $destination_id, $RAG_status, $channel_name) = @_; - - my $sth; - my $new_channel_id; - - try { - $sth = database->prepare("select * from add_node_channel (?, ?, ?, ?, ?)"); - $sth->execute( $hub_id, $channel_id, $destination_id, $RAG_status, $channel_name ); - - $new_channel_id = $sth->fetchrow_array; - - } catch { - errors::dbError->from_handle($sth, 'addNodeConfig')->throw(); - } - - return $new_channel_id; -} - -# gets list of all channels for passed hub id -sub getAllChannels{ - my $hub_id = shift; - - my $sth; - my $channels_list; - - try { - $sth = database->prepare("select * from get_all_otp_channels ( ? )"); - $sth->execute( $hub_id ); - - $channels_list = $sth->fetchall_arrayref({}); - - } catch { - errors::dbError->from_handle($sth, 'getAllChannels')->throw(); - } - - return $channels_list; -} - -# update file_name field in channels table with passed file_name -# file name is only populated if file is in progress (being transferred) for that channel -# otherwise field is kept empty -sub updateChannelTransferActivity{ - my($hub_id, $channel_id, $file_name) = @_; - my $sth; - - try { - $sth = database->prepare("select * from channel_update_filename (?, ?, ?)"); - $sth->execute( $hub_id, $channel_id, $file_name ); - - } catch { - errors::dbError->from_handle($sth, 'updateChannelTransferActivity')->throw(); - } - - return; -} 1; \ No newline at end of file diff --git a/lib/production/production_controller.pm b/lib/production/production_controller.pm index 163051e..8e5fb0c 100644 --- a/lib/production/production_controller.pm +++ b/lib/production/production_controller.pm @@ -32,7 +32,7 @@ sub registerServers { }; } -sub updateServers{ +sub updateServers { my $error; try { @@ -43,7 +43,7 @@ sub updateServers{ my $db_status = $AMBER_COLOR; # If status not received, set to AMBER initially. if(defined $status){ - if($status eq "OK"){ + if($status == 1){ $db_status = $GREEN_COLOR; }else{ $db_status = $RED_COLOR; @@ -62,12 +62,11 @@ sub updateServers{ }; } -sub ragUpdate{ +sub ragUpdate { my $error; try { my $servers_list = production_helper::getAllProdServers(); - my $current_time = time(); foreach my $server (@$servers_list){ @@ -75,8 +74,6 @@ sub ragUpdate{ serverUpdate( $server, $current_time ); } - production_helper::checkStateChange( $nodes_list ); - return ajax_data_response( message => 'Successful.' ); @@ -86,7 +83,7 @@ sub ragUpdate{ } # Update server and set colour as per time passed -sub serverUpdate{ +sub serverUpdate { my ($server, $current_time) = @_; my $amber_threshold = 3*60; # 3 mins @@ -96,59 +93,14 @@ sub serverUpdate{ if($time_diff_in_sec >= $amber_threshold && $time_diff_in_sec < $error_threshold){ #update Amber status - production_helper::updateServer($server->{id}, $AMBER_COLOR); - production_logs_helper::addLogEntry($server->{id}, 'SERVER_TIMEOUT', 'Timeout Warning'); + production_helper::updateServer($server->{server_number}, $AMBER_COLOR); + production_logs_helper::addLogEntry($server->{server_number}, 'SERVER_TIMEOUT', 'Timeout Warning'); }elsif($time_diff_in_sec > $error_threshold){ #update Error status - production_helper::updateServer($server->{id}, $RED_COLOR); - production_logs_helper::addLogEntry($server->{id}, 'SERVER_TIMEOUT', 'Timeout Critical'); + production_helper::updateServer($server->{server_number}, $RED_COLOR); + production_logs_helper::addLogEntry($server->{server_number}, 'SERVER_TIMEOUT', 'Timeout Critical'); } } -sub saveConfig{ - my $error; - try { - my $servers_list = config->{servers_list}; - - foreach my $server_id in (@$servers_list){ - # Register servers in the table and mark them down initially - my $registration_id = production_helper::registerServer($server_id, $RED_COLOR); - } - - return ajax_data_response( - message => 'Successful.' - ); - } catch($error){ - return handleException( $error ); - }; -} - -# Update file transfer information in channels and log table -sub updateFileTransferStatus{ - my $error; - - try { - my $hub_id = param('hubid'); - my $channel_id = param('channelid'); - my $post_params = params(); - my $transfer_status = $post_params->{'transfer_status'}; - my $transfer_msg = $post_params->{'transfer_msg'}; - my $file_name = $post_params->{'file_name'}; - my $file_size = $post_params->{'file_size'}; - - # add file transfer info in log. Size is in MBs - otp_logs_helper::addLogEntry($hub_id, $transfer_status, $transfer_msg, $channel_id, $file_name, $file_size); - - # update filename field in channels table - otp_helper::updateChannelTransferActivity($hub_id, $channel_id, $transfer_status, $file_name); - - return ajax_data_response( - message => 'Successful.' - ); - } catch($error){ - return handleException( $error ); - }; -} - -1; +1; \ No newline at end of file diff --git a/lib/production/production_helper.pm b/lib/production/production_helper.pm index f2f112f..70b64f5 100644 --- a/lib/production/production_helper.pm +++ b/lib/production/production_helper.pm @@ -33,64 +33,5 @@ sub updateServer { return $servers_id; } -# Check if status for any node/channel is changed from previous state, send emails -sub checkStateChange{ - my $nodes_list = shift; - foreach my $node (@$nodes_list){ - # send emails for hub status change - sendHubMasterEmails( $node ); - - # send emails for any status change in hub channels - sendHubChannelEmails( $node ); - } -} - -# send emails for hub status change -sub sendHubMasterEmails{ - my $node = shift; - my $subject = ''; - - if( $node->{status} != 0 && $node->{status} != $node->{last_status} ){ - # send email - if( $node->{status} == 3 ){ - $subject = "OTP Node $node->{hub_id}: $node->{hub_name} has gone RED"; - } - - if( $node->{status} == 1 ){ - $subject = "OTP Node $node->{hub_id}: $node->{hub_name} has reverted to GREEN"; - } - - if( $subject ne '' ){ - email_helper::sendMail(config->{sender_email}, config->{admin_email}, $subject, $subject); - } - } -} - -# send emails for any status change in hub channels -sub sendHubChannelEmails{ - my $node = shift; - my $subject = ''; - - my $node_channels_list = getAllChannels($node->{hub_id}); - - foreach my $channel (@$node_channels_list){ - if( $channel->{status} != 0 && $channel->{status} != $channel->{last_status} ){ - # send channel fail email - if( $channel->{status} == 3 ){ - $subject = "OTP Channel $channel->{channel_id} in Node $node->{hub_id}: $node->{hub_name} has gone RED"; - } - - if( $channel->{status} == 1 ){ - $subject = "OTP Channel $channel->{channel_id} in Node $node->{hub_id}: $node->{hub_name} has reverted to GREEN"; - } - - if( $subject ne '' ){ - email_helper::sendMail(config->{sender_email}, config->{admin_email}, $subject, $subject); - } - } - } -} - - -1; +1; \ No newline at end of file