Plain SQL v.s. Zabbix API for text history items

For one of the past tasks I have the following requirement: a number of hosts that are monitored with Zabbix has a item of history type that provide list of addresses in text format (one per line) and than I need to take all those lists and make one common list (table) where I would have in each row an address from the list, a hostname of the node where this address was last seen and timestamps.

Initially I added in zabbix the item to monitor on each node I needed, created a separate table in MySQL to hold the final list and then made a cron script that would do the following:

  • retrieve a list of nodes that have a given item by item key_ with Zabbix API
  • retrieve all items by itemid that I found out in a previous query with Zabbix API
  • for each item, retrieve the latest history with Zabbix API
  • for each history, split the text by new line to get addresses and then add each address with source host and timestamps in the final MySQL table, or update the timestamp and source in case address is already in the list

With total size of the list about 5K addresses, all of the above was taking around 4-5 minutes and was consuming a lot of CPU and memory on the server. As I was limited on server resources and wanted the list to be updated every minute, I decided to avoid using Zabbix API and try to the job with plain MySQL queries. As I am only interested in the latest history for each node, I recalled my own post that I did a while ago on SQL GROUP BY with subqueries. Checking zabbix SQL structure around and a bit of playing with queries, I ended up with the single request that will give me all I need:

SELECT * FROM (SELECT h.host,hi.id,hi.value\
FROM hosts AS h, items AS i, history_text AS hi\
WHERE i.hostid=h.hostid AND hi.itemid=i.itemid\
AND h.status<>3 AND i.key_ LIKE 'my_item_key%'\
AND hi.value <> '' ORDER BY hi.clock DESC) tmp_table\
GROUP BY host;

The sub-query will give me hostname, history entry id and history values from history_text for non template hosts (status <> 3), with non empty value for the item key_ I want order by time newest first and then the main query will take that list shrink it down to have only on entry per host which will be the newest one.

Now having this list, for each result raw, I can split the value by newline to extract all addresses and add them or update one by one in the final table. Here comes in another trick that I that I described in my old post here: since I need to update the source for already existed entries in the final table while adding anything that is not there, I run the insert with the following SQL statement, considering that the address field is a primary key and is unique:

INSERT INTO final_table (address,source)\
VALUES('$address','$source')\
ON DUPLICATE KEY UPDATE source='$host';

So whenever there is a conflict on address field, the source field is getting updated with the new value.

After changing Zabbix API queries to native SQL, the script runs few seconds and consumes almost nothing, as it is relying on MySQL engine to do most of the job, I MySQL can do it much better.

Finally, if there is no interest in history items after they were imported into final_table, it is possible to delete all raws for the items from history_text for a given key_ table with the following SQL query:

DELETE FROM history_text WHERE itemid IN\
(SELECT hi.id FROM items AS i, history_text AS hi\
WHERE hi.itemid=i.itemid AND i.key_ LIKE 'my_item_key%');

This is an alternative to relying on Zabbix housekeeper that will do the job, but a bit later. And if polling of the nodes for this item is pretty frequent and resulted values are pretty big – it will consume space in MySQL that we want to avoid.

VLC mosaic for multiple RSTP streams

Recently had a task to open streams from 4 cameras over RSTP in a single windows using VLC. There is a bunch of howtos on net with relevant info and examples, but after trying many options non of them worked out of the box. Tweaking things here and there for a while I managed to come up with the working configuration, so I post it here for future reference and hopefully it will be helpful to anyone else.

My cameras stream dimensions is 1280×720 and as I want to fit 4 cameras on one screen, I will scale them in half to have screen size 1280×720 and each stream size 64×360.

First thing to do is to create some background image with exact size of the desired screen (1280×720 in my case) and save somewhere near (bg.jpg in my case).

Then we need to create a VLM config file (let it be cam.vlm.conf) to tell VLC about my streams and how to deal with them:

new channel1 broadcast enabled                                                       
setup channel1 input "rtsp://x.x.x.x:554/?user=foo&password=bar&channel=1&stream=0.sdp"
setup channel1 output #mosaic-bridge{id=1,height=360,width=640}

new channel2 broadcast enabled                                                       
setup channel2 input "rtsp://x.x.x.y:554/?user=foo&password=bar&channel=1&stream=0.sdp"
setup channel2 output #mosaic-bridge{id=2,height=360,width=640}

new channel3 broadcast enabled                                                       
setup channel3 input "rtsp://x.x.x.z:554/?user=foo&password=bar&channel=1&stream=0.sdp"
setup channel3 output #mosaic-bridge{id=3,height=360,width=640}

new channel4 broadcast enabled                                                       
setup channel4 input "rtsp://x.x.x.w:554/?user=foo&password=bar&channel=1&stream=0.sdp"
setup channel4 output #mosaic-bridge{id=4,height=360,width=640}

