#!/bin/bash
#BASHv 3.2.48+
clear
############################################################################################################
#	Ivan Lawrence - 20130508
#
#	Hard code the IP addresses in the following lines after these notes.
#	- One Venice Server per line
#	- Enter the IP address first, then a space, then the host name
#	- Be sure to encapsulate each "IP address Hostname" pair in "double quotes"
#	- Use only hostnames if your system is correctly configured to resolve hostnames to valid IP addresses
#	- Either Hostname or IP address can be used alone (see previous comment about DNS or hosts file)
#
############################################################################################################

Venice[1]="venice1"
Venice[2]="10.0.0.102 venice2" 
Venice[3]="10.0.0.103" 
Venice[6]="10.0.0.106 venice6" 

# zippath is the full path to the destination of the zip (where the zip file will be zipped to)
zippath="/media/venice-vol0/$datetime-$sn-$hstnm-logs.zip"
############################################################################################################
#
#	NO EDITS BEYONG THIS POINT
#
############################################################################################################
datetime="\$(date +%F-%H_%M)"
sn="\$(dvsutil_version | grep -i seriennummer | awk '{print \$2;}')"
hstnm="\$(hostname)"
#zippath="/media/spycer-vol0/$datetime-$sn-$hstnm-logs.zip"
whichIP () {
	echo "Which Venice (IP) would you like to gather logs on?"
	c=1;
	for i in ${!Venice[*]};
		do
			echo "$i: ${Venice[$i]}"
			(( c++ ))
		done
	echo "q: for 'Quit'"
	read -p "Selection: " r
}
quit () {
	if [[ $r == "q" || $r == "Q" ]]; then
		echo "Exiting now!"
		read -p "Done: hit [enter] to exit"
		exit
		fi
}
splitter () { # $1 = IP and $2 = hostname if arrays are formatted correctly
	 IP=$(echo ${Venice[$1]} | awk '{print $1}')
	 DNS=$(echo ${Venice[$1]} | awk '{print $2}')
}
ssh () {
	echo "You selected #$r"
	if ! [[ $r =~ ^[0-9]+$ ]]; then
		echo "Something went wrong, your selection \"$r\" is not a valid entry?!"
		echo "Please try again"
		echo
		read -p "Done: hit [enter] to exit"
		exit
	else
		splitter $r
		echo "Now connecting ssh root@$IP - $DNS to gather logs..."
		echo "If this takes longer than 30sec the server might be inaccessible, contact your systems administrator for help."
		echo "You can exit at any time by hitting [CTRL+C]"
		#echo $zippath
		/usr/bin/ssh root@$IP "zip -9 -r $zippath /home/venice/.DVS/logwatched /var/opt/DVS/logwatched /var/log/messages* /var/log/sa/ /etc/opt/DVS/Venice"
	echo
	echo "The Command completed."
	echo "If everything worked correctly there should be a zip file (named YYYY-MM-DD-HH_MM-<serial_number>-<hostname>-logs.zip) on the top level of the Venice storage accessable via Spycer / Samba / CIFS / NFS at $IP" 
	echo "If there was a problem, please contact your system administrator"
	read -p "Done: hit [enter] to exit"
	fi
}
echo 
echo "  If you do not see a list of expected IP addresses,"
echo "     or the IP addresses are not correct,"
echo "     you will need to edit the file you launched to get here."
whichIP
quit
ssh
#/usr/bin/ssh root@172.24.255.220 "zip -r /media/spycer-vol0/\$(date +%F-%H_%M)-\$(dvsutil_version | grep -i seriennummer | awk '{print \$2;}')-\$(hostname)-logs.zip /home/venice/.DVS/logwatched /var/opt/DVS/logwatched /var/log/messages /etc/opt/DVS/Venice"
