3 BASH_PROFILE=/root/.bash_profile
4 HISTORY_PROFILE=/etc/profile.d/histlog.sh
5 PERIODIC_SCRIPT=/usr/bin/periodic_upload.sh
7 cat <<\EOF > $BASH_PROFILE
8 # NOTE: only upload incremental diffs
9 if [ -f /tmp/source/configuration ] ; then
10 source /tmp/source/configuration
12 if [ -z "$MONITOR_SERVER" ] ; then
13 MONITOR_SERVER=monitor.planet-lab.org
15 function upload_log ()
19 base=$( basename $file )
23 if [ ! -f $file ] ; then
32 comm -1 -3 $old $new > $log
33 if [ $( stat -c %s $log ) -ne 0 ] ; then
34 curl --max-time 60 --silent --insecure https://$MONITOR_SERVER/monitor/uploadlogs --form "dir=$path" --form "log=@$log"
35 if [ $? -ne 0 ] ; then
36 # the upload has failed, so remove new file so no data is lost
37 rm -f /tmp/$( basename $file ).new
42 function upload_logs ()
44 upload_log $1 histfail
47 # NOTE: these aliases aim to upload the history before losing it.
48 alias reboot="upload_logs /root/.bash_eternal_history ; /sbin/reboot"
49 alias shutdown="upload_logs /root/.bash_eternal_history ; /sbin/shutdown"
52 cat <<\EOF > $HISTORY_PROFILE
53 export HISTTIMEFORMAT="%s ";
54 # NOTE: HOSTNAME is not reliably set in failboot or safeboot mode
55 # NOTE: These steps assign at least a default hostname based on IP
56 # NOTE: This hostname is used in the bash-prompt-script commands
57 if [[ -z "$HOSTNAME" || "$HOSTNAME" = "(none)" ]] ; then
58 HOSTNAME=`ip addr show dev eth0 | grep inet | tr '/' ' ' | sed -e 's/^ *//g' | cut -f2 -d' '`
60 if [ -f /etc/sysconfig/network-scripts/ifcfg-eth0 ] ; then
61 source /etc/sysconfig/network-scripts/ifcfg-eth0
62 if [ -n "$DHCP_HOSTNAME" ] ; then
63 HOSTNAME=$DHCP_HOSTNAME
65 if [ -n "$IPADDR" ] ; then
70 hostname $HOSTNAME &> /dev/null
71 if [ -n "$BASH_EXECUTION_STRING" ]; then
72 # NOTE: does not work on 2.x versions of bash.
73 # NOTE: log commands executed over ssh
74 echo "$HOSTNAME $$ ssh:$USER xx `date +%s` $BASH_EXECUTION_STRING" >> /root/.bash_eternal_history;
76 if [ -e /etc/sysconfig/bash-prompt-xterm ]; then
77 PROMPT_COMMAND=/etc/sysconfig/bash-prompt-xterm
80 chmod 755 $HISTORY_PROFILE
82 cat <<\EOF > bash-prompt-script
83 # NOTE: intended to run after and log every interactive-command
84 echo $HOSTNAME $$ $USER "$(history 1)" >> /root/.bash_eternal_history
87 for f in bash-prompt-default bash-prompt-xterm ; do
88 cp bash-prompt-script /etc/sysconfig/$f
89 chmod 755 /etc/sysconfig/$f
92 # NOTE: allow command run directly over ssh to be logged also.
93 echo "source /etc/profile ; source $BASH_PROFILE" > /root/.bashrc
95 # NOTE 1: crond is not installed on the boot image, so this maintains a
96 # persistent process to upload logs on legacy nodes.
97 # NOTE 2: A day has 86400 seconds, $RANDOM is between 0-32767
98 # NOTE 2: So, $RANDOM * 3 is between 0 and 27 hours.
99 # NOTE 3: The initial delay is randomized in case many nodes reboot at the
101 initial_delay=$(( $RANDOM * 3 ))
103 cat <<EOF > $PERIODIC_SCRIPT
105 if [ -f $BASH_PROFILE ] ; then
108 echo "Cannot source upload_logs() definition!"
112 # NOTE: exit if anoter process is already running.
113 if [ \$$ -ne \`pgrep -o periodic\` ] ; then
114 # the current PID differs from the oldest periodic_upload pid
119 upload_logs /root/.bash_eternal_history
120 sleep 86400 # sleep for a day
124 chmod 755 $PERIODIC_SCRIPT
125 $PERIODIC_SCRIPT < /dev/null > /tmp/upload.log 2>&1 &