Need help for the wiki script "to-trigger-something-else ..."

Forum for questions and support relating to the 1.29.x releases only.
Locked
deune
Posts: 3
Joined: Sat Jul 18, 2015 5:26 am

Need help for the wiki script "to-trigger-something-else ..."

Post by deune »

Hello Community,

I need your help , because I ´m not a perl specialist.

I want to trigger at an alarm a bash command. I have the following wiki page found :

http://zoneminder.readthedocs.org/en/la ... s-an-alarm

Code: Select all

#!/usr/bin/perl -w

use strict;

use ZoneMinder;

$| = 1;

zmDbgInit( "my-zm-script", 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$
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 ( 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("perl", "/opt/fhem/fhem.pl", 7072, "set sinocam02 alarm")
            #
        }
    }
    sleep( 1 );
}
Unfortunately, I get the following error :

sudo perl zm-script.pl
Bareword "ZM_DB_NAME" not allowed while "strict subs" in use at zm-script.pl line 11.
Bareword "ZM_DB_HOST" not allowed while "strict subs" in use at zm-script.pl line 11.
Bareword "ZM_DB_USER" not allowed while "strict subs" in use at zm-script.pl line 11.
Bareword "ZM_DB_PASS" not allowed while "strict subs" in use at zm-script.pl line 11.
Execution of zm-script.pl aborted due to compilation errors.

I have already determined the bash command :

Code: Select all

perl /opt/fhem/fhem.pl 7072 "set sinocam02 alarm"
translated in perl , the command should look like this

Code: Select all

system("perl", "/opt/fhem/fhem.pl", 7072, "set sinocam02 alarm");
Can anyone imagine what I'm doing wrong person ? I would be glad if someone could help me !
Best regards
Holger
Last edited by deune on Sun Apr 10, 2016 8:33 am, edited 1 time in total.
rockedge
Posts: 1198
Joined: Fri Apr 04, 2014 1:46 pm
Location: Connecticut,USA

Re: problems with the wiki script "to-trigger-something-else ..."

Post by rockedge »

Give this a try. Comment out this line at the top of the script and it should run.

Code: Select all

#use strict;
deune
Posts: 3
Joined: Sat Jul 18, 2015 5:26 am

Re: problems with the wiki script "to-trigger-something-else ..."

Post by deune »

Dear rockedge,

thank you for your answer, but then I get further error messages.

I think that the script is free of errors and I just too stupid to use it.

Can someone tell what values I have to replace with my own and which values are automatically retrieved from the application zoneminder?

I need simply a short help for a luser ;-)

Holger
flyvert
Posts: 19
Joined: Tue Mar 15, 2016 11:21 am

Re: Need help for the wiki script "to-trigger-something-else ..."

Post by flyvert »

Hi Holger,

I'm new to it all too, asked a question here some time ago (viewtopic.php?f=34&t=24415) for which I up to date have got no answer to.

However, I today probably cracked the same Perl problem you are reporting.
Seems like ZM sample scripts does not match all platforms and Perl-editions.

On the current Debian-version on Raspberry PI (nickname "Jessie") I had to make the following adjustments (or rather workarounds):

1. Instead of trying to read the database logon parameters from some file or environment variable, I hardcoded them in the script
2. zmDbgInit() seem replaced with logInit()

With these changes the sample Perl script becomes (I have commented out the original code in two locations, look for my two added #-rows):

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 );
logInit( "myscript", level=>0, to_log=>0, to_syslog=>0, to_term=>1 );

my $dbh = DBI->connect( "DBI:mysql:database=zm;host=localhost", "zmuser", "zmpass" );
#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 (M.Id)";
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 ( my $last_event_id = zmHasAlarmed( $monitor, $monitor->{LastEventId} ) )
        {
            $monitor->{LastEventId} = $last_event_id;
            print( "Monitor ".$monitor->{Name}." has alarmed\n" );
            #
            # Do your stuff here
            #
        }
    }
    sleep( 1 );
}
It runs but leaves room for many improvements (see the new thread I just posted: viewtopic.php?f=34&t=24614)

Also there is a warning output for each scan (every second) for some uninitialized variable further inside the Perl modules of ZM.
To be continued...
Locked