Коллекция информационных скриптов с официального форума

Здесь выкладываем скрипты
Сообщения: 883
Зарегистрирован: 15 сен 2017, 09:03

20 сен 2019, 12:02

 Collect information from Mikrotik RouterOS v 3.1 Jotne 2019
# Collect information from Mikrotik RouterOS
# v 3.1 Jotne 2019
# ----------------------------------

# What data to collect. Set to false to skip the section
# ----------------------------------
:local SystemResource true
:local SystemInformation true
:local SystemHealth true
:local TrafficData true
:local uPnP true
:local Wireless true
:local AddressLists true
:local DHCP true
:local Neighbor true

:local InterfaceData true

# Interface to get data from (using regex)
:local IF "ether.*"
# Example
# "ether.*" All ethernet interfaces
# "^ether[1-5]\$" Only ethernet 1 to 5
# ".*" All interfaces (Briges/VLAN/pptp/Ether ++)
# "ether(1|2)\$" interface ethernet 1 and 2 (/$ needed to prevent ether11 etc)

# Collect system resource
# ----------------------------------
if ($SystemResource) do={
:local cpuload ([/system resource get cpu-load])
:local freemem ([/system resource get free-memory]/1048576)
:local totmem ([/system resource get total-memory]/1048576)
:local freehddspace ([/system resource get free-hdd-space]/1048576)
:local totalhddspace ([/system resource get total-hdd-space]/1048576)
:local up ([/system resource get uptime])
:log info message="script=resource free_memory=$freemem MB total_memory=$totmem MB free_hdd_space=$freehddspace MB total_hdd_space=$totalhddspace MB cpu_load=$cpuload uptime=$up"

# Get traffic data (accounting data)
# ----------------------------------
if ($TrafficData) do={
# Test if fasttrack is enabled and give warning
:if ([/ip firewall filter find where (action=fasttrack-connection && !disabled)] != "") do={
:log info message=("script=traffic,fasttrack=1")
} else={
:log info message=("script=traffic,fasttrack=0")
# Test if accounting is enabled and if yes, get data
if ([/ip accounting get enabled]=yes) do={
/ip accounting snapshot take
# Get uncounted data
/ip accounting uncounted {
:log info message=("script=uncounted,bytes=".[get bytes].",packets=".[get packets])}
# Send data to loggin server
foreach logline in=[/ip accounting snapshot find] do={
:local output "$[/ip accounting snapshot print as-value from=$logline]"
:set ( "$output"->"script" ) "traffic"
:log info message="$output"

# Get interface data
# ----------------------------------
if ($InterfaceData) do={
:foreach interface in=[/interface find where name~"^ether[1-5]\$"] do={
:delay 100ms
:local iname [/interface get $interface name]
:local monitor [/interface monitor-traffic $interface as-value once]
:local speedRX ($monitor->"rx-bits-per-second")
:local speedTX ($monitor->"tx-bits-per-second")
:log info message="script=monitor interface=$iname RX=$speedRX bps TX=$speedTX bps"

# Finding dynmaic lines used in uPnP
# ----------------------------------
if ($uPnP) do={
:foreach logline in=[/ip firewall nat find dynamic=yes] do={
:local output "$[/ip firewall nat print as-value from=$logline]"
:set ( "$output"->"script" ) "upnp"
:log info message="$output"

# Collect system information
# ----------------------------------
if ($SystemInformation) do={
:local version ([/system resource get version])
:local board ([/system resource get board-name])
:local model ([/system routerboard get model]);
:local serial ([/system routerboard get serial-number])
:local identity ([/system identity get name])
:log info message="script=sysinfo version=\"$version\" board-name=\"$board\" model=\"$model\" serial=$serial identity=\"$identity\""

# Collect system health
# ----------------------------------
if ($SystemHealth) do={
:if (([/system health get]~"state=disabled" || [/system health get]="")=false) do={
:local voltage ([/system health get voltage]/10)
:local temperature ([/system health get temperature])
:log info message="script=health voltage=$voltage V temperature=$temperature C"

# Sends wireless client data to log server
# ----------------------------------
if ($Wireless) do={
:do {
:if ([:len [/interface wireless find ]]>0) do={
:foreach logline in=[/interface wireless registration-table find] do={
:local output "$[/interface wireless registration-table print as-value from=$logline]"
:set ( "$output"->"script" ) "wifi"
:log info message="$output"
} on-error={}

# Count IP in address-lists
if ($AddressLists) do={
:local array [ :toarray "" ]
:local addrcntdyn [:toarray ""]
:local addrcntstat [:toarray ""]
:local test
:foreach id in=[/ip firewall address-list find] do={
:local rec [/ip firewall address-list get $id]
:local listname ($rec->"list")
:local listdynamic ($rec->"dynamic")
:set ( $array->$listname ) 1
if ($listdynamic = true) do={
:set ($addrcntdyn->$listname) ($addrcntdyn->$listname+1)
} else={
:set ($addrcntstat->$listname) ($addrcntstat->$listname+1)}
:foreach k,v in=$array do={
:log info message=("script=address_lists list=$k dynamic=".(($addrcntdyn->$k)+0)." static=".(($addrcntstat->$k)+0))}

# Get MNDP (CDP) Neighbors
# ----------------------------------
if ($Neighbor) do={
:foreach neighborID in=[/ip neighbor find] do={
:local nb [/ip neighbor get $neighborID]
:foreach key,value in=$nb do={
:local newline [:find $value "\n"]
:if ([$newline]>0) do={
:set $value [:pick $value 0 $newline]
:set ( "$nb"->"$key" ) "\"$value\""
:set ( "$nb"->"script" ) "\"neighbor\""
:log info message="$nb"

# Collect DHCP Pool information
# ----------------------------------
if ($DHCP) do={
/ip pool {
:local poolname
:local pooladdresses
:local poolused
:local minaddress
:local maxaddress
:local findindex

# Iterate through IP Pools
:foreach pool in=[find] do={
:set poolname [get $pool name]
:set pooladdresses 0
:set poolused 0

# Iterate through current pool's IP ranges
:foreach range in=[:toarray [get $pool range]] do={

# Get min and max addresses
:set findindex [:find [:tostr $range] "-"]
:if ([:len $findindex] > 0) do={
:set minaddress [:pick [:tostr $range] 0 $findindex]
:set maxaddress [:pick [:tostr $range] ($findindex + 1) [:len [:tostr $range]]]
} else={
:set minaddress [:tostr $range]
:set maxaddress [:tostr $range]

# Calculate number of ip in one range
:set pooladdresses ($maxaddress - $minaddress)

# /foreach range

# Test if pools is used in DHCP or VPN and show leases used
:local dname [/ip dhcp-server find where address-pool=$poolname]
:if ([:len $dname] = 0) do={
# No DHCP server found, assume VPN
:set poolused [:len [used find pool=[:tostr $poolname]]]
} else={
# DHCP server found, count leases
:local dname [/ip dhcp-server get [find where address-pool=$poolname] name]
:set poolused [:len [/ip dhcp-server lease find where server=$dname]]}

# Send data
:log info message=("script=pool pool=$poolname used=$poolused total=$pooladdresses")

# /foreach pool
# /ip pool

Чего не знаем то нагуглим