Skip to content

Commit 955ca4c

Browse files
committed
2 parents 8d0c9ca + c139915 commit 955ca4c

2 files changed

Lines changed: 249 additions & 17 deletions

File tree

README.md

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,105 @@ You need to configure the parameters:
146146
### The results
147147
- For every interaction the results will be save on **c:\test\ResultsN.Log**, where **N** is the number of job execute.
148148

149+
# SQLConnectivityTestMultiDriver.ps1
150+
This application has been designed with a main idea: We usually have service requests when our customers, using different drivers, need to know the connection latency, execution time spent or validate the connection.
151+
For this reason, I developed this small PowerShell script  (based on initiative of AzureSQLConnectivityChecker in multiple languages) which will basically connect to the database using 4 drivers/providers and test this.
152+
153+
## Today, we started with SQLConnectivityTestMultiDriver.ps1 PowerShell Script and you are going to be able to:
154+
155+
- **Connect using the drivers**: Net SqlClient, ODBC 17, OLEDB and MSOLEDBSQL.
156+
- **In every interaction**, you could obtain:
157+
++ Connectivity latency.
158+
++ Time invested in the execution of each query.
159+
++ Average time spent on connectivity and per query.
160+
++ How many ports the customer application has (1433,11000-12000 and others).
161+
++ Check if the DNS is resolving correct IP. 
162+
163+
## Here are the main details:
164+
165+
- **Parameters**, if you don't specify any of them, PowerShell Script will ask the value and check it.
166+
167+
+ **$DatabaseServer**, will be the server name to connect. 
168+
+ **$Database**, will be the database name to connect.
169+
+ **$Username**, user name to connect
170+
+ **$passwordSecure**, password. 
171+
+ **$NumberExecutions**, how many executions that you want to run. 
172+
+ **$LogFile**, will be the log file that all operations will be saved. If this file is not specify, the application will ask the location. 
173+
+ **$Driver** to use, the values are: 
174+
++ SQLCLIENT for .NET SQL Client.
175+
++ ODBC
176+
++ MSOLEDBSQL
177+
++ SQLOLEDB
178+
+ $InputFile**, will be the different TSQL that you want to use. This file needs to contain per line the TSQL to execute. If you don't specify it, we are going to use SELECT 1. For example,:
179+
SELECT 1
180+
SELECT count(Id) FROM PerformanceVarcharnVarchar Where TextToSearch = N'Test Search ' + CONVERT(nvarchar(200),RAND()*(2000000-10000)+10000) OPTION (MAXDOP 1)
181+
182+
## Outcome:
183+
In the $LogFile you are going to have all the results and summary about connection and execution spent, for example, 
184+
185+
2022-08-11 03:33:15 Driver: SQLOLEDB
186+
2022-08-11 03:33:15 Connecting to the database: servername.database.windows.net - DB: dbname...Attempt #1 of 5 - IP:52.138.224.1
187+
2022-08-11 03:33:16 Connected to the database in (ms):324.1887 - Provider:SQLOLEDB -- HostName: Server Version:12.00.0312
188+
2022-08-11 03:33:16 Total Connections Failed : 0
189+
2022-08-11 03:33:16 Total Connections Success: 1
190+
2022-08-11 03:33:16 Total Connections ms : 324
191+
2022-08-11 03:33:16 Ports Count : 1433 0 (0) 11xx: (0) Others:(215)
192+
2022-08-11 03:33:16 Ports Status:
193+
2022-08-11 03:33:16 ---->Established (207)
194+
2022-08-11 03:33:16 ---->TimeWait (5)
195+
2022-08-11 03:33:16 ---->SynSent (2)
196+
2022-08-11 03:33:16 -------------------------------------------------------------------------------------
197+
2022-08-11 03:33:16 Query : SELECT 1
198+
2022-08-11 03:33:16 Iteration : 1 Query 1 / 2
199+
2022-08-11 03:33:16 Time required (ms) : 41,416
200+
2022-08-11 03:33:16 Query Commands Failed : 0
201+
2022-08-11 03:33:16 Query Commands Success: 1
202+
2022-08-11 03:33:16 Query Commands ms : 41
203+
2022-08-11 03:33:16 Total Commands Failed : 0
204+
2022-08-11 03:33:16 Total Commands Success: 1
205+
2022-08-11 03:33:16 TotalCommands ms : 41
206+
2022-08-11 03:33:16 -------------------------------------------------------------------------------------
207+
2022-08-11 03:33:16 Query : SELECT count(Id) FROM PerformanceVarcharnVarchar Where TextToSearch = N'Test Search ' + CONVERT(nvarchar(200),RAND()*(2000000-10000)+10000) OPTION (MAXDOP 1)
208+
2022-08-11 03:33:16 Iteration : 1 Query 2 / 2
209+
2022-08-11 03:33:17 Time required (ms) : 473,7891
210+
2022-08-11 03:33:17 Query Commands Failed : 0
211+
2022-08-11 03:33:17 Query Commands Success: 1
212+
2022-08-11 03:33:17 Query Commands ms : 474
213+
2022-08-11 03:33:17 Total Commands Failed : 0
214+
2022-08-11 03:33:17 Total Commands Success: 2
215+
2022-08-11 03:33:17 TotalCommands ms : 257,5
216+
2022-08-11 03:33:17 ---------------------------------------------------------------------------------------------------------------
217+
2022-08-11 03:33:17 Connecting to the database: servername.database.windows.net - DB: databasena...Attempt #1 of 5 - IP:52.138.224.1
218+
2022-08-11 03:33:17 Connected to the database in (ms):0 - Provider:SQLOLEDB -- HostName: Server Version:12.00.0312
219+
2022-08-11 03:33:17 Total Connections Failed : 0
220+
2022-08-11 03:33:17 Total Connections Success: 2
221+
2022-08-11 03:33:17 Total Connections ms : 162
222+
2022-08-11 03:33:17 Ports Count : 1433 0 (0) 11xx: (0) Others:(215)
223+
2022-08-11 03:33:17 Ports Status:
224+
2022-08-11 03:33:17 ---->Established (207)
225+
2022-08-11 03:33:17 ---->TimeWait (5)
226+
2022-08-11 03:33:17 ---->SynSent (2)
227+
228+
2022-08-11 03:38:01 -------------------------------------- SUMMARY -----------------------------------------------------------------
229+
2022-08-11 03:38:01 Total Connections Failed :0
230+
2022-08-11 03:38:01 Total Connections Success:200
231+
2022-08-11 03:38:01 Total Connections Avg ms :2,515
232+
2022-08-11 03:38:01 Total Number Executions :200
233+
2022-08-11 03:38:01 Total Number Queries :2
234+
2022-08-11 03:38:01 Total Number Process :400
235+
2022-08-11 03:38:01 Total Commands Failed :0
236+
2022-08-11 03:38:01 Total Commands Success :400
237+
2022-08-11 03:38:01 Total Commands ms :120,0725
238+
2022-08-11 03:38:01 ------------------- Queries SummarySELECT count(Id) FROM PerformanceVarcharnVarchar Where TextToSearch = N'Test Search ' + CONVERT(nvarchar(200),RAND()*(2000000-10000)+10000) OPTION (MAXDOP 1)
239+
2022-08-11 03:38:01 Query # :SELECT 1
240+
2022-08-11 03:38:01 Total Commands Failed :0
241+
2022-08-11 03:38:01 Total Commands Success:200
242+
2022-08-11 03:38:01 Total Commands ms :35,57
243+
2022-08-11 03:38:01 Query # :SELECT count(Id) FROM PerformanceVarcharnVarchar Where TextToSearch = N'Test Search ' + CONVERT(nvarchar(200),RAND()*(2000000-10000)+10000) OPTION (MAXDOP 1)
244+
2022-08-11 03:38:01 Total Commands Failed :0
245+
2022-08-11 03:38:01 Total Commands Success:200
246+
2022-08-11 03:38:01 Total Commands ms :204,575
247+
2022-08-11 03:38:01 Review: https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/sql/provider-statistics-for-sql-server
248+
2022-08-11 03:38:01 -------------------------------------- SUMMARY -----------------------------------------------------------------
249+
149250
Enjoy!

