Replace LOGPATH with LOGSUFFIX
[policy/engine.git] / packages / base / src / files / install / elk / bin / elk.sh
1 #!/bin/bash
2 #
3 #============LICENSE_START==================================================
4 #  ONAP Policy Engine
5 #===========================================================================
6 #  Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
7 #===========================================================================
8 # Licensed under the Apache License, Version 2.0 (the "License");
9 # you may not use this file except in compliance with the License.
10 # You may obtain a copy of the License at
11 #
12 #         http://www.apache.org/licenses/LICENSE-2.0
13 #
14 # Unless required by applicable law or agreed to in writing, software
15 # distributed under the License is distributed on an "AS IS" BASIS,
16 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 # See the License for the specific language governing permissions and
18 # limitations under the License.
19 #============LICENSE_END==================================================
20 #
21
22
23 ELK_MAP_SUCCESS_RE="\"acknowledged\": *true"
24
25 function usage() {
26         echo -n "syntax: $(basename $0) "
27         echo -n "[--debug] "
28         echo -n "([--audit] |"  
29 }
30
31 function log() {
32         echo "$(date +"%Y-%m-%d_%H-%M-%S") $1" >> ${POLICY_LOGS}/policy/elk.log
33         echo "$1"
34 }
35
36 function delete_index() {
37         if [[ $DEBUG == y ]]; then
38                 echo "-- ${FUNCNAME[0]} $@ --"
39                 set -x
40         fi
41         
42         COMMAND="curl --silent -X DELETE http://localhost:9200/policy"
43         OUTPUT="$(${COMMAND} 2>&1)"
44         RC=$?
45         
46         log "${RC}: ${COMMAND}"
47         log "${OUTPUT}"
48         
49         if [[ ${RC} != 0 ]] || [[ ! ${OUTPUT} =~ ${ELK_MAP_SUCCESS_RE} ]]; then
50                 log "WARNING: curl: delete /policy: ${RC}"
51                 return 1
52         fi
53         
54         log "OK: curl: delete /policy: ${OUTPUT}"
55         return 0
56 }
57
58 function create_index() {
59         if [[ $DEBUG == y ]]; then
60                 echo "-- ${FUNCNAME[0]} $@ --"
61                 set -x
62         fi
63         
64         COMMAND="curl --silent -X PUT http://localhost:9200/policy"
65         OUTPUT="$(${COMMAND} 2>&1)"
66         RC=$?
67         
68         log "${RC}: ${COMMAND}"
69         log "${OUTPUT}"
70         
71         if [[ ${RC} != 0 ]] || [[ ! ${OUTPUT} =~ ${ELK_MAP_SUCCESS_RE} ]]; then
72                 log "ERROR: curl: put /policy: ${RC}"
73                 return 1
74         fi
75                 
76         log "OK: curl: put /policy."
77         return 0
78 }
79
80 function check_elk_status() {
81         if [[ $DEBUG == y ]]; then
82                 echo "-- ${FUNCNAME[0]} $@ --"
83                 set -x
84         fi
85                 
86         ${POLICY_HOME}/etc/init.d/elk status
87         if [[ $? != 0 ]]; then
88                 log "ERROR: elk is down.   Aborting .."
89                 exit 1
90         fi
91 }
92
93 function check_elk_policy_index() {
94         if [[ $DEBUG == y ]]; then
95                 echo "-- ${FUNCNAME[0]} $@ --"
96                 set -x
97         fi
98         
99         COMMAND="curl --silent -X GET http://localhost:9200/policy"
100         OUTPUT="$(${COMMAND} 2>&1)"
101         RC=$?
102         
103         log "${RC}: ${COMMAND}"
104         
105         if [[ ${RC} != 0 ]] || [[ ! ${OUTPUT} =~ policy ]]; then
106                 log "ERROR: curl: get /policy: ${RC}"
107                 return 1
108         fi
109                 
110         log "OK: curl: get /policy."
111         return 0                
112 }
113
114 #The Script will update the policy data on querying from database as a bulk to Elastic Database
115 function audit() {
116         if [[ $DEBUG == y ]]; then
117                 echo "-- ${FUNCNAME[0]} $@ --"
118                 set -x
119         fi
120         
121         check_elk_status
122         
123         if ! check_elk_policy_index; then
124                 echo "policy index does not exist. So, Policy Index is created."
125                 if ! create_index; then
126                         echo "abort: policy index creation failed."
127                         exit 1
128                 fi
129         fi
130         
131         $JAVA_HOME/bin/java -cp $POLICY_HOME/servers/pap/webapps/pap/WEB-INF/lib/ONAP-PAP-REST-*.jar:$POLICY_HOME/servers/pap/webapps/pap/WEB-INF/lib/*: -DPOLICY_LOGS=$POLICY_LOGS -DPROPERTY_FILE=$POLICY_HOME/servers/pap/bin/policyelk.properties org.onap.policy.pap.xacml.rest.elk.client.ElasticSearchPolicyUpdate
132 }
133
134 #########################################################################
135 ##
136 ## script execution body
137 ##
138 #########################################################################
139
140 DEBUG=n
141 OPERATION=none
142
143 until [[ -z "$1" ]]; do
144         case $1 in
145                 -d|--debug|debug)       DEBUG=y
146                                                         set -x
147                                                         ;;
148                 -a|--audit|audit)       OPERATION=audit
149                                                         ;;                                                                                                                                      
150                 *)      usage
151                         exit 1
152                         ;;
153         esac
154         shift
155 done
156
157 # operation validation
158 case $OPERATION in
159         audit)  ;;
160         *)              echo "invalid operation (${OPERATION}).";
161                         usage
162                         exit 1
163                         ;;
164 esac
165
166 if [[ -z ${POLICY_HOME} ]]; then
167         echo "error: POLICY_HOME is unset."
168         exit 1
169 fi
170
171 log "**** $OPERATION ****"
172
173 if pidof -o %PPID -x $(basename $0) > /dev/null 2>&1; then
174         echo "WARNING: an $(basename $0) process is already running.  Exiting."
175         exit 1
176 fi
177
178 . ${POLICY_HOME}/etc/profile.d/env.sh
179
180 case $OPERATION in
181         audit)  
182                 audit
183                 ;;              
184         *)      echo "invalid operation (${OPERATION}).";
185                 usage
186                 exit 1
187                 ;;
188 esac