Sunday, 1 November 2015

Cdot Log/MLog Log Collector

Something I’ve been meaning to write for a while. Originally I was going to use Invoke-WebRequest like here, but couldn’t get it to work this time, and was impatient to crack on, so switched to Read-NcFile. Unfortunately Read-NcDirectory doesn’t work against nodes so we start checking at log 0000000001 and continue until we find the last log. The tool was originally written for 8.2.3 but has been expanded to include the extra logs in 8.3.1, and has been tested against both 8.2.3 and 8.3.1.

Image: CdotLogCollector.ps1 in action

The Script

Param(
  [String]$LOGorMLOG,
  [String]$LOGFILE,
  [String]$CLUSTER,
  [String]$USERNAME
)

############################
## BASIC DISPLAY FUNCTION ##
############################

FUNCTION Wr { Param([String]$ToDisplay,[String]$Color = "WHITE")
  IF($ToDisplay){ Write-Host $ToDisplay -ForegroundColor $Color -NoNewLine } ELSE { Write-Host } }

################### 
## TYPES OF LOGS ##
###################

Wr; Wr "<<< cDOT LOG/MLOG LOG COLLECTOR >>>" MAGENTA; Wr; Wr
Wr "Logs in 8.3.1 and not in 8.2.X: " CYAN; Wr "smnlog_master" YELLOW; Wr
Wr "Mlogs in 8.3.1 and not in 8.2.X: " CYAN; Wr "cm-daemon,crs,perfstatd,php-fpm.access,php-fpm.error,pipd,sysmgr,vserverdr,ypbind" YELLOW; Wr
Wr "Mlogs in 8.2.3 and not in 8.3.1: " CYAN; Wr "snapmirror" YELLOW; Wr; Wr
[System.Array]$logList = "auditlog","backup","clone","ems","messages","nbu_snapvault","sis","smnlog_master","snapmirror","snapmirror_audit","snapmirror_error","treecompare","vfiler_trans_migrate_cmds_log","vfiler_trans_migrate_log"
[System.Array]$MlogList = "apache_access","apache_error","bcomd","cm-daemon","command-history","crs","debug","fpolicy","hashd","messages","mgwd","ndmpd","notifyd","perfstatd","php-fpm.access","php-fpm.error","php","pipd","secd","servprocd","sktlogd","snapmirror","spmd","sysmgr","vifmgr","vldb","vserverdr","ypbind"
Wr "Valid /etc/log logs are:" CYAN; Wr; Wr $logList GREEN; Wr; Wr
Wr "Valid /etc/log/mlog logs are:" CYAN; Wr; Wr $MlogList GREEN; Wr; Wr

[Boolean]$ValidAnswer = $FALSE
IF( $LOGorMLOG -eq "log" ){ $ValidAnswer = $TRUE; Wr "Log or Mlog log : " CYAN; Wr "log"; Wr }
ELSEIF( $LOGorMLOG -eq "mlog" ){ $ValidAnswer = $TRUE; Wr "Log or Mlog log : " CYAN; Wr "mlog"; Wr }
ELSE{ $LOGorMLOG = "" }
WHILE(!$ValidAnswer){
  Wr "Log or Mlog log : " CYAN; $LOGorMLOG = Read-Host
  IF( $LOGorMLOG -eq "log" ){ $ValidAnswer = $TRUE }
  ELSEIF( $LOGorMLOG -eq "mlog" ){ $ValidAnswer = $TRUE }
  ELSE{ Wr "Not a valid option!" RED; Wr }
}; $LOGorMLOG = $LOGorMLOG.ToLower()

[Boolean]$ValidAnswer = $FALSE
IF( ( ($LOGorMLOG -eq "log") -and ($logList -contains $LOGFILE) )  -or
  ( ($LOGorMLOG -eq "mlog") -and ($MlogList -contains $LOGFILE) ) ){
  $ValidAnswer = $TRUE; Wr "Log File        : " CYAN; Wr $LOGFILE; Wr }
