-
Notifications
You must be signed in to change notification settings - Fork 11
/
DeleteITOPObjects.sh
executable file
·143 lines (113 loc) · 3.39 KB
/
DeleteITOPObjects.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
##############################################################
#
# This script DELETES Objects by defining it in a OQL CONSULT
#
# OQL = Sentence in OQL l,
# eg export OQL="SELECT Server WHERE status = 'stock'"
#
#
# Usage example :
#
#
##############################################################
# Condfigurable parameters: Change this according to your itop credentials
# Change according your installation directory name : eg itop-itsm
CREDENTIALS_FILE=.credentials
# End of configurable parameters
# Other non configurable parameters
HEADER='Content-Type: application/json'
INITIAL=""
LOG_FILE=/var/log/` basename $0`
# End of non configurable parameters
#######################################################
#
# Function PrintLog
#
# Shows a line to screen and log file
#
#######################################################
PrintLog( )
{
echo [`basename $0`] [`date +'%Y_%m_%d %H:%M:%S'`] [$$] [${FUNCNAME[1]}] $@ | /usr/bin/tee -a $LOG_FILE
}
# 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
}
GetWorkingPath( )
{
# GetWorking Path
FULL_SCRIPT_PATH=`readlink -f $0`
WORKING_PATH=`dirname $FULL_SCRIPT_PATH`
}
# Inicio : Clearing enviroment
PreWork( )
{
PrintLog Start $OQL
[ ` echo $OQL | grep SELECT | wc -l ` -ne 1 ] && PrintLog OQL variable not exported :$OQL
GetWorkingPath
clear
cd $WORKING_PATH 2>/dev/null
#PrintLog Loading Credentials prwd:$PWD `ls -altr $PWD/.credentials`
source $PWD/.credentials
}
PrepareCURL( )
{
if [ ` echo $HTTPS | grep -i y | wc -l ` -eq 1 ]
then
# https
URL="http://${ITOP_USER}:${ITOP_PASS}@${ITOP_SERVER}/${INSTALLATION_DIRECTORY}/webservices/rest.php?version=1.0"
INITIAL="%7b%0d%0a+++%22version%22%3a+%221.0%22%2c%0d%0a+++%22operation%22%3a+%22core%2fdelete%22%2c%0d%0a+++%22class%22%3a+%22CLASS%22%2c%0d%0a+++%22key%22%3a+%22OQL%22%0d%0a%7d%0d%0a%0d%0a"
else
# http: Build URL
INITIAL="%7B%0A%20%20%20%22operation%22%3A%20%22core%2Fdelete%22%2C%0A%20%20%20%22class%22%3A%20%22CLASS%22%2C%0A%20%20%20%22key%22%3A%20%22OQL%22%0A%7D%0A%0A"
URL="http://${ITOP_SERVER}/${INSTALLATION_DIRECTORY}/webservices/rest.php?version=1.0&auth_user=${ITOP_USER}&auth_pwd=${ITOP_PASS}"
fi
# Get text replacements
CLASS=`echo $OQL | awk '{print $2}'`
OQL_ENCODED=` urlencode "$OQL"`
# Perform text replacement
RAW=`echo $INITIAL | sed -e "s/CLASS/$CLASS/g" | sed -e "s/OQL/$OQL_ENCODED/g" `
}
PerformCURL( )
{
if [ ` echo $HTTPS | grep -i y | wc -l ` -eq 1 ]
then
# https
PrintLog "Executing curl -k --basic -X POST -v -H \"Content-Type: application/json\" \"$URL&json_data=$RAW\" | jq . "
curl -k --basic -X POST -v -H "Content-Type: application/json" "$URL&json_data=$RAW" | jq . > salida-$$
else
# http
PrintLog "Executing curl -X POST -v -H "$HEADER" \"$URL&json_data=$RAW\" "
curl -X POST -v -H "$HEADER" "$URL&json_data=$RAW" | jq . > salida-$$
fi
cat salida-$$ | jq .
cat salida-$$ >> $LOG_FILE
}
PostWork( )
{
rm -f salida-$$
PrintLog END
}
############################
# Main
############################
# Prepare URL
PreWork
# PrepareCURL
PrepareCURL
# Perform CURL
PerformCURL
# Delete temp files
PostWork
exit 0