137 lines
3.7 KiB
PowerShell
137 lines
3.7 KiB
PowerShell
|
|
$URI = "https://unifi.freifunk-troisdorf.de/v2.1/devices"
|
|
$headers = @{
|
|
"x-auth-token" = ""
|
|
}
|
|
|
|
$devices = (Get-Content -Path .\devices.json | ConvertFrom-Json).devices
|
|
|
|
$request = Invoke-Restmethod -URI $URI -Method GET -Headers $headers
|
|
|
|
# Get timestamp
|
|
$timestamp = Get-Date -UFormat "%Y-%m-%dT%R:00%Z00"
|
|
$timeDiffFromUTC = Get-Date -UFormat "%Z00"
|
|
|
|
|
|
|
|
# create empty nodes array
|
|
$nodes = @()
|
|
$links = @()
|
|
|
|
# loop through each device
|
|
foreach ($device in $devices) {
|
|
$dev = $request | Where-Object { $_.identification.mac -eq $device.mac}
|
|
# implement function that reports not found mac addresses in API
|
|
|
|
## Get info from API
|
|
$firstseen = Get-Date -Date $dev.overview.createdAt -UFormat "%Y-%m-%dT%R:00$timeDiffFromUTC"
|
|
$lastseen = Get-Date -Date $dev.overview.lastSeen -UFormat "%Y-%m-%dT%R:00$timeDiffFromUTC"
|
|
if ($dev.overview.status -eq "active") {
|
|
$is_online = $true
|
|
} else {
|
|
$is_online = $false
|
|
}
|
|
|
|
$uptime = Get-Date -Date $dev.identification.started -UFormat "%Y-%m-%dT%R:00$timeDiffFromUTC"
|
|
$mac = $dev.identification.mac
|
|
$node_id = $mac.replace(":","")
|
|
|
|
|
|
## End of API call 1
|
|
|
|
|
|
# get more info from each device
|
|
## Get more info from second API call
|
|
$id = $dev.identification.id
|
|
$URI = "https://unifi.freifunk-troisdorf.de/v2.1/devices/erouters/$id"
|
|
$details = Invoke-Restmethod -URI $URI -Method GET -Headers $headers
|
|
|
|
$loadavg = $details.overview.cpu / 100
|
|
$memory_usage = $details.overview.ram / 100
|
|
|
|
$addresses = @()
|
|
# cut away network mask
|
|
$addresses += $details.ipAddress.split("/")[0]
|
|
|
|
$hostname = "[RiFu] " + $details.identification.name
|
|
|
|
$firmware = @{
|
|
base = "Ubiquiti - Stock"
|
|
release = $details.firmware.current
|
|
}
|
|
$model = $details.identification.model
|
|
|
|
## End of API call 2
|
|
|
|
## API Call 3 (Links)
|
|
$URI = "https://unifi.freifunk-troisdorf.de/v2.1/devices/airmaxes/$id/stations"
|
|
$airmaxes = Invoke-Restmethod -URI $URI -Method GET -Headers $headers
|
|
$remoteMAC = $airmaxes.deviceIdentification.mac
|
|
|
|
|
|
# check if remote mac address is part of our published network
|
|
if ($devices | Where-Object -Property mac -EQ $remoteMAC) {
|
|
# this is a public link, let's advertise it
|
|
if (!($links | Where-Object -Property source_addr -EQ $remoteMAC)) {
|
|
# this link does not exist already, let's add it
|
|
$links += @{
|
|
type = "wifi"
|
|
source = $mac.replace(":","")
|
|
target = $remoteMAC.replace(":","")
|
|
source_tq = $airmaxes.statistics.linkScore
|
|
target_tq = $airmaxes.statistics.linkScore
|
|
source_addr = $mac
|
|
target_addr = $remoteMAC
|
|
}
|
|
}
|
|
}
|
|
|
|
## Get info from json file (static)
|
|
$currentDev = $devices | Where-Object -Property mac -EQ $dev.identification.mac
|
|
$gateway_nexthop = $currentDev.gateway_nexthop
|
|
$gateway = $currentDev.gateway
|
|
$domain = $currentDev.domain
|
|
$location = $currentDev.location
|
|
|
|
# assemble all into nodes an links
|
|
$nodes += @{
|
|
firstseen = $firstseen
|
|
lastseen = $lastseen
|
|
is_online = $is_online
|
|
is_gateway = $false
|
|
clients = 0
|
|
clients_wifi24 = 0
|
|
clients_wifi5 = 0
|
|
clients_other = 0
|
|
rootfs_usage = 0
|
|
loadavg = $loadavg
|
|
memory_usage = $memory_usage
|
|
uptime = $uptime
|
|
gateway_nexthop = $gateway_nexthop
|
|
gateway = $gateway
|
|
location = $location
|
|
node_id = $node_id
|
|
mac = $mac
|
|
addresses = $addresses
|
|
domain = $domain
|
|
hostname = $hostname
|
|
owner = "Freifunk Rhein-Sieg"
|
|
firmware = $firmware
|
|
autoupdater = @{
|
|
enabled = $false
|
|
branch = "stable"
|
|
}
|
|
nproc = 1
|
|
model = $model
|
|
}
|
|
}
|
|
|
|
|
|
# create file output
|
|
$output = @{
|
|
timestamp = $timestamp
|
|
nodes = $nodes
|
|
links = $links
|
|
}
|
|
|
|
$output | ConvertTo-Json -Depth 4 | Out-File example.json |