connectivityDiag.sh

Lines changed: 148 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
#title :connectivityDiag.sh
44
#description :This script will launch a continuous connectivity test to databases like SQL, PostgreSQL, MySQL and MariaDB
55
#authors :Francisco Javier Pardillo & Jose Manuel Jurado
6-
#date :20201112
7-
#version :0.1
6+
#date :20220818
7+
#version :0.2
88
#usage :./connectivityDiag.sh
9-
#notes :Install "iproute","hostname" and some of the database clients ["sqlcmd","mysql","psql"] to use this script.
9+
#notes :Install "iproute","hostname","" and some of the database clients ["sqlcmd","mysql","psql"] to use this script.
1010
#################################################################################################################################
1111
LY='\033[1;33m' # Light Yellow
1212
LR='\033[1;31m' # Light Red
@@ -57,6 +57,8 @@ echo "Please insert NOTES of this connectivityDiag session, EJ.- TEST1 (Default:
5757
read NOTES
5858
echo "Would you like to DEBUG all commands? (Y/N) (Default=N)"
5959
read DEBUG
60+
echo "Would you like to install missing tools if necessary? (Y/N) (Default=N)"
61+
read INSTALL
6062

6163
DIR=`pwd`;
6264
DIR_LOGS="connectivityDiag_logs";
@@ -78,18 +80,24 @@ if [ "$TEST_QUERY""a" = "a" ]; then TEST_QUERY="SELECT 1"; fi
7880
if [ "$TEST_WAIT""a" = "a" ]; then TEST_WAIT=1; fi
7981
if [ "$TEST_LOOPS""a" = "a" ]; then TEST_LOOPS=86400; fi
8082
if [ "$DEBUG""a" = "a" ]; then DEBUG="N"; fi
83+
if [ "$INSTALL""a" = "a" ]; then INSTALL="N"; fi
8184

