Tool: Using Splunk to analyse MikroTik logs 3.3 (Graphing everything) 💾 🛠 💻 📊

If I can, I want suggest something…

list is from → to

:local hour [:pick ([/system clock print as-value ]->"time") 0 2]
:local hour [:pick [/sys clock get time] 0 2]

	:foreach logline in=[/ip firewall nat find dynamic=yes] do={
	:foreach logline in=[/ip firewall nat find where dynamic=yes and comment~"^upnp "] do={

	:if (([/system health get]~"state=disabled" || [/system health get]="")=false) do={
	:if (!([/system health get]~"(state=disabled|^\$)")) do={

@ jvanhambelgium

Some script runes only once a day and since some part is changed to use KV store, it may take a day before all is populated.

This I do not understand. Can there be other dynamic nat than upnp lines, and why should I include the comments test.
Other line are fine, thanks :slight_smile:

the comment can distinguish the upnp rules from the other dynamics
yes, dynamic SMTP from HotSpot, and other related dynamic hotspot rules, walled garden rules, etc.
for example
/ip firewall nat> pri
Flags: X - disabled, I - invalid, D - dynamic
0 D chain=dstnat action=jump jump-target=hotspot hotspot=from-client

1 D chain=hotspot action=jump jump-target=pre-hotspot

2 D chain=hotspot action=redirect to-ports=64872 protocol=udp dst-port=53

3 D chain=hotspot action=redirect to-ports=64872 protocol=tcp dst-port=53

4 D chain=hotspot action=redirect to-ports=64873 protocol=tcp hotspot=local-dst dst-port=80

5 D chain=hotspot action=redirect to-ports=64875 protocol=tcp hotspot=local-dst dst-port=443

6 D chain=hotspot action=jump jump-target=hs-unauth protocol=tcp hotspot=!auth

7 D chain=hotspot action=jump jump-target=hs-auth protocol=tcp hotspot=auth

9 D chain=hs-unauth action=return dst-address=179.44.232.13

10 D ;;; www.paypal.comchain=hs-unauth action=return dst-address=2.17.140.171

11 D ;;; www.paypal.comchain=hs-unauth action=return dst-address=2.20.40.117
[…]

another hint:


# from
        :set ( $array->$listname ) 1
[...]
    :foreach k,v in=$array do={


# to
        :if (!($array ~ $listname)) do={ :set array ($array , $listname) }
[...]
    :foreach k in=$array do={
# only k without v

WARNING: BREAKED COMPATIBILITY
missing : before some occurrences of if
(no matter is missing on root, but better if you put : on every if also on root to be sure…)

	if ([/ip accounting get enabled]=yes) do={

		if ($listdynamic = true) do={

		if ($i = $cmd) do={:set $f 1}
		if ($f<>1) do={

missing : before some occurrences of foreach

		foreach logline in=[/ip accounting snapshot find] do={

	foreach i in=[/system history find] do={

anoter two fix:

replace all this

:do {
    :if ([:len [/caps-man registration-table find]] > 0 and $CAPsMANN) do={
        
    }
} on-error={}

with only this

:if ( ([:len [/interface find where type="cap"]] > 0) and $CAPsMANN) do={ /system script run capsman }



and replace all this

:do {
} on-error={
    :if ([:len [/system ntp client get last-update-from]]>0) do={
        :log info message="script=ntp status=synchronized"
    } else={
        :log info message="script=ntp status=not-synchronized"
    }
}

with this:

:local ntpstatus ""
:if ([:len [/system package find where !disabled and name=ntp]] > 0) do={
    :set ntpstatus [/system ntp client get status]
} else={
    :if ([:typeof [/system ntp client get last-update-from]] = "nil") do={
        :set ntpstatus "using-local-clock"
    } else={
        :set ntpstatus "synchronized"
    }
}
:log info message="script=ntp status=$ntpstatus"

I do not check (for now) the “Collect DHCP Pool information” because is really complicated and I must have more time.

I hope you like my effort to help you :stuck_out_tongue:

I do own you several beers for helping out :beer_mug::beer_mug::beer_mug::beer_mug::beer_mug:
DHCP part is the only part I have taken from some other :wink:

Added all parts to the script. Even found some local variable without :
Not 100% sure on what I do with the uPnP script. One option is to use your solution, other is to rename Dashboard to Dynamic NAT, and to show all types of NAT added to the Router. Do the other dynamic nat has comment set?

Comments for dynamic nat are some strange. In Winbox overview, you do see the comment, but when you open a rule, the comment button is missing and you can not see the comment.
/ip firewall nat print does not show to command for dynamic lines.

Very thanks for the beers :wink:

I just notice also one :do without the : , :set variable with $ in front of variable, and what you already have noticed for global and local
apparently new routeros 6.48+ ignore if the : are present or not, but previous version get an error when the script is on “subdirectory” like “/ip address” and : are omitted.

# *** missing : on do {
# Get uncounted data
		do {
			/ip accounting uncounted {

# *** remove $ from first $value in front of ":set"
				:set $value [:pick $value 0 $newline]

I rewrite this directly, too much errors

:if ([:tonum [:pick [/system resource get version] 0 1]] > 6 and $CmdHistory) do={
#       v missing : on both global and local
	:global cmd
	:local f 0
	:foreach i in=[/system history find] do={
#               v missing : on both if    v removed $ from $f
		:if ($i = $cmd) do={ :set f 1 }
#               v       v replaced <> with != but not really an error if v7 accept <>
		:if ($f != 1) do={
			:log info message="StartCMD"
			:log info message=[/system history get $i]
			:log info message="EndCMD"
		}
	}
#       v missing :          v missing /
	:global cmd  [:pick [/system history find] 0]
}

About the dynamic NAT, can be one from 3 types:

this match upnp rule because all upnp rule are “upnp : ” real example: “upnp 192.168.0.163: DIRECT_REVERSE_SERVER”

[/ip firewall nat find where dynamic=yes and comment~"^upnp "]

this match dynamic rules added from hotspot

[/ip firewall nat find where dynamic=yes and chain="hotspot"]

this match dynamic rules added from walled garden

[/ip firewall nat find where dynamic=yes and chain="hs-unauth"]

Fixed now :wink:
Sorry for jumping the gun on this :sunglasses:

Script version 4.5

fixed missing :

Simplifyed some commands

Changed foreach loop

Changed NTP section

Changes CAPsMANN section

Removed $ in set command

Fixed v 7 module missing : / and canged test

Added dynamic nat types

Mostly bug fixes. A big thanks to rextended
PS If you upgrade script to 4.5, you will lose uPnP info. Will be fixed in upcoming 3.4 with new dynamic nat rule view.

Do any use the Web Proxy view, or is Web Proxy no longer used by anyone due to HTTPS etc? If so I will remove it.

Hi,
This is great work.
But - Well, something is not working for me.
I have a CHR Mikrotik running the script, and a virtual Debian running Rsyslog + Universal Forwarder.
The UF delivers to a Splunk Enterprise (Free) index MikrotikInfo with the Mikrotik App attached.
Data is coming into the index, but I see nothing in the app. In Data → indexes I see the event counter increasing.

Where did I take a wrong turn ?

Regards from Denmark
Klaus

Normal its problem that the tag is wrong.
It need to be MikroTik with uppercase M and T

Try following search:

index=* sourcetype=mikrotik

if no data, try:

index=* host=

if no data try

index=*

I am able to find events, using “index=* sourcetype=MikroTik”
In the index I am able to find all the Neighbor units.
Nothing in the App
This is how I monitor:
bin/splunk add monitor /var/log/remotelogs/192.168.1.126/script,info.log -index MikrotikInfo -sourcetype MikroTik

I guess the index (MikrotikInfo ) you store the MikroTik data is not set a default search index for you.

If you do not see event here:

sourcetype=MikroTik

But this is ok

index=* sourcetype=MikroTik

If you user is part of the admin role, try this:

Settings->Roles->Admin->Indexes
Find the index (MikrotikInfo ) where you store the MikroTik data and in column Default and mark it.
You may need to log out/inn.

If this work, post it here and I will update this info in top post so that other user not using default index will get it to work.

Getting closer.
After setting the index MikrotikInfo to “default”, and restarting splunk (!), I am now able to see my syslogsrv in the devicelist as a host.
But no devices are listed.
I have set the routerID to “MikroTikCHR”. (A virtual CHR for testing purposes)

Then I started tailing the syslog file on syslogsrv. This came up after I rebooted the router:
2021-08-26T09:26:46.302962+02:00 192.168.1.126 script,info script=version ver=4.5
2021-08-26T09:26:46.305321+02:00 192.168.1.126 script,info script=resource free_memory=54 MB total_memory=96 MB free_hdd_space=26 MB total_hdd_space=63 MB cpu_load=1 uptime=00:02:52 write-sect-total=3577
2021-08-26T09:26:46.307561+02:00 192.168.1.126 script,info script=ntp status=started
2021-08-26T09:26:46.307938+02:00 192.168.1.126 script,info script=traffic,fasttrack=0
2021-08-26T09:26:46.318521+02:00 192.168.1.126 script,info .id=*1;comment=;name=ether1;rx-byte=154737;rx-drop=0;rx-error=0;rx-packet=1532;tx-byte=385454;tx-drop=0;tx-error=0;tx-packet=1508;tx-queue-drop=0;script=if_traffic
2021-08-26T09:26:46.320081+02:00 192.168.1.126 script,info script=sysinfo version=“6.48.4 (stable)” board-name=“CHR” model=“na” serial=na identity=“MikroTikCHR” factory-firmware=“na” current-firmware=“na” upgrade-firmware=“na”
2021-08-26T09:26:46.321610+02:00 192.168.1.126 script,info script=neighbor nid=3 .id=“*3”
2021-08-26T09:26:46.321711+02:00 192.168.1.126 script,info script=neighbor nid=3 address=“192.168.1.1”
2021-08-26T09:26:46.321820+02:00 192.168.1.126 script,info script=neighbor nid=3 address4=“192.168.1.1”
<------ the rest deleted ------>
In the index MikrotikInfo this event appears: :slight_smile:
26/08/2021
09:26:46.320
2021-08-26T09:26:46.320081+02:00 192.168.1.126 script,info script=sysinfo version=“6.48.4 (stable)” board-name=“CHR” model=“na” serial=na identity=“MikroTikCHR” factory-firmware=“na” current-firmware=“na” upgrade-firmware=“na” host = syslogsrvsource = /var/log/remotelogs/192.168.1.126/script,info.logsourcetype = MikroTik

So we have the identity string in the index, but it is not shown in the device list.
If you would like the entire logfile, I will happy to share it.

Regards
/Klaus

Here is how data looks in my logs in Splunk

script,info > MikroTik> : script=pool pool=DHCP-Pool-vlan20-Guest used=50 total=190
script,info > MikroTik> : script=pool pool=VPN-pool used=0 total=18
script,info > MikroTik> : script=pool pool=DHCP-Pool-vlan1-Home used=252 total=455
script,info > MikroTik> : script=neighbor nid=3 version=“6.48.1 (stable)”
script,info > MikroTik> : script=neighbor nid=3 uptime=“05:07:14”

You can see the MikroTik tag. For me it seem that you do not have it in your logs.

Its set in section 2b

/system logging add action=logserver prefix=> MikroTik > topics=dhcp
/system logging add action=logserver prefix=> MikroTik > topics=!debug,!packet,!snmp
/system logging add action=logserver prefix=> MikroTik > topics=hotspot

What do you see with this:

index=* host 192.168.1.126 |  table index host sourcetype source _raw

Script version 4.6

fixed Wifi script not working after upgrading RouterOS to 6.48.4

To upgrade, just cut/past the script to all router.
If you do not use Wifi, not need for upgrade.

My bad - It is a case of RTFM. I did not put the MikroTik prefix in the action.
I cleaned out all old indexfiles and data, and started over, to remove old data.
I am now able to se that a logfile exists in the Mikrotik app.
I will hack on :slight_smile: