Skip to content

Commit

Permalink
Merge branch 'master' of github.com:BrightcoveOS/Diamond
Browse files Browse the repository at this point in the history
  • Loading branch information
kormoc committed Jul 24, 2014
2 parents dffa677 + 52bc5a4 commit cd483d1
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 10 deletions.
4 changes: 4 additions & 0 deletions bin/init.d/diamond
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ stop() {
echo -n $"Stopping $NAME: "
killproc -p $PIDFILE $NAME
retval=$?
if [ $retval -ne 0 ];
then
killall -q diamond
fi
if [ -e "${LOCKFILE}" ]
then
rm -f "${LOCKFILE}"
Expand Down
16 changes: 16 additions & 0 deletions conf/diamond.conf.example
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ batch = 100
# hostname = `hostname`
# hostname_rev = `hostname` in reverse (com.example.www)

# shell = Run the string set in hostname as a shell command and use its
# output(with spaces trimmed off from both ends) as the hostname.

# hostname_method = smart

# Path Prefix and Suffix
Expand Down Expand Up @@ -209,3 +212,16 @@ args = ('/var/log/diamond/diamond.log', 'midnight', 1, 7)
format = [%(asctime)s] [%(threadName)s] %(message)s
datefmt =

################################################################################
### Options for config merging
# [configs]
# path = "/etc/diamond/configs/"
# extension = ".conf"
#-------------------------------------------------------------------------------
# Example:
# /etc/diamond/configs/net.conf
# [collectors]
#
# [[NetworkCollector]]
# enabled = True

5 changes: 5 additions & 0 deletions src/collectors/userscripts/test/fixtures/example.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,8 @@
echo "example.1 42"
echo "example.2 24"
echo "example.3 12.1212"
# Testing invalid data is never a bad idea ;-)
echo "example.4 4 4"
echo "example.5 foo"
echo "example.6"
echo ""
8 changes: 8 additions & 0 deletions src/collectors/userscripts/userscripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ def collect(self):
(absolutescriptpath, err))
# Use filter to remove empty lines of output
for line in filter(None, out.split('\n')):
# Ignore invalid lines
try:
name, value = line.split()
float(value)
except ValueError:
self.log.error("%s returned error output: %s" %
(absolutescriptpath, line))
continue
name, value = line.split()
floatprecision = 0
if "." in value:
Expand Down
23 changes: 15 additions & 8 deletions src/diamond/collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import traceback
import time
import re
import subprocess

from diamond.metric import Metric
from error import DiamondException
Expand All @@ -30,21 +31,27 @@ def get_hostname(config, method=None):
"""
Returns a hostname as configured by the user
"""
if 'hostname' in config:
return config['hostname']

if method is None:
if 'hostname_method' in config:
method = config['hostname_method']
else:
method = 'smart'
method = method or config.get('hostname_method', 'smart')

# case insensitive method
method = method.lower()

if 'hostname' in config and method != 'shell':
return config['hostname']


if method in get_hostname.cached_results:
return get_hostname.cached_results[method]

if method == 'shell':
if 'hostname' not in config:
raise DiamondException(
"hostname must be set to a shell command for hostname_method=shell")
else:
hostname = subprocess.check_output(config['hostname'], shell=True).strip()
get_hostname.cached_results[method] = hostname
return hostname

if method == 'smart':
hostname = get_hostname(config, 'fqdn_short')
if hostname != 'localhost':
Expand Down
10 changes: 8 additions & 2 deletions src/diamond/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,19 @@ def __init__(self, config):

def load_config(self):
"""
Load the full config
Load the full config / merge splitted configs if configured
"""

configfile = os.path.abspath(self.config['configfile'])
config = configobj.ConfigObj(configfile)
config['configfile'] = self.config['configfile']

try:
for cfgfile in os.listdir(config['configs']['path']):
if cfgfile.endswith(config['configs']['extension']):
newconfig = configobj.ConfigObj(config['configs']['path'] + cfgfile)
config.merge(newconfig)
except KeyError:
pass
self.config = config

def load_handler(self, fqcn):
Expand Down
13 changes: 13 additions & 0 deletions src/diamond/test/testcollector.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,16 @@ def test_SetCustomHostname(self):
}
c = Collector(config, [])
self.assertEquals('custom.localhost', c.get_hostname())

def test_SetHostnameViaShellCmd(self):
config = configobj.ConfigObj()
config['server'] = {}
config['server']['collectors_config_path'] = ''
config['collectors'] = {}
config['collectors']['default'] = {
'hostname': 'echo custom.localhost',
'hostname_method': 'shell',
}
c = Collector(config, [])
self.assertEquals('custom.localhost', c.get_hostname())

0 comments on commit cd483d1

Please sign in to comment.