Add scripts

This commit is contained in:
Alex Kup 2023-08-03 11:24:09 +03:00 committed by GitHub
parent 45b95998ae
commit 8609b6ff17
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 213 additions and 0 deletions

View file

@ -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
}

View file

@ -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)
}

View file

@ -0,0 +1,7 @@
function ConvertSecondsTo-TimeSpan {
param (
$insec
)
$TimeSpan = [TimeSpan]::fromseconds($insec)
"{0:dd' day 'hh\:mm\:ss}" -f $TimeSpan
}

View file

@ -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"
}

View file

@ -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
}

View file

@ -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"
}

View file

@ -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
}

View file

@ -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
}

View file

@ -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

View file

@ -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