Blog of Alexander Mamchenkov http://alex.mamchenkov.net ... mammoth cave ... Tue, 14 Apr 2015 09:01:01 +0000 en-US hourly 1 http://wordpress.org/?v=4.1.1 Blog of Alexander Mamchenkov http://0.gravatar.com/avatar/28fa14800d5183df0b0e73cf9c22b5ee.png?s=48 http://alex.mamchenkov.net Instagram Daily Digest http://alex.mamchenkov.net/2015/04/14/instagram-digest-20150414/ http://alex.mamchenkov.net/2015/04/14/instagram-digest-20150414/#comments Tue, 14 Apr 2015 09:01:01 +0000 http://alex.mamchenkov.net/?p=5730 Жена в процессе себяшек) Вид из окошечка деревенской таверны в горах ]]> http://alex.mamchenkov.net/2015/04/14/instagram-digest-20150414/feed/ 0 Instagram Daily Digest http://alex.mamchenkov.net/2015/04/05/instagram-digest-20150405/ http://alex.mamchenkov.net/2015/04/05/instagram-digest-20150405/#comments Sun, 05 Apr 2015 09:00:17 +0000 http://alex.mamchenkov.net/?p=5724 Trio cream Brule Steak time ]]> http://alex.mamchenkov.net/2015/04/05/instagram-digest-20150405/feed/ 0 Instagram Daily Digest http://alex.mamchenkov.net/2015/04/02/instagram-digest-20150402/ http://alex.mamchenkov.net/2015/04/02/instagram-digest-20150402/#comments Thu, 02 Apr 2015 09:00:13 +0000 http://alex.mamchenkov.net/?p=5716 Сто лет в снукер не играл Это Лимассол)]]> http://alex.mamchenkov.net/2015/04/02/instagram-digest-20150402/feed/ 0 Instagram Daily Digest http://alex.mamchenkov.net/2015/03/27/instagram-digest-20150327/ http://alex.mamchenkov.net/2015/03/27/instagram-digest-20150327/#comments Fri, 27 Mar 2015 09:01:46 +0000 http://alex.mamchenkov.net/?p=5710 Вот Лапка с коготочками. Наш маленький серый котёнок, которому скоро будет 3 года. За траву душу продаст]]> http://alex.mamchenkov.net/2015/03/27/instagram-digest-20150327/feed/ 0 Instagram Daily Digest http://alex.mamchenkov.net/2015/03/15/instagram-digest-20150315/ http://alex.mamchenkov.net/2015/03/15/instagram-digest-20150315/#comments Sun, 15 Mar 2015 09:00:27 +0000 http://alex.mamchenkov.net/?p=5696 Gp3 Tio Ellinas Что может быть лучше этой чудесной улыбки рядом весь день?]]> http://alex.mamchenkov.net/2015/03/15/instagram-digest-20150315/feed/ 0 Instagram Daily Digest http://alex.mamchenkov.net/2015/03/09/instagram-digest-20150309/ http://alex.mamchenkov.net/2015/03/09/instagram-digest-20150309/#comments Mon, 09 Mar 2015 09:00:28 +0000 http://alex.mamchenkov.net/?p=5687 The first thing I saw today in the morning) Service and fixing cooling issues ]]> http://alex.mamchenkov.net/2015/03/09/instagram-digest-20150309/feed/ 0 Instagram Daily Digest http://alex.mamchenkov.net/2015/02/10/instagram-digest-20150210/ http://alex.mamchenkov.net/2015/02/10/instagram-digest-20150210/#comments Tue, 10 Feb 2015 09:00:59 +0000 http://alex.mamchenkov.net/?p=5674 Уютный конец ]]> http://alex.mamchenkov.net/2015/02/10/instagram-digest-20150210/feed/ 0 Instagram Daily Digest http://alex.mamchenkov.net/2015/01/26/instagram-digest-20150126/ http://alex.mamchenkov.net/2015/01/26/instagram-digest-20150126/#comments Mon, 26 Jan 2015 09:00:23 +0000 http://alex.mamchenkov.net/?p=5665 Спокойствие... Вот уж точно - весело Просыпашки в рестарашке) Курочка из духовки на обед. Жена умничка]]> http://alex.mamchenkov.net/2015/01/26/instagram-digest-20150126/feed/ 0 Plain SQL v.s. Zabbix API for text history items http://alex.mamchenkov.net/2015/01/18/plain-sql-v-s-zabbix-api-text-history-items/ http://alex.mamchenkov.net/2015/01/18/plain-sql-v-s-zabbix-api-text-history-items/#comments Sun, 18 Jan 2015 10:31:36 +0000 http://alex.mamchenkov.net/?p=5660 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.

]]>
http://alex.mamchenkov.net/2015/01/18/plain-sql-v-s-zabbix-api-text-history-items/feed/ 0
Instagram Daily Digest http://alex.mamchenkov.net/2015/01/14/instagram-digest-20150114/ http://alex.mamchenkov.net/2015/01/14/instagram-digest-20150114/#comments Wed, 14 Jan 2015 09:00:46 +0000 http://alex.mamchenkov.net/?p=5654 Небо супер]]> http://alex.mamchenkov.net/2015/01/14/instagram-digest-20150114/feed/ 0