Stats monitoring in check_mk_agent

Steve Berg edited this page Dec 31, 2016 · 9 revisions

I've written this extremely simple local check for the check_mk_agent. If you've installed the agent on your Pi-Hole place this into an executable file wherever your check_mk_agent expects to find local plugins. Alternatively this could be installed on any system that has access to the Pi-Hole console, I decided to put it on the Pi-Hole directly. I named mine "mk_pihole_stats.py" but as long as it's in the correct subdirectory and the agent script can execute it it should work just fine.

After you reinventory the client where you installed this you should have three new local checks. PiHole_Ads, PiHole_Percent and PiHole_Queries.

 #!/usr/bin/python
 # Simple (always OK) check_mk local check to track # of ads blocked today.
 # Since the console/logs rotate everyday, so will the perf data for this
 # service.
 
 import urllib2
 import json
 import re
 
 # Define your Pi-Hole console address
 console = "http://192.168.1.2/admin/"
 
 try:
     url = console + "api.php"
     result = urllib2.urlopen(url, timeout = 5).read()
     json = json.loads(result)
 
     count = json['ads_blocked_today']
     count2 = re.sub(',', '', count)
     print "0 PiHole_Ads ads_blocked=" + count2 + " OK - " + count + " ads blocked today."
 
     pct = json['ads_percentage_today']
     print "0 PiHole_Percent ads_percent=" + pct + " OK - " + pct + "% of dns requests were for ad domains."

     queries = json['dns_queries_today']
     queries2 = re.sub(',', '', queries)
     print "0 PiHole_DNS queries=" + queries2 + " OK " + queries + " DNS queries today."
 
 except:
     print "3 PiHole_Ads Unknown"

After the logs rotated for the first time since I started using this check the ads_blocked_today perf data zero'd out. I'm not quite sure why yet though, check_mk should still see previous values even if current values are zero. Found the problem. The script was returning ads_blocked and queries values with commas "," in them. Check_mk doesn't like that. So I've stripped the comma out of each returned number and it looks much better now.

Just re-wrote this in perl, only because I'm more comforatable working with perl. There's three modules you'll need to ensure are available for this to work.

#!/usr/bin/perl
use LWP::Simple;
use JSON;
use Data::Dumper;

# api output:
#     {"domains_being_blocked":"104,574","dns_queries_today":"37,188","ads_blocked_today":"708","ads_percentage_today":"1.9"    }
#
# Convert json to perl array:
# $VAR1 = {
#          'domains_being_blocked' => '104,574',
#          'ads_percentage_today' => '1.9',
#          'ads_blocked_today' => '716',
#          'dns_queries_today' => '37,496'
#        };

my $data = get("http://192.168.1.2/admin/api.php");

$data1 = decode_json $data;

$pct = $data1->{ads_percentage_today};
$blocked = $data1->{ads_blocked_today};
$dns = $data1->{dns_queries_today};

$blocked =~ s/,//g;
$dns =~ s/,//g;

print "0 PiHole_Ads ads_blocked=$blocked OK $blocked ads blocked today\n";
print "0 PiHole_Percent ads_percent=$pct OK ${pct}% of dns requests were for ad domains\n";
print "0 PiHole_DNS queries=$dns OK $dns DNS queries today\n";

Example graphs:

Ads blocked - 1 week

Ads percent - 1 week

DNS queries - 1 week

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.