Quantcast
Channel: MikroTik
Viewing all articles
Browse latest Browse all 21099

Scripting • Re: Strip Linefeed/EOL from fetched file

$
0
0
Here is a script I found, modified to send JSON payload to Webhook.
It sends each DHCP Server, total pool size, total IP's in use.
Probably some items here you can pick on but it works.

/ip dhcp-server {
:local ip;
:local servertotalips;
:local servertotalleased;
:local nextpool;
:local tmp;
:local poolname;
:local webhook "https://<your URL>";
:local systemname [/system identity get name];
:local json "";
:local dhcpservertotal 0;
:local debug true;
:local servername;
:local dhcpservers [find]

:log info "Start DHCP Inventory...";
# JSON Payload start...
:set $json "[{\"servername\":\"$systemname\" ,\"dhcp-servers\":{\"entities\":[";

# Log DCHP server total count.
:log info "Total DHCP Servers: $[:len $dhcpservers]"
:foreach item in=$dhcpservers do={
:set servername [get $item name];
:log info ("Get: " . $servername);
:if ($debug) do={:put ("DHCP Server: " . $servername)};

# Get total number of leases for DHCP Server
:set servertotalleased [/ip dhcp-server lease print value-list count-only where server=$servername];
:if ($debug) do={:put ("Total Leased: " . $servertotalleased)}

# Get DHCP Server pool name.
:set poolname [get $item address-pool];

:if ($debug) do={:put ("Pool Name: " . $poolname)}
:if ($poolname = "static-only") do={
:log info ($servername . " No Pool found.")
} else={
#:log info ("Pool Name: " . $poolname);
:set tmp $poolname;

# Get next pool (if exists)
:set nextpool [/ip pool get $tmp next-pool];
#:log info ("Next Pool: " . $nextpool);
# Get pool range
:set ip [:tostr [/ip pool get $poolname ranges]];
#:log info ("IP Range: " . $ip);

# Get total number of pool IP's
:local Start [:pick $ip 0 [:find $ip "-"]];
#:log info ("IP First: " . $Start);
:local Stop [:pick $ip ([:find $ip "-"] + 1) 31];
#:log info ("IP Last: " . $Stop);
:set servertotalips ($Stop - $Start);
#:log info ("IP Range Total: " . $servertotalips);

# Loop thru each DHCP Pool and get an IP Total
:while ([:len $nextpool] != 0) do={
:set ip [:tostr [/ip pool get $nextpool ranges]];

# Get total number of pool IP's
:local Start [:pick $ip 0 [:find $ip "-"]];
:local Stop [:pick $ip ([:find $ip "-"] + 1) 31];
:local pooltotal ($Stop - $Start);

:set servertotalips ($servertotalips + $pooltotal);

:set tmp $nextpool;
:set nextpool [/ip pool get $tmp next-pool];
};

:local available ($servertotalips - $servertotalleased);

:set json ($json . "{\"name\":\"" . $servername . "\",");
:set json ($json . "\"pool-size\":\"" . $servertotalips . "\",");
:set json ($json . "\"available\":\"" . $available . "\"}");
:log info ($servername . " Pool Size: " . $servertotalips . " Available: " . $available);

:set json ($json . ",");
}
}

# Trim last ","
:set json [:pick $json 0 ( [ :len $json ] -1 )];

# Finish JSON Payload
:set json ($json . "]}}]");
:log info $json;
:if ($debug) do={:put ($json)};
# Send webhook
/tool fetch http-method=post http-header-field="Content-Type: application/json" http-data=$json url="$webhook";
}

Statistics: Posted by lanhampr — Tue Dec 12, 2023 5:48 pm



Viewing all articles
Browse latest Browse all 21099

Trending Articles