new mosaic broadcast enabled
setup mosaic input file:///home/user/Pictures/bg.jpg
setup mosaic option image-duration=-1
setup mosaic option image-fps=0
setup mosaic option mosaic-rows=2
setup mosaic option mosaic-cols=2
setup mosaic option mosaic-position=1
setup mosaic output #transcode{sfilter=mosaic,vcodec=mp4v,vb=8500,acodec=none,fps=25,scale=1}:display

control channel1 play
control channel2 play
control channel3 play
control channel4 play
control mosaic play

The input path to the camera streams as well as full path to the background image should be adjusted accordingly.

Somehow VLC doesn’t want to recognize mosaic-(height|width|order) parameters in the VLM file, so need to supply them inline as arguments when calling VLC. Now as we have VLM file ready, we can start the stream with the following command:

cvlc --vlm-conf /home/user/Desktop/cam.vlm.conf --mosaic-width 1280 --mosaic-order "1,2,3,4" --mosaic-height 720

Adjust the path to the VLM file accordingly as well as mosaic order or whatever else you want. For me all of the above worked out perfectly well and I can see all my 4 cameras in single window.

Galaxy Nexus custom ROM

As you may know, Google has announced the next Adnroid (KitKat) some time ago, and at the same moment they told that Galaxy Nexus will not receive this update. This was pretty sad for me, as I got my Nexus mainly because of two reasons: raw Android (without all those apps that normally come pre-installed on branded phones like Sony, LG, whatever) and over-the-air updates to the latest version of Android.

All the time I had Nexus, I was never bothering with routing, custom ROM or what-so-ever as I was pretty happy with the stock SW, but after Google rejected to get the latest SW, I had no choice. Checking all around and doing couple attempts to find what suites me – I finally found what I like and what works for me.

Criteria to choose one or another was as follows:

  • Latest Android
  • Minimal non-stock apps included
  • Most customization
  • Stability

Due to the first criteria or the latest version of Android – there are not that many ROMs out there to pass the test. After checking few, I had finally chose the SlimRoms guys to support me. They have latest Android (currently 4.4.2), frequent updates (even weekly builds), almost all SW is stock one (except Nova Launcher, Simple Browser and SlimCenter to check for updates and some SlimRom IRC tool), and nice customization. For instance, they have increased the standard DPI in SW build, so I have every item smaller, but can have more on the screen, they also have customization on ring short-cuts and so on, but overall it is not over complicated and they keep the ROM really slim.

After getting custom ROM and playing around – I really enjoy and think I gonna stay with rooted phone and such ROMs. Not that I have a lot of things that require root, but some are:

  • remote apps that I don’t need (browser, as I use Chrome, sms, as I use Hangout, Gallery, as I use QuickPic, launchers, as I use Go Launcher and so on)
  • nice SSH client that supports private keys and identities (JuiceSSH – my dream as a system administrator)
  • custom app permissions (as I fed up facebook and others polling GPS and doing other mess)

Anyhow, if you are an owner of Samsung Galaxy Nexus and want latest Android, or you are an owner of any other Android-based phone and wanna play around – check it, it is fun :-)

Touchscreen problems on dual head fedora

Today had an interesting issue that my colleague faced. He has touch screen Sony Vaio and normal, non-touch screen monitor attached for dual head. While he was trying to work with touch screen and special pen, then cursor was going weird.

The solution here is to limit the touch screen input to touch screen area only and it is done with xinput command. But keep in mind that the ID’s for input devices in xinput are different after each boot, so if you wish to have some kind of a script to do the job, make sure you work with correct IDs.

As we were dealing with N-trig DuoSense input device, I wrote a short script that can be put somewhere and then enabled in Start Up Applications for Gnome/MATE/Whatever.

The script is as follows (adjust the input/output devs as per your needs by checking xinput –list and xrandr):

#!/bin/bash

INPUT_DEV="N-trig";
OUTPUT_DEV="eDP1";

for i in `xinput --list | grep $INPUT_DEV | sed 's/.*id=\([0-9]\+\).*/\1/g'`;
do
xinput --map-to-output $i $OUTPUT_DEV;
done;

Google Chrome Fullscreen

Have a problem with Google Chrome not willing to exit fullscreen mode (by F11, or clicking “Exit full screen” bubble link) when run on a secondary monitor on my Fedora. Pretty annoying and the only way to restore to normal was to re-open chrome, but this is not always a good thing for me.

Today I finally figured out the way to exit fullscreen without a need to reopen Chrome:

  • press Ctrl+N to open new Chrome window (it will open on main monitor and not in fullscreen mode)
  • go to settings and select “Use system title bar and borders”
  • go back to the fullscreen window of Chrome that is on the secondary monitor and press F11 to get it out the fullscreen (this time it will work)
  • untick the “Use system title bar and borders” in settings to get back a normal look

A bit messy, but I hope one day this bug will be sorted out.