Bill Landry wrote:
> Dennis Peterson wrote the following on 9/25/2007 8:06 PM -0800:
>> Bill Landry wrote:
>>
>>
>>> Okay, let's try this again. A new update has been posted that will first
>>> try
>>> "date +%s" and if that fails, then it will automatically fall back to a perl
>>> option. I didn't update the version number, just the version info:
>>>
>> You can rip out a lot of code (well, some code) if you just use the Perl
>> date method by default and forget the date +%s stuff entirely. You
>> already have a dependency on Perl so there's no point adding another
>> tool check to see if gnu date is present as it isn't needed. It's a KISS
>> thing.
>>
>
> Yeah, but if you check, you will see that perl is always used as a
> secondary solution, so perl may never be called when the script is run.
> Besides, there may be systems that do not have perl installed (unlikely,
> but possible). And I've noticed that shelling out to perl is very slow
> compared to the primary options:
>
> time echo PING | socat - /var/amavis/clamd.sock
> PONG
>
> real 0m0.003s
> user 0m0.001s
> sys 0m0.002s
> -----
> time perl -MIO::Socket::UNIX -we '$s = IO::Socket::UNIX->new(shift);
> $s->print("PING"); print $s->getline; $s->close' /var/amavis/clamd.sock
> 2> /dev/null
> PONG
>
> real 0m0.040s
> user 0m0.035s
> sys 0m0.006s
Try pgrep clamd (also not installed on all systems). It will return the
PID if it is running. pgrep won't tell you if it is unresponsive, but if
it is running and unresponsive you have bigger problems. Again, though,
this isn't significant to any reasonable system given the duty cycle.
Now if we were to explore the code simply as an exercise in code
efficiency then run time against the entire script to see what
percentage of time can be made up by making different choices as what
tools you use.
>
> If I were proficient in perl, I would have rather written the entire
> script in perl (maybe time to break open my perl book). Anyway,
> hopefully it is generic enough now, with the fall-back options, that it
> will not need much more editing.
In terms of efficiencies though you could make some simple changes - In
this section of code you call date and perl twice. Once would do it:
if [ `date +%s` -gt 0 2> /dev/null ]
then
current_time=`date +%s`
else
if [ `perl -le print+time 2> /dev/null` ]
then
current_time=`perl -le print+time`
fi
fi
Here's an alternate as a stand-alone shells script and again, there's
more than one way to do this.
#!/bin/sh
# get current time using gnu date
current_time=`date +%s 2>/dev/null`
# is it a realistic result?
if [ $current_time -lt "1100000000" ]
# if not then fall back to Perl
then
current_time=`perl -le print+time 2> /dev/null`
if [ -z $current_time ]
then
echo "no time tool available"
exit 1 # Gratuitous exit on error
fi
fi
echo "time: $current_time"
# end
Also - if you do all your tests up front and discover you'll need to run
multiple instances of perl you may find you can collect multiple code
segments into a single execution of Perl and get everything in one pass.
Do as much as you can in a single shot once you've committed to taking
that shot.
dp
_______________________________________________
Help us build a comprehensive ClamAV guide: visit http://wiki.clamav.net
http://lurker.clamav.net/list/clamav-users.html