diff --git a/Scripts/ConvertFrom-Bit.psm1 b/Scripts/ConvertFrom-Bit.psm1 new file mode 100644 index 0000000..991ab0f --- /dev/null +++ b/Scripts/ConvertFrom-Bit.psm1 @@ -0,0 +1,15 @@ +function ConvertFrom-Bit { + param ( + $bit + ) + [int]$int = 0 + $bits = $bit.ToString().ToCharArray() + $index = ($bits.Count)-1 + foreach ($b in $bits) { + if ($b -notlike 0) { + $int += [math]::Pow(2,$index) + } + $index -= 1 + } + $int +} \ No newline at end of file diff --git a/Scripts/ConvertFrom-UnixTime.psm1 b/Scripts/ConvertFrom-UnixTime.psm1 new file mode 100644 index 0000000..81a4638 --- /dev/null +++ b/Scripts/ConvertFrom-UnixTime.psm1 @@ -0,0 +1,9 @@ +function ConvertFrom-UnixTime { + param ( + $intime + ) + $EpochTime = [DateTime]"1/1/1970" + $TimeZone = Get-TimeZone + $UTCTime = $EpochTime.AddSeconds($intime) + $UTCTime.AddMinutes($TimeZone.BaseUtcOffset.TotalMinutes) +} \ No newline at end of file diff --git a/Scripts/ConvertSecondsTo-TimeSpan.psm1 b/Scripts/ConvertSecondsTo-TimeSpan.psm1 new file mode 100644 index 0000000..e71e2d6 --- /dev/null +++ b/Scripts/ConvertSecondsTo-TimeSpan.psm1 @@ -0,0 +1,7 @@ +function ConvertSecondsTo-TimeSpan { + param ( + $insec + ) + $TimeSpan = [TimeSpan]::fromseconds($insec) + "{0:dd' day 'hh\:mm\:ss}" -f $TimeSpan +} \ No newline at end of file diff --git a/Scripts/ConvertTo-Bit.psm1 b/Scripts/ConvertTo-Bit.psm1 new file mode 100644 index 0000000..a865738 --- /dev/null +++ b/Scripts/ConvertTo-Bit.psm1 @@ -0,0 +1,22 @@ +function ConvertTo-Bit { + param ( + [Int]$int + ) + [array]$bits = @() + $test = $true + while ($test -eq $true) { + if (($int/2).GetType() -match [double]) { + $int = ($int-1)/2 + [array]$bits += 1 + } + elseif (($int/2).GetType() -match [int]) { + $int = $int/2 + [array]$bits += 0 + } + if ($int -eq 0) { + $test = $false + } + } + $bits = $bits[-1..-999] + ([string]($bits)) -replace "\s" +} \ No newline at end of file diff --git a/Scripts/ConvertTo-UnixTime.psm1 b/Scripts/ConvertTo-UnixTime.psm1 new file mode 100644 index 0000000..89a15b1 --- /dev/null +++ b/Scripts/ConvertTo-UnixTime.psm1 @@ -0,0 +1,8 @@ +function ConvertTo-UnixTime { + param ( + $date = $(Get-Date) + ) + $tz = (Get-TimeZone).BaseUtcOffset.TotalMinutes + $sec = (New-TimeSpan -Start (Get-Date "01/01/1970") -End ((Get-Date).AddMinutes(-$tz))).TotalSeconds # -3h UTC + [int]$sec +} \ No newline at end of file diff --git a/Scripts/Get-TimeStamp.psm1 b/Scripts/Get-TimeStamp.psm1 new file mode 100644 index 0000000..96210e5 --- /dev/null +++ b/Scripts/Get-TimeStamp.psm1 @@ -0,0 +1,5 @@ +function Get-TImeStamp { + $tz = (Get-TimeZone).BaseUtcOffset.TotalMinutes + $unixtime = (New-TimeSpan -Start (Get-Date "01/01/1970") -End ((Get-Date).AddMinutes(-$tz))).TotalSeconds # -3h UTC + ([string]$unixtime -replace "\..+") + "000000000" +} \ No newline at end of file diff --git a/Scripts/PerformanceTo-InfluxDB.ps1 b/Scripts/PerformanceTo-InfluxDB.ps1 new file mode 100644 index 0000000..6ffa3b0 --- /dev/null +++ b/Scripts/PerformanceTo-InfluxDB.ps1 @@ -0,0 +1,27 @@ +function ConvertTo-Encoding ([string]$From, [string]$To) { + Begin { + $encFrom = [System.Text.Encoding]::GetEncoding($from) + $encTo = [System.Text.Encoding]::GetEncoding($to) + } + Process { + $bytes = $encTo.GetBytes($_) + $bytes = [System.Text.Encoding]::Convert($encFrom, $encTo, $bytes) + $encTo.GetString($bytes) + } +} + +$localization = (Get-Culture).LCID # текущая локализация +if ($localization -eq 1049) { + $performance = "\\$(hostname)\Процессор(_Total)\% загруженности процессора" | ConvertTo-Encoding UTF-8 windows-1251 +} else { + $performance = "\Processor(_Total)\% Processor Time" +} + +$tz = (Get-TimeZone).BaseUtcOffset.TotalMinutes +while ($true) { + $unixtime = (New-TimeSpan -Start (Get-Date "01/01/1970") -End ((Get-Date).AddMinutes(-$tz))).TotalSeconds + $timestamp = ([string]$unixtime -replace "\..+") + "000000000" + [double]$value = (Get-Counter $performance).CounterSamples.CookedValue.ToString("0.00").replace(",",".") + Invoke-RestMethod -Method POST -Uri "http://192.168.3.104:8086/write?db=powershell" -Body "performance,host=$(hostname),counter=CPU value=$value $timestamp" + sleep 5 +} \ No newline at end of file diff --git a/Scripts/PingTo-InfluxDB.ps1 b/Scripts/PingTo-InfluxDB.ps1 new file mode 100644 index 0000000..f352e13 --- /dev/null +++ b/Scripts/PingTo-InfluxDB.ps1 @@ -0,0 +1,10 @@ +while ($true) { + $tz = (Get-TimeZone).BaseUtcOffset.TotalMinutes + $unixtime = (New-TimeSpan -Start (Get-Date "01/01/1970") -End ((Get-Date).AddMinutes(-$tz))).TotalSeconds # -3h UTC + $timestamp = ([string]$unixtime -replace "\..+") + "000000000" + $tnc = tnc 8.8.8.8 + $Status = $tnc.PingSucceeded + $RTime = $tnc.PingReplyDetails.RoundtripTime + Invoke-RestMethod -Method POST -Uri "http://192.168.3.104:8086/write?db=powershell" -Body "ping,host=$(hostname) status=$status,rtime=$RTime $timestamp" + sleep 1 +} \ No newline at end of file diff --git a/Scripts/Zabbix-API-Last-Uptime-All-Hosts.ps1 b/Scripts/Zabbix-API-Last-Uptime-All-Hosts.ps1 new file mode 100644 index 0000000..ec4240b --- /dev/null +++ b/Scripts/Zabbix-API-Last-Uptime-All-Hosts.ps1 @@ -0,0 +1,92 @@ +$ip = "192.168.3.102" +$url = "http://$ip/zabbix/api_jsonrpc.php" +$token = "914ee100f4e8c4b68a70eab2a0a1fb153cfcd4905421d0ffacb82c20a57aa50e" + +function ConvertFrom-UnixTime { + param ( + $intime + ) + $EpochTime = [DateTime]"1/1/1970" + $TimeZone = Get-TimeZone + $UTCTime = $EpochTime.AddSeconds($intime) + $UTCTime.AddMinutes($TimeZone.BaseUtcOffset.TotalMinutes) +} + +function ConvertTo-TimeSpan { + param ( + $insec + ) + $TimeSpan = [TimeSpan]::fromseconds($insec) + "{0:dd' day 'hh\:mm\:ss}" -f $TimeSpan +} + +### Получить список всех хостов (имя и id) +$data = @{ + "jsonrpc"="2.0"; + "method"="host.get"; + "params"=@{ + "output"=@( + "hostid"; + "host"; + ); + }; + "id"=2; + "auth"=$token; +} +$hosts = (Invoke-RestMethod -Method POST -Uri $url -Body ($data | ConvertTo-Json) -ContentType "application/json").Result + +### Получить id элемента данных по наименованию ключа для каждого хоста +$Collections = New-Object System.Collections.Generic.List[System.Object] +foreach ($h in $hosts) { + $data = @{ + "jsonrpc"="2.0"; + "method"="item.get"; + "params"=@{ + "hostids"=@($h.hostid); + }; + "auth"=$token; + "id"=1; + } + $items = (Invoke-RestMethod -Method POST -Uri $url -Body ($data | ConvertTo-Json) -ContentType "application/json").Result + $items_id = ($items | Where-Object key_ -match system.uptime).itemid + if ($items_id -ne $null) { + $Collections.Add([PSCustomObject]@{ + host = $h.host; + hostid = $h.hostid; + item_id_uptime = $items_id + }) + } +} + +### Получить все историю элемента данных по его уникальному id для каждого хоста +$Collections_output = New-Object System.Collections.Generic.List[System.Object] +foreach ($c in $Collections) { + $data = @{ + "jsonrpc"="2.0"; + "method"="history.get"; + "params"=@{ + "hostids"=$c.hostid; + "itemids"=$c.item_id_uptime; + }; + "auth"=$token; + "id"=1; + } + $items_data_uptime = (Invoke-RestMethod -Method POST -Uri $url -Body ($data | ConvertTo-Json) -ContentType "application/json").Result + + ### Convert Secconds To TimeSpan and DateTime + $sec = $items_data_uptime.value + $UpTime = ConvertTo-TimeSpan $sec[-1] + + ### Convert From Unix Time + $time = $items_data_uptime.clock + $GetDataTime = ConvertFrom-UnixTime $time[-1] + + $Collections_output.Add([PSCustomObject]@{ + host = $c.host; + hostid = $c.hostid; + item_id_uptime = $c.item_id_uptime; + GetDataTime = $GetDataTime + UpTime = $UpTime + }) +} +$Collections_output | Format-Table \ No newline at end of file diff --git a/Scripts/Zabbix-Agent-Deploy.ps1 b/Scripts/Zabbix-Agent-Deploy.ps1 new file mode 100644 index 0000000..55e0c6d --- /dev/null +++ b/Scripts/Zabbix-Agent-Deploy.ps1 @@ -0,0 +1,18 @@ +$url = "https://cdn.zabbix.com/zabbix/binaries/stable/6.4/6.4.5/zabbix_agent2-6.4.5-windows-amd64-static.zip" +$path = "$home\Downloads\zabbix-agent2-6.4.5.zip" +$WebClient = New-Object System.Net.WebClient +$WebClient.DownloadFile($url, $path) +Expand-Archive $path -DestinationPath "C:\zabbix-agent2-6.4.5\" +Remove-Item $path +New-NetFirewallRule -DisplayName "Zabbix-Agent" -Profile Any -Direction Inbound -Action Allow -Protocol TCP -LocalPort 10050,10051 + +$Zabbix_Server = "192.168.3.102" +$conf = "C:\zabbix-agent2-6.4.5\conf\zabbix_agent2.conf" +$cat = cat $conf +$rep = $cat -replace "Server=.+","Server=$Zabbix_Server" +$rep | Select-String Server= +$rep > $conf + +$exe = "C:\zabbix-agent2-6.4.5\bin\zabbix_agent2.exe" +.$exe --config $conf --install +Get-Service *Zabbix*Agent* | Start-Service \ No newline at end of file