Advertising:

Zabbix Sender en Powershell

From Zabbix-ES
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

[CmdletBinding(DefaultParameterSetName="Set0")]

param(
    [parameter(ParameterSetName="Set1")]
    [alias("o")]
    [string]$Data,

    [parameter(ParameterSetName="Set1")]
    [alias("h")]
    [string]$HostName,

    [parameter(ParameterSetName="Set1")]
    [alias("k")]
    [string]$Key
)

function ZabbixTrapper {
   param (
       [string]$Value
   )
 
   [string]$Server = '10.151.0.69'
   [int]$Port = '10051'
   #[string]$HostName = 'PRTG9'
   [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
   }
}

try {
   ZabbixTrapper($Data);
} catch {
   Write-Error 'Error al enviar los datos';
   break
}