Page 1 of 1
running a program
Posted: Tue Nov 13, 2007 4:03 am
by Sir Veillance
Hi,
I'd like ZM to run a program whenever an event is triggered.
The example script in the
FAQ seems to be suitable for my purpose.
But I am not a perl programmer, so the simple task of placing a call to my own (bash) script is too high for me
Could somebody please show me, what I have to write instead of
Code: Select all
print( "Monitor ".$monitor->{Name}." has alarmed\n" );
#
# Do your stuff here
#
in order to run MYSCRIPT.SH?
And - if possible - I want to pass parameters like Event ID, time, cause and so on..
Thanks a lot!
Posted: Tue Nov 13, 2007 1:26 pm
by zoneminder
In it's simplest form you probably want to have something like this
Code: Select all
$sql = "select * from Events where Id = ?";
$sth = $dbh->prepare_cached( $sql ) or die( "Can't prepare '$sql': ".$dbh->errstr() );
$res = $sth->execute( $last_event_id ) or die( "Can't execute '$sql': ".$sth->errstr() );
my $event = $sth->fetchrow_hashref();
my $command = "/<pathto>/myscript.sh --event=".$event->{Id};
my $output = qx( $command );
my $status = $? >> 8;
# Print output if required
# Check status if required
[solved] running a program
Posted: Tue Nov 13, 2007 3:25 pm
by Sir Veillance
it works
Here is my script alarm_loop.pl
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 (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 );
}
$sql = "select * from Events where Id = ?";
$sth = $dbh->prepare_cached( $sql ) or die( "Can't prepare '$sql': ".$dbh->errstr() );
while( 1 )
{
foreach my $monitor ( @monitors )
{
next if ( !zmShmVerify( $monitor ) );
if ( my $last_event_id = zmHasAlarmed( $monitor, $monitor->{LastEventId} ) )
{
$monitor->{LastEventId} = $last_event_id;
# print( "Monitor ".$monitor->{Name}." has alarmed\n" );
$res = $sth->execute( $last_event_id ) or die( "Can't execute '$sql': ".$sth->errstr() );
my $event = $sth->fetchrow_hashref();
if ( $event->{Id} ) {
# print( "EventID= ".$event->{Id} );
my $command = "./alarm1.sh"
# ." --monitor=" .$monitor->{Id}
." --event=" .$event->{Id}
." --starttime=\"" .$event->{StartTime} ."\"";
my $output = qx( $command );
my $status = $? >> 8;
# print ("Output(" .$status ."):" .$output);
# Print output if required
# Check status if required
#
# Do your stuff here
#
}
}
}
sleep( 1 );
}
This is my alarm1.sh (playing an mp3)
Code: Select all
#!/bin/bash
echo "1=$1 2=$2 3=$3 4=$4"
/usr/local/bin/mplayer /home/Martin/cam/web/sounds/alarm1.mp3>/dev/nul
Thanks a lot, again
Posted: Tue Nov 13, 2007 3:39 pm
by zoneminder
No problem, glad I could help.
Posted: Wed Nov 14, 2007 10:01 am
by achix
A dog bark would be great!
Or smth like playing from the external speakers:
YOUR ACTIONS ARE CURRENTLY MONITORED BY THE POLICE!!!
Thats cool!
Posted: Wed Nov 14, 2007 12:07 pm
by Sir Veillance
achix wrote:A dog bark would be great!
that's exactly what intruders get to hear in my garden
BTW: I haven't yet found any good (scary) dog bark sound files on the net.
I guess I have to go to a scrapyard and find a mastiff
How do I load the script?
Posted: Wed Nov 14, 2007 7:24 pm
by danielareas
Sir Veillance,
At what time do you run the alarm_loop.pl file? When loading zoneminder?
How do I load it automatically? Is it a service?
Thank you,
Daniel
Re: How do I load the script?
Posted: Wed Nov 14, 2007 7:34 pm
by Sir Veillance
danielareas wrote:At what time do you run the alarm_loop.pl file?
at the moment (still testing), I run it manually (from a console):
./alarm_loop.pl &
("&" means: in the background)
Later, I am going to start it from a console script together with ZM. This script can be placed in the /etc/init.d folder (may differ in your distribution)