Never been to CodeSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

s_nodelist.pl

Liste les managed nodes installés sur le serveur OVOW

#
# POLICY: 
#
# CONFIGURATION: N/A
#
# PARAMETRES D'APPEL: N/A
#
# SORTIE: mail
#
# FICHIERS ENTREE:
#
#
# FICHIERS SORTIE:
#
#
# COMMENTAIRES:
#	
#
#
# MODIFICATIONS:
# 15/01/2008    Ajout de trace pour debugging => eval/die
#
#

use strict;
use Getopt::Std;
use Win32;
use Win32::OLE qw( in );
use Win32::OLE('in');

my $RepositoryFile = "\\\\ilbur001\\SrvIBP\\Projet\\Surv_HPOVO (P9SUR024)\\Production\\log\\NodesOn$ENV{COMPUTERNAME}.txt";

if ( $ENV{COMPUTERNAME} =~ /HPO001/ ) {
	$RepositoryFile = "\\\\ilbur001\\SrvIBP\\Projet\\Surv_HPOVO (P9SUR024)\\Production\\log\\old\\NodesOn$ENV{COMPUTERNAME}.txt";
}


my $server      = '';    # connect to local machine
my $debug 	= 'TRUE';
my $log_file    = "$ENV{OVINSTALLDIR}"."temp/s_nodelist.log";

sub TRACE {
	my $f = shift;
	logger( 'TRACE' . "\t" . $f, @_ );
}

sub ERROR {
	my $f = shift;
	logger( 'ERREUR' . "\t" . $f, @_ );
}

sub logger {
	my $f = shift;
	my $msg = sprintf( ts() . "\t" . $f . "\n", @_ );
	print {*STDOUT} $msg;
	open my $desc_log_file , '+>>', $log_file ;
	print {$desc_log_file} $msg ;
	close $desc_log_file;
}

sub ts {
	my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) =
	  localtime( time() );
	return (
		sprintf(
			'%04d-%02d-%02d %02d:%02d:%02d',
			$year + 1900,
			$mon + 1, $mday, $hour, $min, $sec
		)
	);
}


eval {
	open my $file, '>', "$RepositoryFile" or die "Can't open file $RepositoryFile: $!\n";
#--------------------------------
# Récupère un objet SWbemLocator
#-------------------------------
	my $lobj = Win32::OLE->new('WbemScripting.SWbemLocator')
		or die "can't create locator object: " . Win32::OLE->LastError() . "\n";
#----------------------------------------------------------------
# Prépare une connexion avec un niveau de sécurité "impersonate"
#---------------------------------------------------------------
	$lobj->{Security_}->{impersonationlevel} = 3;

#-----------------------------------
# Connexion à l'objet SWbemServices
#----------------------------------
	my $sobj = $lobj->ConnectServer( $server, 'root\hewlettpackard\openview\data' )
		or die "can't create server object: " . Win32::OLE->LastError() . "\n";

	my $wmiquery = "Select * from OV_ManagedNode";

	print "\$wmiquery=$wmiquery\n" if ($debug);

	my $nodelist = $sobj->ExecQuery($wmiquery)
		or die "La requête a echouée " . Win32::OLE->LastError() . "\n";

	my ( $sourceName, $count );
	my ( @listenoeud, @sortedlist );
	foreach my $node ( in $nodelist )
	{
		#next if ( $node->{Caption} =~ m/(Management Server)/ );
		$sourceName = $node->{PrimaryNodeName};
		push @listenoeud, $sourceName;
	}
	@sortedlist = sort @listenoeud;
	foreach my $node (@sortedlist)
	{
		#next if ( $node =~ /ESX/ );
		print {$file} $node."\n" ;
		$count++;
	}

	close $file;

	TRACE($count . " Managed Nodes listed\n");
};
if ( $@ ) {
	ERROR($@);
}

s_nodelistSynth.pl

Liste les managed nodes installés sur les serveurs OVOW feuilles et compare le résultat avec les noeuds du supra-manager

