One of my co-workers recently approached me, that he needed a simple shell script which would generate a simple report about a Vserver’s current connections. After ironing out a few things with him (he had the intention of it being on a CIFS share on our file-server - which I changed to a simple HTML page) I went to work.

Out came two scripts. One is the collection instance, and the other is the processing instance. First the collection script runs, finds the current HA master node and then collects the Vserver’s current connections. After that script has dumped the information (date, time, current connections) into a file, the processing script will go and create a simple HTML page that’ll show exactly those informations.

You’ll also need to have configured the public key authentification on both HA nodes, since entering a password in combination with scripting is a bit lame.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/bin/bash

IDENTITY_FILE="/root/.ssh/netscaler"
NS_USER="nsroot"
NS_HOST="$1"
NS_VSERVER="$2"

if [ -z "$2" ] ; then
	echo "Usage: ns-gather-connections.sh <IP/HOSTNAME> <VSERVER_NAME>"
	echo ""
	echo "    o IP/HOSTNAME     -  IP or Hostname of the NetScaler"
	echo "    o VSERVER_NAME    -  Name of the NetScaler vServer"
	echo ""
	exit 1
fi

ns_ssh() {
	COMMANDS="$@"
	ssh -i $IDENTITY_FILE $NS_USER@$NS_HOST $COMMANDS
}

# Get HA information from the NetScaler and check if the current IP is the Master
NS_NODE_STATUS="$( ns_ssh sh ha node | egrep -A2 "IP:.*$NS_HOST" | grep Master | \
	awk '{ print $3 }' )"

if [ "$NS_NODE_STATUS" != "Primary" ] ; then
	# Search for the Primary node
	NS_MASTER_NODE="$( ns_ssh sh ha node | egrep -B2 "Master State: Primary" | \
		grep "IP: " | awk '{ print $2 }' )"
	NS_HOST=$NS_MASTER_NODE

elif [ "$NS_NODE_STATUS" == "Primary" ] ; then
	NS_MASTER_NODE="$NS_HOST"
	NS_HOST=$NS_MASTER_NODE
fi

# Check the connection table
NS_CONNTABLE="$( ns_ssh "sh ns connectiontable \"VSVRNAME = ${NS_VSERVER}\" -detail LINK" | grep 'ESTABLISHED' | wc -l )"

echo "`date '+%F_%R'`;$NS_CONNTABLE" >> /srv/www/${NS_VSERVER}_conntable.log
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/bin/bash

NS_VSERVER="$1"
HTML_FILE="/srv/www/htdocs/${NS_VSERVER}.html"

cat > $HTML_FILE << EOF
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
	"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	<title>$NS_VSERVER - Anzahl Clients nach Datum</title>
	<style>
	.ns {
		margin:0px;padding:0px;
		width:100%;
		border:1px solid #000000;
		-moz-border-radius-bottomleft:0px;
		-webkit-border-bottom-left-radius:0px;
		border-bottom-left-radius:0px;
		-moz-border-radius-bottomright:0px;
		-webkit-border-bottom-right-radius:0px;
		border-bottom-right-radius:0px;
		-moz-border-radius-topright:0px;
		-webkit-border-top-right-radius:0px;
		border-top-right-radius:0px;
		-moz-border-radius-topleft:0px;
		-webkit-border-top-left-radius:0px;
		border-top-left-radius:0px;
	}.ns table{
		border-collapse: collapse;
		border-spacing: 0;
		width:100%;
		height:100%;
		margin:0px;padding:0px;
	}.ns th{
		font-size:14px;
		font-family:Verdana;
		font-weight:bold;
		border:1px solid #000000;
		border-width:0px 1px 1px 0px;
		text-align:left;
		padding:7px;
	}.ns tr:last-child td:last-child {
		-moz-border-radius-bottomright:0px;
		-webkit-border-bottom-right-radius:0px;
		border-bottom-right-radius:0px;
	}
	.ns table tr:first-child td:first-child {
		-moz-border-radius-topleft:0px;
		-webkit-border-top-left-radius:0px;
		border-top-left-radius:0px;
	}
	.ns table tr:first-child td:last-child {
		-moz-border-radius-topright:0px;
		-webkit-border-top-right-radius:0px;
		border-top-right-radius:0px;
	}.ns tr:last-child td:first-child{
		-moz-border-radius-bottomleft:0px;
		-webkit-border-bottom-left-radius:0px;
		border-bottom-left-radius:0px;
	}.ns tr:hover td{

	}
	.ns tr:nth-child(odd){ background-color:#aad4ff; }
	.ns tr:nth-child(even)    { background-color:#ffffff; }.ns td{
		vertical-align:middle;
		border:1px solid #000000;
		border-width:0px 1px 1px 0px;
		text-align:left;
		padding:7px;
		font-size:14px;
		font-family:Verdana;
		font-weight:normal;
		color:#000000;
	}.ns tr:last-child td{
		border-width:0px 1px 0px 0px;
	}.ns tr td:last-child{
		border-width:0px 0px 1px 0px;
	}.ns tr:last-child td:last-child{
		border-width:0px 0px 0px 0px;
	}
	.ns tr:first-child td{
			background:-o-linear-gradient(bottom, #005fbf 5%, #003f7f 100%);	background:-webkit-gradient( linear, left top, left bottom, color-stop(0.05, #005fbf), color-stop(1, #003f7f) );
		background:-moz-linear-gradient( center top, #005fbf 5%, #003f7f 100% );
		filter:progid:DXImageTransform.Microsoft.gradient(startColorstr="#005fbf", endColorstr="#003f7f");	background: -o-linear-gradient(top,#005fbf,003f7f);
		background-color:#005fbf;
		border:0px solid #000000;
		text-align:center;
		border-width:0px 0px 1px 1px;
		font-size:14px;
		font-family:Verdana;
		font-weight:bold;
		color:#ffffff;
	}
	.ns tr:first-child:hover td{
		background:-o-linear-gradient(bottom, #005fbf 5%, #003f7f 100%);	background:-webkit-gradient( linear, left top, left bottom, color-stop(0.05, #005fbf), color-stop(1, #003f7f) );
		background:-moz-linear-gradient( center top, #005fbf 5%, #003f7f 100% );
		filter:progid:DXImageTransform.Microsoft.gradient(startColorstr="#005fbf", endColorstr="#003f7f");	background: -o-linear-gradient(top,#005fbf,003f7f);
		background-color:#005fbf;
	}
	.ns tr:first-child td:first-child{
		border-width:0px 0px 1px 0px;
	}
	.ns tr:first-child td:last-child{
		border-width:0px 0px 1px 1px;
	}
	</style>
</head>
<body>

<h3>NetScaler Vserver $NS_VSERVER - sortiert nach Datum</h1>

<div class="ns">
	<table border="1">
		<tr>
			<th>Datum</th>
			<th>Zeit</th>
			<th>Anzahl Clients</th>
		</tr>
EOF

for line in $( tac /srv/www/${NS_VSERVER}_conntable.log ); do
	DATE="$( echo $line | cut -d\; -f1 | cut -d_ -f1 )"
	TIME="$( echo $line | cut -d\; -f1 | cut -d_ -f2 )"
	CONNECTIONS="$( echo $line | cut -d\; -f2 )"

	cat >> $HTML_FILE << EOF
		<tr>
			<td>$DATE</td>
			<td>$TIME</td>
			<td>$CONNECTIONS</td>
		</tr>
EOF
done

cat >> $HTML_FILE << EOF
</div>
</body>
</html>
EOF