8285
# We need an output file per test
8386
logConnectivity=$DIR_LOGS/connectivity.$NOTES.$date_time.csv
8487
logDns=$DIR_LOGS/dns.$NOTES.$date_time.csv
85-
logNetstat=$DIR_LOGS/dns.$NOTES.$date_time.csv
88+
logNetstat=$DIR_LOGS/netstat.$NOTES.$date_time.csv
8689
logDebug=$DIR_LOGS/debug.$NOTES.$date_time.log
8790

8891
# DB connections
92+
export PGPASSWORD=$PASSWORD
8993
db_postgresql="psql -h $SERVER -U $USERNAME -w$PASSWORD -d $DB -t";
9094
db_mysql="mysql -N -u $USERNAME -h $SERVER -p$PASSWORD -D $DB";
9195
db_sql="/opt/mssql-tools/bin/sqlcmd -S $SERVER -d $DB -U $USERNAME -P$PASSWORD -h-1";
9296

97+
# Other commands
98+
dns="dig $SERVER +short"
99+
#netstat="netstat -nap|grep $PORT"
100+
93101
CHK17="$LG""CHECK.017:Existence of directory $DIR$NC";
94102
CHK18="$LG""CHECK.018:Existence of directory $DIR_LOGS$NC";
95103

@@ -122,9 +130,62 @@ if [ $exit_condition = "Y" ]; then echo -e "\n################################\n
122130

123131
# DEFINE AUXILIARY FUNCTIONS
124132