#! Perl\bin\Perl.exe
#
# PERL
# VERSION: "@(#)s_nodelistSynth.pl v1.0 31/08/2006"
#
# FONCTION: Liste les managed nodes installés sur les serveurs OVOW feuilles et compare le résultat avec les noeuds du supra-manager
#
# POLICY: 
#
# CONFIGURATION: N/A
#
# PARAMETRES D'APPEL: N/A
#
# SORTIE: mail
#
# FICHIERS ENTREE:
#
#
# FICHIERS SORTIE:
#
#
# COMMENTAIRES:
#	
#
#
# MODIFICATIONS:
#
#
#

use strict;
use Getopt::Std;
use Win32;
use Win32::OLE qw( in );
use Win32::OLE('in');
use Mail::Sendmail;

my $debug 	= '';

my $RepositoryDirectory = "\\Production\\log";
my $server      = '';    # connect to local machine

my $smtpserver   = '93.93.205.196'; # ibxhrv01.ctr.ibp
my $monikeraddr  = 'IBHPOE00';

my @ListeMail        = ('destinataire@mail.fr');

my (@AllNodes, @AllNodesSorted);
my %AllNodes;
my $manager;

#--------------------------------
# Récupère un objet SWbemLocator
#-------------------------------
my $lobj = Win32::OLE->new('WbemScripting.SWbemLocator')
  or die "can't create locator object: " . Win32::OLE->LastError() . "\n";

#----------------------------------------------------------------
# Prépare une connexion avec un niveau de sécurité "impersonate"
#---------------------------------------------------------------
$lobj->{Security_}->{impersonationlevel} = 3;

#-----------------------------------
# Connexion à l'objet SWbemServices
#----------------------------------
my $sobj = $lobj->ConnectServer( $server, 'root\hewlettpackard\openview\data' )
  or die "can't create server object: " . Win32::OLE->LastError() . "\n";

my $wmiquery = "Select * from OV_ManagedNode";

print "\$wmiquery=$wmiquery\n" if ($debug);

my $nodelist = $sobj->ExecQuery($wmiquery)
  or die "La requête a echouée " . Win32::OLE->LastError();


my ( $sourceName, $count );
my ( @listenoeud, @sortedlist );
foreach my $node ( in $nodelist )
  {
	  	#next if ( $node->{Caption} =~ m/(Management Server)/ );
		$sourceName = $node->{PrimaryNodeName};
		push @listenoeud, $sourceName;
  }
@sortedlist = sort @listenoeud;
foreach my $node (@sortedlist)
{
	#next if ( $node =~ /ESX/ );
	#print $node."\n" ;
	$count++;
}




my @filelist_txt = &checkFileType($RepositoryDirectory, 'txt');
print "\@filelist_txt=@filelist_txt\n" if $debug;
foreach my $file (@filelist_txt)
{
	($manager) = $file =~ m/NodesOn(.*).txt$/;
	print "\$manager=$manager\n" if $debug;;
	open (FILE, $RepositoryDirectory."\\".$file);
	while (<FILE>)
	{		
		chomp();
		#push (@AllNodes,$_);
		$AllNodes{$_}=$manager;
	}
	close(FILE);
}

@AllNodesSorted = sort @AllNodes;

if ($debug)
{
	print "\@AllNodes=@AllNodesSorted\n";
	print "\@AllNodes=>$#AllNodesSorted\n";
	print "\@sortedlist=@sortedlist\n";
	print "\@sortedlist=$#sortedlist\n";
}

my $missingCount = 0;
my $message;

for my $item (keys %AllNodes) {
	unless (grep $item eq $_,@sortedlist ) {
		print "Il manque $item <= $AllNodes{$item}.\n" if $debug;
		$message = $message."Il manque $item <= $AllNodes{$item}.\n";
		$missingCount++;
	}
}
print "Différence de $missingCount machines.\n" if $debug;
$message = $message."\n\nDifférence de $missingCount machines.\n";

