Triggering Scripts on Queue Traffic Pattern

Hi, I am new to the MikroTik world and already addicted. :mrgreen: Very nice products!

Ok, I have a music studio that is monitored by a network attached webcam and I would like to trigger a script on the MikroTik router if someone enters the room.

The cam uploads a picture every minute to display the current image on a website. If the sensor of the cam discovers some activity in the room it starts an upload to an ftp server to keep the pictures during the activity. I already made a queue (see screenshots attached) to see the usage of the studio.

As you can see there is always a little peak at the beginning of the activity. This is because the cam also uploads the last 5 pictures before the activity is discovered. There are also little outtages which are caused by the weak 3g signal at this place.

I would appreciate if you can give me a hint how to build a script that triggers an action (like sending an email or playing a sound on the router) when someone enters the room based on the queue (or some completely different idea). Maybe there is also a similar script that I did not came across with my search.

Thanks,
Shoe
cam-queue-weekly.gif
cam-queue-daily.gif

You can also check with /tool fetch if uploaded new file exist on the server, that way indicating some activity.

Thanks for your input, mrz. Since the 3g connection is really weak I would like to avoid to check external resources like the FTP server.

I played around a little bit and ended with the following script:

:local queueID [/queue simple find name="cam-xyz"];
:local queueComment [/queue simple get $queueID comment];
:local bytes [/queue simple get $queueID bytes];

:local bytesNow 0;

# get bytes sent from rcvd-sent-string
:for i from=0 to=([:len $bytes] - 1) do={ 
  :if ( [:pick $bytes $i] = "/") do={ 
    :set bytesNow [:pick $bytes ($i + 1) ([:len $bytes])];
  } 
}

:delay 177000ms;

:set bytes [/queue simple get $queueID bytes];
:local bytesL8r 0;

# get bytes sent from rcvd-sent-string
:for i from=0 to=([:len $bytes] - 1) do={ 
  :if ( [:pick $bytes $i] = "/") do={ 
    :set bytesL8r [:pick $bytes ($i + 1) ([:len $bytes])];
  } 
}

:local bytesDiff ($bytesL8r - $bytesNow);

:if ($bytesDiff > 200000) do={
  :if ($queueComment != "online") do={
    /queue simple set $queueID comment="online"
    /log info ("someone entered the studio");
  }
} else={
  :if ($queueComment = "online") do={
    /queue simple set $queueID comment="offline"
    /log info ("someone went home");
  }
}

Basically the script checks if the the upload traffic is larger than 200KB during ~3 minutes to check if someone is in the room while using the queue’s comment field to save the online/offline flag. At non-activity times the upload of the cam is around 140KB during this time frame. The script is scheduled to run every 3 minutes.

You’ll probably see minor or even major misdoings in my code, this is because I am a experimental amateur coder. But I do appreciate any suggestions for improvement.