<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Piece of Unix Hints &#187; Perl</title>
	<atom:link href="http://hints.jeb.be/category/perl/feed/" rel="self" type="application/rss+xml" />
	<link>http://hints.jeb.be</link>
	<description>hints.jeb.be</description>
	<lastBuildDate>Fri, 04 Dec 2009 20:35:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Misconfigured Perl install path</title>
		<link>http://hints.jeb.be/2008/12/17/misconfigured-perl-install-path/</link>
		<comments>http://hints.jeb.be/2008/12/17/misconfigured-perl-install-path/#comments</comments>
		<pubDate>Wed, 17 Dec 2008 01:50:52 +0000</pubDate>
		<dc:creator>Jeb</dc:creator>
				<category><![CDATA[Perl]]></category>
		<category><![CDATA[CPAN]]></category>

		<guid isPermaLink="false">http://hints.jeb.be/?p=38</guid>
		<description><![CDATA[I got a strange problem on a server. CPAN was installing module outside of perl @INC, this is quite a nightmare. I found what was wrong and it was quite easy to solve. Run this command: # perl -V:'install.*' Look at installprivlib and installarchlib. If they don&#8217;t match @INC you can change them at the [...]]]></description>
			<content:encoded><![CDATA[<p>I got a strange problem on a server. CPAN was installing module outside of perl @INC, this is quite a nightmare.<br />
I found what was wrong and it was quite easy to solve.<br />
Run this command:</p>
<blockquote><p><code># perl -V:'install.*'</code></p></blockquote>
<p>Look at installprivlib and installarchlib. If they don&#8217;t match @INC you can change them at the end of perl Config.pm (in tie %Config, &#8216;Config&#8217;, {..})</p>
<p>To find your Config.pm file type</p>
<blockquote><p><code># perl -MConfig -le 'print $INC{"Config.pm"};'</code></p></blockquote>
<p>If you only use CPAN to install module I guess you can just change PREFIX in the CPAN Config.pm (but I haven&#8217;t tested this)</p>
<p>N.B.: If you can&#8217;t find installprivlib and installarchlib in tie %Config just add them.</p>
]]></content:encoded>
			<wfw:commentRss>http://hints.jeb.be/2008/12/17/misconfigured-perl-install-path/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Getting human reading time info from RRD files</title>
		<link>http://hints.jeb.be/2008/12/17/getting-human-reading-time-info-from-rrd-files/</link>
		<comments>http://hints.jeb.be/2008/12/17/getting-human-reading-time-info-from-rrd-files/#comments</comments>
		<pubDate>Wed, 17 Dec 2008 01:49:59 +0000</pubDate>
		<dc:creator>Jeb</dc:creator>
				<category><![CDATA[Perl]]></category>
		<category><![CDATA[RRDTool]]></category>
		<category><![CDATA[Humain Reading]]></category>

		<guid isPermaLink="false">http://hints.jeb.be/?p=36</guid>
		<description><![CDATA[You have a .rrd file but you don&#8217;t remember how much time RRAs can store data ? all right, no problem. First save this Perl code into a .pl file. $step = $1 if (m/step = (\d+)/); $rows = $1 if (m/rra.*\.rows = (\d+)/); if (m/(.*)\.pdp_per_row = (\d+)/) { $pdp = $2; $time = $step*$rows*$pdp; [...]]]></description>
			<content:encoded><![CDATA[<p>You have a .rrd file but you don&#8217;t remember how much time RRAs can store data ? all right, no problem.</p>
<p>First save this Perl code into a .pl file.</p>
<blockquote><p><code>$step = $1 if (m/step = (\d+)/);<br />
$rows = $1 if (m/rra.*\.rows = (\d+)/);<br />
if (m/(.*)\.pdp_per_row = (\d+)/) {<br />
$pdp = $2; $time = $step*$rows*$pdp;<br />
if ($time &gt; 31536000) { $time = sprintf("%.2f year",$time/31536000) }<br />
elsif ($time &gt; 86400) { $time = sprintf("%.2f days",$time/86400) }<br />
elsif ($time &gt; 3600)  { $time = sprintf("%.2f hours",$time/3600) }<br />
print "$1: $step*$rows*$pdp = $time\n";<br />
}</code></p></blockquote>
<p>Assuming the filename of the script is rrd_info.pl, run this Shell command:</p>
<blockquote><p><code>rrdtool info file.rrd | perl -n rrd_info.pl</code></p></blockquote>
<p>Here is an output example:</p>
<pre>rra[0]: 300*864*1 = 3.00 days
rra[1]: 300*864*5 = 15.00 days
rra[2]: 300*702*25 = 60.94 days
rra[3]: 300*840*125 = 364.58 days
rra[4]: 300*840*625 = 4.99 year
rra[5]: 300*864*1 = 3.00 days
rra[6]: 300*864*5 = 15.00 days
rra[7]: 300*702*25 = 60.94 days
rra[8]: 300*840*125 = 364.58 days
rra[9]: 300*840*625 = 4.99 year
rra[10]: 300*864*1 = 3.00 days
rra[11]: 300*864*5 = 15.00 days
rra[12]: 300*702*25 = 60.94 days
rra[13]: 300*840*125 = 364.58 days
rra[14]: 300*840*625 = 4.99 year</pre>
<p>The formula is update step time * numbers of rows in the rra * pdp_per_row</p>
]]></content:encoded>
			<wfw:commentRss>http://hints.jeb.be/2008/12/17/getting-human-reading-time-info-from-rrd-files/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How big is you MP3 collection all over your hard drives ?</title>
		<link>http://hints.jeb.be/2008/12/17/how-big-is-you-mp3-collection-all-over-your-hard-drives/</link>
		<comments>http://hints.jeb.be/2008/12/17/how-big-is-you-mp3-collection-all-over-your-hard-drives/#comments</comments>
		<pubDate>Wed, 17 Dec 2008 01:40:01 +0000</pubDate>
		<dc:creator>Jeb</dc:creator>
				<category><![CDATA[Perl]]></category>
		<category><![CDATA[Unix]]></category>
		<category><![CDATA[MP3]]></category>

		<guid isPermaLink="false">http://hints.jeb.be/?p=19</guid>
		<description><![CDATA[You want to know the total size of all your .mp3 files ? (or any kind of file, juste change the locate argument) Try : locate .mp3 &#124; perl -e 'while(&#60;STDIN&#62;) { chop ; $tsize += -s $_; } print $tsize/1048576 . "Mo\n"' or locate .mp3 &#124; perl -e 'foreach (&#60;&#62;) { chop and $_["+"]+=-s$_ [...]]]></description>
			<content:encoded><![CDATA[<p>You want to know the total size of all your .mp3 files ? (or any kind of file, juste change the locate argument)</p>
<p>Try :</p>
<pre>locate .mp3 | perl -e 'while(&lt;STDIN&gt;) { chop ; $tsize += -s $_; } print $tsize/1048576 . "Mo\n"'</pre>
<p>or
<pre>
locate .mp3 | perl -e 'foreach (&lt;&gt;) { chop and $_["+"]+=-s$_ } print $_["+"]/1048576 . "Mo\n"'</pre>
<p>or if you didn&#8217;t have perl (sorry for you <img src='http://hints.jeb.be/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> )</p>
<pre>locate .mp3 | awk '{print "\"" $0 "\""}' | xargs ls -l | awk 'BEGIN{s=0}{s+=($5/1024/1024)}END{print s "Mo"}'</pre>
]]></content:encoded>
			<wfw:commentRss>http://hints.jeb.be/2008/12/17/how-big-is-you-mp3-collection-all-over-your-hard-drives/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Add IPF rule automatically from log files</title>
		<link>http://hints.jeb.be/2008/12/17/add-ipf-rule-automatically-from-log-files/</link>
		<comments>http://hints.jeb.be/2008/12/17/add-ipf-rule-automatically-from-log-files/#comments</comments>
		<pubDate>Wed, 17 Dec 2008 01:30:14 +0000</pubDate>
		<dc:creator>Jeb</dc:creator>
				<category><![CDATA[NetBSD]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[Unix]]></category>
		<category><![CDATA[Apache]]></category>
		<category><![CDATA[IPF]]></category>
		<category><![CDATA[Log]]></category>

		<guid isPermaLink="false">http://hints.jeb.be/?p=16</guid>
		<description><![CDATA[Here is a very simple command to add a rule to your firewall (IPF in my example) when you match something in a log file (apache in this case) for item in `tail -n 150 access_log &#124; grep "c+dir" &#124; awk '{print $1}'` ; do echo "block in quick on ne0 proto ip from $item [...]]]></description>
			<content:encoded><![CDATA[<p>Here is a very simple command to add a rule to your firewall (IPF in my example) when you match something in a log file (apache in this case)</p>
<div class="code">
<pre>for item in `tail -n 150 access_log | grep "c+dir" | awk '{print $1}'` ;
  do echo "block in quick on ne0 proto ip from $item to any" &gt;&gt; /etc/ipf.conf ;
done</pre>
</div>
<p>This read 150 last line of access_log using tail, use grep as matching operator, use awk to catch ip (note that you could do /c+dir/{print $1} in awk to don&#8217;t use grep) then add a blocking rule in /etc/ipf.conf</p>
<p>You may want to add a comment to the end of the blocking rule saying why it was blocked.</p>
<p>Don&#8217;t forget to reload the firewall, /sbin/ipf -Fa -f /etc/ipf.conf for ipf, from time to time with cron to active the rule.</p>
<p>You may reload the firewall each time with</p>
<div class="code">
<pre>for item in `tail -n 150 access_log | grep "c+dir" | awk '{print $1}'` ;
  do echo "block in quick on ne0 proto ip from $item to any" &gt;&gt; /etc/ipf.conf; /sbin/ipf -Fa -f /etc/ipf.conf ;
done</pre>
</div>
<p>This system has 2 problems:</p>
<ul>
<li>You must run tail from cron as -f can&#8217;t work with the for statement.</li>
<li>Rules are added at the end of ipf.conf, this is very useless if you have <tt>pass in quick proto ip any to any port 80</tt> before.</li>
</ul>
<p>So, here is a Perl script that will do a better job.</p>
<div class="code">
<pre>my $IPF_FILE="/etc/ipf.conf";
my $TMP_FILE="/tmp/ipf.new.rules";
my %h;
open (FILE,"tail -fn 1 /usr/local/apache/logs/access_log|") || die "can't open FILE: $!";
 while (&lt;FILE&gt;) {
  if ($_ =~ /^(.*)s-s-.*c+dir/) {
   if(exists($h{"$1"})) { $h{"$1"}++ }
   else {
    $h{"$1"} = 1;
    open(IPF, "&lt; $IPF_FILE") or die "can't open $IPF_FILE: $!";
    open(TMP, "&gt; $TMP_FILE") or die "can't open $TMP_FILE: $!";
    print TMP "block in log quick on ne0 from $1 to anyn" or die "can't write to $TMP_FILE: $!";
    while (&lt;IPF&gt;) { (print TMP $_) or die "can't write to $TMP_FILE: $!"; }
    close(IPF)                  or die "can't close $IPF_FILE: $!";
    close(TMP)                  or die "can't close $TMP_FILE: $!";
    rename("$TMP_FILE", "$IPF_FILE") or die "can't rename $TMP_FILE to $IPF_FILE: $!";
    system("ipf -Fa -f $IPF_FILE");
   }
  }
 }
close (FILE);
}</pre>
</div>
<p>Incrementation of $h{&#8220;$1&#8243;} is totally useless here but you may use it for something (like waiting more than one attemp of the IP before adding it to IPF). $h is used to don&#8217;t firewall two time the same IP.</p>
<p>You may think that $h is not usefull because as we have blocked the IP, we will not get any new request from it. Not really</p>
<ul>
<li>Tail is not working really in live, it check time to time for new line then print them, so between the first request of the IP and the reload of the firwall, you may have more than one request (don&#8217;t forget that reloading ipf take time also);</li>
<li>My IPF rule is very strict, you may only block port 80, so you can still get request on port 443, or things like that.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://hints.jeb.be/2008/12/17/add-ipf-rule-automatically-from-log-files/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
