Here’s a tiny little snippet that I have in my
.bashrc which really comes in handy when trying to figure out what I did on a system when. One of the first things I do when (eek) building out or working on a one-off machine (or setting up a new laptop/desktop, as I am right now) is set this in bashrc for my user and root, so I can go back and document the setup process with a little more ease and sanity. Just add this (it’s just a strftime (3) format string according to the docs, so adjust as desired) to
export HISTTIMEFORMAT="%F %T "
and bash will store commented-out integer timestamps before each line in
.bash_history like so:
tail -30 .bash_history
the output of
history now uses the specified time format:
997 2013-06-11 07:26:45 less .bashrc
998 2013-06-11 07:26:57 history
999 2013-06-11 07:31:19 tail -30 .bash_history
1000 2013-06-11 07:31:33 exit
If you’re like me and most humans, the Nagios logfile timestamp (a unix timestamp) isn’t terribly useful when trying to grep through the logs and correlate events:
# head -2 nagios.log
 LOG ROTATION: DAILY
 LOG VERSION: 2.0
Here’s a nifty Perl one-liner that you can pipe your logs through:
perl -pe ‘s/(\d+)/localtime($1)/e’
to get nicer output like:
# head -2 nagios.log
[Tue Oct 16 00:00:00 2012] LOG ROTATION: DAILY
[Tue Oct 16 00:00:00 2012] LOG VERSION: 2.0
I’ve been having some issues that may be Puppet-related. Unfortunately, Puppet (at least the old 0.25.4 client that I’m running) doesn’t timestamp the debug logs sent to stdout. I know it’s hanging somewhere, but I need concrete numbers to look at. Here’s a wonderfully simple bash script that timestamps everything sent to it on stdin, and echoes it back to stdout:
while read line; do
echo -e "$($DATECMD) $line"
Call it as simply as:
command | ~/bin/ts, or maybe like
command 2>&1 | ~/bin/ts | tee foo.log. Dead simple, but very helpful when the developers didn’t think to timestamp debug log output.
I happened to be working with some timestamps today and noticed that we’re starting to get into territory like
1234094400. So, being the geek that I am, I wondered when we’d finally hit
1234567890. The answer is 2009-02-13T18:31:30-05:00, which happens to be 18:31:30 on my birthday. Yay!