Advertising:

Zabbix Sender en Powershell

From Zabbix-ES
Revision as of 08:11, 24 September 2021 by Unknown user (talk)
Jump to navigation Jump to search
File: scom2zabbix.cmd
@ECHO OFF 
set LOGFILE=C:\zabbix\scom2zabbix.log

call :sub >%LOGFILE%
exit /b

:sub
"C:\Windows\System32\WindowsPowershell\v1.0\powershell.exe" -File C:\zabbix\bin\scom2zabbix.ps1
File: scom2zabbix.ps1
function ZabbixTrapper {
   param (
       [string]$Value
   )
 
   [string]$Server = 'ZABBIX SERVER'
   [int]$Port = '10051'
   [string]$HostName = 'SCOM01'
   [string]$Key = 'scom.traps'
   [string[]]$Header = @('host','key','value')
   [string]$JsonString
   [switch]$OnlyPreview = $false
 
   if ( [bool]($HostName -or $Key -or $Value) ) {
           if(! [bool]($HostName -and $Key -and $Value) ) {
               Write-Error 'HostName, Key and Value must not be null';
               break
           } else {
               $Json = [pscustomobject][ordered]@{
                   'request' = 'sender data' ;
                   'data' = @([pscustomobject][ordered]@{'host' = $HostName;'key' = $Key;'value' = $Value})
               } | ConvertTo-Json -Compress
 
       }
   } elseif ($InputObject) {
       $Json = [pscustomobject][ordered]@{
                   'request' = 'sender data' ;
                   'data' = @(
                       $InputObject | Select-Object -Property @(
                           @{'Name' = 'host'; Expression = {$_.$($Header[0])}},
                           @{'Name' = 'key'; Expression = {$_.$($Header[1])}},
                           @{'Name' = 'value'; Expression = {$_.$($Header[2])}}
                       )
                   )
               } | ConvertTo-Json -Compress
   } elseif ($JsonString) {
       $Json = $JsonString | ConvertFrom-Json | ConvertTo-Json -Compress
   } else {
       Write-Error 'Input data not found';
       break
   }
 
   if(!$Json){
       Write-Error 'Can not convert InputData to Json string';
       break
   }
 
   if($OnlyPreview){
       $Json | ConvertFrom-Json | ConvertTo-Json;
       break
   }
 
   try {
       [byte[]]$Header = @([System.Text.Encoding]::ASCII.GetBytes('ZBXD')) + [byte]1
       [byte[]]$Length = @([System.BitConverter]::GetBytes($([long]$Json.Length)))
       [byte[]]$Data = @([System.Text.Encoding]::ASCII.GetBytes($Json))
       
       $All = $Header + $Length + $Data
       
   } catch {
       Write-Error 'Can not convert Json string to byte';
       break
   }
 
   try {
       $Socket = New-Object System.Net.Sockets.Socket ([System.Net.Sockets.AddressFamily]::InterNetwork, [System.Net.Sockets.SocketType]::Stream, [System.Net.Sockets.ProtocolType]::Tcp)
       $Socket.Connect($Server,$Port)
       $Socket.Send($All) | Out-Null
       [byte[]]$Buffer = New-Object System.Byte[] 1000
       [int]$ReceivedLength = $Socket.Receive($Buffer)
       $Socket.Close()
   } catch {
       Write-Error 'TCP-level Error connecting, sending or receiving';
       break
   }
 
   $Received = [System.Text.Encoding]::ASCII.GetString(@($Buffer[13 .. ($ReceivedLength - 1)]))
 
   try{
       $Received | ConvertFrom-Json
   } catch {
       Write-Warning 'It is not possible to convert the output to a Json string, maybe the server has rejected invalid data'
       $Received
   }
}
 
Import-Module OperationsManager
$AlertsJson = New-Object System.Collections.Generic.List[System.Object]
[string]$Severity_raw = 
[string]$Priority_raw = 
try {
   # $alerts = Get-SCOMAlert -Severity 2 | Select-Object Id, Name, Severity, Priority, MonitoringObjectPath, Description, TimeRaised, ResolutionState | ? {$_.ResolutionState -ne 255} | Sort-Object TimeRaised -Descending
   $alerts = Get-SCOMAlert | Select-Object Id, Name, Severity, Priority, MonitoringObjectPath, Description, TimeRaised, ResolutionState | ? {$_.ResolutionState -ne 255 -and ($_.Severity -eq 1 -or $_.Severity -eq 2)} | Sort-Object TimeRaised -Descending
   if (-Not ($alerts -eq $null)){
       foreach ($alert in $alerts) {
           if ($alert.Severity -eq 0)     { $Severity_raw = 'Informational'; }
           elseif ($alert.Severity -eq 1) { $Severity_raw = 'Warning';       }
           elseif ($alert.Severity -eq 2) { $Severity_raw = 'Critical';      }
       
           if ($alert.Priority -eq 0)     { $Priority_raw = 'Low';    }
           elseif ($alert.Priority -eq 1) { $Priority_raw = 'Medium'; }
           elseif ($alert.Priority -eq 2) { $Priority_raw = 'High';   }
 
           if ($alert.ResolutionState -eq 0)       { $ResolutionState_raw = 'Nueva Alerta';    }
           elseif ($alert.ResolutionState -eq 255) { $ResolutionState_raw = 'Cerrada'; }
           elseif ($alert.ResolutionState -eq 249) { $ResolutionState_raw = 'ACK - Se esta trabajando en ella';   }
           elseif ($alert.ResolutionState -eq 248) { $ResolutionState_raw = 'ACK - Asignado a ingeniería';   }
           elseif ($alert.ResolutionState -eq 247) { $ResolutionState_raw = 'ACK - En espera de evidencia';   }                                   
           elseif ($alert.ResolutionState -eq 254) { $ResolutionState_raw = 'ACK - Resuelto';   }
           elseif ($alert.ResolutionState -eq 250) { $ResolutionState_raw = 'ACK - Programado';   }
           
           $AlertsJson.Add(@{"id"=$alert.Id;"device"=$alert.MonitoringObjectPath;"name"=$alert.Name;"severity"=$alert.Severity;"severity_raw"=$Severity_raw;"priority"=$alert.Priority;"priority_raw"=$Priority_raw;"description"=$alert.Description;"resolutionstate"=$alert.ResolutionState;"resolutionstate_raw"=$ResolutionState_raw;});
       }
   }
   ZabbixTrapper(ConvertTo-Json -InputObject $AlertsJson -Compress);
} catch {
   Write-Error 'Error al enviar los datos';
   break
}