Replace LOGPATH with LOGSUFFIX
[policy/engine.git] / packages / base / src / files / bin / monitor.sh
1 ###
2 # ============LICENSE_START=======================================================
3 # ONAP Policy Engine
4 # ================================================================================
5 # Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 # ================================================================================
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10
11 #      http://www.apache.org/licenses/LICENSE-2.0
12
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 # ============LICENSE_END=========================================================
19 ###
20
21 #!/bin/bash
22
23 function usage() {
24         echo -n "syntax: $(basename $0) "
25         echo "[--debug]"
26 }
27
28 function log() {
29         echo "$(date +"%Y-%m-%d_%H-%M-%S") $1" >> ${POLICY_LOGS}/policy/monitor.log
30 }
31
32 function monitor() {
33         if [[ $DEBUG == y ]]; then
34                 echo "-- ${FUNCNAME[0]} --"
35                 set -x
36         fi
37                 
38         COMPONENT=$1
39         STATUS=$2
40
41         if [[ -z ${COMPONENT} ]]; then
42                 log "WARNING: invalid invocation: no component provided"
43                 return
44         fi
45         
46         if [[ -z ${STATUS} ]]; then
47                 log "WARNING: invalid invocation: no on/off/uninstalled switch provided for ${COMPONENT}"
48                 return
49         fi
50
51         if [[ "${STATUS}" == "off" ]]; then
52                 off ${COMPONENT}
53         else
54                 if [[ "${STATUS}" == "on" ]]; then
55                         on ${COMPONENT}
56                 fi
57         fi
58 }
59
60 function on() {
61         if [[ $DEBUG == y ]]; then
62                 echo "-- ${FUNCNAME[0]} --"
63                 set -x
64         fi
65                 
66         COMPONENT=$1
67
68         ${POLICY_HOME}/etc/init.d/${COMPONENT} status
69         if [[ $? != 0 ]]; then
70                 log "starting ${COMPONENT}"
71
72                 ${POLICY_HOME}/etc/init.d/${COMPONENT} umstart
73         else            
74                 log "OK: ${COMPONENT} (UP)"
75         fi
76 }
77
78 function off() {
79         if [[ $DEBUG == y ]]; then
80                 echo "-- ${FUNCNAME[0]} --"
81                 set -x
82         fi
83                 
84         COMPONENT=$1
85
86         ${POLICY_HOME}/etc/init.d/${COMPONENT} status
87         if [[ $? != 0 ]]; then
88                 log "OK: ${COMPONENT} (DOWN)"
89
90         else
91                 log "stopping ${COMPONENT}"
92                 ${POLICY_HOME}/etc/init.d/${COMPONENT} umstop   
93         fi
94 }
95
96 function config() {
97         if [[ $DEBUG == y ]]; then
98                 echo "-- ${FUNCNAME[0]} --"
99                 set -x
100         fi
101         
102         CONF_FILE=${POLICY_HOME}/etc/monitor/monitor.cfg
103         while read line || [ -n "${line}" ]; do
104         if [[ -n ${line} ]] && [[ ${line} != *#* ]]; then
105                 component=$(echo "${line}" | awk -F = '{print $1;}')
106                 status=$(echo "${line}" | awk -F = '{print $2;}')
107                         if [[ -n ${component} ]] && [[ -n ${status} ]]; then
108                         monitor ${component} ${status}
109                 fi
110         fi
111         done < "${CONF_FILE}"
112         return 0
113 }
114
115 log "Enter monitor"
116
117 DEBUG=n
118 until [[ -z "$1" ]]; do
119         case $1 in
120                 -d|--debug|debug)       DEBUG=y
121                                                         set -x
122                                                         ;;                                                              
123                 *)                                      usage
124                                                         exit 1
125                                                         ;;
126         esac
127         shift
128 done
129
130 if pidof -o %PPID -x $(basename $0) > /dev/null 2>&1; then
131         log "WARNING: $(basename $0) from the previous iteration still running.  Exiting."
132         exit 1
133 fi
134
135 . ${POLICY_HOME}/etc/profile.d/env.sh
136
137
138 config
139