Skip to content

Commit

Permalink
Remove unused code, use a namedtuple
Browse files Browse the repository at this point in the history
  • Loading branch information
Remi Hakim committed Apr 11, 2013
1 parent f6fa16f commit b654a6b
Showing 1 changed file with 27 additions and 19 deletions.
46 changes: 27 additions & 19 deletions checks.d/cacti.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from fnmatch import fnmatch
import os
import time
from util import namedtuple

CFUNC_TO_AGGR = {
'AVERAGE': 'avg',
Expand All @@ -27,32 +28,34 @@
class Cacti(AgentCheck):
def __init__(self, name, init_config, agentConfig):
AgentCheck.__init__(self, name, init_config, agentConfig)
self.dbs = {}
self.last_ts = {}

def check(self, instance):

# Load the instance config
host, user, password, db, rrd_path, whitelist, device_names = self._get_config(instance)

# Generate an instance key to store state across checks
key = self._instance_key(instance)
config = self._get_config(instance)

# The rrdtool module is required for the check to work
import rrdtool
try:
import rrdtool
except ImportError, e:
raise Exception("Cannot import rrdtool module. Check the instructions to install this module at https://app.datadoghq.com/account/settings#integrations/mysql")

# Try importing MySQL and connecting to the database
import MySQLdb
self.dbs[key] = MySQLdb.connect(host, user, password, db)
# Try importing MySQL
try:
import MySQLdb
except ImportError, e:
raise Exception("Cannot import MySQLdb module. Check the instructions to install this module at https://app.datadoghq.com/account/settings#integrations/cacti")

connection = MySQLdb.connect(config.host, config.user, config.password, config.db)

self.log.debug("Connected to MySQL to fetch Cacti metadata")

# Get whitelist patterns, if available
patterns = self._get_whitelist_patterns(whitelist)
patterns = self._get_whitelist_patterns(config.whitelist)

# Fetch the RRD metadata from MySQL
db = self.dbs[key]
rrd_meta = self._fetch_rrd_meta(db, rrd_path, patterns, device_names)
rrd_meta = self._fetch_rrd_meta(connection, config.rrd_path, config.patterns, config.device_names)

# Load the metrics from each RRD, tracking the count as we go
metric_count = 0
Expand Down Expand Up @@ -92,8 +95,17 @@ def _get_config(self, instance):
whitelist = instance.get('rrd_whitelist')
field_names = instance.get('field_names', ['ifName', 'dskDevice'])

return host, user, password, db, rrd_path, whitelist, field_names
Config = namedtuple('Config', [
'host',
'user',
'password',
'db',
'rrd_path',
'whitelist',
'field_names']
)

return Config(host, user, password, db, rrd_path, whitelist, field_names)

def _read_rrd(self, rrd_path, hostname, device_name):
''' Main metric fetching method '''
Expand Down Expand Up @@ -149,11 +161,7 @@ def _read_rrd(self, rrd_path, hostname, device_name):
self.last_ts[last_ts_key] = last_ts
return metric_count

def _instance_key(*args):
''' return a key unique for this instance '''
return '|'.join([str(a) for a in args])

def _fetch_rrd_meta(self, db, rrd_path_root, whitelist, field_names):
def _fetch_rrd_meta(self, connection, rrd_path_root, whitelist, field_names):
''' Fetch metadata about each RRD in this Cacti DB, returning a list of
tuples of (hostname, device_name, rrd_path)
'''
Expand All @@ -164,7 +172,7 @@ def _in_whitelist(rrd):
return True
return False

c = db.cursor()
c = connection.cursor()

and_parameters = " OR ".join(["hsc.field_name = '%s'" % field_name for field_name in field_names])

Expand Down

0 comments on commit b654a6b

Please sign in to comment.