ELSE{ $LOGFILE = "" }
While(!$ValidAnswer){
  Wr "Log File        : " CYAN; $LOGFILE = Read-Host
  IF( ($LOGorMLOG -eq "log") -and ($logList -contains $LOGFILE) ){ $ValidAnswer = $TRUE }
  ELSEIF( ($LOGorMLOG -eq "mlog") -and ($MlogList -contains $LOGFILE) ){ $ValidAnswer = $TRUE }
  ELSE{ Wr "Not a valid log file!" RED; Wr }
}; Wr

#############################
## CREDENTIALS & GET NODES ##
#############################

Wr "Cluster   : " CYAN; If($CLUSTER){ Wr $CLUSTER; Wr }else{ $CLUSTER  = Read-Host }
Wr "User name : " CYAN; If($USERNAME){ Wr $USERNAME; Wr }else{ $USERNAME = Read-Host }
Wr "Password  : " CYAN; $PASSWORD = Read-Host -AsSecureString; Wr
$Credential = New-Object System.Management.Automation.PsCredential($USERNAME,$PASSWORD)

FUNCTION Load-DataOntapPSTK {
  If(Get-Module DataONTAP){ RETURN }
  [Void](Import-Module DataONTAP -ErrorAction SilentlyContinue)
  If(!(Get-Module DataONTAP)){ Wr "Unable to load the DataONTAP PowerShell Toolkit - exiting!" RED; Wr; Wr; EXIT }
  Wr "Loaded the Data ONTAP PowerShell Toolkit!" GREEN; Wr; Wr
}

[System.Array]$NODELIST = @()
[Void](Load-DataOntapPSTK)
$Connect = Connect-NcController -Name $CLUSTER -Credential $Credential -ErrorAction SilentlyContinue
If(!$Connect){ Wr "Unable to connect to $CLUSTER!" RED; Wr; Wr; EXIT }
$attrs = get-ncnode -template
$attrs.Node = ""
$GetNode = Get-NcNode -Attributes $Attrs
$GetNode | Foreach { $NODELIST += $_.Node }
$NODELIST | Foreach { [Void](New-Item -ItemType Directory -Path "$_/" -Force) } # Create folder for each node's etc/log logs
$NODELIST | Foreach { [Void](New-Item -ItemType Directory -Path "$_/mlog/" -Force) } # ... and etc/log/mlog logs

############################################
## FIND WHETHER THE LOG IS IN LOG OR MLOG ##
############################################

IF($LOGorMLOG -eq "log"){
  [String]$LogFilePath = "vol0/etc/log/"
  [String]$SavePath = ""
} ELSE {
  [String]$LogFilePath = "vol0/etc/log/mlog/"
  [String]$SavePath = "mlog/"
}

Foreach ($Node in $NODELIST){
  [Int]$Counter = 1
  [Boolean]$Starting = $TRUE
  [Boolean]$Continue = $TRUE
  While($Continue){
    [String]$FileNumber  = ([String]$Counter).PadLeft(10,"0")
    [String]$LogFileName = $LOGFILE + ".log." + $FileNumber
    [String]$ReadNcPath  = $LogFilePath + $LogFileName
    $ReadFile = Read-Ncfile -path $ReadNcPath -VserverContext $Node -ErrorAction SilentlyContinue
    [String]$Outpath = $Node + "/" + $SavePath + $LogFileName
    If($ReadFile){ Wr "Acquired from " GREEN; Wr $Node; Wr " path " GREEN; Wr $ReadNcPath; Wr; Wr " to " GREEN; Wr $Outpath; Wr }
    ELSE{ Wr "Checked node " RED; Wr $Node; Wr " path " RED; Wr $ReadNcPath; Wr }
    If($ReadFile){ $ReadFile | Out-File $OutPath }
    If($ReadFile -and $Starting){ $Starting = $FALSE }
    If(!$ReadFile -and !$Starting){ $Continue = $FALSE }
    $Counter++
  }; Wr
}

No comments:

Post a Comment