Advertising:
Zabbix Sender en Powershell: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
Line 12: | Line 12: | ||
'''File:''' scom2zabbix.ps1 | '''File:''' scom2zabbix.ps1 | ||
[CmdletBinding(DefaultParameterSetName="Set0")] | |||
param( | param( | ||
[parameter(ParameterSetName="Set1")] | [parameter(ParameterSetName="Set1")] | ||
[alias("o")] | [alias("o")] | ||
[string]$Data, | [string]$Data, | ||
[parameter(ParameterSetName="Set1")] | [parameter(ParameterSetName="Set1")] | ||
[alias("h")] | [alias("h")] | ||
[string]$HostName, | [string]$HostName, | ||
[parameter(ParameterSetName="Set1")] | [parameter(ParameterSetName="Set1")] | ||
[alias("k")] | [alias("k")] | ||
[string]$Key | [string]$Key | ||
) | ) | ||
function ZabbixTrapper { | function ZabbixTrapper { | ||
param ( | param ( | ||
[string]$Value | [string]$Value | ||
) | ) | ||
[string]$Server = '10.151.0.69' | [string]$Server = '10.151.0.69' | ||
[int]$Port = '10051' | [int]$Port = '10051' | ||
#[string]$HostName = 'PRTG9' | #[string]$HostName = 'PRTG9' | ||
[string[]]$Header = @('host','key','value') | [string[]]$Header = @('host','key','value') | ||
[string]$JsonString | [string]$JsonString | ||
[switch]$OnlyPreview = $false | [switch]$OnlyPreview = $false | ||
if ( [bool]($HostName -or $Key -or $Value) ) { | if ( [bool]($HostName -or $Key -or $Value) ) { | ||
if(! [bool]($HostName -and $Key -and $Value) ) { | if(! [bool]($HostName -and $Key -and $Value) ) { | ||
Write-Error 'HostName, Key and Value must not be null'; | Write-Error 'HostName, Key and Value must not be null'; | ||
break | break | ||
} else { | } else { | ||
$Json = [pscustomobject][ordered]@{ | $Json = [pscustomobject][ordered]@{ | ||
'request' = 'sender data' ; | 'request' = 'sender data' ; | ||
'data' = @([pscustomobject][ordered]@{'host' = $HostName;'key' = $Key;'value' = $Value}) | 'data' = @([pscustomobject][ordered]@{'host' = $HostName;'key' = $Key;'value' = $Value}) | ||
} | ConvertTo-Json -Compress | } | ConvertTo-Json -Compress | ||
} | } | ||
} elseif ($InputObject) { | } elseif ($InputObject) { | ||
$Json = [pscustomobject][ordered]@{ | $Json = [pscustomobject][ordered]@{ | ||
'request' = 'sender data' ; | 'request' = 'sender data' ; | ||
'data' = @( | 'data' = @( | ||
$InputObject | Select-Object -Property @( | $InputObject | Select-Object -Property @( | ||
@{'Name' = 'host'; Expression = {$_.$($Header[0])}}, | @{'Name' = 'host'; Expression = {$_.$($Header[0])}}, | ||
@{'Name' = 'key'; Expression = {$_.$($Header[1])}}, | @{'Name' = 'key'; Expression = {$_.$($Header[1])}}, | ||
@{'Name' = 'value'; Expression = {$_.$($Header[2])}} | @{'Name' = 'value'; Expression = {$_.$($Header[2])}} | ||
) | ) | ||
) | ) | ||
} | ConvertTo-Json -Compress | } | ConvertTo-Json -Compress | ||
} elseif ($JsonString) { | } elseif ($JsonString) { | ||
$Json = $JsonString | ConvertFrom-Json | ConvertTo-Json -Compress | $Json = $JsonString | ConvertFrom-Json | ConvertTo-Json -Compress | ||
} else { | } else { | ||
Write-Error 'Input data not found'; | Write-Error 'Input data not found'; | ||
break | break | ||
} | } | ||
if(!$Json){ | if(!$Json){ | ||
Write-Error 'Can not convert InputData to Json string'; | Write-Error 'Can not convert InputData to Json string'; | ||
break | break | ||
} | } | ||
if($OnlyPreview){ | if($OnlyPreview){ | ||
$Json | ConvertFrom-Json | ConvertTo-Json; | $Json | ConvertFrom-Json | ConvertTo-Json; | ||
break | break | ||
} | } | ||
try { | try { | ||
[byte[]]$Header = @([System.Text.Encoding]::ASCII.GetBytes('ZBXD')) + [byte]1 | [byte[]]$Header = @([System.Text.Encoding]::ASCII.GetBytes('ZBXD')) + [byte]1 | ||
[byte[]]$Length = @([System.BitConverter]::GetBytes($([long]$Json.Length))) | [byte[]]$Length = @([System.BitConverter]::GetBytes($([long]$Json.Length))) | ||
[byte[]]$Data = @([System.Text.Encoding]::ASCII.GetBytes($Json)) | [byte[]]$Data = @([System.Text.Encoding]::ASCII.GetBytes($Json)) | ||
$All = $Header + $Length + $Data | $All = $Header + $Length + $Data | ||
} catch { | } catch { | ||
Write-Error 'Can not convert Json string to byte'; | Write-Error 'Can not convert Json string to byte'; | ||
break | break | ||
} | } | ||
try { | try { | ||
$Socket = New-Object System.Net.Sockets.Socket ([System.Net.Sockets.AddressFamily]::InterNetwork, [System.Net.Sockets.SocketType]::Stream, [System.Net.Sockets.ProtocolType]::Tcp) | $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.Connect($Server,$Port) | ||
$Socket.Send($All) | Out-Null | $Socket.Send($All) | Out-Null | ||
[byte[]]$Buffer = New-Object System.Byte[] 1000 | [byte[]]$Buffer = New-Object System.Byte[] 1000 | ||
[int]$ReceivedLength = $Socket.Receive($Buffer) | [int]$ReceivedLength = $Socket.Receive($Buffer) | ||
$Socket.Close() | $Socket.Close() | ||
} catch { | } catch { | ||
Write-Error 'TCP-level Error connecting, sending or receiving'; | Write-Error 'TCP-level Error connecting, sending or receiving'; | ||
break | break | ||
} | } | ||
$Received = [System.Text.Encoding]::ASCII.GetString(@($Buffer[13 .. ($ReceivedLength - 1)])) | $Received = [System.Text.Encoding]::ASCII.GetString(@($Buffer[13 .. ($ReceivedLength - 1)])) | ||
try{ | try{ | ||
$Received | ConvertFrom-Json | $Received | ConvertFrom-Json | ||
} catch { | } catch { | ||
Write-Warning 'It is not possible to convert the output to a Json string, maybe the server has rejected invalid data' | Write-Warning 'It is not possible to convert the output to a Json string, maybe the server has rejected invalid data' | ||
$Received | $Received | ||
} | } | ||
} | } | ||
try { | try { | ||
ZabbixTrapper($Data); | ZabbixTrapper($Data); | ||
} catch { | } catch { | ||
Write-Error 'Error al enviar los datos'; | Write-Error 'Error al enviar los datos'; | ||
break | break | ||
} | } |
Latest revision as of 08:41, 24 September 2021
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 }