<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Ellipsis: Tag time</title>
    <link>http://typo.pburkholder.com/articles/tag/time?tag=time</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>...</description>
    <item>
      <title>Ruby epoch2localtime</title>
      <description>&lt;p&gt;I love it when 82 characters do a ton of work for me.&lt;/p&gt;


	&lt;p&gt;Today I&amp;#8217;m trying to debug Courier-IMAP ssl errors that are reported in the log file as &amp;#8220;DEBUG: Unexpected &lt;span class="caps"&gt;SSL&lt;/span&gt; connection shutdown&amp;#8221;.   We&amp;#8217;re using QMail  so the times in the log file are in tai64, and I&amp;#8217;m using Eric Rescorla&amp;#8217;s SSLDump:&amp;#8221;http://www.rtfm.com/ssldump/&amp;#8221; to debug the &lt;span class="caps"&gt;SSL&lt;/span&gt; traffic, which reports time in epoch seconds.  I&amp;#8217;d like to correlate the &lt;span class="caps"&gt;SSL&lt;/span&gt; traffic with the logged errors.  Here&amp;#8217;s how:&lt;/p&gt;


	&lt;p&gt;In window 1,  I monitor the QMail logs with:&lt;/p&gt;


&lt;code&gt;&lt;pre&gt;
tail -f /var/log/qmail/imap4-ssl/current | tail64nlocal
&lt;/pre&gt;&lt;/code&gt;

	&lt;p&gt;which gives output like:&lt;/p&gt;


&lt;code&gt;&lt;pre&gt;
2007-10-30 06:26:02.322254500 tcpserver: status: 30/40
2007-10-30 06:26:02.322255500 tcpserver: pid 9635 from 1.2.3.4
2007-10-30 06:26:02.322257500 tcpserver: ok 9635 buzz.example.net:10.1.1.20:993 :216.220.209.17::57693
2007-10-30 06:26:02.439369500 DEBUG: Connection, ip=[1.2.3.4]
&lt;/pre&gt;&lt;/code&gt;

	&lt;p&gt;In window 2, I monitor the &lt;span class="caps"&gt;SSL&lt;/span&gt; traffic with:&lt;/p&gt;


&lt;code&gt;&lt;pre&gt;
sudo ssldump -e -k imap.example.com.pem  port 993 | ~/bin/epoch2local.rb
&lt;/pre&gt;&lt;/code&gt;

	&lt;p&gt;where the script @epoch2local.rb is:&lt;/p&gt;


&lt;code&gt;&lt;pre&gt;
#!/usr/bin/ruby -p

$_.sub!(/1\d{9}/) { |t| Time.at(t.to_f).strftime("%Y-%m-%d %H:%M:%S") }
&lt;/pre&gt;&lt;/code&gt;

	&lt;p&gt;A quick dissection:&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;&lt;code&gt;ruby -p&lt;/code&gt; places your code in a &lt;code&gt;while gest; ....; print; end&lt;/code&gt; loop&lt;/li&gt;
		&lt;li&gt;&lt;code&gt;$_&lt;/code&gt; is the current line&lt;/li&gt;
		&lt;li&gt;&lt;code&gt;.sub!&lt;/code&gt; does in place substitution, changing the value of &lt;code&gt;$_&lt;/code&gt;&lt;/li&gt;
		&lt;li&gt;&lt;code&gt;sub!(pattern) { |match| block }&lt;/code&gt; the string matting &lt;code&gt;pattern&lt;/code&gt; is passed into the {} block as the variable &lt;code&gt;match&lt;/code&gt;.  The result from the block is substituted for the original string&lt;/li&gt;
		&lt;li&gt;&lt;code&gt;/1\d{9}/&lt;/code&gt;: assume that a 10-digit number starting with 1 is the epoch time (true for another 30 years or so)&lt;/li&gt;
		&lt;li&gt;&lt;code&gt; |t| Time.at(t.to_f).strftime("%Y-%m-%d %H:%M:%S") &lt;/code&gt;: &lt;span class="caps"&gt;PFM&lt;/span&gt;. No, not really. Pass the match into the block as &lt;code&gt;t&lt;/code&gt;.  Convert to a float, then a Time value, then format as &lt;code&gt;%Y-%m-%d %H:%M:%S&lt;/code&gt;&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;I then get SSLDump out put as:&lt;/p&gt;


&lt;code&gt;&lt;pre&gt;
23 21 2007-10-30 06:41:19.1945 (0.1309)  C&amp;gt;S  application_data
24 3  2007-10-30 06:41:19.2474 (0.1185)  S&amp;gt;C  Handshake
      Certificate
24 4  2007-10-30 06:41:19.2474 (0.0000)  S&amp;gt;C  Handshake
      ServerHelloDone  
&lt;/pre&gt;&lt;/code&gt;

	&lt;p&gt;And I can match up the times with the QMail logs.  Fini (although I still have the original question to resolve)&lt;/p&gt;</description>
      <pubDate>Tue, 30 Oct 2007 06:21:00 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:78dad476-44d2-4be1-806d-0ec2aeecc63a</guid>
      <author>Peter Burkholder</author>
      <link>http://typo.pburkholder.com/articles/2007/10/30/ruby-epoch2localtime</link>
      <category>Ruby and Rails</category>
      <category>time</category>
      <category>ruby</category>
      <category>noobie</category>
    </item>
  </channel>
</rss>
