Skip to content

[wip] Python 3 #762

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions bin/diamond
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def main():

# Read existing pid file
try:
pf = file(options.pidfile, 'r')
pf = open(options.pidfile, 'r')
pid = int(pf.read().strip())
pf.close()
except (IOError, ValueError):
Expand Down Expand Up @@ -187,7 +187,7 @@ def main():
# Write pid file
pid = str(os.getpid())
try:
pf = file(options.pidfile, 'w+')
pf = open(options.pidfile, 'w+')
except IOError as e:
print("Failed to write PID file: %s" % (e), file=sys.stderr)
sys.exit(1)
Expand Down Expand Up @@ -271,7 +271,7 @@ def main():
# Write pid file
pid = str(os.getpid())
try:
pf = file(options.pidfile, 'w+')
pf = open(options.pidfile, 'w+')
except IOError as e:
log.error("Failed to write child PID file: %s" % (e))
sys.exit(1)
Expand Down
16 changes: 8 additions & 8 deletions bin/diamond-setup
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def getCollectors(path):


def typeToString(key):
if isinstance(obj.config[key], basestring):
if isinstance(obj.config[key], str):
user_val = obj.config[key]
elif isinstance(obj.config[key], bool):
user_val = str(obj.config[key])
Expand All @@ -95,15 +95,15 @@ def typeToString(key):
def stringToType(key, val):
if type(obj.config[key]) is type(val):
config_file[key] = val
elif isinstance(obj.config[key], basestring):
elif isinstance(obj.config[key], str):
if val.lower() == 'false':
config_file[key] = False
elif val.lower() == 'true':
config_file[key] = True
else:
config_file[key] = val
elif isinstance(obj.config[key], bool):
if isinstance(val, basestring):
if isinstance(val, str):
config_file[key] = str_to_bool(val)
else:
config_file[key] = bool(val)
Expand All @@ -117,7 +117,7 @@ def stringToType(key, val):


def boolCheck(val):
if isinstance(val, basestring):
if isinstance(val, str):
return str_to_bool(val)
elif isinstance(val, bool):
return val
Expand All @@ -137,7 +137,7 @@ def configureKey(key):
print("\n")
if key in default_conf_help:
print(default_conf_help[key])
val = raw_input(key + ' [' + user_val + ']: ')
val = input(key + ' [' + user_val + ']: ')

# Empty user input? Default to current value
if len(val) == 0:
Expand Down Expand Up @@ -191,7 +191,7 @@ if __name__ == "__main__":
print(config['server']['collectors_config_path'])
print('Please type yes to continue')

val = raw_input('Are you sure? ')
val = input('Are you sure? ')
if val != 'yes':
sys.exit()

Expand Down Expand Up @@ -257,8 +257,8 @@ if __name__ == "__main__":

config_file.write()

except IOError as (errno, strerror):
print("I/O error({}): {}".format(errno, strerror))
except IOError as err:
print("I/O error({}): {}".format(err.errno, err.strerror))
except KeyboardInterrupt:
print()
sys.exit()
Expand Down
25 changes: 11 additions & 14 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import os
from glob import glob
import platform
import distro


def running_under_virtualenv():
Expand Down Expand Up @@ -42,11 +43,11 @@ def running_under_virtualenv():
('share/diamond/user_scripts', []),
]

distro = platform.dist()[0]
distro_major_version = platform.dist()[1].split('.')[0]
if not distro:
distro_name = distro.name()
distro_major_version = int(distro.version().split('.')[0])
if not distro_name:
if 'amzn' in platform.uname()[2]:
distro = 'centos'
distro_name = 'centos'

