X-Git-Url: https://defaria.com/gitweb/?a=blobdiff_plain;ds=sidebyside;f=clearadm%2Flib%2FClearadm.pm;h=a9bcb364e9f1d67937cfe92d7ca2f597c8fa9919;hb=b19aff368e8114dd44bbd7e306b0946f060e3051;hp=719b7722974215d8c60ea04d61da1f75e8ee7aee;hpb=ed7943b5913aae90452e00009a19aaa86605b820;p=clearscm.git diff --git a/clearadm/lib/Clearadm.pm b/clearadm/lib/Clearadm.pm index 719b772..a9bcb36 100644 --- a/clearadm/lib/Clearadm.pm +++ b/clearadm/lib/Clearadm.pm @@ -38,7 +38,7 @@ specifics about the method you are envoking. my $clearadm = new Clearadm; # Add a new system - my %system = ( + my %system =( name => 'jupiter', alias => 'defaria.com', admin => 'Andrew DeFaria', @@ -47,13 +47,13 @@ specifics about the method you are envoking. description => 'Home server', ); - my ($err, $msg) = $clearadm->AddSystem (%system); + my ($err, $msg) = $clearadm->AddSystem(%system); # Find systems matching 'jup' - my @systems = $clearadm->FindSystem ('jup'); + my @systems = $clearadm->FindSystem('jup'); # Get a system by name - my %system = $clearadm->GetSystem ('jupiter'); + my %system = $clearadm->GetSystem('jupiter'); # Update system my %update = ( @@ -64,7 +64,7 @@ specifics about the method you are envoking. # Delete system (Warning: will delete all related records regarding this # system). - my ($err, $msg) = $clearadm->DeleteSystem ('jupiter'); + my ($err, $msg) = $clearadm->DeleteSystem('jupiter'); =head1 DESCRIPTION @@ -88,6 +88,7 @@ use Carp; use DBI; use File::Basename; use Net::Domain qw(hostdomain); +use Sys::Hostname; use FindBin; @@ -97,10 +98,12 @@ use DateUtils; use Display; use GetConfig; use Mail; +use Clearcase::Vob; +use Clearcase::View; -my $conf = dirname (__FILE__) . '/../etc/clearadm.conf'; +my $conf = dirname(__FILE__) . '/../etc/clearadm.conf'; -our %CLEAROPTS = GetConfig ($conf); +our %CLEAROPTS = GetConfig($conf); # Globals our $VERSION = '$Revision: 1.54 $'; @@ -127,7 +130,7 @@ my $defaultFilesystemHist = '6 months'; my $defaultLoadavgHist = '6 months'; # Internal methods -sub _dberror ($$) { +sub _dberror($$) { my ($self, $msg, $statement) = @_; my $dberr = $self->{db}->err; @@ -139,7 +142,7 @@ sub _dberror ($$) { my $message = ''; if ($dberr) { - my $function = (caller (1)) [3]; + my $function = (caller(1)) [3]; $message = "$function: $msg\nError #$dberr: $dberrmsg\n" . "SQL Statement: $statement"; @@ -148,59 +151,56 @@ sub _dberror ($$) { return $dberr, $message; } # _dberror -sub _formatValues (@) { +sub _formatValues(@) { my ($self, @values) = @_; my @returnValues; # Quote data values - push @returnValues, $_ eq '' ? 'null' : $self->{db}->quote ($_) - for (@values); + push @returnValues, $_ eq '' ? 'null' : $self->{db}->quote($_) for (@values); return @returnValues; } # _formatValues -sub _formatNameValues (%) { +sub _formatNameValues(%) { my ($self, %rec) = @_; my @nameValueStrs; - push @nameValueStrs, "$_=" . $self->{db}->quote ($rec{$_}) - for (keys %rec); + push @nameValueStrs, "$_=" . $self->{db}->quote($rec{$_}) for (keys %rec); return @nameValueStrs; } # _formatNameValues -sub _addRecord ($%) { +sub _addRecord($%) { my ($self, $table, %rec) = @_; my $statement = "insert into $table ("; $statement .= join ',', keys %rec; $statement .= ') values ('; - $statement .= join ',', $self->_formatValues (values %rec); + $statement .= join ',', $self->_formatValues(values %rec); $statement .= ')'; my ($err, $msg); - $self->{db}->do ($statement); + $self->{db}->do($statement); - return $self->_dberror ("Unable to add record to $table", $statement); + return $self->_dberror("Unable to add record to $table", $statement); } # _addRecord -sub _deleteRecord ($;$) { +sub _deleteRecord($;$) { my ($self, $table, $condition) = @_; my $count; my $statement = "select count(*) from $table "; - $statement .= "where $condition" - if $condition; + $statement .= "where $condition" if $condition; - my $sth = $self->{db}->prepare ($statement) - or return $self->_dberror ('Unable to prepare statement', $statement); + my $sth = $self->{db}->prepare($statement) + or return $self->_dberror('Unable to prepare statement', $statement); $sth->execute - or return $self->_dberror ('Unable to execute statement', $statement); + or return $self->_dberror('Unable to execute statement', $statement); my @row = $sth->fetchrow_array; @@ -212,36 +212,33 @@ sub _deleteRecord ($;$) { $count = 0; } # if - return ($count, 'Records deleted') - if $count == 0; + return ($count, 'Records deleted') if $count == 0; $statement = "delete from $table "; - $statement .= "where $condition" - if $condition; + $statement .= "where $condition" if $condition; - $self->{db}->do ($statement); + $self->{db}->do($statement); if ($self->{db}->err) { - return $self->_dberror ("Unable to delete record from $table", $statement); + return $self->_dberror("Unable to delete record from $table", $statement); } else { return $count, 'Records deleted'; } # if } # _deleteRecord -sub _updateRecord ($$%) { +sub _updateRecord($$%) { my ($self, $table, $condition, %rec) = @_; my $statement = "update $table set "; - $statement .= join ',', $self->_formatNameValues (%rec); - $statement .= " where $condition" - if $condition; + $statement .= join ',', $self->_formatNameValues(%rec); + $statement .= " where $condition" if $condition; - $self->{db}->do ($statement); + $self->{db}->do($statement); - return $self->_dberror ("Unable to update record in $table", $statement); + return $self->_dberror("Unable to update record in $table", $statement); } # _updateRecord -sub _checkRequiredFields ($$) { +sub _checkRequiredFields($$) { my ($fields, $rec) = @_; for my $fieldname (@$fields) { @@ -254,24 +251,27 @@ sub _checkRequiredFields ($$) { } # if } # for - return "$fieldname is required" - unless $found; + return "$fieldname is required" unless $found; } # for return; } # _checkRequiredFields -sub _getRecords ($$) { - my ($self, $table, $condition) = @_; +sub _getRecords($$;$) { + my ($self, $table, $condition, $additional) = @_; my ($err, $msg); - my $statement = "select * from $table where $condition"; + $additional ||= ''; + + my $statement = "select * from $table"; + $statement .= " where $condition" if $condition; + $statement .= $additional; - my $sth = $self->{db}->prepare ($statement); + my $sth = $self->{db}->prepare($statement); unless ($sth) { - ($err, $msg) = $self->_dberror ('Unable to prepare statement', $statement); + ($err, $msg) = $self->_dberror('Unable to prepare statement', $statement); croak $msg; } # if @@ -293,7 +293,7 @@ sub _getRecords ($$) { $err = 0; last; } else { - ($err, $msg) = $self->_dberror ('Unable to execute statement', + ($err, $msg) = $self->_dberror('Unable to execute statement', $statement); } # if @@ -303,16 +303,16 @@ sub _getRecords ($$) { my $timestamp = YMDHMS; - $self->Error ("$timestamp: Unable to talk to DB server.\n\n$msg\n\n" + $self->Error("$timestamp: Unable to talk to DB server.\n\n$msg\n\n" . "Will try again in $sleepTime seconds", -1); # Try to reconnect - $self->_connect ($self->{dbserver}); + $self->_connect($self->{dbserver}); sleep $sleepTime; } # while - $self->Error ("After $maxAttempts attempts I could not connect to the database", $err) + $self->Error("After $maxAttempts attempts I could not connect to the database", $err) if ($err == 2006 and $attempts > $maxAttempts); my @records; @@ -322,31 +322,31 @@ sub _getRecords ($$) { } # while return @records; -} # _getRecord +} # _getRecords -sub _aliasSystem ($) { +sub _aliasSystem($) { my ($self, $system) = @_; - my %system = $self->GetSystem ($system); + my %system = $self->GetSystem($system); if ($system{name}) { return $system{name}; } else { - return; + return; } # if } # _aliasSystem -sub _getLastID () { +sub _getLastID() { my ($self) = @_; my $statement = 'select last_insert_id()'; - my $sth = $self->{db}->prepare ($statement); + my $sth = $self->{db}->prepare($statement); my ($err, $msg); unless ($sth) { - ($err, $msg) = $self->_dberror ('Unable to prepare statement', $statement); + ($err, $msg) = $self->_dberror('Unable to prepare statement', $statement); croak $msg; } # if @@ -354,7 +354,7 @@ sub _getLastID () { my $status = $sth->execute; unless ($status) { - ($err, $msg) = $self->_dberror ('Unable to execute statement', $statement); + ($err, $msg) = $self->_dberror('Unable to execute statement', $statement); croak $msg; } # if @@ -366,7 +366,7 @@ sub _getLastID () { return $row[0]; } # _getLastID -sub _connect (;$) { +sub _connect(;$) { my ($self, $dbserver) = @_; $dbserver ||= $CLEAROPTS{CLEARADM_SERVER}; @@ -374,12 +374,12 @@ sub _connect (;$) { my $dbname = 'clearadm'; my $dbdriver = 'mysql'; - $self->{db} = DBI->connect ( + $self->{db} = DBI->connect( "DBI:$dbdriver:$dbname:$dbserver", $CLEAROPTS{CLEARADM_USERNAME}, $CLEAROPTS{CLEARADM_PASSWORD}, {PrintError => 0}, - ) or croak ( + ) or croak( "Couldn't connect to $dbname database " . "as $CLEAROPTS{CLEARADM_USERNAME}\@$CLEAROPTS{CLEARADM_SERVER}" ); @@ -389,17 +389,17 @@ sub _connect (;$) { return; } # _connect -sub new (;$) { +sub new(;$) { my ($class, $dbserver) = @_; my $self = bless {}, $class; - $self->_connect ($dbserver); + $self->_connect($dbserver); return $self; } # new -sub SetNotify () { +sub SetNotify() { my ($self) = @_; $self->{NOTIFY} = $CLEAROPTS{CLEARADM_NOTIFY}; @@ -407,7 +407,7 @@ sub SetNotify () { return; } # SetNotify -sub Error ($;$) { +sub Error($;$) { my ($self, $msg, $errno) = @_; # If $errno is specified we need to stop. However we need to notify somebody @@ -416,7 +416,7 @@ sub Error ($;$) { if ($errno) { if ($self->{NOTIFY}) { - mail ( + mail( to => $self->{NOTIFY}, subject => 'Internal error occurred in Clearadm', data => "
An unexpected, internal error occurred in Clearadm:
$msg
", @@ -430,7 +430,7 @@ sub Error ($;$) { return; } # Error -sub AddSystem (%) { +sub AddSystem(%) { my ($self, %system) = @_; my @requiredFields = ( @@ -439,33 +439,31 @@ sub AddSystem (%) { my $result = _checkRequiredFields \@requiredFields, \%system; - return -1, "AddSystem: $result" - if $result; + return -1, "AddSystem: $result" if $result; $system{loadavgHist} ||= $defaultLoadavgHist; - return $self->_addRecord ('system', %system); + return $self->_addRecord('system', %system); } # AddSystem -sub DeleteSystem ($) { +sub DeleteSystem($) { my ($self, $name) = @_; - return $self->_deleteRecord ('system', "name='$name'"); + return $self->_deleteRecord('system', "name='$name'"); } # DeleteSystem sub UpdateSystem ($%) { my ($self, $name, %update) = @_; - return $self->_updateRecord ('system', "name='$name'", %update); + return $self->_updateRecord('system', "name='$name'", %update); } # UpdateSystem -sub GetSystem ($) { +sub GetSystem($) { my ($self, $system) = @_; - return - unless $system; + return unless $system; - my @records = $self->_getRecords ( + my @records = $self->_getRecords( 'system', "name='$system' or alias like '%$system%'" ); @@ -473,29 +471,29 @@ sub GetSystem ($) { if ($records[0]) { return %{$records[0]}; } else { - return; + return; } # if } # GetSystem -sub FindSystem (;$) { +sub FindSystem(;$) { my ($self, $system) = @_; $system ||= ''; my $condition = "name like '%$system%' or alias like '%$system%'"; - return $self->_getRecords ('system', $condition); + return $self->_getRecords('system', $condition); } # FindSystem -sub SearchSystem (;$) { +sub SearchSystem(;$) { my ($self, $condition) = @_; $condition = "name like '%'" unless $condition; - return $self->_getRecords ('system', $condition); + return $self->_getRecords('system', $condition); } # SearchSystem -sub AddPackage (%) { +sub AddPackage(%) { my ($self, %package) = @_; my @requiredFields = ( @@ -506,43 +504,38 @@ sub AddPackage (%) { my $result = _checkRequiredFields \@requiredFields, \%package; - return -1, "AddPackage: $result" - if $result; + return -1, "AddPackage: $result" if $result; - return $self->_addRecord ('package', %package); + return $self->_addRecord('package', %package); } # AddPackage -sub DeletePackage ($$) { +sub DeletePackage($$) { my ($self, $system, $name) = @_; - return $self->_deleteRecord ( + return $self->_deleteRecord( 'package', "(system='$system' or alias='$system') and name='$name'"); } # DeletePackage -sub UpdatePackage ($$%) { +sub UpdatePackage($$%) { my ($self, $system, $name, %update) = @_; - $system = $self->_aliasSystem ($system); + $system = $self->_aliasSystem($system); - return - unless $system; + return unless $system; - return $self->_updateRecord ('package', "system='$system'", %update); + return $self->_updateRecord('package', "system='$system'", %update); } # UpdatePackage sub GetPackage($$) { my ($self, $system, $name) = @_; - $system = $self->_aliasSystem ($system); + $system = $self->_aliasSystem($system); - return - unless $system; + return unless $system; + return unless $name; - return - unless $name; - - my @records = $self->_getRecords ( + my @records = $self->_getRecords( 'package', "system='$system' and name='$name'" ); @@ -550,26 +543,25 @@ sub GetPackage($$) { if ($records[0]) { return %{$records[0]}; } else { - return; + return; } # if } # GetPackage -sub FindPackage ($;$) { +sub FindPackage($;$) { my ($self, $system, $name) = @_; $name ||= ''; - $system = $self->_aliasSystem ($system); + $system = $self->_aliasSystem($system); - return - unless $system; + return unless $system; my $condition = "system='$system' and name like '%$name%'"; - return $self->_getRecords ('package', $condition); + return $self->_getRecords('package', $condition); } # FindPackage -sub AddFilesystem (%) { +sub AddFilesystem(%) { my ($self, %filesystem) = @_; my @requiredFields = ( @@ -580,56 +572,50 @@ sub AddFilesystem (%) { my $result = _checkRequiredFields \@requiredFields, \%filesystem; - return -1, "AddFilesystem: $result" - if $result; + return -1, "AddFilesystem: $result" if $result; # Default filesystem threshold $filesystem{threshold} ||= $defaultFilesystemThreshold; - return $self->_addRecord ('filesystem', %filesystem); + return $self->_addRecord('filesystem', %filesystem); } # AddFilesystem -sub DeleteFilesystem ($$) { +sub DeleteFilesystem($$) { my ($self, $system, $filesystem) = @_; - $system = $self->_aliasSystem ($system); + $system = $self->_aliasSystem($system); - return - unless $system; + return unless $system; - return $self->_deleteRecord ( + return $self->_deleteRecord( 'filesystem', "system='$system' and filesystem='$filesystem'" ); } # DeleteFilesystem -sub UpdateFilesystem ($$%) { +sub UpdateFilesystem($$%) { my ($self, $system, $filesystem, %update) = @_; - $system = $self->_aliasSystem ($system); + $system = $self->_aliasSystem($system); - return - unless $system; + return unless $system; - return $self->_updateRecord ( + return $self->_updateRecord( 'filesystem', "system='$system' and filesystem='$filesystem'", %update ); } # UpdateFilesystem -sub GetFilesystem ($$) { +sub GetFilesystem($$) { my ($self, $system, $filesystem) = @_; - $system = $self->_aliasSystem ($system); + $system = $self->_aliasSystem($system); - return - unless $system; + return unless $system; + return unless $filesystem; - return - unless $filesystem; - - my @records = $self->_getRecords ( + my @records = $self->_getRecords( 'filesystem', "system='$system' and filesystem='$filesystem'" ); @@ -641,123 +627,167 @@ sub GetFilesystem ($$) { } # if } # GetFilesystem -sub FindFilesystem ($;$) { +sub FindFilesystem($;$) { my ($self, $system, $filesystem) = @_; $filesystem ||= ''; - $system = $self->_aliasSystem ($system); + $system = $self->_aliasSystem($system); - return - unless $system; + return unless $system; my $condition = "system='$system' and filesystem like '%$filesystem%'"; - return $self->_getRecords ('filesystem', $condition); + return $self->_getRecords('filesystem', $condition); } # FindFilesystem -sub AddVob (%) { +sub AddVob(%) { my ($self, %vob) = @_; my @requiredFields = ( - 'system', 'tag', + 'region', ); my $result = _checkRequiredFields \@requiredFields, \%vob; - return -1, "AddVob: $result" - if $result; + return -1, "AddVob: $result" if $result; - return $self->_addRecord ('vob', %vob); + return $self->_addRecord('vob', %vob); } # AddVob -sub DeleteVob ($) { - my ($self, $tag) = @_; +sub DeleteVob($$) { + my ($self, $tag, $region) = @_; - return $self->_deleteRecord ('vob', "tag='$tag'"); + return $self->_deleteRecord('vob', "tag='$tag' and region='$region'"); } # DeleteVob -sub GetVob ($) { - my ($self, $tag) = @_; +sub GetVob($$) { + my ($self, $tag, $region) = @_; - return - unless $tag; + return unless $tag; - my @records = $self->_getRecords ('vob', "tag='$tag'"); + # Windows vob tags begin with "\", which is problematic. The solution is to + # escape the "\" + $tag =~ s/^\\/\\\\/; + + my @records = $self->_getRecords('vob', "tag='$tag' and region='$region'"); if ($records[0]) { return %{$records[0]}; } else { - return; + return; } # if } # GetVob -sub FindVob ($) { - my ($self, $tag) = @_; +<<<<<<< HEAD +sub FindVobStorage(;$$) { + my ($self, $tag, $region) = @_; + + $tag ||= ''; + $region ||= ''; - return $self->_getRecords ('vob', "tag like '%$tag%'"); +======= +sub FindVob($;$) { + my ($self, $tag, $region) = @_; + + # Windows vob tags begin with "\", which is problematic. The solution is to + # escape the "\" + $tag =~ s/^\\/\\\\/; + + my $condition = "tag like '%$tag%'"; + + $condition .= " and region='$region'" if $region; + + return $self->_getRecords('vobstorage', $condition); +} # FindVobStorage + +sub FindVob(;$$) { + my ($self, $tag, $region) = @_; + + $tag ||= ''; + $region ||= ''; + + # Windows vob tags begin with "\", which is problematic. The solution is to + # escape the "\" + $tag =~ s/^\\/\\\\/; + + my $condition = "tag like '%$tag%'"; + + $condition .= " and region='$region'" if $region; + + return $self->_getRecords('vob', $condition); } # FindVob -sub AddView (%) { +sub UpdateVob(%) { + my ($self, %vob) = @_; + + # Windows vob tags begin with "\", which is problematic. The solution is to + # escape the "\" + my $vobtag = $vob{tag}; + + $vobtag =~ s/^\\/\\\\/; + + return $self->_updateRecord('vob', "tag='$vobtag' and region='$vob{region}'", %vob); +} # UpdateVob + +sub AddView(%) { my ($self, %view) = @_; my @requiredFields = ( - 'system', 'tag', + 'region' ); my $result = _checkRequiredFields \@requiredFields, \%view; - return -1, "AddView: $result" - if $result; + return -1, "AddView: $result" if $result; - return $self->_addRecord ('view', %view); + return $self->_addRecord('view', %view); } # AddView -sub DeleteView ($) { - my ($self, $tag) = @_; +sub DeleteView($$) { + my ($self, $tag, $region) = @_; - return $self->_deleteRecord ('vob', "tag='$tag'"); + return $self->_deleteRecord('vob', "tag='$tag' and region='$region'"); } # DeleteView -sub GetView ($) { - my ($self, $tag) = @_; +sub UpdateView(%) { + my ($self, %view) = @_; + + return $self->_updateRecord('view', "tag='$view{tag}' and region='$view{region}'", %view); +} # UpdateView - return - unless $tag; +sub GetView($$) { + my ($self, $tag, $region) = @_; - my @records = $self->_getRecords ('view', "tag='$tag'"); + return unless $tag; + + my @records = $self->_getRecords('view', "tag='$tag' and region='$region'"); if ($records[0]) { return %{$records[0]}; } else { - return; + return; } # if } # GetView -sub FindView (;$$$$) { - my ($self, $system, $region, $tag, $ownerName) = @_; - - $system ||= ''; - $region ||= ''; - $tag ||= ''; - $ownerName ||= ''; +sub FindView(;$$$$) { + my ($self, $tag, $region, $ownerName) = @_; my $condition; + my @conditions; - $condition = "system like '%$system%'"; - $condition .= ' and '; - $condition = "region like '%$region%'"; - $condition .= ' and '; - $condition .= "tag like '%$tag'"; - $condition .= ' and '; - $condition .= "ownerName like '%$ownerName'"; + push @conditions, "tag like '%$tag%'" if $tag; + push @conditions, "region = '$region'" if $region; + push @conditions, "ownerName like '$ownerName'" if $ownerName; - return $self->_getRecords ('view', $condition); + $condition = join " and ", @conditions if @conditions; + + return $self->_getRecords('view', $condition); } # FindView -sub AddFS (%) { +sub AddFS(%) { my ($self, %fs) = @_; my @requiredFields = ( @@ -773,20 +803,19 @@ sub AddFS (%) { # Timestamp record $fs{timestamp} = Today2SQLDatetime; - return $self->_addRecord ('fs', %fs); + return $self->_addRecord('fs', %fs); } # AddFS -sub TrimFS ($$) { +sub TrimFS($$) { my ($self, $system, $filesystem) = @_; - my %filesystem = $self->GetFilesystem ($system, $filesystem); + my %filesystem = $self->GetFilesystem($system, $filesystem); - return - unless %filesystem; + return unless %filesystem; - my %task = $self->GetTask ('scrub'); + my %task = $self->GetTask('scrub'); - $self->Error ("Unable to find scrub task!", 1) unless %task; + $self->Error("Unable to find scrub task!", 1) unless %task; my $days; my $today = Today2SQLDatetime; @@ -801,14 +830,13 @@ sub TrimFS ($$) { my $oldage = SubtractDays $today, $days; - my ($dberr, $dbmsg) = $self->_deleteRecord ( + my ($dberr, $dbmsg) = $self->_deleteRecord( 'fs', "system='$system' and filesystem='$filesystem' and timestamp<='$oldage'" ); if ($dbmsg eq 'Records deleted') { - return (0, $dbmsg) - if $dberr == 0; + return (0, $dbmsg) if $dberr == 0; my %runlog; @@ -818,25 +846,24 @@ sub TrimFS ($$) { $runlog{message} = "Scrubbed $dberr fs records for filesystem $system:$filesystem"; - my ($err, $msg) = $self->AddRunlog (%runlog); + my ($err, $msg) = $self->AddRunlog(%runlog); - $self->Error ("Unable to add runlog - (Error: $err)\n$msg") if $err; + $self->Error("Unable to add runlog - (Error: $err)\n$msg") if $err; } # if return ($dberr, $dbmsg); } # TrimFS -sub TrimLoadavg ($) { +sub TrimLoadavg($) { my ($self, $system) = @_; - my %system = $self->GetSystem ($system); + my %system = $self->GetSystem($system); - return - unless %system; + return unless %system; - my %task = $self->GetTask ('loadavg'); + my %task = $self->GetTask('loadavg'); - $self->Error ("Unable to find loadavg task!", 1) unless %task; + $self->Error("Unable to find loadavg task!", 1) unless %task; my $days; my $today = Today2SQLDatetime; @@ -851,14 +878,13 @@ sub TrimLoadavg ($) { my $oldage = SubtractDays $today, $days; - my ($dberr, $dbmsg) = $self->_deleteRecord ( + my ($dberr, $dbmsg) = $self->_deleteRecord( 'loadavg', "system='$system' and timestamp<='$oldage'" ); if ($dbmsg eq 'Records deleted') { - return (0, $dbmsg) - if $dberr == 0; + return (0, $dbmsg) if $dberr == 0; my %runlog; @@ -868,24 +894,119 @@ sub TrimLoadavg ($) { $runlog{message} = "Scrubbed $dberr loadavg records for system $system"; - my ($err, $msg) = $self->AddRunlog (%runlog); + my ($err, $msg) = $self->AddRunlog(%runlog); - $self->Error ("Unable to add runload (Error: $err)\n$msg") if $err; + $self->Error("Unable to add runload (Error: $err)\n$msg") if $err; } # if return ($dberr, $dbmsg); } # TrimLoadavg -sub GetFS ($$;$$$$) { - my ($self, $system, $filesystem, $start, $end, $count, $interval) = @_; +sub TrimStorage($$$) { + my ($self, $type, $tag, $region) = @_; - $system = $self->_aliasSystem ($system); + my $today = Today2SQLDatetime; - return - unless $system; + my $oldage = SubtractDays $today, $Clearadm::CLEAROPTS{CLEARADM_SCRUBDAYS}; - return - unless $filesystem; + my $table = $type =~ /vob/i + ? 'vobstorage' + : 'viewstorage'; + + my ($dberr, $dbmsg) = $self->_deleteRecord( + $table, + "tag='$tag' and region='$region' and timestamp<='$oldage'" + ); + + if ($dbmsg eq 'Records deleted') { + return (0, $dbmsg) if $dberr == 0; + + my %runlog; + + $runlog{task} = 'Scrub'; + $runlog{started} = $today; + $runlog{status} = 0; + $runlog{message} = + "Scrubbed $dberr ${type}storage records"; + + my ($err, $msg) = $self->AddRunlog(%runlog); + + $self->Error("Unable to add runload (Error: $err)\n$msg") if $err; + } # if + + return ($dberr, $dbmsg); +} # TrimStorage + +sub ValidateCCObjects() { + my ($self) = @_; + + my $vobRecordsDeleted = 0; + my $viewRecordsDeleted = 0; + + for my $region ($Clearcase::CC->regions) { + for my $type (qw(vob view)) { + my @ccobjs; + verbose "Processing ${type}s in $region"; + + if ($type eq 'vob') { + verbose "Finding all vobs in region $region"; + @ccobjs = $self->FindVob(undef, $region); + verbose 'Found ' . scalar @ccobjs . ' vobs to process'; + } elsif ($type eq 'view') { + verbose "Finding all views in region $region"; + @ccobjs = $self->FindView(undef, $region); + verbose 'Found ' . scalar @ccobjs . ' views to process'; + } # if + + for my $object (@ccobjs) { + my %ccobjrec = %$object; + + verbose "Processing $ccobjrec{tag}:$ccobjrec{region}"; + + my $ccobj; + + if ($type eq 'vob') { + $ccobj = Clearcase::Vob->new($ccobjrec{tag}, $ccobjrec{region}); + } else { + $ccobj = Clearcase::View->new($ccobjrec{tag}, $ccobjrec{region}); + } # if + + verbose_nolf 'Checking if ' . $ccobj->{tag} . ':' . $ccobj->{region} . ' exists anymore...'; + + if ($ccobj->exists) { + verbose ' it does! Skipping...'; + next; + } else { + verbose ' it doesn\'t!'; + } # if + + #next if $ccobj->exists; + + verbose "Deleting $type $ccobjrec{tag}:$ccobjrec{region}"; + + my ($recordsDeleted, $msg) = $self->_deleteRecord($type, + "tag='$ccobjrec{tag}' and region='$ccobjrec{region}'"); + + if ($msg ne 'Records deleted') { + return ($recordsDeleted, $msg); + } else { + $viewRecordsDeleted += $recordsDeleted if $type eq 'view'; + $vobRecordsDeleted += $recordsDeleted if $type eq 'vob'; + } # if + } # for + } # for + } # for + + return ($viewRecordsDeleted, $vobRecordsDeleted); +} # ValidateCCObjects + +sub GetFS($$;$$$$) { + my ($self, $system, $filesystem, $start, $end, $count, $interval) = @_; + + $system = $self->_aliasSystem($system); + + return unless $system; + return unless $filesystem; $interval ||= 'Minute'; @@ -912,7 +1033,7 @@ sub GetFS ($$;$$$$) { # returns 40 rows we'll see only rows 1-10, not rows 31-40). We need limit # $offset, $count where $offset = the number of qualifying records minus # $count - my $nbrRecs = $self->Count ('fs', $condition); + my $nbrRecs = $self->Count('fs', $condition); my $offset = $nbrRecs - $count; # Offsets of < 0 are not allowed. @@ -939,10 +1060,10 @@ END my ($err, $msg); - my $sth = $self->{db}->prepare ($statement); + my $sth = $self->{db}->prepare($statement); unless ($sth) { - ($err, $msg) = $self->_dberror ('Unable to prepare statement', $statement); + ($err, $msg) = $self->_dberror('Unable to prepare statement', $statement); croak $msg; } # if @@ -950,7 +1071,7 @@ END my $status = $sth->execute; unless ($status) { - ($err, $msg) = $self->_dberror ('Unable to execute statement', $statement); + ($err, $msg) = $self->_dberror('Unable to execute statement', $statement); croak $msg; } # if @@ -964,31 +1085,28 @@ END return @records; } # GetFS -sub GetLatestFS ($$) { +sub GetLatestFS($$) { my ($self, $system, $filesystem) = @_; - $system = $self->_aliasSystem ($system); + $system = $self->_aliasSystem($system); - return - unless $system; - - return - unless $filesystem; + return unless $system; + return unless $filesystem; - my @records = $self->_getRecords ( + my @records = $self->_getRecords( 'fs', "system='$system' and filesystem='$filesystem'" . " order by timestamp desc limit 0, 1", ); if ($records[0]) { - return %{$records[0]}; + return %{$records[0]}; } else { - return; + return; } # if } # GetLatestFS -sub AddLoadavg () { +sub AddLoadavg() { my ($self, %loadavg) = @_; my @requiredFields = ( @@ -997,22 +1115,20 @@ sub AddLoadavg () { my $result = _checkRequiredFields \@requiredFields, \%loadavg; - return -1, "AddLoadavg: $result" - if $result; + return -1, "AddLoadavg: $result" if $result; # Timestamp record $loadavg{timestamp} = Today2SQLDatetime; - return $self->_addRecord ('loadavg', %loadavg); + return $self->_addRecord('loadavg', %loadavg); } # AddLoadavg -sub GetLoadavg ($;$$$$) { +sub GetLoadavg($;$$$$) { my ($self, $system, $start, $end, $count, $interval) = @_; - $system = $self->_aliasSystem ($system); + $system = $self->_aliasSystem($system); - return - unless $system; + return unless $system; $interval ||= 'Minute'; @@ -1041,12 +1157,11 @@ sub GetLoadavg ($;$$$$) { # returns 40 rows we'll see only rows 1-10, not rows 31-40). We need limit # $offset, $count where $offset = the number of qualifying records minus # $count - my $nbrRecs = $self->Count ('loadavg', $condition); + my $nbrRecs = $self->Count('loadavg', $condition); my $offset = $nbrRecs - $count; # Offsets of < 0 are not allowed. - $offset = 0 - if $offset < 0; + $offset = 0 if $offset < 0; $condition .= " limit $offset, $count"; } # if @@ -1065,10 +1180,10 @@ END my ($err, $msg); - my $sth = $self->{db}->prepare ($statement); + my $sth = $self->{db}->prepare($statement); unless ($sth) { - ($err, $msg) = $self->_dberror ('Unable to prepare statement', $statement); + ($err, $msg) = $self->_dberror('Unable to prepare statement', $statement); croak $msg; } # if @@ -1076,7 +1191,7 @@ END my $status = $sth->execute; unless ($status) { - ($err, $msg) = $self->_dberror ('Unable to execute statement', $statement); + ($err, $msg) = $self->_dberror('Unable to execute statement', $statement); croak $msg; } # if @@ -1090,15 +1205,14 @@ END return @records; } # GetLoadvg -sub GetLatestLoadavg ($) { +sub GetLatestLoadavg($) { my ($self, $system) = @_; - $system = $self->_aliasSystem ($system); + $system = $self->_aliasSystem($system); - return - unless $system; + return unless $system; - my @records = $self->_getRecords ( + my @records = $self->_getRecords( 'loadavg', "system='$system'" . " order by timestamp desc limit 0, 1", @@ -1111,7 +1225,92 @@ sub GetLatestLoadavg ($) { } # if } # GetLatestLoadavg -sub AddTask (%) { +sub GetStoragePool($$$;$$$$$) { + my ($self, $type, $tag, $storage, $region, $start, $end, $count, $interval) = @_; + + $interval ||= 'Day'; + $region ||= $Clearcase::CC->region; + + return unless $type =~ /vob/i or $type =~ /view/; + + my $size = $interval =~ /month/i + ? 7 + : $interval =~ /day/i + ? 10 + : $interval =~ /hour/i + ? 13 + : 16; + + undef $start if $start and $start =~ /earliest/i; + undef $end if $end and $end =~ /latest/i; + + # Windows vob tags begin with "\", which is problematic. The solution is to + # escape the "\" + $tag =~ s/^\\/\\\\/; + + my $condition; + my $table = $type eq 'vob' ? 'vobstorage' : 'viewstorage'; + + $condition = "tag='$tag' and region='$region'"; + $condition .= " and timestamp>='$start'" if $start; + $condition .= " and timestamp<='$end'" if $end; + + $condition .= " group by left(timestamp,$size)"; + + if ($count) { + # We can't simply do a "limit 0, $count" as that just gets the front end of + # the records return (i.e. if $count = say 10 and the timestamp range + # returns 40 rows we'll see only rows 1-10, not rows 31-40). We need limit + # $offset, $count where $offset = the number of qualifying records minus + # $count + my $nbrRecs = $self->Count($table, $condition); + my $offset = $nbrRecs - $count; + + # Offsets of < 0 are not allowed. + $offset = 0 if $offset < 0; + + $condition .= " limit $offset, $count"; + } # if + + my $statement = <<"END"; +select + tag, + region, + left(timestamp,$size) as timestamp, + avg($storage) as size +from + $table + where $condition +END + + my ($err, $msg); + + my $sth = $self->{db}->prepare($statement); + + unless ($sth) { + ($err, $msg) = $self->_dberror('Unable to prepare statement', $statement); + + croak $msg; + } # if + + my $status = $sth->execute; + + unless ($status) { + ($err, $msg) = $self->_dberror('Unable to execute statement', $statement); + + croak $msg; + } # if + + my @records; + + while (my $row = $sth->fetchrow_hashref) { + push @records, $row; + } # while + + return @records; +} # GetStoragePool + +sub AddTask(%) { my ($self, %task) = @_; my @requiredFields = ( @@ -1121,35 +1320,33 @@ sub AddTask (%) { my $result = _checkRequiredFields \@requiredFields, \%task; - return -1, "AddTask: $result" - if $result; + return -1, "AddTask: $result" if $result; - return $self->_addRecord ('task', %task); + return $self->_addRecord('task', %task); } # AddTask -sub DeleteTask ($) { +sub DeleteTask($) { my ($self, $name) = @_; - return $self->_deleteRecord ('task', "name='$name'"); + return $self->_deleteRecord('task', "name='$name'"); } # DeleteTask -sub FindTask ($) { +sub FindTask($) { my ($self, $name) = @_; $name ||= ''; my $condition = "name like '%$name%'"; - return $self->_getRecords ('task', $condition); + return $self->_getRecords('task', $condition); } # FindTask -sub GetTask ($) { +sub GetTask($) { my ($self, $name) = @_; - return - unless $name; + return unless $name; - my @records = $self->_getRecords ('task', "name='$name'"); + my @records = $self->_getRecords('task', "name='$name'"); if ($records[0]) { return %{$records[0]}; @@ -1158,13 +1355,13 @@ sub GetTask ($) { } # if } # GetTask -sub UpdateTask ($%) { +sub UpdateTask($%) { my ($self, $name, %update) = @_; - return $self->_updateRecord ('task', "name='$name'", %update); + return $self->_updateRecord('task', "name='$name'", %update); } # Update -sub AddSchedule (%) { +sub AddSchedule(%) { my ($self, %schedule) = @_; my @requiredFields = ( @@ -1173,35 +1370,34 @@ sub AddSchedule (%) { my $result = _checkRequiredFields \@requiredFields, \%schedule; - return -1, "AddSchedule: $result" - if $result; + return -1, "AddSchedule: $result" if $result; - return $self->_addRecord ('schedule', %schedule); + return $self->_addRecord('schedule', %schedule); } # AddSchedule -sub DeleteSchedule ($) { +sub DeleteSchedule($) { my ($self, $name) = @_; - return $self->_deleteRecord ('schedule', "name='$name'"); + return $self->_deleteRecord('schedule', "name='$name'"); } # DeleteSchedule -sub FindSchedule (;$$) { +sub FindSchedule(;$$) { my ($self, $name, $task) = @_; $name ||= ''; - $task||= ''; + $task ||= ''; my $condition = "name like '%$name%'"; $condition .= ' and '; $condition .= "task like '%$task%'"; - return $self->_getRecords ('schedule', $condition); + return $self->_getRecords('schedule', $condition); } # FindSchedule -sub GetSchedule ($) { +sub GetSchedule($) { my ($self, $name) = @_; - my @records = $self->_getRecords ('schedule', "name='$name'"); + my @records = $self->_getRecords('schedule', "name='$name'"); if ($records[0]) { return %{$records[0]}; @@ -1210,13 +1406,13 @@ sub GetSchedule ($) { } # if } # GetSchedule -sub UpdateSchedule ($%) { +sub UpdateSchedule($%) { my ($self, $name, %update) = @_; - return $self->_updateRecord ('schedule', "name='$name'", %update); + return $self->_updateRecord('schedule', "name='$name'", %update); } # UpdateSchedule -sub AddRunlog (%) { +sub AddRunlog(%) { my ($self, %runlog) = @_; my @requiredFields = ( @@ -1225,69 +1421,71 @@ sub AddRunlog (%) { my $result = _checkRequiredFields \@requiredFields, \%runlog; - return -1, "AddRunlog: $result" - if $result; + return -1, "AddRunlog: $result" if $result; $runlog{ended} = Today2SQLDatetime; - my ($err, $msg) = $self->_addRecord ('runlog', %runlog); + $runlog{system} = hostname if $runlog{system} =~ /localhost/i; + + my ($err, $msg) = $self->_addRecord('runlog', %runlog); return ($err, $msg, $self->_getLastID); } # AddRunlog -sub DeleteRunlog ($) { +sub DeleteRunlog($) { my ($self, $condition) = @_; - return $self->_deleteRecord ('runlog', $condition); + return $self->_deleteRecord('runlog', $condition); } # DeleteRunlog -sub FindRunlog (;$$$$$$) { +sub FindRunlog(;$$$$$$) { my ($self, $task, $system, $status, $id, $start, $page) = @_; - $task ||= ''; - # If ID is specified then that's all that really matters as it uniquely # identifies a runlog entry; - my $condition; + my ($condition, $conditions); + my $limit = ''; unless ($id) { - $condition = "task like '%$task%'"; + if ($task !~ /all/i) { + $conditions++; + $condition = "task like '%$task%'"; + } # if - if ($system) { - $condition .= " and system like '%$system%'" - unless $system eq 'All'; - } else { - $condition .= ' and system is null'; - } # unless + if ($system !~ /all/i) { + $condition .= ' and ' if $conditions; + $condition .= "system like '%$system%'"; + $conditions++; + } # if + + if ($status) { + $condition .= ' and ' if $conditions; - if (defined $status) { if ($status =~ /!(-*\d+)/) { - $condition .= " and status<>$1"; + $condition .= "status<>$1"; } else { - $condition .= " and status=$status" + $condition .= "status=$status" } # if } # if - $condition .= " order by started desc"; - + # Need defined here as $start may be 0! if (defined $start) { $page ||= 10; - $condition .= " limit $start, $page"; + $limit = "limit $start, $page"; } # unless } else { $condition = "id=$id"; } # unless - return $self->_getRecords ('runlog', $condition); + return $self->_getRecords('runlog', $condition, " order by started desc $limit"); } # FindRunlog -sub GetRunlog ($) { +sub GetRunlog($) { my ($self, $id) = @_; - return - unless $id; + return unless $id; - my @records = $self->_getRecords ('runlog', "id=$id"); + my @records = $self->_getRecords('runlog', "id=$id"); if ($records[0]) { return %{$records[0]}; @@ -1296,13 +1494,13 @@ sub GetRunlog ($) { } # if } # GetRunlog -sub UpdateRunlog ($%) { +sub UpdateRunlog($%) { my ($self, $id, %update) = @_; - return $self->_updateRecord ('runlog', "id=$id", %update); + return $self->_updateRecord('runlog', "id=$id", %update); } # UpdateRunlog -sub Count ($;$) { +sub Count($;$) { my ($self, $table, $condition) = @_; $condition = $condition ? 'where ' . $condition : ''; @@ -1311,10 +1509,10 @@ sub Count ($;$) { my $statement = "select count(*) from $table $condition"; - my $sth = $self->{db}->prepare ($statement); + my $sth = $self->{db}->prepare($statement); unless ($sth) { - ($err, $msg) = $self->_dberror ('Unable to prepare statement', $statement); + ($err, $msg) = $self->_dberror('Unable to prepare statement', $statement); croak $msg; } # if @@ -1322,7 +1520,7 @@ sub Count ($;$) { my $status = $sth->execute; unless ($status) { - ($err, $msg) = $self->_dberror ('Unable to execute statement', $statement); + ($err, $msg) = $self->_dberror('Unable to execute statement', $statement); croak $msg; } # if @@ -1363,7 +1561,7 @@ sub Count ($;$) { # execute the work to be done, timing it, and subtracting it from the $sleep # time returned. If the caller exhausts the $sleep time then they should call # us again. -sub GetWork () { +sub GetWork() { my ($self) = @_; my ($err, $msg); @@ -1386,10 +1584,10 @@ where order by lastrun END - my $sth = $self->{db}->prepare ($statement); + my $sth = $self->{db}->prepare($statement); unless ($sth) { - ($err, $msg) = $self->_dberror ('Unable to prepare statement', $statement); + ($err, $msg) = $self->_dberror('Unable to prepare statement', $statement); croak $msg; } # if @@ -1397,7 +1595,7 @@ END my $status = $sth->execute; unless ($status) { - ($err, $msg) = $self->_dberror ('Unable to execute statement', $statement); + ($err, $msg) = $self->_dberror('Unable to execute statement', $statement); croak $msg; } # if @@ -1407,7 +1605,7 @@ END while (my $row = $sth->fetchrow_hashref) { if ($$row{system} !~ /localhost/i) { - my %system = $self->GetSystem ($$row{system}); + my %system = $self->GetSystem($$row{system}); # Skip inactive systems next if $system{active} eq 'false'; @@ -1436,8 +1634,8 @@ END } # if my $today = Today2SQLDatetime; - my $lastrun = Add ($$row{lastrun}, (seconds => $seconds)); - my $waitTime = DateToEpoch ($lastrun) - DateToEpoch ($today); + my $lastrun = Add($$row{lastrun}, (seconds => $seconds)); + my $waitTime = DateToEpoch($lastrun) - DateToEpoch($today); if ($waitTime < 0) { # We're late - push this onto records and move on @@ -1462,17 +1660,17 @@ END return ($sleep, @records); } # GetWork -sub GetUniqueList ($$) { +sub GetUniqueList($$) { my ($self, $table, $field) = @_; my ($err, $msg); my $statement = "select $field from $table group by $field"; - my $sth = $self->{db}->prepare ($statement); + my $sth = $self->{db}->prepare($statement); unless ($sth) { - ($err, $msg) = $self->_dberror ('Unable to prepare statement', $statement); + ($err, $msg) = $self->_dberror('Unable to prepare statement', $statement); croak $msg; } # if @@ -1480,7 +1678,7 @@ sub GetUniqueList ($$) { my $status = $sth->execute; unless ($status) { - ($err, $msg) = $self->_dberror ('Unable to execute statement', $statement); + ($err, $msg) = $self->_dberror('Unable to execute statement', $statement); croak $msg; } # if @@ -1508,35 +1706,34 @@ sub AddAlert(%) { my $result = _checkRequiredFields \@requiredFields, \%alert; - return -1, "AddAlert: $result" - if $result; + return -1, "AddAlert: $result" if $result; - return $self->_addRecord ('alert', %alert); + return $self->_addRecord('alert', %alert); } # AddAlert -sub DeleteAlert ($) { +sub DeleteAlert($) { my ($self, $name) = @_; - return $self->_deleteRecord ('alert', "name='$name'"); + return $self->_deleteRecord('alert', "name='$name'"); } # DeleteAlert -sub FindAlert (;$) { +sub FindAlert(;$) { my ($self, $alert) = @_; $alert ||= ''; my $condition = "name like '%$alert%'"; - return $self->_getRecords ('alert', $condition); + return $self->_getRecords('alert', $condition); } # FindAlert -sub GetAlert ($) { +sub GetAlert($) { my ($self, $name) = @_; return unless $name; - my @records = $self->_getRecords ('alert', "name='$name'"); + my @records = $self->_getRecords('alert', "name='$name'"); if ($records[0]) { return %{$records[0]}; @@ -1545,7 +1742,7 @@ sub GetAlert ($) { } # if } # GetAlert -sub SendAlert ($$$$$$$) { +sub SendAlert($$$$$$$) { my ( $self, $alert, @@ -1563,12 +1760,12 @@ sub SendAlert ($$$$$$$) { $footing .= "ClearadmYou will receive this alert no more than $nomorethan.
"; - ($err, $msg) = $self->SendAlert ( + ($err, $msg) = $self->SendAlert( $notification{alert}, $system, $notification{name}, @@ -1797,29 +1993,29 @@ sub Notify ($$$$$$) { $runlogID, ); - $self->Error ("Unable to send alert (Status: $err)\n$msg", $err) if $err; + $self->Error("Unable to send alert (Status: $err)\n$msg", $err) if $err; verbose "Sent alert to $to"; # Update runlog to indicate we notified the user for this execution - ($err, $msg) = $self->UpdateRunlog ( + ($err, $msg) = $self->UpdateRunlog( $runlogID, ( alerted => 'true', ), ); - $self->Error ("Unable to update runlog (Status: $err)\n$msg", $err) if $err; + $self->Error("Unable to update runlog (Status: $err)\n$msg", $err) if $err; return; } # Notify -sub ClearNotifications ($$;$) { +sub ClearNotifications($$;$) { my ($self, $system, $filesystem) = @_; my ($err, $msg); if ($filesystem) { - ($err, $msg) = $self->UpdateFilesystem ( + ($err, $msg) = $self->UpdateFilesystem( $system, $filesystem, (notification => undef), ); @@ -1834,57 +2030,54 @@ sub ClearNotifications ($$;$) { # 'Filesystem' then we can toggle off the notification on the system too my $filesystemsAlerted = 0; - for ($self->FindFilesystem ($system)) { + for ($self->FindFilesystem($system)) { $filesystemsAlerted++ if $$_{notification}; } # for - my %system = $self->GetSystem ($system); + my %system = $self->GetSystem($system); - return - unless $system; + return unless $system; if ($system{notification} and $system{notification} eq 'Filesystem' and $filesystemsAlerted == 0) { - ($err, $msg) = $self->UpdateSystem ($system, (notification => undef)); + ($err, $msg) = $self->UpdateSystem($system, (notification => undef)); - $self->Error ("Unable to clear notification for system $system " + $self->Error("Unable to clear notification for system $system " . "(Status: $err)\n$msg", $err) if $err; } # if } else { - ($err, $msg) = $self->UpdateSystem ($system, (notification => undef)); + ($err, $msg) = $self->UpdateSystem($system, (notification => undef)); - $self->Error ("Unable to clear notification for system $system " + $self->Error("Unable to clear notification for system $system " . "(Status: $err)\n$msg", $err) if $err; } # if return; } # ClearNotifications -sub SystemAlive (%) { +sub SystemAlive(%) { my ($self, %system) = @_; # If we've never heard from this system then we will assume that the system # has not been set up to run clearagent and has never checked in. In any event # we cannot say the system died because we've never known it to be alive! - return 1 - unless $system{lastheardfrom}; + return 1 unless $system{lastheardfrom}; # If a system is not active (may have been temporarily been deactivated) then # we don't want to turn on the bells and whistles alerting people it's down. - return 1 - if $system{active} eq 'false'; + return 1 if $system{active} eq 'false'; my $today = Today2SQLDatetime; my $lastheardfrom = $system{lastheardfrom}; my $tenMinutes = 10 * 60; - $lastheardfrom = Add ($lastheardfrom, (seconds => $tenMinutes)); + $lastheardfrom = Add($lastheardfrom, (seconds => $tenMinutes)); - if (DateToEpoch ($lastheardfrom) < DateToEpoch ($today)) { - $self->UpdateSystem ( + if (DateToEpoch($lastheardfrom) < DateToEpoch($today)) { + $self->UpdateSystem( $system{name}, ( notification => 'Heartbeat' ), @@ -1893,7 +2086,7 @@ sub SystemAlive (%) { return; } else { if ($system{notification}) { - $self->UpdateSystem ( + $self->UpdateSystem( $system{name}, ( notification => undef ), @@ -1903,17 +2096,17 @@ sub SystemAlive (%) { } # if } # SystemAlive -sub UpdateAlert ($%) { +sub UpdateAlert($%) { my ($self, $name, %update) = @_; - return $self->_updateRecord ( + return $self->_updateRecord( 'alert', "name='$name'", %update ); } # UpdateAlert -sub AddAlertlog (%) { +sub AddAlertlog(%) { my ($self, %alertlog) = @_; my @requiredFields = ( @@ -1923,29 +2116,28 @@ sub AddAlertlog (%) { my $result = _checkRequiredFields \@requiredFields, \%alertlog; - return -1, "AddAlertlog: $result" - if $result; + return -1, "AddAlertlog: $result" if $result; # Timestamp record $alertlog{timestamp} = Today2SQLDatetime; - return $self->_addRecord ('alertlog', %alertlog); + return $self->_addRecord('alertlog', %alertlog); } # AddAlertlog -sub DeleteAlertlog ($) { +sub DeleteAlertlog($) { my ($self, $condition) = @_; return unless $condition; if ($condition =~ /all/i) { - return $self->_deleteRecord ('alertlog'); + return $self->_deleteRecord('alertlog'); } else { - return $self->_deleteRecord ('alertlog', $condition); + return $self->_deleteRecord('alertlog', $condition); } # if } # DeleteAlertlog -sub FindAlertlog (;$$$$$) { +sub FindAlertlog(;$$$$$) { my ($self, $alert, $system, $notification, $start, $page) = @_; $alert ||= ''; @@ -1959,21 +2151,20 @@ sub FindAlertlog (;$$$$$) { $condition .= "notification like '%$notification%'"; $condition .= " order by timestamp desc"; - if (defined $start) { - $page ||= 10; - $condition .= " limit $start, $page"; - } # unless + if (defined $start) { + $page ||= 10; + $condition .= " limit $start, $page"; + } # unless - return $self->_getRecords ('alertlog', $condition); + return $self->_getRecords('alertlog', $condition); } # FindAlertLog -sub GetAlertlog ($) { +sub GetAlertlog($) { my ($self, $alert) = @_; - return - unless $alert; + return unless $alert; - my @records = $self->_getRecords ('alertlog', "alert='$alert'"); + my @records = $self->_getRecords('alertlog', "alert='$alert'"); if ($records[0]) { return %{$records[0]}; @@ -1982,17 +2173,17 @@ sub GetAlertlog ($) { } # if } # GetAlertlog -sub UpdateAlertlog ($%) { +sub UpdateAlertlog($%) { my ($self, $alert, %update) = @_; - return $self->_updateRecord ( + return $self->_updateRecord( 'alertlog', "alert='$alert'", %update ); } # UpdateAlertlog -sub AddNotification (%) { +sub AddNotification(%) { my ($self, %notification) = @_; my @requiredFields = ( @@ -2003,19 +2194,18 @@ sub AddNotification (%) { my $result = _checkRequiredFields \@requiredFields, \%notification; - return -1, "AddNotification: $result" - if $result; + return -1, "AddNotification: $result" if $result; - return $self->_addRecord ('notification', %notification); + return $self->_addRecord('notification', %notification); } # AddNotification -sub DeleteNotification ($) { +sub DeleteNotification($) { my ($self, $name) = @_; - return $self->_deleteRecord ('notification', "name='$name'"); + return $self->_deleteRecord('notification', "name='$name'"); } # DeletePackage -sub FindNotification (;$$) { +sub FindNotification(;$$) { my ($self, $name, $cond, $ordering) = @_; $name ||= ''; @@ -2024,16 +2214,15 @@ sub FindNotification (;$$) { $condition .= " and $cond" if $cond; - return $self->_getRecords ('notification', $condition); + return $self->_getRecords('notification', $condition); } # FindNotification -sub GetNotification ($) { +sub GetNotification($) { my ($self, $name) = @_; - return - unless $name; + return unless $name; - my @records = $self->_getRecords ('notification', "name='$name'"); + my @records = $self->_getRecords('notification', "name='$name'"); if ($records[0]) { return %{$records[0]}; @@ -2042,16 +2231,50 @@ sub GetNotification ($) { } # if } # GetNotification -sub UpdateNotification ($%) { +sub UpdateNotification($%) { my ($self, $name, %update) = @_; - return $self->_updateRecord ( + return $self->_updateRecord( 'notification', "name='$name'", %update ); } # UpdateNotification +sub AddVobStorage(%) { + my ($self, %vobstorage) = @_; + + my @requiredFields = ( + 'tag', + ); + + my $result = _checkRequiredFields \@requiredFields, \%vobstorage; + + return -1, "AddVobStorage: $result" if $result; + + # Timestamp record + $vobstorage{timestamp} = Today2SQLDatetime; + + return $self->_addRecord('vobstorage', %vobstorage); +} # AddVobStorage + +sub AddViewStorage(%) { + my ($self, %viewstorage) = @_; + + my @requiredFields = ( + 'tag', + ); + + my $result = _checkRequiredFields \@requiredFields, \%viewstorage; + + return -1, "AddViewStorage: $result" if $result; + + # Timestamp record + $viewstorage{timestamp} = Today2SQLDatetime; + + return $self->_addRecord('viewstorage', %viewstorage); +} # AddViewStorage + 1; =pod