236 lines
6.2 KiB
Perl
Executable File
236 lines
6.2 KiB
Perl
Executable File
#!/usr/bin/perl -w
|
|
#
|
|
# This script checks for consistency between the event filesystem and
|
|
# the database. If events are found in one and not the other they are
|
|
# deleted (optionally). Additionally any monitor event directories that
|
|
# do not correspond to a database monitor are similarly disposed of.
|
|
# However monitors in the database that don't have a directory are left
|
|
# alone as this is valid if they are newly created and have no events
|
|
# yet.
|
|
#
|
|
|
|
use strict;
|
|
use DBI;
|
|
use Getopt::Long;
|
|
use Term::ReadKey;
|
|
|
|
$| = 1;
|
|
|
|
my $report = 0;
|
|
my $yes = 0;
|
|
my $delay = 0;
|
|
|
|
sub usage
|
|
{
|
|
print( "
|
|
Usage: zme.pl [-r,-report|-y,-yes] [-d <seconds>,-delay=<seconds>]
|
|
Parameters are :-
|
|
-r, --report - Just report don't actually do anything
|
|
-y, --yes - Just do all actions without confirmation
|
|
-d <seconds>, --delay=<seconds> - how long to delay between each delete action, default 0
|
|
");
|
|
exit( -1 );
|
|
}
|
|
|
|
sub confirm
|
|
{
|
|
my $yesno = $yes?1:0;
|
|
if ( $report )
|
|
{
|
|
print( "\n" );
|
|
}
|
|
elsif ( $yes )
|
|
{
|
|
print( ", deleting\n" );
|
|
}
|
|
else
|
|
{
|
|
print( ", delete y/n: " );
|
|
my $char = ReadKey( 0 );
|
|
#print( "C:'".ord($char)."'\n" );
|
|
if ( $char eq 'q' )
|
|
{
|
|
print( "\n" );
|
|
exit( 0 );
|
|
}
|
|
if ( ord($char) == 10 )
|
|
{
|
|
$char = 'y';
|
|
}
|
|
else
|
|
{
|
|
print( "$char\n" );
|
|
}
|
|
if ( $char eq "a" )
|
|
{
|
|
$yes = 1;
|
|
return( 1 );
|
|
}
|
|
$yesno = ( $char =~ /[yY]/ );
|
|
}
|
|
return( $yesno );
|
|
}
|
|
|
|
if ( !GetOptions( 'report'=>\$report, 'yes'=>\$yes, 'delay=i'=>\$delay ) )
|
|
{
|
|
usage();
|
|
}
|
|
|
|
if ( $report && $yes )
|
|
{
|
|
print( STDERR "Error, only one of --report and --yes may be specified\n" );
|
|
usage();
|
|
}
|
|
if ( $report && $delay )
|
|
{
|
|
print( STDERR "Error, using --report and --delay is meaningless (and bogus)\n" );
|
|
usage();
|
|
}
|
|
|
|
ReadMode( 'cbreak' );
|
|
|
|
my $event_path = "/data/zm/events";
|
|
|
|
chdir( $event_path );
|
|
|
|
my $fs_monitors;
|
|
foreach my $monitor ( <*> )
|
|
{
|
|
print( "Found filesystem monitor '$monitor'" );
|
|
my $fs_events = $fs_monitors->{$monitor} = {};
|
|
if ( 1 ) {
|
|
chdir( $monitor );
|
|
foreach my $event ( <*> )
|
|
{
|
|
$fs_events->{$event} = !undef;
|
|
}
|
|
chdir( '..' );
|
|
}
|
|
print( ", got ".int(keys(%$fs_events))." events\n" );
|
|
}
|
|
|
|
my $database = "zm";
|
|
my $username = "zmadmin";
|
|
my $password = "zmadminzm";
|
|
|
|
my $dbh = DBI->connect( "DBI:mysql:$database", $username, $password );
|
|
|
|
my $db_monitors;
|
|
my $sql = "select * from Monitors order by Name";
|
|
my $sth = $dbh->prepare_cached( $sql ) or die( "Can't prepare '$sql': ".$dbh->errstr() );
|
|
my $sql2 = "select * from Events where MonitorId = ? order by Id";
|
|
my $sth2 = $dbh->prepare_cached( $sql2 ) or die( "Can't prepare '$sql2': ".$dbh->errstr() );
|
|
my $res = $sth->execute() or die( "Can't execute: ".$sth->errstr() );
|
|
while( my $monitor = $sth->fetchrow_hashref() )
|
|
{
|
|
print( "Found database monitor '$monitor->{Name}'" );
|
|
my $db_events = $db_monitors->{$monitor->{Name}} = {};
|
|
my $res = $sth2->execute( $monitor->{Id} ) or die( "Can't execute: ".$sth2->errstr() );
|
|
while ( my $event = $sth2->fetchrow_hashref() )
|
|
{
|
|
$db_events->{$event->{Id}} = !undef;
|
|
}
|
|
print( ", got ".int(keys(%$db_events))." events\n" );
|
|
$sth2->finish();
|
|
}
|
|
$sth->finish();
|
|
|
|
while ( my ( $fs_monitor, $fs_events ) = each(%$fs_monitors) )
|
|
{
|
|
if ( my $db_events = $db_monitors->{$fs_monitor} )
|
|
{
|
|
if ( $fs_events )
|
|
{
|
|
while ( my ( $fs_event, $val ) = each(%$fs_events ) )
|
|
{
|
|
if ( !$db_events->{$fs_event} )
|
|
{
|
|
print( "Filesystem event '$fs_monitor/$fs_event' does not exist in database" );
|
|
if ( confirm() )
|
|
{
|
|
qx( rm -rf $event_path/$fs_monitor/$fs_event );
|
|
sleep( $delay ) if ( $delay );
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
print( "Filesystem monitor '$fs_monitor' does not exist in database" );
|
|
if ( confirm() )
|
|
{
|
|
qx( rm -rf $event_path/$fs_monitor );
|
|
sleep( $delay ) if ( $delay );
|
|
}
|
|
}
|
|
}
|
|
|
|
my $sql3 = "delete from Monitors where Id = ?";
|
|
my $sth3 = $dbh->prepare_cached( $sql3 ) or die( "Can't prepare '$sql3': ".$dbh->errstr() );
|
|
my $sql4 = "delete from Events where Id = ?";
|
|
my $sth4 = $dbh->prepare_cached( $sql4 ) or die( "Can't prepare '$sql4': ".$dbh->errstr() );
|
|
my $sql5 = "delete from Frames where EventId = ?";
|
|
my $sth5 = $dbh->prepare_cached( $sql5 ) or die( "Can't prepare '$sql5': ".$dbh->errstr() );
|
|
my $sql6 = "delete from Stats where EventId = ?";
|
|
my $sth6 = $dbh->prepare_cached( $sql6 ) or die( "Can't prepare '$sql6': ".$dbh->errstr() );
|
|
while ( my ( $db_monitor, $db_events ) = each(%$db_monitors) )
|
|
{
|
|
if ( my $fs_events = $fs_monitors->{$db_monitor} )
|
|
{
|
|
if ( $db_events )
|
|
{
|
|
while ( my ( $db_event, $val ) = each(%$db_events ) )
|
|
{
|
|
if ( !$fs_events->{$db_event} )
|
|
{
|
|
print( "Database event '$db_monitor/$db_event' does not exist in filesystem" );
|
|
if ( confirm() )
|
|
{
|
|
my $res = $sth4->execute( $db_event ) or die( "Can't execute: ".$sth4->errstr() );
|
|
$res = $sth5->execute( $db_event ) or die( "Can't execute: ".$sth5->errstr() );
|
|
$res = $sth6->execute( $db_event ) or die( "Can't execute: ".$sth5->errstr() );
|
|
sleep( $delay ) if ( $delay );
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
print( "Database monitor '$db_monitor' does not exist in filesystem" );
|
|
if ( confirm() )
|
|
{
|
|
# We don't actually do this in case it's new
|
|
#my $res = $sth3->execute( $db_monitor ) or die( "Can't execute: ".$sth3->errstr() );
|
|
#sleep( $delay ) if ( $delay );
|
|
}
|
|
}
|
|
}
|
|
|
|
my $sql7 = "select distinct EventId from Frames left join Events on Frames.EventId = Events.Id where isnull(Events.Id)";
|
|
my $sth7 = $dbh->prepare_cached( $sql7 ) or die( "Can't prepare '$sql7': ".$dbh->errstr() );
|
|
$res = $sth7->execute() or die( "Can't execute: ".$sth7->errstr() );
|
|
while( my $frame = $sth7->fetchrow_hashref() )
|
|
{
|
|
print( "Found orphaned frame records for event '$frame->{EventId}'" );
|
|
if ( confirm() )
|
|
{
|
|
$res = $sth5->execute( $frame->{EventId} ) or die( "Can't execute: ".$sth7->errstr() );
|
|
}
|
|
}
|
|
|
|
my $sql8 = "select distinct EventId from Stats left join Events on Stats.EventId = Events.Id where isnull(Events.Id)";
|
|
my $sth8 = $dbh->prepare_cached( $sql8 ) or die( "Can't prepare '$sql8': ".$dbh->errstr() );
|
|
$res = $sth8->execute() or die( "Can't execute: ".$sth8->errstr() );
|
|
while( my $stat = $sth8->fetchrow_hashref() )
|
|
{
|
|
print( "Found orphaned statistic records for event '$stat->{EventId}'" );
|
|
if ( confirm() )
|
|
{
|
|
$res = $sth6->execute( $stat->{EventId} ) or die( "Can't execute: ".$sth8->errstr() );
|
|
}
|
|
}
|
|
|
|
ReadMode( 'restore' );
|