Skip to content

Instantly share code, notes, and snippets.

@thenoid
Last active May 21, 2019 19:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thenoid/09bee74000c3332f809f2e4ef9dbb6bb to your computer and use it in GitHub Desktop.
Save thenoid/09bee74000c3332f809f2e4ef9dbb6bb to your computer and use it in GitHub Desktop.
import urllib2
import simplejson
import socket
try:
# first, try to import the base class from old versions of the Agent...
from checks import AgentCheck
except ImportError:
# ...if the above failed, the check is running in Agent version 6 or later
from datadog_checks.checks import AgentCheck
__version__ = '19.5.1'
class CustomVault(AgentCheck):
def check(self, instance):
token = instance.get('token', "")
hostname = instance.get('host', socket.gethostname())
port = instance.get('port', 8200)
ssl = 'https' if instance.get('ssl', True) else 'http'
lburl = "{}://{}:{}".format(ssl, hostname, port)
fullurl = "{}/v1/sys/internal/counters/requests".format(lburl)
req = urllib2.Request(fullurl, headers={'X-Vault-Token': token})
try:
response = urllib2.urlopen(req)
data = simplejson.load(response)
except urllib2.HTTPError, error:
data = {}
value = data.get("data", {}).get("counters", [{}])[-1].get("total", 0)
self.gauge('custom_vault.counters.requests', value)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment