The script already logs the output to a file called "server_mon.txt. I would like to append a timestamp to each entry for the purpose of tracking server activity.
I now understand that standard AWK doesn't have an inherent time/date function that can easily be assigned to a variable. I attempted the following but didn't work for me:
tail -fn0 /var/log/user | /usr/bin/awk '
BEGIN {
str = "date +%Y-%m-%d";
str = | getline date;
close str;
The following is my full script so far:
#!/bin/bash
PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
tail -fn0 /var/log/user | /usr/bin/awk '
/disconnect_tcp_conn/ { report("down") }
/daemon apps started/ { report("up") }
function report(curr_state, msg) {
if ( prev_state != curr_state ) {
msg = "Server is " curr_state
system("mail -s \047" msg "\047 [email protected] </dev/null")
print msg | "cat>&2"
prev_state = curr_state
}
}
'
&
PID=$!
DIEAT=`expr $SECONDS + 58`
while [ -d /proc/$PID ] && [ "$SECONDS" -lt "$DIEAT" ]
do
sleep 1
done
[ -d /proc/$PID ] && kill "$PID"
wait
Expect to see a timestamp associated with each log entry to server_mon.txt.
Thanks
disconnect_tcp_connanddaemon apps startedoccur? If so show that section of the log file in your question.