-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog_collector.sh
executable file
·116 lines (99 loc) · 3.65 KB
/
log_collector.sh
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
#!/bin/bash
######################################################################
# Template to collect selective logs from required servers
######################################################################
#Set Script Name variable
SCRIPT=`basename ${BASH_SOURCE[0]}`
#Initialize variables to default values.
days=1
destination=/data2
log_type=""
date=`date +"%m-%d-%Y"`
controllers=('controller1' 'controller2' 'controller3')
#Set fonts for Help.
NORM=`tput sgr0`
BOLD=`tput bold`
REV=`tput smso`
#Help function
function HELP {
echo -e \\n"Help documentation for ${BOLD}${SCRIPT}.${NORM}"\\n
echo "Command line switches are optional. The following switches are recognized."
echo "${REV}-t${NORM} --Time in days you wanna collect logs for. Default is ${BOLD}${days}${NORM}."
echo "${REV}-l${NORM} --Type of logs you want to collect. Default is ${BOLD}All${NORM}."
echo "${REV}-d${NORM} --Destination directory where you want to copy the log. Default is ${BOLD}${destination}${NORM}."
echo "${REV}-u${NORM} --Username for doing all the operations."
echo "${REV}-p${NORM} --User's password."
echo "${REV}-k${NORM} --User's private key. Currently non-functional, for future use."
echo -e "${REV}-h${NORM} --Displays this help message. No further functions are performed."\\n
echo -e "Example 1: ${BOLD}$SCRIPT -t 2 -l contrail-api -u foo -p bar${NORM}"
echo -e "Example 2: ${BOLD}$SCRIPT -t 1 -l ifmap${NORM}"\\n
exit 1
}
### Start getopts code ###
#Parse command line flags
#If an option should be followed by an argument, it should be followed by a ":".
#Notice there is no ":" after "h". The leading ":" suppresses error messages from
#getopts. This is required to get my unrecognized option code to work.
while getopts :t:l:d:u:p:k:h FLAG; do
case $FLAG in
t) #set option "d"
days=$OPTARG
;;
l) #set option "l"
log_type=$OPTARG
;;
d) #set option "d"
destination=$OPTARG
;;
u) #set option "u"
user=$OPTARG
;;
p) #set option "p"
password=$OPTARG
;;
k) #set option "k"
private_key=$(cat $OPTARG)
;;
h) #show help
HELP
;;
\?) #unrecognized option - show help
echo -e \\n"Option -${BOLD}$OPTARG${NORM} not allowed."
HELP
#If you just want to display a simple error message instead of the full
#help, remove the 2 lines above and uncomment the 2 lines below.
#echo -e "Use ${BOLD}$SCRIPT -h${NORM} to see the help documentation."\\n
#exit 2
;;
esac
done
shift $((OPTIND-1)) #This tells getopts to move on to the next argument.
if [ -z $user ]; then
echo "Please provide your user_name."
read user;
fi
if [ -z $password ]; then
echo "Please provide your password."
stty -echo
read password;
stty echo
fi
### End getopts code ###
echo "Collecting $log_type logs for last $days days with username $user and saving them in $destination directory"
### Main loop to process files ###
#This is where your main file processing will take place
for controller in ${controllers[@]};
do
controller_name=$(echo $controller | cut -d'.' -f1)
directory_name=$controller_name-$date
if [ -d $destination/$directory_name ]; then
sudo rm -rf $destination/$directory_name
fi
/home/varun_lodaya/ssh_helper.sh $controller $user $password $directory_name $days $log_type
# Now copy over the folder locally
sshpass -p $password scp -r $user@$controller:/tmp/${directory_name}.tar.gz $destination
#scp -i /home/$user/.ssh/id_rsa -r $user@$controller:/tmp/${directory_name}.tar.gz $destination
echo "Done copying $controller_name log"
done
### End main loop ###
exit 0