LibreNMS Extend script for DNSDist took too long for executing. After seeking into its source code, I realized that after using cURL to get JSON stat via API Call, it executes jq command to parse every single line on that JSON.
I think it sure had unnecessary steps just to obtain stat values. So I wrote simple PHP script. It basically call cURL, save those value to variable, encode it with JSON Encode, then echoing them so SNMP Extend could got its values.
#!/usr/bin/php
<?php
/*
LibreNMS DNSDist Extend Replacement
Widianto - 20230118
*/
$API_AUTH_USER="admin";
$API_AUTH_PASS="";
$API_URL="";
$API_STATS="jsonstat?command=stats";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $API_URL . $API_STATS);
curl_setopt($ch, CURLOPT_USERPWD, "$API_AUTH_USER:$API_AUTH_PASS");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
if(FALSE !== ($arr = json_decode($result, TRUE))){
echo $arr["cache-hits"] . "\n";
echo $arr["cache-misses"] . "\n";
echo $arr["downstream-send-errors"] . "\n";
echo $arr["downstream-timeouts"] . "\n";
echo $arr["dyn-block-nmg-size"] . "\n";
echo $arr["dyn-blocked"] . "\n";
echo $arr["queries"] . "\n";
echo $arr["rdqueries"] . "\n";
echo $arr["empty-queries"] . "\n";
echo $arr["no-policy"] . "\n";
echo $arr["noncompliant-queries"] . "\n";
echo $arr["noncompliant-responses"] . "\n";
echo $arr["self-answered"] . "\n";
echo $arr["servfail-responses"] . "\n";
echo $arr["trunc-failures"] . "\n";
echo $arr["acl-drops"] . "\n";
echo $arr["rule-drop"] . "\n";
echo $arr["rule-nxdomain"] . "\n";
echo $arr["rule-refused"] . "\n";
echo $arr["latency-avg100"] . "\n";
echo $arr["latency-avg1000"] . "\n";
echo $arr["latency-avg10000"] . "\n";
echo $arr["latency-avg1000000"] . "\n";
echo $arr["latency-slow"] . "\n";
echo $arr["latency0-1"] . "\n";
echo $arr["latency1-10"] . "\n";
echo $arr["latency10-50"] . "\n";
echo $arr["latency50-100"] . "\n";
echo $arr["latency100-1000"] . "\n";
}
Original LibreNMS Extend for DNSDist took about 11 seconds to finished. The replacement only took 0.145 seconds to do the same job.