if running_under_virtualenv():
data_files.append(('etc/diamond',
Expand All @@ -65,20 +66,20 @@ def running_under_virtualenv():
data_files.append(('/var/log/diamond',
['.keep']))

if distro == 'Ubuntu':
if distro_name == 'Ubuntu':
if distro_major_version >= 16:
data_files.append(('/usr/lib/systemd/system',
['rpm/systemd/diamond.service']))
else:
data_files.append(('/etc/init',
['debian/diamond.upstart']))
if distro in ['centos', 'redhat', 'debian', 'fedora', 'oracle']:
if distro_name in ['centos', 'redhat', 'debian', 'fedora', 'oracle']:
data_files.append(('/etc/init.d',
['bin/init.d/diamond']))
if distro_major_version >= 7 and not distro == 'debian':
if distro_major_version >= 7 and not distro_name == 'debian':
data_files.append(('/usr/lib/systemd/system',
['rpm/systemd/diamond.service']))
elif distro_major_version >= 6 and not distro == 'debian':
elif distro_major_version >= 6 and not distro_name == 'debian':
data_files.append(('/etc/init',
['rpm/upstart/diamond.conf']))

Expand All @@ -88,11 +89,7 @@ def running_under_virtualenv():
if running_under_virtualenv():
install_requires = ['configobj', 'psutil', ]
else:
if distro in ['debian', 'Ubuntu']:
install_requires = ['python-configobj', 'python-psutil', ]
# Default back to pip style requires
else:
install_requires = ['configobj', 'psutil', ]
install_requires = ['configobj', 'psutil', ]


def get_version():
Expand Down Expand Up @@ -150,7 +147,7 @@ def pkgPath(root, path, rpath="/"):
packages=['diamond', 'diamond.handler', 'diamond.utils'],
scripts=['bin/diamond', 'bin/diamond-setup'],
data_files=data_files,
python_requires='~=2.7',
python_requires='~=3.0',
install_requires=install_requires,
classifiers=[
'Programming Language :: Python',
Expand Down
10 changes: 5 additions & 5 deletions src/collectors/aerospike/aerospike.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def collect_latency(self, data):

# Create metrics dict for the namespace/histogram pair
dataset = datasets[i].split(',')[1:]
metrics = dict(zip(fields, dataset))
metrics = dict(list(zip(fields, dataset)))

# Publish a metric for each field in the histogram
for field in fields:
Expand All @@ -156,11 +156,11 @@ def collect_latency(self, data):
) = data.split(';')[1::2]

# Collapse each type of data line into a dict of metrics
for op_type in raw_lines.keys():
metrics = dict(zip(fields, raw_lines[op_type].split(',')[1:]))
for op_type in list(raw_lines.keys()):
metrics = dict(list(zip(fields, raw_lines[op_type].split(',')[1:])))

# publish each metric
for metric in metrics.keys():
for metric in list(metrics.keys()):
self.publish_gauge('latency.%s.%s' %
(op_type, metric), metrics[metric])

Expand Down Expand Up @@ -206,7 +206,7 @@ def collect_throughput(self, data):
raw_lines['query'],
) = data.split(';')[1::2]

for op_type in raw_lines.keys():
for op_type in list(raw_lines.keys()):
metric = raw_lines[op_type].split(',')[1]
self.publish_gauge('throughput.%s' % op_type, metric)

Expand Down
2 changes: 1 addition & 1 deletion src/collectors/amavis/amavis.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def collect(self):
if res:
groups = res.groupdict()
name = groups['name']
for metric, value in groups.items():
for metric, value in list(groups.items()):
if metric == 'name':
continue
mtype = 'GAUGE'
Expand Down
4 changes: 2 additions & 2 deletions src/collectors/aurora/aurora.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import diamond.collector

import urllib2
from urllib.request import urlopen


class AuroraCollector(diamond.collector.Collector):
Expand Down Expand Up @@ -32,7 +32,7 @@ def collect(self):
self.config['host'],
self.config['port'])

response = urllib2.urlopen(url)
response = urlopen(url)

for line in response.readlines():
properties = line.split()
Expand Down
4 changes: 2 additions & 2 deletions src/collectors/beanstalkd/beanstalkd.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ def collect(self):

info = self._get_stats()