133+
function initialSetup(){
134+
out=$(which dig)
135+
res=$?
136+
if [ $res == 1 ];
137+
then
138+
139+
[[ -x "/usr/bin/apt-get" ]] && apt install -y dnsutils
140+
[[ -x "/usr/bin/yum" ]] && yum install -y bind-utils
141+
fi
142+
143+
out=$(which netstat)
144+
res=$?
145+
if [ $res == 1 ];
146+
then
147+
148+
[[ -x "/usr/bin/apt-get" ]] && apt install -y net-tools
149+
[[ -x "/usr/bin/yum" ]] && yum install -y net-tools
150+
fi
151+
152+
case $SERVERTYPE in
153+
"sql")
154+
out=$(which /opt/mssql-tools/bin/sqlcmd)
155+
res=$?
156+
if [ $res == 1 ];
157+
then
158+
echo -e $NC
159+
echo "Missing sqlcmd command to test connectivity for SQL databases, please refer to: https://docs.microsoft.com/en-us/sql/linux/sql-server-linux-setup-tools?view=sql-server-ver16"
160+
exit 0
161+
fi
162+
;;
163+
"postgresql")
164+
out=$(which psql)
165+
res=$?
166+
if [ $res == 1 ];
167+
then
168+
[[ -x "/usr/bin/apt-get" ]] && apt install -y postgresql-client
169+
[[ -x "/usr/bin/yum" ]] && yum install -y postgresql-client
170+
fi
171+
;;
172+
"mysql")
173+
out=$(which mysql)
174+
res=$?
175+
if [ $res == 1 ];
176+
then
177+
[[ -x "/usr/bin/apt-get" ]] && apt install -y default-mysql-client
178+
[[ -x "/usr/bin/yum" ]] && yum install -y mysql
179+
fi
180+
;;
181+
*)
182+
#Nothing
183+
;;
184+
esac
185+
}
125186
function logDebug(){
126-
# $1 should be the starttime, $2 should be the output to be saved, $3 should be the endtime, $4 type of test performed
127-
duration=$(($3-$1))
187+
# $1 should be the starttime, $2 should be the output to be saved, $3 should be the endtime, $4 type of test performed, $5 duration
188+
duration=$5
128189
if [ $DEBUG == "Y" ];
129190
then
130191
echo "==============================" >> $logDebug
@@ -142,48 +203,63 @@ function logDebug(){
142203
}
143204

144205
function chkConnMySQL(){
145-
start=`date +%Y%m%d%H%M%S%N|cut -b1-17`
206+
# date+Nanoseconds+secondsSince1970+Nanoseconds
207+
startB=`date +%Y%m%d%H%M%S%N%s%N`
208+
startC=`echo ${startB}|cut -b24-36`
209+
start=`echo ${startB}|cut -b1-17`
146210
out=$($db_mysql -e "$TEST_QUERY" 2>&1)
147211
res=$?
148212
# We need to take output in res and parse with outfin
149213
outfin=`echo $out|sed s/,/#/g|sed s/'mysql: \[Warning\] Using a password on the command line interface can be insecure\. '//g`
150-
end=`date +%Y%m%d%H%M%S%N|cut -b1-17`
151-
duration=$((end-start))
214+
endB=`date +%Y%m%d%H%M%S%N%s%N`
215+
endC=`echo ${endB}|cut -b24-36`
216+
end=`echo ${endB}|cut -b1-17`
217+
duration=$((endC-startC))
152218
if [ $res == 0 ];
153219
then
154220
result='OK';
155221
else
156222
result='ERR';
157223
fi
158-
logDebug $start "$out" $end "MYSQL CONNECTIVITY"
224+
logDebug $start "$out" $end "MYSQL CONNECTIVITY" $duration
159225
echo $NOTES,$HOSTIPS,sqlcmd,$HOSTNAME,$start,$duration,$result,$outfin
160226
}
161227
function chkConnPostgreSQL(){
162-
start=`date +%Y%m%d%H%M%S%N|cut -b1-17`
228+
# date+Nanoseconds+secondsSince1970+Nanoseconds
229+
startB=`date +%Y%m%d%H%M%S%N%s%N`
230+
startC=`echo ${startB}|cut -b24-36`
231+
start=`echo ${startB}|cut -b1-17`
163232
out=$($db_postgresql -c "$TEST_QUERY" 2>&1)
164233
res=$?
165234
# We need to take output in res and parse with outfin
166235
outfin=`echo $out|sed s/,/#/g|sed s/'REMOVABLESTRING'//g`
167-
end=`date +%Y%m%d%H%M%S%N|cut -b1-17`
168-
duration=$((end-start))
236+
endB=`date +%Y%m%d%H%M%S%N%s%N`
237+
endC=`echo ${endB}|cut -b24-36`
238+
end=`echo ${endB}|cut -b1-17`
239+
duration=$((endC-startC))
169240
if [ $res == 0 ];
170241
then
171242
result='OK';
172243
else
173244
result='ERR';
174245
fi
175-
logDebug $start "$out" $end "POSTGRESQL CONNECTIVITY"
246+
logDebug $start "$out" $end "POSTGRESQL CONNECTIVITY" $duration
176247
echo $NOTES,$HOSTIPS,sqlcmd,$HOSTNAME,$start,$duration,$result,$outfin
177248
}
178249
function chkConnSQL(){
179-
start=`date +%Y%m%d%H%M%S%N|cut -b1-17`
250+
# date+Nanoseconds+secondsSince1970+Nanoseconds
251+
startB=`date +%Y%m%d%H%M%S%N%s%N`
252+
startC=`echo ${startB}|cut -b24-36`
253+
start=`echo ${startB}|cut -b1-17`
180254
echo "SET NOCOUNT ON;SELECT $start;$TEST_QUERY"
181255
out=$($db_sql -Q "SET NOCOUNT ON;SELECT $start;$TEST_QUERY" 2>&1)
182256
res=$?
183257
# We need to take output in res and parse with outfin
184258
outfin=`echo $out|sed s/,/\#/g|sed s/'(.*rows affected)'//g`
185-
end=`date +%Y%m%d%H%M%S%N|cut -b1-17`
186-
duration=$((end-start))
259+
endB=`date +%Y%m%d%H%M%S%N%s%N`
260+
endC=`echo ${endB}|cut -b24-36`
261+
end=`echo ${endB}|cut -b1-17`
262+
duration=$((endC-startC))
187263
if [ $res == 0 ];
188264
then
189265
result='OK';
@@ -194,10 +270,65 @@ function chkConnSQL(){
194270
echo $NOTES,$HOSTIPS,sqlcmd,$HOSTNAME,$start,$duration,$result,$outfin
195271
}
196272

273+
function chkDns(){
274+
# date+Nanoseconds+secondsSince1970+Nanoseconds
275+
startB=`date +%Y%m%d%H%M%S%N%s%N`
276+
startC=`echo ${startB}|cut -b24-36`
277+
start=`echo ${startB}|cut -b1-17`
278+
out=$($dns 2>&1)
279+
res=$?
280+
# We need to take output in res and parse with outfin
281+
outfin=`echo $out|sed s/,/#/g`
282+
endB=`date +%Y%m%d%H%M%S%N%s%N`
283+
endC=`echo ${endB}|cut -b24-36`
284+
end=`echo ${endB}|cut -b1-17`
285+
duration=$((endC-startC))
286+
if [ $res == 0 ];
287+
then
288+
result='OK';
289+
else
290+
result='ERR';
291+
fi
292+
logDebug $start "$out" $end "DNS CONNECTIVITY" $duration
293+
echo $NOTES,$HOSTIPS,dns,$HOSTNAME,$start,$duration,$result,$outfin
294+
}
295+
296+
function chkNetstat(){
297+
# date+Nanoseconds+secondsSince1970+Nanoseconds
298+
startB=`date +%Y%m%d%H%M%S%N%s%N`
299+
startC=`echo ${startB}|cut -b24-36`
300+
start=`echo ${startB}|cut -b1-17`
301+
out1="ESTABLISHED:"$(netstat -nap|grep $PORT|grep ESTABLISHED|wc -l)
302+
out2="#TIME_WAIT:"$(netstat -nap|grep $PORT|grep TIME_WAIT|wc -l)
303+
out3="#OTHERS:"$(netstat -nap|grep $PORT|grep -v TIME_WAIT|grep -v ESTABLISHED|wc -l)
304+
out=$out1$out2$out3
305+
res=$?
306+
# We need to take output in res and parse with outfin
307+
outfin=`echo $out|sed s/,/#/g`
308+
endB=`date +%Y%m%d%H%M%S%N%s%N`
309+
endC=`echo ${endB}|cut -b24-36`
310+
end=`echo ${endB}|cut -b1-17`
311+
duration=$((endC-startC))
312+
if [ $res == 0 ];
313+
then
314+
result='OK';
315+
else
316+
result='ERR';
317+
fi
318+
logDebug $start "$out" $end "NETSTAT CONNECTIVITY" $duration
319+
echo $NOTES,$HOSTIPS,dns,$HOSTNAME,$start,$duration,$result,$outfin
320+
}
321+
322+
if [ $INSTALL == "Y" ];
323+
then
324+
initialSetup
325+
fi
197326
for (( l=1; l<=$TEST_LOOPS; l++ ))
198327
do
199328
# Execute every loop because ips can change
200329
HOSTIPS=$(hostname --all-ip-addresses)
330+
echo `chkDns` >> $logDns
331+
echo `chkNetstat` >> $logNetstat
201332
case "$SERVERTYPE" in
202333
"sql")
203334
echo `chkConnSQL` >> $logConnectivity

0 commit comments

Comments
 (0)