Description

 When the device is in a chassis cluster and the time does not sync between node0 and node1, it is difficult to check what occurs in node0 and node1 at the same time. These tools can be used to shift one of the node's log time to sync the time between node0 and node1.

Symptoms

These tools in Linux can be used to shift the Junos log time. They are useful when the device is in the chassis cluster and the time does not sync between node0 and node1. You can shift the log time to make it easy to check what occurs in node0 and node1 at the same time.

Solution

At times, node0 and node1's log time does not sync. For example, node0’s log time is Nov 26 23:15:26 and node1’s log time is Nov 26 23:20:26 ; node0’s time can be shifted with 5 minutes to sync the log time between node0 and node1.

Log before shifting the time :
Nov 26 23:15:30 local RT_FLOW: RT_FLOW_SESSION_CREATE: session created 192.168.100.2/57854->10.208.0.50/53 junos-dns-udp 172.27.103.43/57854->10.208.0.50/53 r10 None 17 p1 trust untrust 9704
Log after shifting the time :
Nov 26 23:20:30 local RT_FLOW: RT_FLOW_SESSION_CREATE: session created 192.168.100.2/57854->10.208.0.50/53 junos-dns-udp 172.27.103.43/57854->10.208.0.50/53 r10 None 17 p1 trust untrust 9704
The shell tool usage is as follows:
root@LINUX-a:~# sh ./timeshift
usage: sh timeshift <filename> <delta time>
<filename> : the initial log file
<delta time> : the seconds which needs to increase
The output file name should be “shift- <initial file name>
The shell code for the standard format log is as follows:
#!/usr/bin/bash
echo "usage: sh timeshift <filename> <delta time> \n"
echo "file converting...\n"
rm -rf ./shift*


#time1=$(awk -F" +" '{print $1}' /root/message)
#time2=$(date +%s -d 'time1')
awk -F" +" '{print $1}' $1 > /tmp/keep-time
awk -F" +" '{$1="";print;}' $1 > /tmp/cutoff-time

#add delta time
cat /tmp/keep-time | while read LINE
do
time2=$(date +%s -d "$LINE")
t=$2
time3=$(($t+$time2))
echo $time3 >> /tmp/temp_file1
done

#convert new time
cat /tmp/temp_file1 | while read LINE
do
time4=$(date +"%b %d %T" -d "1970-01-01 UTC $LINE seconds")
echo $time4 >> /tmp/timed_file2
done
paste /tmp/timed_file2 /tmp/cutoff-time > ./shift-$1

rm -rf /tmp/temp_file1
rm -rf /tmp/timed_file2
rm -rf /tmp/cutoff-time
rm -rf /tmp/keep-time

exit 0
The shell code for the structured date format log is as follows:
#!/usr/bin/bash
echo "usage:sh timeshift2 <filename> <delta time> \n"
echo "file converting...\n"
rm -rf ./shift*

#time1=$(awk -F" +" '{print $1}' /root/message)
#time2=$(date +%s -d 'time1')
awk -F"[ .]" '{print $2}' $1 > /tmp/keep-time
sed 's/T/ /g' /tmp/keep-time > /tmp/keep-time-T
awk -F"[ .]" '{print $1}' $1 > /tmp/cutoff-time1
awk -F"[ .]" '{$1=$2=""; print}' $1 | cut -b 3- > /tmp/cutoff-time

#add delta time
cat /tmp/keep-time-T | while read LINE
do
time2=$(date +%s -d "$LINE")
t=$2
time3=$(($t+$time2))
echo $time3 >> /tmp/temp_file1
done

#convert new time
cat /tmp/temp_file1 | while read LINE
do
time4=$(date +"%Y-%m-%dT%H:%M:%S." -d "1970-01-01 UTC $LINE seconds")
echo $time4 >> /tmp/timed_file2
done
paste -d " " /tmp/cutoff-time1 /tmp/timed_file2 > /tmp/paste1
paste -d "" /tmp/paste1 /tmp/cutoff-time > ./shift-$1

rm -rf /tmp/temp_file1
rm -rf /tmp/timed_file2
rm -rf /tmp/cutoff-time
rm -rf /tmp/cutoff-time1
rm -rf /tmp/keep-time
rm -rf /tmp/keep-time-T
rm -rf /tmp/paste1

exit 0
These two scripts work normally on Fedora and Ubuntu with the following detailed versions:
uname -a
Linux lab-svr-2 3.4.2-4.fc17.i686.PAE #1 SMP Thu Jun 14 22:11:26 UTC 2012 i686 i686 i386 GNU/Linux

uname -a
Linux LINUX-a.cn 2.6.32-32-generic #62-Ubuntu SMP Wed Apr 20 21:54:21 UTC 2011 i686 GNU/Linux