Bandwidth usage report per IP address

I created a simple python script for collecting data from mikrotik accounting (documetation) feature.

Here is my “mik_collector.py” script:

all you need to change is router_ip variable to match your router IP

after that you can run this script simply with this command:

python mik_collector.py

this will collect data from mikrotik every 5 seconds and store it in data.db sqlite3 database in the same directory as the script is.

All you need to do at mikrotik side is to enable accounting feature:

If you want to analyze your collected data you can do it with sqlite3 command from Ubuntu command prompt:

sqlite3 data.db "select * from sum_per_month order by month"

like this:

you can write your own queries, all data is stored in a table named accounting. You can share your query in the comments :)

Send telegram messages from Mikrotik RouterOS

If you are here you probably already know what telegram is. If not you can read about it here.

I created a simple script (SendToTelegram) that allows you to send messages to telegram:

:global telegramMessage
:local botid
:local chatid

set botid "sdfzgasf7126jwsd7a8s12>" # <- change this
set chatid "21156423187"            # <- change this

if ($telegramMessage != "") do={
   /tool fetch url="https://api.telegram.org/bot$botid/sendMessage\?chat_id=$chatid&text=$telegramMessage" keep-result=no
   set telegramMessage ""
}

Usage:

set your message to telegramMessage global variable

global telegramMessage "Wolfy was here"

run script

/system script run SendToTelegram

You can use this in combination with netwach:

you should recieve a message to your telegram :)

Check log files with grep

I sometime need to check some logs and I do this with this command:

egrep -o "p1|p2|...|pn" filename | sort | uniq -c

Example:

egrep -o "success|error|fail" test_file | sort | uniq -c

Sample input:

test started at 00:00
test delete fail
test error
test connect success
test insert success
test started at 00:00
test delete fail
test error
test connect success
test insert success
test started at 00:00
test delete  fail
test error
test connect success
test insert success
test started at 00:00
test delete  fail
test error
test connect success
test insert success

Sample output:

4 error
4 fail
8 success

How it works?

Continue reading Check log files with grep

Backup (from command line)/Restore OpenFiler

Backup Openfiler

When I need a Openfiler backup for the first time, I did this:

1. Log in OpenFiler Web UI
2. Click System
3. Click Backup/Restore
4. Click Download

This works well if you have time to do this steps every day… but if you don’t want to do this steps every day, you must find another way to do daily backups…

After my little research, I find out that when I click on Backup/Restore, Openfiler cal this file: https://openfiler:446/admin/system_backup.html

I examined the content of this file and finded out, that all you need to backup your OpenFiler configuration is this:
Continue reading Backup (from command line)/Restore OpenFiler

Power on/off script for Xen Running VMs on XenServer

Days ago I needed a script to power on/off my runing VMs on XenServer… In my case I needed this to power off VMs when my UPS is running on batery and later to power on when the power comes back.

I was looking for a script that can do the job, but after a while I didn’t find anything usefull… so I made my own script…
Continue reading Power on/off script for Xen Running VMs on XenServer