forked from Osthanes/utilities
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogging_utils.sh
executable file
·261 lines (236 loc) · 7.84 KB
/
logging_utils.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
#!/bin/bash
#********************************************************************************
# Copyright 2015 IBM
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
#********************************************************************************
# uncomment the next line to debug this script
#set -x
# get a value which came from a line like "name": "value",
# trim quotes, spaces, comma from value if needed before returning
get_trimmed_value() {
local trimmedString=$1
if [ "${trimmedString}x" == "x" ]; then
echo
return
fi
# trim leading and trailing spaces, if any
trimmedString="$(echo -e ${trimmedString} | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
# remove leading "
trimmedString="${trimmedString#\"}"
# remove trailing , if there is one
trimmedString="${trimmedString%,}"
# remove trailing "
trimmedString="${trimmedString%\"}"
echo "$trimmedString"
}
# set up for calls to the logging service - takes parameters
# User : required, bluemix userid
# Pwd : required, bluemix password
# Space : required, bluemix space
# Org : required, bluemix org
# Target : optional, may be 'prod' or 'staging'. defaults to 'prod'
setup_met_logging() {
local BMIX_USER=""
local BMIX_PWD=""
local BMIX_SPACE=""
local BMIX_ORG=""
local BMIX_TARGET=""
local BMIX_TARGET_PREFIX=""
if [ -z $1 ]; then
# missing bluemix username
return 1
else
BMIX_USER=$1
fi
if [ -z $2 ]; then
# missing bluemix password
return 2
else
BMIX_PWD=$2
fi
if [ -z $3 ]; then
# missing bluemix space
return 3
else
BMIX_SPACE=$3
fi
if [ -z $4 ]; then
# missing bluemix org
return 4
else
BMIX_ORG=$4
fi
if [ -z $5 ]; then
# empty target, set to prod
BMIX_TARGET='prod'
else
BMIX_TARGET=$5
fi
# adjust logging system for prod/staging
if [ "${BMIX_TARGET}x" == "stagingx" ]; then
BMIX_TARGET_PREFIX="logmet.stage1"
else
BMIX_TARGET_PREFIX="logmet"
fi
# get our necessary logging keys
local curl_data="user=${BMIX_USER}&passwd=${BMIX_PWD}&space=${BMIX_SPACE}&organization=${BMIX_ORG}"
curl -k --silent -d "$curl_data" https://${BMIX_TARGET_PREFIX}.ng.bluemix.net/login > logmet.setup.info
local RC=$?
local local_val=""
if [ $RC == 0 ]; then
while read -r line || [[ -n $line ]]; do
if [[ $line == *"\"access_token\":"* ]]; then
local_val=$(get_trimmed_value "${line#*:}")
if [ "${local_val}x" != "x" ]; then
export LOG_ACCESS_TOKEN=$local_val
fi
elif [[ $line == *"\"logging_token\":"* ]]; then
local_val=$(get_trimmed_value "${line#*:}")
if [ "${local_val}x" != "x" ]; then
export LOG_LOGGING_TOKEN=$local_val
fi
elif [[ $line == *"\"space_id\":"* ]]; then
local_val=$(get_trimmed_value "${line#*:}")
if [ "${local_val}x" != "x" ]; then
export LOG_SPACE_ID=$local_val
fi
fi
done <logmet.setup.info
rm logmet.setup.info
else
rm logmet.setup.info
# unable to curl our tokens, fail out
return 10
fi
# setup our repo
local cur_dir=`pwd`
cd /etc/apt/trusted.gpg.d
wget https://${BMIX_TARGET_PREFIX}.opvis.bluemix.net:5443/apt/BM_OpVis_repo.gpg
echo "deb https://${BMIX_TARGET_PREFIX}.opvis.bluemix.net:5443/apt stable main" > /etc/apt/sources.list.d/BM_opvis_repo.list
apt-get update
cd $cur_dir
# install the logstash forwarder
apt-get -y install mt-logstash-forwarder
# setup up its configuration
echo "LSF_INSTANCE_ID=\"${BMIX_USER}-pipeline\"" >>/etc/mt-logstash-forwarder/mt-lsf-config.sh
echo "LSF_TARGET=\"${BMIX_TARGET_PREFIX}.opvis.bluemix.net:9091\"" >>/etc/mt-logstash-forwarder/mt-lsf-config.sh
echo "LSF_TENANT_ID=\"${LOG_SPACE_ID}\"" >>/etc/mt-logstash-forwarder/mt-lsf-config.sh
echo "LSF_PASSWORD=\"${LOG_LOGGING_TOKEN}\"" >>/etc/mt-logstash-forwarder/mt-lsf-config.sh
echo "LSF_GROUP_ID=\"${BMIX_ORG}-pipeline\"" >>/etc/mt-logstash-forwarder/mt-lsf-config.sh
# restart it to pick up the config changes
service mt-logstash-forwarder restart
# flag logging enabled for other extensions to use
export LOGMET_LOGGING_ENABLED=1
return 0
}
DEBUGGING="DEBUGGING_LEVEL"
INFO="INFO_LEVEL"
LABEL="LABEL_LEVEL"
WARN="WARN_LEVEL"
ERROR="ERROR_LEVEL"
DEBUGGING_LEVEL=6
INFO_LEVEL=4
WARN_LEVEL=2
ERROR_LEVEL=1
OFF_LEVEL=0
if [ -z "$ERROR_LOG_FILE" ]; then
ERROR_LOG_FILE="${EXT_DIR}/errors.log"
export ERROR_LOG_FILE
fi
log_and_echo() {
if [ -z "$LOGGER_LEVEL" ]; then
if [[ $DEBUG = 1 ]]; then
#setting as local so other code won't think it has been set externally
local LOGGER_LEVEL=$DEBUGGING_LEVEL
else
#setting as local so other code won't think it has been set externally
local LOGGER_LEVEL=$INFO_LEVEL
fi
fi
local MSG_TYPE="$1"
if [ "$INFO" == "$MSG_TYPE" ]; then
shift
local pre=""
local post=""
local MSG_LEVEL=$INFO_LEVEL
elif [ "$DEBUGGING" == "$MSG_TYPE" ]; then
shift
local pre=""
local post=""
local MSG_LEVEL=$DEBUGGING_LEVEL
elif [ "$LABEL" == "$MSG_TYPE" ]; then
shift
local pre="${label_color}"
local post="${no_color}"
local MSG_LEVEL=$INFO_LEVEL
elif [ "$WARN" == "$MSG_TYPE" ]; then
shift
local pre="${label_color}"
local post="${no_color}"
local MSG_LEVEL=$WARN_LEVEL
elif [ "$ERROR" == "$MSG_TYPE" ]; then
shift
local pre="${red}"
local post="${no_color}"
local MSG_LEVEL=$ERROR_LEVEL
else
#NO MSG type specified; fall through to INFO level
#Do not shift
local pre=""
local post=""
local MSG_LEVEL=$INFO_LEVEL
fi
local L_MSG=`echo -e "$*"`
local D_MSG=`echo -e "${pre}${L_MSG}${post}"`
if [ $LOGGER_LEVEL -ge $MSG_LEVEL ]; then
echo "$D_MSG"
fi
if [ "$ERROR" == "$MSG_TYPE" ]; then
#store the error for later
echo "$D_MSG" >> "$ERROR_LOG_FILE"
fi
# always log
logger -t "pipeline" "$L_MSG"
}
print_errors() {
if [ -e "${ERROR_LOG_FILE}" ]; then
local ERROR_COUNT=`wc "${ERROR_LOG_FILE}" | awk '{print $1}'`
if [ ${ERROR_COUNT} -eq 1 ]; then
echo -e "${label_color}There was ${ERROR_COUNT} error recorded during execution:${no_color}"
else
echo -e "${label_color}There were ${ERROR_COUNT} errors recorded during execution:${no_color}"
fi
cat "${ERROR_LOG_FILE}"
fi
#No output if no errors were recorded
}
# begin main execution sequence
export -f setup_met_logging
export -f log_and_echo
export -f print_errors
# export message types for log_and_echo
# ERRORs will be collected
export DEBUGGING
export INFO
export LABEL
export WARN
export ERROR
#export logging levels for log_and_echo
# messages will be echoed if LOGGER_LEVEL is set to or above the LEVEL
# messages are always logged
# default LOGGER_LEVEL is INFO_LEVEL, unless DEBUG=1, then DEBUGGING_LEVEL
export DEBUGGING_LEVEL
export INFO_LEVEL
export WARN_LEVEL
export ERROR_LEVEL
export OFF_LEVEL