From 30bc521a908fe41135539492837c45dfd0724b2a Mon Sep 17 00:00:00 2001 From: linggao Date: Wed, 28 Nov 2007 19:44:47 +0000 Subject: [PATCH] added cron job functions in Utils.pm. Added monitoring table. Added status column in nodelist table. Added monserver column in noderes table. Added SetAttributesWhere function in Table.pm git-svn-id: https://svn.code.sf.net/p/xcat/code/xcat-core/trunk@91 8638fb3e-16cb-4fca-ae20-7b5d299a9bcd --- perl-xCAT-2.0/xCAT/NotifHandler.pm | 16 ++-- perl-xCAT-2.0/xCAT/Schema.pm | 9 +- perl-xCAT-2.0/xCAT/Table.pm | 103 ++++++++++++++++++++++- perl-xCAT-2.0/xCAT/Utils.pm | 130 ++++++++++++++++++++++++----- 4 files changed, 229 insertions(+), 29 deletions(-) diff --git a/perl-xCAT-2.0/xCAT/NotifHandler.pm b/perl-xCAT-2.0/xCAT/NotifHandler.pm index 8f79b4587..a54a7757f 100644 --- a/perl-xCAT-2.0/xCAT/NotifHandler.pm +++ b/perl-xCAT-2.0/xCAT/NotifHandler.pm @@ -11,7 +11,8 @@ use File::Basename qw(fileparse); # } # } %notif; -$masterpid=$$; +$masterpid; + 1; #------------------------------------------------------------------------------- @@ -42,11 +43,12 @@ sub setup $masterpid=shift; } refreshNotification(); - $SIG{USR1}=\&handleNotifSigal; + + $SIG{USR1}=\&handleNotifSignal; } #-------------------------------------------------------------------------------- -=head3 handleNotifSigal +=head3 handleNotifSignal It is called when the signal is received. It then update the cache with the latest data in the notification table. Arguments: @@ -55,9 +57,9 @@ sub setup none =cut #------------------------------------------------------------------------------- -sub handleNotifSigal { +sub handleNotifSignal { refreshNotification(); - $SIG{USR1}=\&handleNotifSigal; + $SIG{USR1}=\&handleNotifSignal; } #-------------------------------------------------------------------------------- @@ -70,7 +72,9 @@ sub handleNotifSigal { =cut #------------------------------------------------------------------------------- sub sendNotifSignal { - kill('USR1', $masterpid); + if ($masterpid) { + kill('USR1', $masterpid); + } } diff --git a/perl-xCAT-2.0/xCAT/Schema.pm b/perl-xCAT-2.0/xCAT/Schema.pm index 04e0203c1..387cb4ec8 100644 --- a/perl-xCAT-2.0/xCAT/Schema.pm +++ b/perl-xCAT-2.0/xCAT/Schema.pm @@ -58,7 +58,7 @@ package xCAT::Schema; keys => [qw(node)], }, noderes => { - cols => [qw(node servicenode netboot tftpserver nfsserver kernel initrd kcmdline nfsdir serialport installnic primarynic xcatmaster current_osimage next_osimage comments disable)], + cols => [qw(node servicenode netboot tftpserver nfsserver monserver kernel initrd kcmdline nfsdir serialport installnic primarynic xcatmaster current_osimage next_osimage comments disable)], keys => [qw(node)], }, networks => { @@ -78,7 +78,7 @@ package xCAT::Schema; keys => [qw(node switch port)] }, nodelist => { - cols => [qw(node nodetype groups comments disable)], + cols => [qw(node nodetype groups status comments disable)], keys => [qw(node)], }, site => { @@ -105,6 +105,11 @@ package xCAT::Schema; cols => [qw(filename tables tableops comments disable)], keys => [qw(filename)], required => [qw(tables filename)] + }, + monitoring => { + cols => [qw(pname nodestatmon comments disable)], + keys => [qw(pname)], + required => [qw(pname)] } ); diff --git a/perl-xCAT-2.0/xCAT/Table.pm b/perl-xCAT-2.0/xCAT/Table.pm index d606fa333..ccd3bf243 100644 --- a/perl-xCAT-2.0/xCAT/Table.pm +++ b/perl-xCAT-2.0/xCAT/Table.pm @@ -693,7 +693,6 @@ sub setAttribs #notify the interested parties if ($notif == 1) { - #create new data ref my %new_notif_data = %keypairs; foreach (keys %$elems) @@ -706,6 +705,106 @@ sub setAttribs return 0; } +#-------------------------------------------------------------------------- + +=head3 setAttribsWhere + + Description: + This function sets the attributes for the rows selected by the where clause. + Arguments: + Where clause. + Hash reference of column-value pairs to set + Returns: + None + Globals: + Error: + Example: + my $tab = xCAT::Table->new( 'ppc', -create=>1, -autocommit=>1 ); + $updates{'type'} = lc($type); + $updates{'id'} = $lparid; + $updates{'hcp'} = $server; + $updates{'profile'} = $prof; + $updates{'frame'} = $frame; + $updates{'mtms'} = "$model*$serial"; + $tab->setAttribs( "node in ('node1', 'node2', 'node3')", \%updates ); + Comments: + none +=cut +#-------------------------------------------------------------------------------- +sub setAttribsWhere +{ + #Takes three arguments: + #-Where clause + #-Hash reference of column-value pairs to set + my $self = shift; + my $where_clause = shift; + my $elems = shift; + my $cols = ""; + my @bind = (); + my $action; + my @notif_data; + my $qstring = "SELECT * FROM " . $self->{tabname} . " WHERE " . $where_clause; + my @qargs = (); + my $query = $self->{dbh}->prepare($qstring); + $query->execute(@qargs); + + #get the first row + my $data = $query->fetchrow_arrayref(); + if (defined $data){ $action = "u";} + else { return (0, "no rows selected."); } + + #prepare the notification data + my $notif = + xCAT::NotifHandler->needToNotify($self->{tabname}, $action); + if ($notif == 1) + { + #put the column names at the very front + push(@notif_data, $query->{NAME}); + + #copy the data out because fetchall_arrayref overrides the data. + my @first_row = @$data; + push(@notif_data, \@first_row); + #get the rest of the rows + my $temp_data = $query->fetchall_arrayref(); + foreach (@$temp_data) { + push(@notif_data, $_); + } + } + + $query->finish(); + + #update the rows + for my $col (keys %$elems) + { + $cols = $cols . $col . " = ?,"; + push @bind, (($$elems{$col} =~ /NULL/) ? undef: $$elems{$col}); + } + chop($cols); + my $cmd = "UPDATE " . $self->{tabname} . " set $cols where " . $where_clause; + my $sth = $self->{dbh}->prepare($cmd); + my $err = $sth->execute(@bind); + if (not defined($err)) + { + return (undef, $sth->errstr); + } + + #notify the interested parties + if ($notif == 1) + { + #create new data ref + my %new_notif_data = (); + foreach (keys %$elems) + { + $new_notif_data{$_} = $$elems{$_}; + } + xCAT::NotifHandler->notify($action, $self->{tabname}, + \@notif_data, \%new_notif_data); + } + return 0; +} + + + #-------------------------------------------------------------------------- =head3 getNodeAttribs @@ -1249,7 +1348,7 @@ sub delEntries Example: $table = xCAT::Table->new('passwd'); - $tmp=$table->getAttribs({'key'=>'ipmi'},['username','password']; + @tmp=$table->getAttribs({'key'=>'ipmi'},('username','password'); Comments: none diff --git a/perl-xCAT-2.0/xCAT/Utils.pm b/perl-xCAT-2.0/xCAT/Utils.pm index 18d0e8633..f6f19a08c 100644 --- a/perl-xCAT-2.0/xCAT/Utils.pm +++ b/perl-xCAT-2.0/xCAT/Utils.pm @@ -7,25 +7,19 @@ use Data::Dumper; use xCAT::NodeRange; #-------------------------------------------------------------------------------- - =head1 xCAT::Utils =head2 Package Description This program module file, is a set of utilities used by xCAT commands. - - =cut - #-------------------------------------------------------------------------------- - =head3 quote Quote a string, taking into account embedded quotes. This function is most useful when passing string through the shell to another cmd. It handles one level of embedded double quotes, single quotes, and dollar signs. - Arguments: string to quote Returns: @@ -40,9 +34,7 @@ This program module file, is a set of utilities used by xCAT commands. } Comments: none - =cut - #-------------------------------------------------------------------------------- sub quote { @@ -73,11 +65,8 @@ sub quote } #------------------------------------------------------------------------------- - =head3 isAIX - returns 1 if localHost is AIX - Arguments: none Returns: @@ -91,11 +80,9 @@ sub quote if (xCAT::Utils->isAIX()) { blah; } Comments: none - =cut #------------------------------------------------------------------------------- - sub isAIX { if ($^O =~ /^aix/i) { return 1; } @@ -103,11 +90,8 @@ sub isAIX } #------------------------------------------------------------------------------- - =head3 isLinux - returns 1 if localHost is Linux - Arguments: none Returns: @@ -121,11 +105,8 @@ sub isAIX if (xCAT::Utils->isLinux()) { blah; } Comments: none - =cut - #------------------------------------------------------------------------------- - sub isLinux { if ($^O =~ /^linux/i) { return 1; } @@ -409,6 +390,107 @@ sub get_site_attribute return $values; } + + + +#----------------------------------------------------------------------- +=head3 + add_cron_job + This function adds a new cron job. + Arguments: + job--- string in the crontab job format. + Returns: + (code, message) + Globals: + none + Error: + undef + Example: + xCAT::Utils->add_cron_job("*/5 * * * * /usr/bin/myjob"); + Comments: + none + +=cut +#------------------------------------------------------------------------ +sub add_cron_job { + $newentry = shift; + if ($newentry =~ /xCAT::Utils/) { + $newentry=shift; + } + #read the cron tab entries + my @tabs=`/usr/bin/crontab -l 2>/dev/null`; + my @newtabs=(); + foreach(@tabs) { + chomp($_); + # stop adding if it's already there + if ($_ eq $newentry) { return (0, "started"); } + #skip headers for Linux + next if $_ =~ m/^\#.+(DO NOT EDIT THIS FILE|\(.+ installed on |Cron version )/; + push(@newtabs, $_); + } + + #add new entries to the cron tab + push(@newtabs, $newentry); + my $tabname=""; + if (xCAT::Utils::isLinux) { $tabname="-";} + open(CRONTAB, "|/usr/bin/crontab $tabname") or return (1, "cannot open crontab."); + foreach (@newtabs) { print CRONTAB $_."\n"; } + close(CRONTAB); + + return (0, ""); +} + + +#----------------------------------------------------------------------- +=head3 + remove_cron_job + This function removes a new cron job. + Arguments: + job--- a substring that is contained in a crontab entry. + (use crontab -l to see all the job entries.) + Returns: + (code, message) + Globals: + none + Error: + undef + Example: + xCAT::Utils->remove_cron_job("/usr/bin/myjob"); + This will remove any cron job that contains this string. + Comments: + none + +=cut +#------------------------------------------------------------------------ +sub remove_cron_job { + $job = shift; + if ($job =~ /xCAT::Utils/) { + $job=shift; + } + + #read the cron tab entries and remove the one that contains $job + my @tabs=`/usr/bin/crontab -l 2>/dev/null`; + my @newtabs=(); + foreach(@tabs) { + chomp($_); + # stop adding if it's already there + next if index($_, $job, 0) >= 0; + #skip headers for Linux + next if $_ =~ m/^\#.+(DO NOT EDIT THIS FILE|\(.+ installed on |Cron version )/; + push(@newtabs, $_); + } + + #refresh the cron + my $tabname=""; + if (xCAT::Utils::isLinux) { $tabname="-";} + open(CRONTAB, "|/usr/bin/crontab $tabname") or return (1, "cannot open crontab."); + foreach (@newtabs) { print CRONTAB $_."\n"; } + close(CRONTAB); + + return (0, ""); +} + + #------------------------------------------------------------------------------- =head3 runcmd @@ -524,3 +606,13 @@ sub runcmd } 1; + + + + + + + + + +