$message = "Cohérence dans la déclaration des noeuds OK entre les managers OVOW" if ($missingCount == 0);

foreach my $email (@ListeMail)
{
	&SendEmail($email);
} 
    
sub checkFileType
{
    my ( $pathToCheck, $extension ) = @_;
    my $ret_value = 0;
    my @list;
    opendir DIR,$pathToCheck or die "Can't open directory $pathToCheck: $!\n";
    push @list, grep /\.$extension$/, readdir DIR;
    # $ret_value = 1 if grep /\.$extension$/, readdir DIR;
    return  @list;
}


sub SendEmail
{
    my ($email) = @_;
    #----------------
    # Envoie du mail
    #---------------
    sendmail(
        To      => $email,
        Cc      => '',
        Subject => "Rapport de cohérence sur l'installation des noeuds sur IBHPOE00",
        From    => $monikeraddr,
        Message => $message,
        Server  => $smtpserver,
        delay   => 1,
        retries => 3
    )
    || die "Cannot send mail: $Mail::Sendmail::error\n";
}


VBScript function for editing the registry via WMI

'= ***********************************************************************
'= Function Name: RegWrite( )
'= Purpose:		  Creates a value, if it does not exist, and writes a value
'=
'= Arguments Supplied:  strRoot - registry subtree
'=                      strType - value type
'=                      strPath - path of value
'=                      strName - name of value
'=                      unkValue - value contents (varies by type)
'= Return Value:        <NONE>
'= Function Calls:      <NONE>
'= ***********************************************************************
Function RegWrite( ByVal strRoot, ByVal strType, ByVal strPath, ByVal strName, ByVal unkValue )

  
  Dim hexRoot, intType
  Dim objReg
  Const strComputer = "."

  '= Convert string value into native hexadecimial value
  Select Case strRoot
    Case "HKCR"                hexRoot = &H80000000
    Case "HKCU"                hexRoot = &H80000001
    Case "HKLM"                hexRoot = &H80000002
    Case "HKEY_USERS"          hexRoot = &H80000003
    Case "HKEY_CURRENT_CONFIG" hexRoot = &H80000005
  End Select
  
  '= Convert value type into native integer format
  Select Case strType
    Case "REG_SZ"        intType = 1
    Case "REG_EXPAND_SZ" intType = 2
    Case "REG_BINARY"    intType = 3
    Case "REG_DWORD"     intType = 4
    Case "REG_MULTI_SZ"  intType = 7
  End Select

  '= If in debug mode print out the arguments                                                                                         
  ' If blnDebug Then
'     WScript.StdOut.WriteLine "Root = " & Hex(hexRoot)
'     WScript.StdOut.WriteLine "Type = " & intType
'     WScript.StdOut.WriteLine "Path = " & strPath
'     WScript.StdOut.WriteLine "Name = " & strName
'     WScript.StdOut.WriteLine "Value = " & unkValue
'   End If
  
  '= Connect to WMI of specified target
  Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
      strComputer & "\root\default:StdRegProv")
  
  '= Create Key if it does not already exist
  objReg.CreateKey hexRoot, strPath
    
  '= Call Write methode depending on value type  
  Select Case intType
    Case 1               
      objReg.SetStringValue hexRoot, strPath, strName, unkValue
    Case 2
      objReg.SetExpandedStringValue hexRoot, strPath, strName, unkValue
    Case 3
      '= NOTE: Writing BINARY reg types is not available
    Case 4
      objReg.SetDWORDValue hexRoot, strPath, strName, unkValue
    Case 7
      objReg.SetMultiStringValue hexRoot, strPath, strName, unkValue 
  End Select
  
  '= If error occurs, then return 1 for the value of the function
  '= NOTE: WMI does not appear to return any error objects.
  If Err.Number <> 0 Then
    WScript.Echo "Error: " & Err.Number & " - " & Err.Description
    RegWrite = 1
  End If

End Function
'= ***********************************************************************