Skip to content

Commit 820b54b

Browse files
Paul Warrenjulian-hj
Paul Warren
authored andcommitted
Refactor human readable logging impl for ctl and drain scripts
- replace individually annotated logging with stdout/stderr redirection using exec [#171903790](https://www.pivotaltracker.com/story/show/171903790)
1 parent 1d10ef9 commit 820b54b

File tree

2 files changed

+12
-21
lines changed

2 files changed

+12
-21
lines changed

jobs/nfsv3driver/templates/ctl.erb

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,10 @@ PIDFILE=$RUN_DIR/nfsv3driver.pid
88
SCRIPT=$(basename $0)
99
mkdir -p /var/vcap/sys/log/monit
1010

11-
function prepend_rfc3339_datetime() {
12-
perl -ne 'BEGIN { use Time::HiRes "time"; use POSIX "strftime"; STDOUT->autoflush(1) }; my $t = time; my $fsec = sprintf ".%09d", ($t-int($t))*1000000000; my $time = strftime("[%Y-%m-%dT%H:%M:%S".$fsec."Z]", localtime $t); print("$time $_")'
13-
}
11+
exec 1>> /var/vcap/sys/log/monit/$SCRIPT.log
12+
exec 2>> /var/vcap/sys/log/monit/$SCRIPT.err.log
1413

15-
exec 1> prepend_rfc3339_datetime >> /var/vcap/sys/log/monit/${SCRIPT}.log
16-
exec 2> prepend_rfc3339_datetime >> /var/vcap/sys/log/monit/${SCRIPT}.err.log
17-
18-
echo "------------ `basename $0` $* at `date` --------------" | tee /dev/stderr
14+
echo "[$(date +%Y-%m-%dT%H:%M:%S.%NZ)] ------------ `basename $0` $* --------------" | tee /dev/stderr
1915

2016
case $1 in
2117

jobs/nfsv3driver/templates/drain.erb

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,7 @@ mkdir -p $LOG_DIR
1212

1313
exec 3>&1
1414

15-
function prepend_rfc3339_datetime() {
16-
perl -ne 'BEGIN { use Time::HiRes "time"; use POSIX "strftime"; STDOUT->autoflush(1) }; my $t = time; my $fsec = sprintf ".%09d", ($t-int($t))*1000000000; my $time = strftime("[%Y-%m-%dT%H:%M:%S".$fsec."Z]", localtime $t); print("$time $_")'
17-
}
18-
19-
exec 1> prepend_rfc3339_datetime >> $LOGFILE
20-
exec 2> prepend_rfc3339_datetime >> $LOGFILE
15+
exec &> >(while read line; do echo "[$(date +%Y-%m-%dT%H:%M:%S.%NZ)] $line" >> ${LOGFILE}; done;)
2116

2217
evacuate() {
2318
curl --fail --max-time 600 http://$ADDRESS/evacuate >/dev/null 2>&1
@@ -30,7 +25,7 @@ heartbeat() {
3025
wait_for_apps_to_be_evacuated() {
3126
for i in {1..90}; do
3227
if pgrep -x "rep" > /dev/null; then
33-
echo "$(date): waiting for rep..."
28+
echo "waiting for rep..."
3429
sleep 10
3530
else
3631
break
@@ -46,9 +41,9 @@ output_for_bosh() {
4641
exit_code=$?
4742

4843
if [ $exit_code -eq 0 ]; then
49-
echo "$(date): nfsv3driver exited"
44+
echo "nfsv3driver exited"
5045
else
51-
echo "$(date): evacuation failed"
46+
echo "evacuation failed"
5247
fi
5348

5449
#See https://www.pivotaltracker.com/story/show/165747144/comments/202416034
@@ -58,20 +53,20 @@ output_for_bosh() {
5853
trap output_for_bosh EXIT
5954

6055
if [ ! -f $PIDFILE ]; then
61-
echo "$(date): PIDFILE does not exist"
56+
echo "PIDFILE does not exist"
6257
exit 0
6358
fi
6459

6560
pid=$(cat $PIDFILE)
6661

6762
if [ ! -e /proc/$pid ]; then
68-
echo "$(date): nfsv3driver process not running"
63+
echo "nfsv3driver process not running"
6964
exit 0
7065
fi
7166

7267
wait_for_apps_to_be_evacuated
7368

74-
echo "$(date): rep is done..evacuating nfsv3driver"
69+
echo "rep is done..evacuating nfsv3driver"
7570

7671
set +e
7772
evacuate
@@ -81,7 +76,7 @@ set -e
8176
if [ $evacuate_exit_code -eq 0 ]; then
8277
for i in {1..60}; do
8378
if heartbeat; then
84-
echo "$(date): waiting"
79+
echo "waiting"
8580
sleep 5
8681
else
8782
break
@@ -91,7 +86,7 @@ if [ $evacuate_exit_code -eq 0 ]; then
9186
kill -9 $pid || true
9287
rm -rf $PIDFILE || true
9388
elif [ $evacuate_exit_code -eq 28 ]; then
94-
echo "$(date): Drain timed out"
89+
echo "Drain timed out"
9590
kill -9 $pid || true
9691
rm -rf $PIDFILE || true
9792
else

0 commit comments

Comments
 (0)