forked from jaimevalero/itop-utilities
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FromITOPtoVCenter.sh
119 lines (95 loc) · 3.31 KB
/
FromITOPtoVCenter.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
# This script uses FromITOPtoAnsible.sh FromMysqltoAnsible.sh to execute an operation agains vmware virtual machine list
#
# Operation to perform:
# powerOn,powerOff,powerStatus,resetMachine,suspendMachine,
FICHERO_TRAZA=/var/log/inventario/$0.log
FILE_LIST=/tmp/itop_file_list
SCRIPT_FILE=$FILE_LIST.sh
PERL_SCRIPT=/opt/dSNmanivela/sources/src/bin/VMoperations.pl
#######################################################
#
# Funcion MostrarLog
#
# Saca por log el texto pasado por argumento
# #######################################################
MostrarLog( )
{
echo [`basename $0`] [`date +'%Y_%m_%d %H:%M:%S'`] [$$] [${FUNCNAME[1]}] $@ | /usr/bin/tee -a $FICHERO_TRAZA }
######
[ ! -z "$OQL" ] && [ ! -z "$SQL" ] && MostrarLog "Both variables SQL and OQL are defined. Error" && exit 1 [ -z "$OQL" ] && [ -z "$SQL" ] && MostrarLog "None of SQL and OQL variables are defined" && exit 1
Generate_VMList( )
{
if [ -z "$OQL" ]
then
echo "OQL is unset, SQL"
/etc/ansible/FromMySQLtoAnsible.sh | sed -e 's/\,/\n/g' | sed -e 's/\:/\n/g' | grep -v \{ | tr -d \" | tr -d \] | tr -d \} | tr -d \[ | sed -e 's/^[ ]*//g' > $FILE_LIST else
echo "OQL is set to '$OQL'"
/etc/ansible/FromITOPtoAnsible.sh | sed -e 's/\,/\n/g' | sed -e 's/\:/\n/g' | grep -v \{ | tr -d \" | tr -d \] | tr -d \} | tr -d \[ | sed -e 's/^[ ]*//g' > $FILE_LIST fi }
Show_Help( )
{
echo -n"
$0
--server (variable VI_SERVER, )
VI server to connect to. Required
--password (variable VI_PASSWORD)
Password. Required
--username (variable VI_USERNAME)
Username. Required
--operation (required)
Operation to perform:
powerOn,powerOff,powerStatus,resetMachine,suspendMachine,
"
}
ARGS=$(getopt -o a:b:c -l "user:,password:,server:,operation:,help:," -n "getopt.sh" -- "$@"); #ARGS=$(getopt -o a:b:c -l ",host:,report:,format:,credential:,credentials:,file:,paramname:,paramvalue:" -n "getopt.sh" -- "$@");
#--credstore (variable VI_CREDSTORE)
# Name of the credential store file defaults to <HOME>/.vmware/credstore/vicredentials.xml
eval set -- "$ARGS";
while true ; do
MostrarLog : Parseamos argumento $1
case "$1" in
--user)
shift ;
vi_user=$1
MostrarLog vi_user=$1
shift ;
continue ;
;;
--password)
shift ;
vi_password=$1
MostrarLog vi_password=$1
shift ;
continue ;
;;
--server)
shift ;
vi_center=$1
MostrarLog vi_center=$1
shift ;
continue ;
;;
--operation)
shift;
operation=$1
MostrarLog operation=$1
shift ;
continue ;
;;
*)
Show_Help
exit 1
break ;
;;
esac
done
[ -z "$operation" ] || [ -z "$vi_center" ] || [ -z "$vi_password" ] || [ -z "$vi_user" ] && MostrarLog "Error. Some of the variables --operation $operation --server $vi_center --password $vi_password --user $vi_user no estan definidos " && Show_Help && exit 1
Generate_VMList
> $SCRIPT_FILE
while read vm
do
echo $PERL_SCRIPT --vmName $vm --operation $operation --username $vi_user --password $vi_password --server $vi_center >> $SCRIPT_FILE
done < $FILE_LIST
MostrarLog Ejecutamos la operacion : ${operation}, contra: `cat $SCRIPT_FILE | wc -l ` maquinas
cat $SCRIPT_FILE
chmod +x $SCRIPT_FILE
. $SCRIPT_FILE