-
Notifications
You must be signed in to change notification settings - Fork 11
/
FromITOPtoAnsible.sh
executable file
·266 lines (210 loc) · 7.29 KB
/
FromITOPtoAnsible.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#!/bin/bash
##############################################################
#
# This script pulls info from an ITOP cmdb to generate
# a yaml hosts list to be used as a Dynamic Inventory Source
# for ansible commands
#
# Parameters : Passed as enviroment variable, it could be one of the following
#
# OQL = Sentence in OQL l,
# eg export OQL="SELECT Server WHERE status = 'stock'"
#
# OQL = Link to an audit rule,
# eg OQL="https://demo.combodo.com/simple/pages/audit.php?operation=csv&category=3&rule=1&c[menu]=Audit"
#
# OQL = Name of an existing audit rule, to get objects from,
# eg OQL="Server in Stock"
# FIELD = (optional) name of the field to be used as hostname
#
# You can also set variables to ansible, eg :
# export VAR="\"ansible_ssh_pass\" : \"mypassword\" "
# so you can set ssh password to be used.
#
# Usage example :
#
# Do a ping against all VM belonging to hypervisor prod-epg-esxi-04.hi.inet
# export OQL="SELECT VirtualMachine WHERE virtualhost_name = 'prod-epg-esxi-04.hi.inet' " ; ansible all -i FromITOPtoAnsible.sh -m shell -m "ping"
#
#
##############################################################
###########################################################
#
# Configurable parameters
#
############################################################
# Stored in .credentials
###########################################################
# End of configurable parameters
#
############################################################
# Other non configurable parameters
SERVER=
PROTOCOL=http
TEMP_CSV_FILE=out.csv
MODE_FLAG=description
CATEGORY=
RULE=
CURL_OPTIONS='-s '
WGET_OPTIONS='-q '
# End of non configurable parameters
# This functions is not mine. Credits to
# cdown / gist:1163649 https://gist.github.com/cdown/1163649
urlencode( )
{
# urlencode <string>
local length="${#1}"
for (( i = 0; i < length; i++ )); do
local c="${1:i:1}"
case $c in
[a-zA-Z0-9.~_-]) printf "$c" ;;
*) printf '%%%02X' "'$c"
esac
done
}
# Set variables
# If env variable FIELD is unset, set it to "name"
SetVariables( )
{
# Load From Credentials
MY_USER=${ITOP_USER}
MY_PASS=${ITOP_PASS}
# If we use https, change options accordingly
[ ` echo $HTTPS | grep -i Y | wc -l ` -eq 1 ] && PROTOCOL=https && CURL_OPTIONS='-s -k ' && WGET_OPTIONS='-q --no-check-certificate'
[[ -z "$FIELD" ]] && FIELD=name
ENCODED_STRING=`urlencode "$OQL"`
SERVER="${PROTOCOL}://${ITOP_SERVER}/${INSTALLATION_DIRECTORY}/webservices/export.php?c%5Bmenu%5D=ExportMenu"
URL_STRING="&expression="${ENCODED_STRING}
LAST_URL_OPTIONS="&format=csv&login_mode=basic&fields=${FIELD}"
}
GetWorkingPath( )
{
# GetWorking Path
FULL_SCRIPT_PATH=`readlink -f $0`
WORKING_PATH=`dirname $FULL_SCRIPT_PATH`
}
PreWork( )
{
GetWorkingPath
source $PWD/.credentials
MY_USER=${ITOP_USER}
MY_PASS=${ITOP_PASS}
SetVariables
# If we detect OQL variable is a link from an audit category, change the mode
[ ` echo $OQL | grep SELECT | wc -l ` -eq 1 ] && MODE_FLAG='select'
[ ` echo $OQL | grep -i http | grep -i category | grep -i rule | wc -l ` -eq 1 ] && MODE_FLAG='audit'
[ ` echo $OQL | grep -i http | grep -i filter | wc -l` -eq 1 ] && MODE_FLAG='filter'
}
QueryITOP( )
{
wget ${WGET_OPTIONS} -O $TEMP_CSV_FILE --http-user=$MY_USER --http-password=$MY_PASS ${SERVER}${URL_STRING}${LAST_URL_OPTIONS} 2>/dev/null
RESUL=$?
[ $RESUL -ne 0 ] && curl ${CURL_OPTIONS} -d "auth_pwd=$MY_PASS&auth_user=$MY_USER&loginop=login" --dump-header headers \
"${SERVER}${URL_STRING}${LAST_URL_OPTIONS}" > $TEMP_CSV_FILE
}
QueryITOPAudit( )
{
curl ${CURL_OPTIONS} -d "auth_pwd=$MY_PASS&auth_user=$MY_USER&loginop=login" --dump-header headers "${PROTOCOL}://${ITOP_SERVER}/${INSTALLATION_DIRECTORY}/pages/audit.php?operation=csv&category=$CATEGORY&rule=$RULE&filename=audit.csv&c%5Borg_id%5D=$ORGANIZATION" > $TEMP_CSV_FILE
}
QueryITOPFilter( )
{
curl ${CURL_OPTIONS} -d "auth_pwd=$MY_PASS&auth_user=$MY_USER&loginop=login" --dump-header headers "${PROTOCOL}://${ITOP_SERVER}/${INSTALLATION_DIRECTORY}/pages/UI.php?operation=search&filter=${FILTER}&format=csv" > $TEMP_CSV_FILE
RIGHT_PART=`grep -o expression.*\"\>Dow $TEMP_CSV_FILE | cut -d\" -f1`
curl ${CURL_OPTIONS} -d "auth_pwd=$MY_PASS&auth_user=$MY_USER&loginop=login" --dump-header headers "${PROTOCOL}://${ITOP_SERVER}/${INSTALLATION_DIRECTORY}/webservices/export.php?${RIGHT_PART}" > $TEMP_CSV_FILE
}
BuildYAMLOutputAudit( )
{
grep -i '^\"' $TEMP_CSV_FILE > $TEMP_CSV_FILE.tmp
mv -f $TEMP_CSV_FILE.tmp $TEMP_CSV_FILE
HOST_LIST=` cat $TEMP_CSV_FILE | cut -d\, -f1 | sed ':a;N;$!ba;s/\n/ , /g'`
}
BuildYAMLOutput( )
{
grep -v "\[" $TEMP_CSV_FILE > $TEMP_CSV_FILE.tmp
mv -f $TEMP_CSV_FILE.tmp $TEMP_CSV_FILE
HOST_LIST=` cat $TEMP_CSV_FILE | grep \" | sed ':a;N;$!ba;s/\n/ , /g'`
}
AnsibleReturnHostsList( )
{
# echo "{ \"hosts\" : [ ${HOST_LIST} ] }"
if [ ${#VAR} -gt 0 ]
then
echo "{ \"hosts\" : [ ${HOST_LIST} ] ,"
echo '"_meta" : {'
echo ' "hostvars" : {'
echo ${HOST_LIST} | sed -e "s/\" /\" : \{ ${VAR} \} /g" | sed -e "s/\"$/\" : \{ ${VAR} \} /g"
echo ' }'
echo ' }'
echo '}'
else
echo "{ \"hosts\" : [ ${HOST_LIST} ] }"
fi
}
PostWork( )
{
rm -f $TEMP_CSV_FILE.tmp $TEMP_CSV_FILE headers 2>/dev/null
}
ExtractValuesAudit( )
{
CATEGORY=`echo $OQL | egrep -o --colour category=[0-9]. | cut -d\= -f2`
RULE=`echo $OQL | egrep -o --colour rule=[0-9]* | cut -d\= -f2`
ORGANIZATION=`echo $OQL | egrep -o --colour 'c\[org_id\]='[0-9]* | cut -d\= -f2`
}
ExtractValuesFilter( )
{
FILTER=`echo $OQL | egrep -o --colour 'filter='[a-zA-Z0-9\%]* | cut -d\= -f2`
ORGANIZATION=`echo $OQL | egrep -o --colour 'c\[org_id\]='[0-9]* | cut -d\= -f2`
}
ExtractValuesDescription( )
{
# Resolve the audit category (Name of the audit category must by univoque)
FILE_TMP=/tmp/kk-tmp-$$
curl ${CURL_OPTIONS} -d "auth_pwd=$MY_PASS&auth_user=$MY_USER&loginop=login" --dump-header headers "${PROTOCOL}://${ITOP_SERVER}/${INSTALLATION_DIRECTORY}/pages/audit.php?c%5Borg_id%5D=999&c%5Bmenu%5D=Audit" | sed -e 's/\á/á/g' -e 's/\é/é/g' -e 's/\í/í/g' -e 's/\ó/ó/g' -e 's/\ú/ú/g' -e 's/\ñ/ñ/g' -e 's/\Á/Á/g' -e 's/\É/É/g' -e 's/\Í/Í/g' -e 's/\Ó/Ó/g' -e 's/\Ú/Ú/g' -e 's/\Ñ/Ñ/g' > $FILE_TMP
LAST_URL=`grep -i "$OQL" $FILE_TMP | egrep -o audit.* | cut -d\" -f1`
OQL="${PROTOCOL}://${ITOP_SERVER}/${INSTALLATION_DIRECTORY}/pages/$LAST_URL"
rm -f $FILE_TMP
}
#
# Query the OQL looking for audit category or OQL SELECT
#, urlencode it, send it to the itop server
# and format its response
#
ItopDialog( )
{
case $MODE_FLAG in
( description )
ExtractValuesDescription
ExtractValuesAudit
QueryITOPAudit
BuildYAMLOutputAudit
;;
( select )
# Query ITOP's php export webservice
QueryITOP
# Format itop response
BuildYAMLOutput
;;
( audit ) #
ExtractValuesAudit
QueryITOPAudit
BuildYAMLOutputAudit
;;
( filter )
ExtractValuesFilter
QueryITOPFilter
BuildYAMLOutputAudit
;;
esac
}
############################
# Main
############################
# Prepare URL
PreWork
# Itop Dialoge
ItopDialog
# Return response to Ansible
AnsibleReturnHostsList
# Delete temp files
PostWork
exit 0