for stat, value in info['instance'].items():
for stat, value in list(info['instance'].items()):
if stat not in self.SKIP_LIST:
self.publish(stat, value,
metric_type=self.get_metric_type(stat))

for tube_stats in info['tubes']:
tube = tube_stats['name']
for stat, value in tube_stats.items():
for stat, value in list(tube_stats.items()):
if stat != 'name':
self.publish('tubes.%s.%s' % (tube, stat), value,
metric_type=self.get_metric_type(stat))
Expand Down
4 changes: 2 additions & 2 deletions src/collectors/bind/bind.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"""

import diamond.collector
import urllib2
from urllib.request import urlopen
import xml.etree.cElementTree as ElementTree


Expand Down Expand Up @@ -69,7 +69,7 @@ def clean_counter(self, name, value):

def collect(self):
try:
req = urllib2.urlopen('http://%s:%d/' % (
req = urlopen('http://%s:%d/' % (
self.config['host'], int(self.config['port'])))
except Exception as e:
self.log.error('Couldnt connect to bind: %s', e)
Expand Down
4 changes: 2 additions & 2 deletions src/collectors/celerymon/celerymon.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"""

import diamond.collector
import urllib2
from urllib.request import urlopen
import time

try:
Expand Down Expand Up @@ -68,7 +68,7 @@ def collect(self):

celerymon_url = "http://%s:%s/api/task/?since=%i" % (
host, port, self.LastCollectTime)
response = urllib2.urlopen(celerymon_url)
response = urlopen(celerymon_url)
body = response.read()
celery_data = json.loads(body)

Expand Down
2 changes: 1 addition & 1 deletion src/collectors/ceph/ceph.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def flatten_dictionary(input, sep='.', prefix=None):
[('a.b', 10), ('c', 20)]
"""
for name, value in sorted(input.items()):
fullname = sep.join(filter(None, [prefix, name]))
fullname = sep.join([_f for _f in [prefix, name] if _f])
if isinstance(value, dict):
for result in flatten_dictionary(value, sep, fullname):
yield result
Expand Down
4 changes: 2 additions & 2 deletions src/collectors/ceph/test/testceph.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,13 @@ def test_counter_default_prefix(self):
expected = 'ceph.osd.325'
sock = '/var/run/ceph/ceph-osd.325.asok'
actual = self.collector._get_counter_prefix_from_socket_name(sock)
self.assertEquals(actual, expected)
self.assertEqual(actual, expected)

def test_counter_alternate_prefix(self):
expected = 'ceph.keep-osd.325'
sock = '/var/run/ceph/keep-osd.325.asok'
actual = self.collector._get_counter_prefix_from_socket_name(sock)
self.assertEquals(actual, expected)
self.assertEqual(actual, expected)

@patch('glob.glob')
def test_get_socket_paths(self, glob_mock):
Expand Down
2 changes: 1 addition & 1 deletion src/collectors/chronyd/chronyd.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def get_output(self):
return ""

def collect(self):
output = self.get_output()
output = self.get_output().decode('utf-8')

for line in output.strip().split("\n"):
m = LINE_PATTERN.search(line)
Expand Down
6 changes: 3 additions & 3 deletions src/collectors/conntrack/conntrack.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ def collect(self):
collected = {}
files = []

if isinstance(self.config['dir'], basestring):
if isinstance(self.config['dir'], str):
dirs = [d.strip() for d in self.config['dir'].split(',')]
elif isinstance(self.config['dir'], list):
dirs = self.config['dir']

if isinstance(self.config['files'], basestring):
if isinstance(self.config['files'], str):
files = [f.strip() for f in self.config['files'].split(',')]
elif isinstance(self.config['files'], list):
files = self.config['files']
Expand Down Expand Up @@ -85,5 +85,5 @@ def collect(self):
'nf_conntrack/ip_conntrack kernel module was '
'not loaded')
else:
for key in collected.keys():
for key in list(collected.keys()):
self.publish(key, collected[key])
Loading