Skip to content

Instantly share code, notes, and snippets.

@kylejohnson
Created January 28, 2014 13:02
Show Gist options
  • Select an option

  • Save kylejohnson/8667240 to your computer and use it in GitHub Desktop.

Select an option

Save kylejohnson/8667240 to your computer and use it in GitHub Desktop.
This script will continuously output the current state of each monitor in your system. You could replace ``` case 3 { print "$time - $monitor_name:\t Alarm!\n" }``` with ```case 3 { system("do_something.sh") }``` for example to call an external script when an alarm goes into Alert mode.
#!/usr/bin/perl
use strict;
use warnings;
use ZoneMinder;
use Switch;
$| = 1;
my @monitors;
my $dbh = DBI->connect( "DBI:mysql:database=".ZM_DB_NAME.";host=".ZM_DB_HOST, ZM_DB_USER, ZM_DB_PASS ) or die "Could not connect to the database.\n";
my $sql = "SELECT * FROM Monitors";
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() );
while ( my $monitor = $sth->fetchrow_hashref() ) {
push( @monitors, $monitor );
}
while (1) {
foreach my $monitor (@monitors) {
my $monitorState = zmGetMonitorState($monitor);
printState($monitor->{Id}, $monitor->{Name}, $monitorState);
}
sleep 1;
}
sub printState {
my ($monitor_id, $monitor_name, $state) = @_;
my $time = localtime();
switch ($state) {
case 0 { print "$time - $monitor_name:\t Idle!\n" }
case 1 { print "$time - $monitor_name:\t Prealarm!\n" }
case 2 { print "$time - $monitor_name:\t Alarm!\n" }
case 3 { print "$time - $monitor_name:\t Alert!\n" }
}
}
@jackherer27
Copy link
Copy Markdown

Hello,

Could you please help me on this script ?
Indeed, this script start. No problem. But even when zoneminder web console shows me an alarm, the output of the script is still empty.
I tried to add a print of $monitorstate in the while loop.
Here is the perl return : Use of uninitialized value $state in concatenation (.) or string at ./zmAlarmScriptNew.pl line 36.
It seems that zmGetMonitorState does not return anything.

Thanks in advance.
Nicolas

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment