Page 1 of 1

Using the sample perl script from the FAQ?

Posted: Thu Feb 17, 2011 5:34 pm
by wkulecz
I'd like to try and do a few things using the "perl API" starting with the sample script shown in the FAQ at the Wiki, but don't know how to get started with it.

I'm not a Perl person but hope I can quickly learn enough about the shared memory layout to access it in "C" although I suspect I can do what I want in Perl too once I go up its learning curve a bit.


Assume I've cut and pasted the sample script into an xxx.pl file and made it executable by the www-data user, how do I get it to run when the desired "run state" is in effect? Where do I put it?

Its very frustrating that the Wiki is not accurate for the 1.24.2 release, for example "perldoc ZoneMinder::SharedMem" is incorrect now and gives an error. Doing "perldoc ZoneMinder::Memory" appears to be what does it now.

Posted: Thu Feb 17, 2011 7:42 pm
by Nate23
If we are talking about the same Perl script, I have had to change "zmShmVerify" and add am additional line. So the new script looks like this

Code: Select all

#!/usr/bin/perl -w

use strict;

use ZoneMinder;

$| = 1;

zmDbgInit( "myscript", level=>0, to_log=>0, to_syslog=>0, to_term=>1 );

my $dbh = DBI->connect( "DBI:mysql:database=".ZM_DB_NAME.";host=".ZM_DB_HOST, ZM_DB_USER, ZM_DB_PASS );

my $sql = "select M.*, max(E.Id) as LastEventId from Monitors as M left join Events as E on M.Id = E.MonitorId where M.Function != 'None' group by (E.MonitorId)";
my $sth = $dbh->prepare_cached( $sql ) or die( "Can't prepare '$sql': ".$dbh->errstr() );

my $res = $sth->execute() or die( "Can't execute '$sql': ".$sth->errstr() );
my @monitors;
while ( my $monitor = $sth->fetchrow_hashref() )
{
    push( @monitors, $monitor );
}

while( 1 )
{
   foreach my $monitor ( @monitors )
   {   
   next if ( !zmMemVerify( $monitor ) );

      if ( defined( zmMemRead( $monitor, "shared_data:last_event" )) ) {
         if ( !defined( $monitor->{LastEventId} ) || ( my $last_event_id = zmHasAlarmed( $monitor, $monitor->{LastEventId} ) ) )
         {
            $monitor->{LastEventId} = $last_event_id;
            print( "Monitor ".$monitor->{Name}." has alarmed\n" );
            #
            # Do your stuff here
            #
            system("x10cmd rf b2 on")
           }
        }
    }
    sleep( 1 );
}
I don't know much about Perl, I have found these changes searching the forum. to run it "sudo perl script.pl. I'm currently trying to figure out how to narrow this script down to a particular monitor. Hope this helps.

Posted: Tue Feb 22, 2011 3:14 pm
by wkulecz
Thanks, this should help a lot, so the scrip is launched outside of Zoneminder. Gives me a starting point.

different script actions per monitor

Posted: Fri Feb 25, 2011 7:35 pm
by tamray
have you figured out how to launch different script actions based on which monitor was tripped?