Page 1 of 1

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

Posted: Sat Apr 02, 2016 6:41 am
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

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

Posted: Tue Apr 05, 2016 2:20 am
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;

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

Posted: Sun Apr 10, 2016 5:38 am
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

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

Posted: Sun Apr 24, 2016 9:24 am
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...