2003-01-14 19:47:24 +08:00
|
|
|
#!/usr/bin/perl -wT
|
|
|
|
#
|
|
|
|
# ==========================================================================
|
|
|
|
#
|
2003-04-14 01:29:12 +08:00
|
|
|
# ZoneMinder Package Control Script, $Date$, $Revision$
|
2003-01-14 19:47:24 +08:00
|
|
|
# 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
|
|
|
|
#
|
|
|
|
# ==========================================================================
|
2003-01-15 23:06:15 +08:00
|
|
|
use constant ZM_PATH_BIN => "<from zmconfig>";
|
|
|
|
use constant ZM_PATH_WEB => "<from zmconfig>";
|
|
|
|
use constant ZM_PATH_CGI => "<from zmconfig>";
|
|
|
|
use constant ZM_PATH_LOGS => "<from zmconfig>";
|
|
|
|
use constant ZM_WEB_USER => "<from zmconfig>";
|
2003-01-16 01:47:58 +08:00
|
|
|
use constant ZM_WEB_GROUP => "<from zmconfig>";
|
2003-01-14 19:47:24 +08:00
|
|
|
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>";
|
2003-06-12 22:25:42 +08:00
|
|
|
use constant ZM_SHM_KEY => "<from zmconfig>";
|
2003-01-14 19:47:24 +08:00
|
|
|
use constant ZM_OPT_FAST_DELETE => "<from zmconfig>";
|
|
|
|
use constant ZM_OPT_X10 => "<from zmconfig>";
|
2003-04-22 22:18:20 +08:00
|
|
|
use constant ZM_OPT_FRAME_SERVER => "<from zmconfig>";
|
2003-01-14 19:47:24 +08:00
|
|
|
|
2003-01-14 21:07:16 +08:00
|
|
|
use constant LOG_FILE => ZM_PATH_LOGS.'/zmpkg.log';
|
2003-01-14 19:47:24 +08:00
|
|
|
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];
|
|
|
|
|
2003-01-14 19:54:32 +08:00
|
|
|
if ( !$command || $command !~ /^(?:start|stop|restart|status)$/ )
|
2003-01-14 19:47:24 +08:00
|
|
|
{
|
2003-01-14 21:07:16 +08:00
|
|
|
print( "Usage: zmpkg.pl <start|stop|restart|status>\n" );
|
2003-01-14 19:47:24 +08:00
|
|
|
exit( -1 );
|
|
|
|
}
|
|
|
|
|
2003-06-12 22:25:42 +08:00
|
|
|
sub remove_shm
|
|
|
|
{
|
|
|
|
print( STDERR "Removing shared memory\n" );
|
|
|
|
# Find ZoneMinder shared memory
|
|
|
|
my $command = "ipcs -m | grep '^".substr( sprintf( "0x%x", ZM_SHM_KEY ), 0, -2 )."'";
|
|
|
|
print( "Checking for shared memory with '$command'\n" ) if ( VERBOSE );
|
|
|
|
open( CMD, "$command |" ) or die( "Can't execute '$command': $!" );
|
|
|
|
while( <CMD> )
|
|
|
|
{
|
|
|
|
chomp;
|
|
|
|
my ( $key, $id ) = split( /\s+/ );
|
|
|
|
if ( $id =~ /^(\d+)/ )
|
|
|
|
{
|
|
|
|
$id = $1;
|
|
|
|
my $command = "ipcrm shm $id";
|
|
|
|
print( "Removing shared memory with '$command'\n" ) if ( VERBOSE );
|
|
|
|
qx( $command );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close( CMD );
|
|
|
|
}
|
|
|
|
|
2003-01-14 20:37:47 +08:00
|
|
|
sub execute
|
|
|
|
{
|
|
|
|
my $command = shift;
|
2003-01-14 21:07:16 +08:00
|
|
|
my ( $name ) = getpwuid( $> );
|
2003-01-15 23:06:15 +08:00
|
|
|
if ( $name ne ZM_WEB_USER )
|
2003-01-14 21:07:16 +08:00
|
|
|
{
|
2003-01-15 23:06:15 +08:00
|
|
|
$command = "su ".ZM_WEB_USER." --shell=/bin/sh --command='$command'";
|
2003-01-14 21:07:16 +08:00
|
|
|
}
|
|
|
|
print( STDERR "Executing: $command\n" );
|
|
|
|
return( qx( $command ) );
|
2003-01-14 20:37:47 +08:00
|
|
|
}
|
|
|
|
|
2003-01-21 06:25:19 +08:00
|
|
|
# Move to the right place
|
|
|
|
chdir( ZM_PATH_WEB ) or die( "Can't chdir to '".ZM_PATH_WEB."': $!" );
|
|
|
|
|
2003-01-14 21:07:16 +08:00
|
|
|
my $log_file = LOG_FILE;
|
|
|
|
open( LOG, ">>$log_file" ) or die( "Can't open log file: $!" );
|
2003-01-14 20:37:47 +08:00
|
|
|
#open( STDOUT, ">&LOG" ) || die( "Can't dup stdout: $!" );
|
|
|
|
#select( STDOUT ); $| = 1;
|
2003-01-14 21:07:16 +08:00
|
|
|
open( STDERR, ">&LOG" ) || die( "Can't dup stderr: $!" );
|
|
|
|
select( STDERR ); $| = 1;
|
|
|
|
select( LOG ); $| = 1;
|
2003-01-14 19:47:24 +08:00
|
|
|
|
2003-01-16 01:47:58 +08:00
|
|
|
my $web_uid = (getpwnam( ZM_WEB_USER ))[2];
|
|
|
|
my $web_gid = (getgrnam( ZM_WEB_GROUP ))[2];
|
|
|
|
if ( $> != $web_uid )
|
|
|
|
{
|
|
|
|
chown( $web_uid, $web_gid, $log_file ) or die( "Can't change permissions on log file: $!" )
|
|
|
|
}
|
|
|
|
|
2003-01-14 20:37:47 +08:00
|
|
|
my $retval = 0;
|
|
|
|
|
2003-01-14 19:47:24 +08:00
|
|
|
if ( $command =~ /^(?:stop|restart)$/ )
|
|
|
|
{
|
2003-01-21 06:25:19 +08:00
|
|
|
my $status = execute( ZM_PATH_BIN."/zmdc.pl check" );
|
|
|
|
chomp( $status );
|
2003-01-14 20:37:47 +08:00
|
|
|
if ( $status eq "running" )
|
|
|
|
{
|
2003-01-15 23:06:15 +08:00
|
|
|
execute( ZM_PATH_BIN."/zmdc.pl shutdown" );
|
2003-06-12 22:25:42 +08:00
|
|
|
remove_shm();
|
2003-01-14 20:37:47 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$retval = 1;
|
|
|
|
}
|
2003-01-14 19:47:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( $command =~ /^(?:start|restart)$/ )
|
|
|
|
{
|
2003-01-21 06:25:19 +08:00
|
|
|
my $status = execute( ZM_PATH_BIN."/zmdc.pl check" );
|
|
|
|
chomp( $status );
|
2003-01-14 20:37:47 +08:00
|
|
|
if ( $status eq "stopped" )
|
|
|
|
{
|
2003-06-12 22:25:42 +08:00
|
|
|
remove_shm();
|
2003-01-15 23:06:15 +08:00
|
|
|
execute( ZM_PATH_BIN."/zmfix" );
|
2003-01-16 22:54:33 +08:00
|
|
|
execute( ZM_PATH_BIN."/zmdc.pl status" );
|
2003-01-14 19:47:24 +08:00
|
|
|
|
2003-01-14 20:37:47 +08:00
|
|
|
my $dbh = DBI->connect( "DBI:mysql:database=".ZM_DB_NAME.";host=".ZM_DB_SERVER, ZM_DB_USERA, ZM_DB_PASSA );
|
2003-01-14 19:47:24 +08:00
|
|
|
|
2003-01-14 20:37:47 +08:00
|
|
|
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() )
|
2003-01-14 19:47:24 +08:00
|
|
|
{
|
2003-01-14 20:37:47 +08:00
|
|
|
if ( $monitor->{Function} ne 'None' )
|
2003-01-14 19:47:24 +08:00
|
|
|
{
|
2003-03-26 19:41:22 +08:00
|
|
|
if ( $monitor->{Type} eq 'Local' )
|
|
|
|
{
|
|
|
|
execute( ZM_PATH_BIN."/zmdc.pl start zmc -d $monitor->{Device}" );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
execute( ZM_PATH_BIN."/zmdc.pl start zmc -H $monitor->{Host} -P $monitor->{Port} -p '$monitor->{Path}'" );
|
|
|
|
}
|
2003-01-14 21:20:57 +08:00
|
|
|
if ( $monitor->{Function} eq 'Active' )
|
2003-01-14 20:37:47 +08:00
|
|
|
{
|
2003-04-22 22:18:20 +08:00
|
|
|
if ( ZM_OPT_FRAME_SERVER )
|
|
|
|
{
|
|
|
|
execute( ZM_PATH_BIN."/zmdc.pl start zmf -m $monitor->{Id}" );
|
|
|
|
}
|
2003-01-15 23:06:15 +08:00
|
|
|
execute( ZM_PATH_BIN."/zmdc.pl start zma -m $monitor->{Id}" );
|
2003-04-07 22:35:41 +08:00
|
|
|
execute( ZM_PATH_BIN."/zmdc.pl start zmfilter.pl -m $monitor->{Id} -e -1" );
|
2003-01-14 20:37:47 +08:00
|
|
|
}
|
2003-01-14 19:47:24 +08:00
|
|
|
}
|
|
|
|
}
|
2003-01-14 20:37:47 +08:00
|
|
|
$sth->finish();
|
2003-01-14 19:47:24 +08:00
|
|
|
|
2003-01-14 20:37:47 +08:00
|
|
|
if ( ZM_OPT_FAST_DELETE )
|
|
|
|
{
|
2003-01-15 23:06:15 +08:00
|
|
|
execute( ZM_PATH_BIN."/zmdc.pl start zmaudit.pl -d 900 -y" );
|
2003-01-14 20:37:47 +08:00
|
|
|
}
|
|
|
|
if ( ZM_OPT_X10 )
|
|
|
|
{
|
2003-01-15 23:06:15 +08:00
|
|
|
execute( ZM_PATH_BIN."/zmdc.pl start zmx10.pl -c start" );
|
2003-01-14 20:37:47 +08:00
|
|
|
}
|
2003-01-15 23:06:15 +08:00
|
|
|
execute( ZM_PATH_BIN."/zmdc.pl start zmwatch.pl" );
|
2003-01-14 19:47:24 +08:00
|
|
|
}
|
2003-01-14 20:37:47 +08:00
|
|
|
else
|
2003-01-14 19:47:24 +08:00
|
|
|
{
|
2003-01-14 20:37:47 +08:00
|
|
|
$retval = 1;
|
2003-01-14 19:47:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $command eq "status" )
|
|
|
|
{
|
2003-01-21 06:25:19 +08:00
|
|
|
my $status = execute( ZM_PATH_BIN."/zmdc.pl check" );
|
|
|
|
chomp( $status );
|
2003-01-14 21:07:16 +08:00
|
|
|
print( STDOUT $status."\n" );
|
2003-01-14 19:47:24 +08:00
|
|
|
}
|
|
|
|
|
2003-01-14 20:37:47 +08:00
|
|
|
exit( $retval );
|