DNS Masq
Dashboard of dnsmasq statistic
### On Dashboard:
* Internal dnsmasq stats (evictions, hits, auth, insections and misses queries)
* Upstream queries
* Upstream errors

#### Requirement: python-dnspython

grafana->graphite->collectd
```
LoadPlugin python
<Plugin python>
	ModulePath "/opt/collectd/python"

	Import "dns_masq"
	<Module dns_masq>
#		Verbose true
		Interval 60
        Address "<dnsmasq_address>"
        Timeout 1
	</Module>
</Plugin>
```

### /opt/collectd/python/dns_masq.py
```
import collectd

import dns.resolver

class dns_masq(object):
    def __init__(self):
        self.plugin_name = 'dns_masq'
        self.verbose_logging = False
        self.address = '127.0.0.1'
        self.timeout = 0.5

    def log_verbose(self, msg):
        if not self.verbose_logging:
            return
        collectd.info('%s plugin [verbose]: %s' % (self.plugin_name, msg))

    def store(self, key, val):
        val = collectd.Values(type='gauge', plugin=self.plugin_name, type_instance=key, values=[val])
        val.dispatch()
        self.log_verbose('Sending value: %s:%s' % (key, val))

    def read(self):
        # init
        self.resolver = dns.resolver.Resolver(configure=False)
        self.resolver.lifetime = self.timeout
        self.resolver.nameservers = [self.address]
        #dig +short chaos txt *.bind. @address
        for name_metric in ['insertions','evictions','misses','hits','auth']:
            answer = self.resolver.query('%s.bind' % name_metric, 'TXT', 'CHAOS')
            self.store(name_metric, str(answer.rrset[0])[1:-1])
        #dig +short chaos txt servers.bind. @address
        answer = self.resolver.query('servers.bind', 'TXT', 'CHAOS')
        for uplink in str(answer.rrset[0]).split('"'):
            raw = uplink.split(" ")
            if isinstance(raw, list) and len(raw) == 3:
                name = raw[0].replace('.','_')
                queries = raw[1]
                errors = raw[2]
                self.store('upstream_%s_queries' % name, queries)
                self.store('upstream_%s_errors' % name, errors)

    def configure_callback(self, conf):
        for node in conf.children:
            vals = [str(v) for v in node.values]
            if node.key == 'Verbose':
                self.verbose_logging = (vals[0].lower() == 'true')
            elif node.key == 'Interval':
                self.interval = float(vals[0])
            elif node.key == 'Address':
                self.address = str(vals[0])
            elif node.key == 'Timeout':
                self.timeout = float(vals[0])
            else:
                raise ValueError('%s plugin: Unknown config key: %s' % (self.plugin_name, node.key))
        collectd.register_read(self.read, self.interval)
        self.log_verbose('configured with interval=%s' % self.interval)


app_dns_masq = dns_masq()
collectd.register_config(app_dns_masq.configure_callback)
```
Dashboard of dnsmasq statistic
{
  "__inputs": [
    {
      "name": "DS_RCO",
      "label": "Graphite",
      "description": "graphite datasource",
      "type": "datasource",
      "pluginId": "graphite",
      "pluginName": "Graphite"
    },
    {
      "name": "VAR_PREFIX",
      "type": "constant",
      "label": "prefix",
      "value": "rco.collectd",
      "description": "prefix of collectd write_graphite plugin"
    }
  ],
  "__requires": [
    {
      "type": "grafana",
      "id": "grafana",
      "name": "Grafana",
      "version": "7.1.3"
    },
    {
      "type": "panel",
      "id": "graph",
      "name": "Graph",
      "version": ""
    },
    {
      "type": "datasource",
      "id": "graphite",
      "name": "Graphite",
      "version": "1.0.0"
    }
  ],
  "annotations": {
    "list": [
      {
        "builtIn": 1,
        "datasource": "-- Grafana --",
        "enable": true,
        "hide": true,
        "iconColor": "rgba(0, 211, 255, 1)",
        "name": "Annotations & Alerts",
        "type": "dashboard"
      }
    ]
  },
  "description": "Dashboard of dnsmasq statistic",
  "editable": true,
  "gnetId": 12837,
  "graphTooltip": 1,
  "id": null,
  "iteration": 1597666678093,
  "links": [
    {
      "$$hashKey": "object:728",
      "asDropdown": true,
      "icon": "external link",
      "includeVars": true,
      "keepTime": true,
      "tags": [
        "menu"
      ],
      "targetBlank": false,
      "title": "main",
      "type": "dashboards"
    }
  ],
  "panels": [
    {
      "aliasColors": {},
      "bars": false,
      "dashLength": 10,
      "dashes": false,
      "datasource": "${DS_RCO}",
      "decimals": 0,
      "description": "- auth - The number of authoritative queries answered.\n- hits - The number of cache hits.\n- misses - The number of cache misses.\n- insertions - Тotal number of names that have been inserted into the cache.\n- evictions - The number of names which have had to removed from the cache before they expired in order to make room for new names.",
      "fieldConfig": {
        "defaults": {
          "custom": {}
        },
        "overrides": []
      },
      "fill": 1,
      "fillGradient": 0,
      "gridPos": {
        "h": 7,
        "w": 24,
        "x": 0,
        "y": 0
      },
      "hiddenSeries": false,
      "id": 2,
      "legend": {
        "alignAsTable": true,
        "avg": true,
        "current": true,
        "max": true,
        "min": true,
        "rightSide": true,
        "show": true,
        "total": true,
        "values": true
      },
      "lines": true,
      "linewidth": 1,
      "links": [],
      "nullPointMode": "null",
      "paceLength": 10,
      "percentage": false,
      "pluginVersion": "7.1.3",
      "pointradius": 5,
      "points": false,
      "renderer": "flot",
      "repeat": null,
      "repeatDirection": "h",
      "seriesOverrides": [],
      "spaceLength": 10,
      "stack": false,
      "steppedLine": false,
      "targets": [
        {
          "refCount": 0,
          "refId": "A",
          "target": "alias(nonNegativeDerivative($prefix.$vm.dns_masq.gauge-evictions), 'evictions')"
        },
        {
          "refCount": 0,
          "refId": "B",
          "target": "alias(nonNegativeDerivative($prefix.$vm.dns_masq.gauge-auth), 'auth')"
        },
        {
          "refCount": 0,
          "refId": "C",
          "target": "alias(nonNegativeDerivative($prefix.$vm.dns_masq.gauge-hits), 'hits')"
        },
        {
          "refCount": 0,
          "refId": "D",
          "target": "alias(nonNegativeDerivative($prefix.$vm.dns_masq.gauge-insertions), 'insertions')"
        },
        {
          "refCount": 0,
          "refId": "E",
          "target": "alias(nonNegativeDerivative($prefix.$vm.dns_masq.gauge-misses), 'misses')"
        }
      ],
      "thresholds": [],
      "timeFrom": null,
      "timeRegions": [],
      "timeShift": null,
      "title": "Internal dnsmasq stats",
      "tooltip": {
        "shared": true,
        "sort": 0,
        "value_type": "individual"
      },
      "type": "graph",
      "xaxis": {
        "buckets": null,
        "mode": "time",
        "name": null,
        "show": true,
        "values": []
      },
      "yaxes": [
        {
          "$$hashKey": "object:670",
          "decimals": 0,
          "format": "none",
          "label": null,
          "logBase": 1,
          "max": null,
          "min": "0",
          "show": true
        },
        {
          "$$hashKey": "object:671",
          "format": "short",
          "label": null,
          "logBase": 1,
          "max": null,
          "min": null,
          "show": false
        }
      ],
      "yaxis": {
        "align": false,
        "alignLevel": null
      }
    },
    {
      "aliasColors": {},
      "bars": false,
      "dashLength": 10,
      "dashes": false,
      "datasource": "${DS_RCO}",
      "decimals": 0,
      "description": "The number of queries sent to upstream.",
      "fieldConfig": {
        "defaults": {
          "custom": {}
        },
        "overrides": []
      },
      "fill": 1,
      "fillGradient": 0,
      "gridPos": {
        "h": 7,
        "w": 24,
        "x": 0,
        "y": 7
      },
      "hiddenSeries": false,
      "id": 5,
      "legend": {
        "alignAsTable": true,
        "avg": true,
        "current": true,
        "max": true,
        "min": true,
        "rightSide": true,
        "show": true,
        "total": true,
        "values": true
      },
      "lines": true,
      "linewidth": 1,
      "links": [],
      "nullPointMode": "null",
      "paceLength": 10,
      "percentage": false,
      "pluginVersion": "7.1.3",
      "pointradius": 5,
      "points": false,
      "renderer": "flot",
      "repeatDirection": "h",
      "seriesOverrides": [],
      "spaceLength": 10,
      "stack": false,
      "steppedLine": false,
      "targets": [
        {
          "refCount": 0,
          "refId": "A",
          "target": "aliasByMetric(aliasSub(nonNegativeDerivative($prefix.$vm.dns_masq.gauge-upstream_*_queries), '^.*\\.gauge-upstream_(.*)_queries', '\\1'))",
          "textEditor": true
        }
      ],
      "thresholds": [],
      "timeFrom": null,
      "timeRegions": [],
      "timeShift": null,
      "title": "upstream queries",
      "tooltip": {
        "shared": true,
        "sort": 0,
        "value_type": "individual"
      },
      "type": "graph",
      "xaxis": {
        "buckets": null,
        "mode": "time",
        "name": null,
        "show": true,
        "values": []
      },
      "yaxes": [
        {
          "$$hashKey": "object:253",
          "decimals": 0,
          "format": "none",
          "label": null,
          "logBase": 1,
          "max": null,
          "min": "0",
          "show": true
        },
        {
          "$$hashKey": "object:254",
          "format": "short",
          "label": null,
          "logBase": 1,
          "max": null,
          "min": null,
          "show": false
        }
      ],
      "yaxis": {
        "align": false,
        "alignLevel": null
      }
    },
    {
      "aliasColors": {},
      "bars": false,
      "dashLength": 10,
      "dashes": false,
      "datasource": "${DS_RCO}",
      "decimals": 0,
      "description": "The number which resulted in an error.",
      "fieldConfig": {
        "defaults": {
          "custom": {}
        },
        "overrides": []
      },
      "fill": 1,
      "fillGradient": 0,
      "gridPos": {
        "h": 7,
        "w": 24,
        "x": 0,
        "y": 14
      },
      "hiddenSeries": false,
      "id": 6,
      "legend": {
        "alignAsTable": true,
        "avg": true,
        "current": true,
        "max": true,
        "min": true,
        "rightSide": true,
        "show": true,
        "total": true,
        "values": true
      },
      "lines": true,
      "linewidth": 1,
      "links": [],
      "nullPointMode": "null",
      "paceLength": 10,
      "percentage": false,
      "pluginVersion": "7.1.3",
      "pointradius": 5,
      "points": false,
      "renderer": "flot",
      "repeatDirection": "h",
      "seriesOverrides": [],
      "spaceLength": 10,
      "stack": false,
      "steppedLine": false,
      "targets": [
        {
          "refCount": 0,
          "refId": "A",
          "target": "aliasByMetric(aliasSub(nonNegativeDerivative($prefix.$vm.dns_masq.gauge-upstream_*_errors), '^.*\\.gauge-upstream_(.*)_errors', '\\1'))",
          "textEditor": true
        }
      ],
      "thresholds": [],
      "timeFrom": null,
      "timeRegions": [],
      "timeShift": null,
      "title": "upstream errors",
      "tooltip": {
        "shared": true,
        "sort": 0,
        "value_type": "individual"
      },
      "type": "graph",
      "xaxis": {
        "buckets": null,
        "mode": "time",
        "name": null,
        "show": true,
        "values": []
      },
      "yaxes": [
        {
          "$$hashKey": "object:464",
          "decimals": 0,
          "format": "none",
          "label": null,
          "logBase": 1,
          "max": null,
          "min": "0",
          "show": true
        },
        {
          "$$hashKey": "object:465",
          "format": "short",
          "label": null,
          "logBase": 1,
          "max": null,
          "min": null,
          "show": false
        }
      ],
      "yaxis": {
        "align": false,
        "alignLevel": null
      }
    }
  ],
  "schemaVersion": 26,
  "style": "dark",
  "tags": [
    "collectd",
    "dns",
    "main"
  ],
  "templating": {
    "list": [
      {
        "current": {
          "value": "${VAR_PREFIX}",
          "text": "${VAR_PREFIX}",
          "selected": false
        },
        "hide": 2,
        "label": "prefix",
        "name": "prefix",
        "options": [
          {
            "value": "${VAR_PREFIX}",
            "text": "${VAR_PREFIX}",
            "selected": false
          }
        ],
        "query": "${VAR_PREFIX}",
        "skipUrlSync": false,
        "type": "constant"
      },
      {
        "allValue": null,
        "current": {},
        "datasource": "${DS_RCO}",
        "definition": "$prefix.*",
        "hide": 0,
        "includeAll": false,
        "label": "vm",
        "multi": false,
        "name": "vm",
        "options": [],
        "query": "$prefix.*",
        "refresh": 1,
        "regex": "",
        "skipUrlSync": false,
        "sort": 0,
        "tagValuesQuery": "",
        "tags": [],
        "tagsQuery": "",
        "type": "query",
        "useTags": false
      }
    ]
  },
  "time": {
    "from": "now-6h",
    "to": "now"
  },
  "timepicker": {
    "refresh_intervals": [
      "5s",
      "10s",
      "30s",
      "1m",
      "5m",
      "15m",
      "30m",
      "1h",
      "2h",
      "1d"
    ],
    "time_options": [
      "5m",
      "15m",
      "1h",
      "6h",
      "12h",
      "24h",
      "2d",
      "7d",
      "30d"
    ]
  },
  "timezone": "",
  "title": "DNS Masq",
  "uid": "dns_masq",
  "version": 20
}