Skip to content

Instantly share code, notes, and snippets.

@philliphaines
Last active May 1, 2021 08:26
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 philliphaines/06e7cef908f921de94b5 to your computer and use it in GitHub Desktop.
Save philliphaines/06e7cef908f921de94b5 to your computer and use it in GitHub Desktop.
apache.py
# stdlib
import urllib2
import urlparse
import ssl
# project
from util import headers
from checks import AgentCheck
from checks.utils import add_basic_auth
class Apache(AgentCheck):
"""Tracks basic connection/requests/workers metrics
See http://httpd.apache.org/docs/2.2/mod/mod_status.html for more details
"""
GAUGES = {
'IdleWorkers': 'apache.performance.idle_workers',
'BusyWorkers': 'apache.performance.busy_workers',
'CPULoad': 'apache.performance.cpu_load',
'Uptime': 'apache.performance.uptime',
'Total kBytes': 'apache.net.bytes',
'Total Accesses': 'apache.net.hits',
}
RATES = {
'Total kBytes': 'apache.net.bytes_per_s',
'Total Accesses': 'apache.net.request_per_s'
}
def __init__(self, name, init_config, agentConfig, instances=None):
AgentCheck.__init__(self, name, init_config, agentConfig, instances)
self.assumed_url = {}
def check(self, instance):
if 'apache_status_url' not in instance:
raise Exception("Missing 'apache_status_url' in Apache config")
url = self.assumed_url.get(instance['apache_status_url'], instance['apache_status_url'])
tags = instance.get('tags', [])
req = urllib2.Request(url, None, headers(self.agentConfig))
if 'apache_user' in instance and 'apache_password' in instance:
add_basic_auth(req, instance['apache_user'], instance['apache_password'])
# Submit a service check for status page availability.
parsed_url = urlparse.urlparse(url)
apache_host = parsed_url.hostname
apache_port = parsed_url.port or 80
service_check_name = 'apache.can_connect'
service_check_tags = ['host:%s' % apache_host, 'port:%s' % apache_port]
try:
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
request = urllib2.urlopen(req, context = ctx)
except Exception:
self.service_check(service_check_name, AgentCheck.CRITICAL,
tags=service_check_tags)
raise
else:
self.service_check(service_check_name, AgentCheck.OK,
tags=service_check_tags)
response = request.read()
metric_count = 0
# Loop through and extract the numerical values
for line in response.split('\n'):
values = line.split(': ')
if len(values) == 2: # match
metric, value = values
try:
value = float(value)
except ValueError:
continue
# Special case: kBytes => bytes
if metric == 'Total kBytes':
value = value * 1024
# Send metric as a gauge, if applicable
if metric in self.GAUGES:
metric_count += 1
metric_name = self.GAUGES[metric]
self.gauge(metric_name, value, tags=tags)
# Send metric as a rate, if applicable
if metric in self.RATES:
metric_count += 1
metric_name = self.RATES[metric]
self.rate(metric_name, value, tags=tags)
if metric_count == 0:
if self.assumed_url.get(instance['apache_status_url'], None) is None and url[-5:] != '?auto':
self.assumed_url[instance['apache_status_url']]= '%s?auto' % url
self.warning("Assuming url was not correct. Trying to add ?auto suffix to the url")
self.check(instance)
else:
raise Exception("No metrics were fetched for this instance. Make sure that %s is the proper url." % instance['apache_status_url'])
@philliphaines
Copy link
Author

This field needs to replace /opt/datadog-agent/agent/checks.d/apache.py

@philliphaines
Copy link
Author

Run the following script to fix an apache instance

curl https://gist.github.com/philliphaines/06e7cef908f921de94b5/raw/9c0674f973ae6e49bdf0c5d32ef43fd5c0feb30b/gistfile1.py > /opt/datadog-agent/agent/checks.d/apache.py
sudo /etc/init.d/datadog-agent restart

@renowijoyo
Copy link

is this solution still current? im having problem with datadog-agent cant find the pem file although the file is there

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment