zoneminder/scripts/zmpkg.pl.z

150 lines
4.3 KiB
Perl
Executable File

#!/usr/bin/perl -wT
#
# ==========================================================================
#
# Zone Minder Package Control Script, $Date$, $Revision$
# Copyright (C) 2003 Philip Coombes
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# ==========================================================================
#
# This script is used to start and stop the ZoneMinder package primarily to
# allow command line control for automatic restart on reboot (see zm script)
#
use strict;
# ==========================================================================
#
# These are the elements you need to edit to suit your installation
#
# ==========================================================================
use constant ZM_DB_SERVER => "<from zmconfig>";
use constant ZM_DB_NAME => "<from zmconfig>";
use constant ZM_DB_USERA => "<from zmconfig>";
use constant ZM_DB_PASSA => "<from zmconfig>";
use constant ZM_PATH_LOGS => "<from zmconfig>";
use constant ZM_OPT_FAST_DELETE => "<from zmconfig>";
use constant ZM_OPT_X10 => "<from zmconfig>";
use constant LOG_FILE => ZM_PATH_LOGS.'/zmpkg.log';
use constant COMMAND_PATH => '@prefix@/bin';
use constant WEB_USER => '@WEB_USER@/';
use constant VERBOSE => 0; # Whether to output more verbose debug
# ==========================================================================
#
# Don't change anything below here
#
# ==========================================================================
use DBI;
# Detaint our environment
$ENV{PATH} = '/bin:/usr/bin';
$ENV{SHELL} = '/bin/sh' if exists $ENV{SHELL};
delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
my $command = $ARGV[0];
if ( !$command || $command !~ /^(?:start|stop|restart|status)$/ )
{
print( "Usage: zmpkg.pl <start|stop|restart|status>\n" );
exit( -1 );
}
sub execute
{
my $command = shift;
my ( $name ) = getpwuid( $> );
if ( $name ne '@WEB_USER@' )
{
$command = "su @WEB_USER@ --shell=/bin/sh --command='$command'";
}
print( STDERR "Executing: $command\n" );
return( qx( $command ) );
}
my $log_file = LOG_FILE;
open( LOG, ">>$log_file" ) or die( "Can't open log file: $!" );
#open( STDOUT, ">&LOG" ) || die( "Can't dup stdout: $!" );
#select( STDOUT ); $| = 1;
open( STDERR, ">&LOG" ) || die( "Can't dup stderr: $!" );
select( STDERR ); $| = 1;
select( LOG ); $| = 1;
my $status = execute( COMMAND_PATH."/zmdc.pl check" );
my $retval = 0;
chomp( $status );
if ( $command =~ /^(?:stop|restart)$/ )
{
if ( $status eq "running" )
{
execute( COMMAND_PATH."/zmdc.pl shutdown" );
}
else
{
$retval = 1;
}
}
if ( $command =~ /^(?:start|restart)$/ )
{
if ( $status eq "stopped" )
{
execute( COMMAND_PATH."/zmfix" );
my $dbh = DBI->connect( "DBI:mysql:database=".ZM_DB_NAME.";host=".ZM_DB_SERVER, ZM_DB_USERA, ZM_DB_PASSA );
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: ".$sth->errstr() );
while( my $monitor = $sth->fetchrow_hashref() )
{
if ( $monitor->{Function} ne 'None' )
{
execute( COMMAND_PATH."/zmdc.pl start zmc -d $monitor->{Device}" );
if ( $monitor->{Function} eq 'Active' )
{
execute( COMMAND_PATH."/zmdc.pl start zma -m $monitor->{Id}" );
}
}
execute( COMMAND_PATH."/zmdc.pl start zmfilter.pl -m $monitor->{Id} -e -1" );
}
$sth->finish();
if ( ZM_OPT_FAST_DELETE )
{
execute( COMMAND_PATH."/zmdc.pl start zmaudit.pl -d 900 -y" );
}
if ( ZM_OPT_X10 )
{
execute( COMMAND_PATH."/zmdc.pl start zmx10.pl -c start" );
}
execute( COMMAND_PATH."/zmdc.pl start zmwatch.pl" );
}
else
{
$retval = 1;
}
}
if ( $command eq "status" )
{
print( STDOUT $status."\n" );
}
exit( $retval );