Dear monks ,

I have a working script which does the following.

  1. Reads /var/log/messages in real time for DHCP leases and extracts the MAC
  2. Uses the MAC to contact a http API (through LWP) which responds with status 200 (yes) or 404(no) . This decides if the MAC is allowed to do a RADIUS Auth.
  3. If 200, then contact a local RADIUS server through a http script using LWP and do a RADIUS Auth.
  4. Independent of DHCP lease: Every 5 minutes, check (another http call through LWP ) which MACs are offline and delete them.

Throughout the script, I use a single %clients hash that monitors all the MACs. For example, after step 1 above , I check if the MAC exists in the %clients hash first. If it exists and has a value 0, this MAC has failed Radius Auth earlier. I prevent it from going to step 2 in this case. If it exists and has a value 1, this MAC has passed RAdius Auth earlier and therefore I avoid step 2 and jump to step 3 directly for a new session.

All this works fine. Tested and all. Now , here is where I am unclear about. Is it possible to make the above faster through threads? What if I use threads to do the call to the API and the local RADIUS server. Will it speed up my process when I enable new DHCP leases to create new threads for the API and the RADIUS call.

Here is a small script i wrote to test the API call part using threads. It is a little bit different to the status codes of %clients mentioned earlier. But the concept is the same.

#!/usr/bin/perl use strict; use LWP::UserAgent; use HTTP::Request; use threads; use threads::shared; use strict; # A list of MACs extracted from DHCP leases. my @maclist = qw ( 7D:6D:62:C6:B4:3D 01:12:79:3E:14:2E 80:27:E4:EA:60: +74 E8:06:88:7F:8C:83 01:26:08:E8:B6:5D 34:15:9E:5D:E6:49 03:1E:64:CE:25: +88 01:0C:29:3F:67:1B 01:22:69:76:5D:F4 64:B9:E8:10:BF:20 01:1D:FE:D +4:7F:E0 ); my %clients :shared; foreach my $mac (@maclist){ async( sub{ my $macAuthURL = "https://XXXXXXXX/api/macCheck/"; my ($request, $ua, $response, $respcon, $rval); my ($returnkey,$returnval); print "MAC: ".$mac." Contacting $macAuthURL$mac.\n"; print "This is ".threads->tid()."\n"; # Wrap call to API with eval eval { local $SIG{ALRM} = sub { die "Alarm\n" }; alarm(16); my $macr = $macAuthURL.$mac; $request = HTTP::Request->new(GET => $macr); $ua = LWP::UserAgent->new(); $response = $ua->request($request); $respcon = $response->content(); alarm(0); }; # We check the response object's status if($response->status_line =~ /^500/ ){ print "MAC: ".$mac." Network Delay/Error."; { lock (%clients); $clients{$mac} = 0; } } print "MAC: ".$mac." Server response: $respcon \n\n\n" ; # All OK #Ensure that sent mac is returned back on success. if( $response->{_rc} == 200 ){ $response->{_content} =~ s/[\{\} \"]//g; ($returnkey,$returnval) = split(/:/,$response->{_c +ontent},2); print "MAC: ".$mac." returned with status: $respo +nse->{_rc} "; if( $mac eq uc($returnval) ){ { lock (%clients); $clients{$mac} = 1; } }else{ # Malformed URL { lock (%clients); $clients{$mac} = 0; } } }else{ { lock (%clients); $clients{$mac} = 0; } } sleep 1; }, $_ )->join; # async block join. #)->detach; }

JOIN:
The join part is where I am having trouble. What I want to have is that as several DHCP leases come in a short time, I create threads for each MAC that check with the API without waiting for the earlier MAC . That is, as MAC1 and MAC2 arrive close to each other in the DHCP lease, I do not want the script to wait for the response from the API for MAC1. I want the script to launch a thread to make a call to the API for MAC2 even as MAC1 is getting processed and is updating %clients. The join , however , waits for MAC1 to finish before starting off MAC2. That makes it sequential!. join is blocking until the first thread completes.
DETACH:
But detach does not seem to help either. With detach, the eval block does not seem to complete at all. And naturally, ehen I print the %clients hash at the end, all MACS have no value

How can I make several MACs make calls to the API concurrently and all updating the %clients hash safely. Am I even attempting a threadable problem? I have spent quite some time reading perlmonks, perldocs to no avail.

I do hope I made myself clear.


In reply to Should I use threads? Perl/DHCP/Radius by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.