-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddtemplate_api.sh
executable file
·88 lines (76 loc) · 2.45 KB
/
addtemplate_api.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
#!/bin/bash
# addtemplate_api.sh
# version 1.00
# date 05/10/2019
# Usage info
show_help() {
cat << EOF
Usage: ${0##*/} -u=<user centreon> -p=<passwd centreon> -r=<url Rest API Centreon> -H=<Host> -T=<Host template>
This program send submit to Centreon Rest API
-u|--user User Centreon.
-p|--password Password Centreon.
-r|--url url Rest API Centreon ex : http(s)://<ip centreon>
-H|--Hostname Hostname of service
-s|--service passive service
-S|--Status Service status 0 Ok,1 Warning,2 Critical,3 Unknown
-o|--output Service message
-d|--perfdata Service perfdata optionnal
-i|--insecure for only https optionnal
-h|--help help
EOF
}
for i in "$@"
do
case $i in
-u=*|--user=*)
USER_CENTREON="${i#*=}"
shift # past argument=value
;;
-p=*|--password=*)
PWD_CENTREON="${i#*=}"
shift # past argument=value
;;
-r=*|--url=*)
URL="${i#*=}"
shift # past argument=value
;;
-H=*|--Hostname=*)
HOSTNAME="${i#*=}"
shift # past argument=value
;;
-T=*|--htpl=*)
HTPL="${i#*=}"
shift # past argument=value
;;
-i=*|--insecure=*)
INSECURE="${i#*=}"
shift # past argument=value
;;
-h|--help)
show_help
exit 2
;;
*)
# unknown option
;;
esac
done
# Check for missing parameters
if [[ -z "$USER_CENTREON" ]] || [[ -z "$PWD_CENTREON" ]] || [[ -z "$URL" ]] || [[ -z "$HOSTNAME" ]] || [[ -z "$HTPL" ]] ; then
echo "Missing parameters!"
show_help
exit 2
fi
# Check yes/no
if [[ $INSECURE =~ ^[yY][eE][sS]|[yY]$ ]]; then
INSECURE="--insecure "
else
INSECURE=""
fi
CURL="/usr/bin/curl"
JQ="/usr/bin/jq"
SED="/bin/sed"
TOKEN=`$CURL -s $INSECURE -s -d "username=$USER_CENTREON&password=$PWD_CENTREON" -H "Content-Type: application/x-www-form-urlencoded" -X POST $URL/centreon/api/index.php?action=authenticate | $JQ '.["authToken"]'| $SED -e 's/^"//' -e 's/"$//'`
RESULT=`$CURL -s $INSECURE -X POST $URL'/centreon/api/index.php?action=action&object=centreon_clapi' -H 'Content-Type: application/json' -H 'centreon-auth-token: '${TOKEN}'' -d '{"action": "addtemplate","object": "host","values": "'"${HOSTNAME};${HTPL}"'" }'`
RESULT=`$CURL -s $INSECURE -X POST $URL'/centreon/api/index.php?action=action&object=centreon_clapi' -H 'Content-Type: application/json' -H 'centreon-auth-token: '${TOKEN}'' -d '{"action": "applytpl","object": "host","values": "'"${HOSTNAME}"'" }'`
echo $RESULT