<?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>sidenotes &#187; perl</title>
	<atom:link href="http://chetnichols.org/category/tech/perl/feed/" rel="self" type="application/rss+xml" />
	<link>http://chetnichols.org</link>
	<description>and other random technical bantering, by chet nichols</description>
	<lastBuildDate>Sat, 24 Jul 2010 19:47:19 +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>writing a custom Netscaler health check for NTP</title>
		<link>http://chetnichols.org/2010/04/11/writing-a-custom-netscaler-health-check-for-ntp/</link>
		<comments>http://chetnichols.org/2010/04/11/writing-a-custom-netscaler-health-check-for-ntp/#comments</comments>
		<pubDate>Sun, 11 Apr 2010 09:12:48 +0000</pubDate>
		<dc:creator>chet</dc:creator>
				<category><![CDATA[netscaler]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[tech]]></category>

		<guid isPermaLink="false">http://chetnichols.org/?p=18</guid>
		<description><![CDATA[There are lots of load balancing products on the market today. Some are good, some are bad. Opinions aside, one such product is the Citrix Netscaler. Now, the Netscaler has built in health checking for many different types of backend service protocols: HTTP, FTP, DNS, and more. However, one protocol not included is NTP. If [...]]]></description>
			<content:encoded><![CDATA[<p>There are lots of load balancing products on the market today. Some are good, some are bad. Opinions aside, one such product is the Citrix Netscaler.</p>
<p>Now, the Netscaler has built in health checking for many different types of backend service protocols: HTTP, FTP, DNS, and more. However, one protocol not included is NTP. If you happen to be load balancing a farm of NTP servers, and want to ensure you&#8217;re not routing to a hosed NTP daemon (even if it&#8217;s ping-able), it would be nice to be able to health check the service [and take it out of rotation if it's not responding].</p>
<p>Luckily, the Netscaler provides (with documentation) a very flexible way to write your own custom health checks. Using the system, you can test whatever you want, in any way you want, with just a little Perl. Even better, the custom health check subsystem provides a few useful things to simplify the whole process, including:</p>
<ul>
<li>passing in the service IP, and port, as arguments to your script</li>
<li>passing in a custom argument string (key value pairs, user/pass info, whatever)
<li>handling the timeout for you from a higher level; if your check doesn&#8217;t respond within -respTimeout seconds, it will be considered a failure.</li>
</ul>
<p>Now, for just the basics, this monitor will actually be pretty easy to put together. We are given the IP/port to send an NTP request to, and we don&#8217;t need to worry about a timeout, because the underlying subsystem will handle that for us. All we really need to do is send a request and [hope] for a response!</p>
<p>That being said, we&#8217;ll need to do a couple things to get to that point. These things are:</p>
<ol>
<li>Load the health check subsystem module.</li>
<li>Load the Socket module, and create a socket to use to send the request [using the parameters passed in as args].</li>
</ol>
<p>From that point, we can build and send our request. Since we don&#8217;t need to worry about handling a timeout, we can just sit there waiting for a response. First up:</p>
<pre>
use IO::Socket;
use Netscaler::KAS;
</pre>
<p>We&#8217;ll need both of these modules &#8211; one will tap into the health check subsystem, and the other is so we can send our UDP-based request.</p>
<p>Next is our function, which we&#8217;ll just call <i>ntp_probe</i>. It assigns a few variables from <i>$_[0]</i> (the service IP) and <i>$_[1]</i> (the service port) as arguments, which get passed in from work within probe().</p>
<p>So, the magic-fu to this is that, later on, we will pass our <i>ntp_probe</i> function as a coderef to the KAS probe() command. That will ultimately result in our function being called with the arguments passed in; it is similar to this:</p>
<pre>
$custom_function->($host,$port,$args);
</pre>
<p>If you look at the KAS code (and since it&#8217;s perl, you can), you can see the exact line; this just gives you an idea of what&#8217;s going on.</p>
<p>In any case, after that, we&#8217;ll create a new UDP socket, send the NTP request message down it, and either:</p>
<ul>
<li>return 1 if something wasn&#8217;t defined (ip, port, or sock) &#8211; failure!</li>
<li>return 0 if we got a response &#8211; success!</li>
</ul>
<p>Again, we don&#8217;t need to worry about timeouts, since the subsystem will handle that for us. So, here&#8217;s the function in it&#8217;s entirety:</p>
<pre>
sub ntp_probe {

    my $host    = $_[0];
    my $port    = $_[1];
    my $req     = "\010"."\0"x47;

    if(!$host || !$port) {
        return(1,"Host or port not specified.");
    }

    my $sock = IO::Socket::INET->new(
        Proto     => "udp",
        PeerAddr  => $host,
        PeerPort  => $port,
    );

   if($sock) {
        $sock->send($req);
        $sock->recv($_,1);
        return 0;
    }

    return(1,"Could not create socket");

}
</pre>
<p>Not bad, was it? Now that we have loaded our modules, and defined a function, the next step is to call the probe() method, with a coderef to our new function, to tell KAS what function to use for probing:</p>
<pre>
probe(\&#038;ntp_probe);
</pre>
<p>And you&#8217;re off to the races! Just don&#8217;t forget to start your script with <i>#!/usr/bin/perl</i>, and it should be good to go <img src='http://chetnichols.org/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>Now, you might be asking &#8220;how do I actually configure the Netscaler to use this?&#8221; That&#8217;s also just as easy. Let&#8217;s pretend our new script is called <i>customNTP.pl</i>. Just plop it into the <b>/nsconfig/monitors/</b> directory, and do something like this from the CLI:</p>
<pre>
add lb monitor "custom-ntp-mon" USER -scriptName customNTP.pl
   -scriptArgs 1 -dispatcherIP 127.0.0.1 -dispatcherPort 3013
   -interval 20 -respTimeout 3
</pre>
<p>With that line, you will now have a working NTP healthcheck (20 second intervals, 3 second timeout). Just bind it to your service(s) that need it and enjoy!. Note that the type of monitor is <b>USER</b>; this is the type used for custom health checks.</p>
<p>Well, that&#8217;s all for this evening. There are bits and pieces of info like this around the web, on the Citrix AppExpert site, etc. And, even though it wasn&#8217;t too difficult to figure out, hopefully this will help someone out some day.</p>
<p>Take care!</p>
]]></content:encoded>
			<wfw:commentRss>http://chetnichols.org/2010/04/11/writing-a-custom-netscaler-health-check-for-ntp/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>use perl: pack() to send floats between intel/sparc with proper endian-ness.</title>
		<link>http://chetnichols.org/2010/02/22/use-perl-pack-to-send-floats-between-intelsparc-with-proper-endian-ness/</link>
		<comments>http://chetnichols.org/2010/02/22/use-perl-pack-to-send-floats-between-intelsparc-with-proper-endian-ness/#comments</comments>
		<pubDate>Mon, 22 Feb 2010 08:13:39 +0000</pubDate>
		<dc:creator>chet</dc:creator>
				<category><![CDATA[perl]]></category>
		<category><![CDATA[tech]]></category>

		<guid isPermaLink="false">http://chetnichols.org/?p=78</guid>
		<description><![CDATA[So I like using Perl, if anyone hasn&#8217;t realized yet. Python seems awesome and all, but I haven&#8217;t had the time to start writing things in Python. I&#8217;m still at the stage when, if a tool needs to be put together, it needs to be done ASAP; I can&#8217;t delay trying to figure out Python [...]]]></description>
			<content:encoded><![CDATA[<p>So I like using Perl, if anyone hasn&#8217;t realized yet. Python seems awesome and all, but I haven&#8217;t had the time to start writing things in Python. I&#8217;m still at the stage when, if a tool needs to be put together, it needs to be done ASAP; I can&#8217;t delay trying to figure out Python particulars. </p>
<p>In any case, one of our metrics publishing systems is wrapped by a few different object classes I put together in Perl (follows a certain protocol created by another group, etc). Of the many data types supported by our metrics receivers, we can do things like 32-bit unsigned integers, single precision (32-bit) floating points, and a few others.</p>
<p>Now, when I would publish the 32-bit uint&#8217;s, it would work great- no issues. The receivers would expect a byte stream, so I would use pack() to send my values as binary. For example:</p>
<pre code="Perl">
$val=29;
print $sock pack('N',$val);
</pre>
<p>Easy, whatever. However, when I would send values as a single precision float, it would totally be hosed on the other end. For example:</p>
<pre code="Perl">
$val="30.0";
print $sock pack('f',$val);
</pre>
<p>On the other end, they would get some massive negative number. I don&#8217;t deal with this too much, things usually just work. Thankfully, my mind was jogged in regards to standards- there are lots of standards around byte order for integers (16 bit, 32 bit, 64 bit), but not as much for float; one architecture may use big-endian, and another architecture might use little-endian.</p>
<p>In my searches, I found that Intel stores floating points in little-endian byte order, and Sparc stores them in big-endian byte order. Sure enough, I was trying to publish from an Intel-based BSD host to a Sparc based Solaris host. Aha!</p>
<p>Luckily, pack() can *also* handle this for us! With a simple carat added to the templates section of pack(), I can easily pack the value in big-endian byte order and resolve our issue. I do that like so:</p>
<pre code="Perl">
$val="30.0";
print $sock pack('f>',$val);
</pre>
<p>And, ta da! Worked like a charm. Was it difficult to get it working? No. But, it was fun to have to deal with something like this. On any given day, you don&#8217;t really worry about endian-ness; things just work. This was one of those days where I actually had to use the old noggin, and that was refreshing.</p>
]]></content:encoded>
			<wfw:commentRss>http://chetnichols.org/2010/02/22/use-perl-pack-to-send-floats-between-intelsparc-with-proper-endian-ness/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>adventures in perl: foreach returns pointers to elements</title>
		<link>http://chetnichols.org/2009/12/17/adventures-in-perl-foreach-returns-pointers-to-elements/</link>
		<comments>http://chetnichols.org/2009/12/17/adventures-in-perl-foreach-returns-pointers-to-elements/#comments</comments>
		<pubDate>Thu, 17 Dec 2009 08:58:50 +0000</pubDate>
		<dc:creator>chet</dc:creator>
				<category><![CDATA[perl]]></category>
		<category><![CDATA[tech]]></category>

		<guid isPermaLink="false">http://chetnichols.org/?p=55</guid>
		<description><![CDATA[I&#8217;m not sure how I&#8217;ve never run into this issue before. In some work I was doing recently, I ran into what I thought was a nasty bug [in my code] but couldn&#8217;t explain it. Without thinking, I started trying to undo this, fix that, hack this, and ignore that. After the meeting I was [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m not sure how I&#8217;ve never run into this issue before. In some work I was doing recently, I ran into what I thought was a nasty bug [in my code] but couldn&#8217;t explain it. Without thinking, I started trying to undo this, fix that, hack this, and ignore that. </p>
<p>After the meeting I was in finished, I was able to sit down and actually put some thought into what was happening. The one thing that stood out was interesting, but until then, I had no clue it was how Perl interpreted [in that scenario]. So, I wrote a short test script, sure enough proving the theory, and was able to fix my error.</p>
<p>Take a look at this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@animals</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;cat&quot;</span><span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;dog&quot;</span><span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;emu&quot;</span><span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;frog&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">foreach</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$animal</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">@animals</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;do not eat the %s<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$animal</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>which dumps out this:</p>
<p><code>do not eat the cat<br />
do not eat the dog<br />
do not eat the emu<br />
do not eat the frog<br />
</code></p>
<p><b>In my head</b>, the code above would work like this: for each element in the array @animals, copy the data from that element into a new scalar $animal, then print it out. Simple enough. Now, consider this sample:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@animals</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;cat&quot;</span><span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;dog&quot;</span><span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;emu&quot;</span><span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;lemur&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">foreach</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$animal</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">@animals</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;do not eat the %s<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$animal</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$animal</span> <span style="color: #339933;">=</span> <span style="color: #000066;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;rabid %s&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$animal</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">foreach</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$animal</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">@animals</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;do not eat the %s<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$animal</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>I had *EXPECTED* it to output this:</p>
<p><code>do not eat the cat<br />
do not eat the dog<br />
do not eat the emu<br />
do not eat the frog</p>
<p>do not eat the cat<br />
do not eat the dog<br />
do not eat the emu<br />
do not eat the frog<br />
</code></p>
<p>For the first loop, again, <b>my thinking</b> was that we copy each array element&#8217;s data into the new scalar $animal, print it, modify it (by adding &#8216;rabid&#8217; to it), but do nothing with the modification (we are just modifying $animal, which should be assigned by copy, of which would be lost when we iterate to the next element). Then, in our next loop, we iterate over @animals again, initializing $animal yet again for each element, so we just hit the un-modified @animals array and see the same thing.</p>
<p>[un]Surprisingly, <b>I was totally wrong</b>. This was the output I got:</p>
<p><code>do not eat the cat<br />
do not eat the dog<br />
do not eat the emu<br />
do not eat the frog</p>
<p>do not eat the rabid cat<br />
do not eat the rabid dog<br />
do not eat the rabid emu<br />
do not eat the rabid frog<br />
</code></p>
<p>Wow! What happened? Turns out, like the title suggests, when you use foreach in Perl, <b>it actually assigns $animal to be a pointer</b> (reference, whatever) to that array element, and not a copy. When you make any changes, the change applies directly to the element the array. As another example, it&#8217;s for-loop equivalent would be this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@animals</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;cat&quot;</span><span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;dog&quot;</span><span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;emu&quot;</span><span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;lemur&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$index</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span> <span style="color: #339933;">;</span> <span style="color: #0000ff;">$index</span> <span style="color: #339933;">&lt;</span> <span style="color: #0000ff;">@animals</span> <span style="color: #339933;">;</span> <span style="color: #0000ff;">$index</span><span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;do not eat the %s<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$animals</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$index</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$animals</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$index</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000066;">sprintf</span><span style="color: #009900;">&#40;</span> <span style="color: #ff0000;">&quot;rabid %s&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$animals</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$index</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Fun! Doing some research after the fact, it turns out there has been some mild discussion on the topic. Some consider it a bug, but <b>in reality, it works by design</b>. What does this mean for you? Well, if you happen to be iterating over an array using foreach, and want to modify each element, and plan on looping over the array multiple times, make sure you understand what&#8217;s going on behind the scenes, or else you&#8217;ll run into the same issue I did. You can create a temporary variable (which will assign by copy), ie:</p>
<p><code>my $new_animal = $animal;</code></p>
<p>or you can iterate using a for-loop and just do it like this:</p>
<p><code>my $animal = $animals[$index];</code></p>
<p>There of course may be some cases where you want to take advantage of the referencing feature, and if you do, all the more power to you.</p>
<p>Well, that&#8217;s all I have for now. Hopefully this helps someone out some day. Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://chetnichols.org/2009/12/17/adventures-in-perl-foreach-returns-pointers-to-elements/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
