Basic concept is simple – BIND will generate XML file with statistics, PHP script output values we want into standalone files and we read those values with ZABBIX agent.
We gonna need PHP to process XML statistics from BIND, install it with:
apt-get install php5-common php5-cli
Turn on integrated BIND webserver with statistics in configuration file named.conf:
statistics-channels { inet * port 8053 allow { 127.0.0.1;}; };
And reload: service bind9 reload
Let’s say that we will have our statistics in /home/bindstat. Save there PHP file stats.php:
<?php $path = dirname(__FILE__)."/"; $xml = simplexml_load_file($path."stats.xml"); $save = array("Requestv4", "ReqEdns0", "ReqTCP", "Response", "TruncatedResp", "RespEDNS0", "QrySuccess", "QryAuthAns", "QryNoauthAns", "QryReferral", "QryNxrrset", "QrySERVFAIL", "QryNXDOMAIN", "QryRecursion", "QryDuplicate", "QryDropped", "QryFailure", "XfrReqDone"); foreach($xml->bind->statistics->server->nsstat as $stat) { if(in_array($stat->name, $save)) { $f = fopen($path.$stat->name, "w"); fwrite($f, $stat->counter, strlen($stat->counter)); fclose($f); } }
Edit crontab and add lines to download statistics and parse them:
*/1 * * * * curl -s http://127.0.0.1:8053/ > /home/bindstat/stats.xml && /usr/bin/php5 -f /home/bindstat/stats.php
Make sure we have proper rights and owners:
chmod 0660 /home/bindstat/* chown zabbix /home/bindstat/*
Finally add file for ZABBIX agent /etc/zabbix/zabbix_agentd.d/bind.conf with content:
UserParameter=bind.status[*],head -n 1 /home/bindstat/bind/$1
And restart: /etc/init.d/zabbix-agent reload
To use with ZABBIX start with this template from ZABBIX 2.0 with basic items and triggers. Also you can download template from Zabbix 4.4 here.
Enjoy!
29.11.2015 v 17:39
Hi,
Thanks your work. I found 2 mistake in this write.
1st: You not closed the stats.php with ?> tag.
2nd: the crontab settings wrong, becasue the 2 process always run at the same time, and a stats.php couldn’t open stats.xml when is open t owrite. The correct crontab is the following:
*/1 * * * * curl -s http://127.0.0.1:8053/ > /var/local/zabbix/stats.xml && /usr/bin/php5 -f /var/local/zabbix/stats.php
You have to concatenate the 2 process with double & sign. The second part of this row runs after success end of fist part. So every process will make a good exit.
29.11.2015 v 17:53
Hello,
thank you for comment and help!
1) It is not a mistake – I’m omiting closing tag in all my PHP scripts, it’s a habbit and I can encourage others to do so. Some reasons why to do that are for example here: http://stackoverflow.com/questions/4410704/why-would-one-omit-the-close-tag
2) You’re right, I though I had this on my mind, but clearly there is a mistake. I fixed it in article.
13.1.2017 v 21:07
Hi,
I’m using your script and I have been face this ERROR PHP .
PHP Fatal error: Uncaught Error: Call to undefined function simplexml_load_file() in /usr/local/etc/bind/stats.php:4
Stack trace:
#0 {main}
thrown in /usr/local/etc/bind/stats.php on line 4
Do you know how can I fixed it?
14.1.2017 v 9:46
Hello,
function simplexml_load_file is in both PHP 5 and 7: http://php.net/manual/en/function.simplexml-load-file.php
But it seems you do not have XML extension installed (and enabled). Please follow this Stack overflow answer:
http://stackoverflow.com/questions/31206186/call-to-undefined-function-simplexml-load-string-in-cron-file
It should solve your problem.
20.2.2020 v 16:55
Hello ,
First thank u very much for ur work
but i have issue , i can’t import the template , i have problem
Details Import failed
Invalid tag „/zabbix_export/templates/template(1)/items/item(1)“: the tag „logtimefmt“ is missing.
20.2.2020 v 17:47
Hello,
you’re welcome! Are you sure that it is my tepmplate you’re importing? That error message doesn’t make much sense to me, there are no tags at all (so no logtimefmt). URL to my template which is in article above is: https://tricker.cz/assets/zbx_bind.xml
It should work, I also checked the first items in template and haven’t found any strange tags there 🙂
21.2.2020 v 10:53
Hello ,
thank u for ur response , so i try again and i have the same problem,
can u help me to fix this problem sir
Best regards,
21.2.2020 v 11:14
Hello,
not sure how much can I do for you. Obviously problem is not presence of any unsupported tags, but quite the opposite. Your ZABBIX is having problem with missing tag, that I do not use (and need). That template I provided before is from old server, I made new export from actual version 4.4:
https://tricker.cz/assets/zbx_export_templates-bind-zbx44.xml
Try this. If it doesn’t work (and there is no tag logtimefmt as well), try updating your ZABBIX.
Hope you will make this work.
24.2.2020 v 14:42
Hello ,
Thank u for updating template , i import this template it works ,
but zabbix didn’t collect data for exemple the graph is empty no data in the graph
Sorry for my bad english
Regards,
25.2.2020 v 9:04
Hello,
no problem, I can understand your english. Now you need to debug where the problem is. I really do not have time to do this with you step by step, but in general:
1) Can you downolad and see statistics in XML on machine with BIND server?
2) Is PHP processing that XML file OK (you should see files like QryNXDOMAIN within stats.php and stats.xml directory)?
3) Do ZABBIX agent have and read keys defined in zabbix_agentd.d/bind.conf (on same server with bind and ZABBIX agent you can try to get these values with zabbix_agent and parameter „t“)?
If so, then you should be able to use this template with no problem and it should read all correctly.