Debian Codegraph @

debian/patches/check_graphite/python2to3

raw
1 Index: pkg-nagios-plugins-contrib/check_graphite/check_graphite.py
2 ===================================================================
3 --- pkg-nagios-plugins-contrib.orig/check_graphite/check_graphite.py
4 +++ pkg-nagios-plugins-contrib/check_graphite/check_graphite.py
5 @@ -1,4 +1,4 @@
6 -#!/usr/bin/env python
7 +#!/usr/bin/env python3
8 """
9 check_graphite.py
10 ~~~~~~~
11 @@ -9,8 +9,8 @@ check_graphite.py
12
13 import json
14 import optparse
15 -import urllib
16 -import urllib2
17 +import urllib.request, urllib.parse, urllib.error
18 +import urllib.request, urllib.error, urllib.parse
19 import sys
20
21 from numbers import Real
22 @@ -34,7 +34,7 @@ class Graphite(object):
23 [('until', self._until)] +\
24 [('format', 'json')]
25 self.full_url = self.url + '/render?' +\
26 - urllib.urlencode(params)
27 + urllib.parse.urlencode(params)
28
29 def check_datapoints(self, datapoints, check_func, **kwargs):
30 """Find alerting datapoints
31 @@ -56,19 +56,19 @@ class Graphite(object):
32 return [x for x in datapoints if isinstance(x, Real) and check_func(x, kwargs['threshold'])]
33 elif 'bounds' in kwargs:
34 if 'compare' in kwargs:
35 - return [datapoints[x] for x in xrange(len(datapoints)) if all([datapoints[x], kwargs['bounds'][x], kwargs['compare'][x]]) and check_func(datapoints[x] / kwargs['bounds'][x], kwargs['beyond']) and check_func(datapoints[x], kwargs['compare'][x])]
36 + return [datapoints[x] for x in range(len(datapoints)) if all([datapoints[x], kwargs['bounds'][x], kwargs['compare'][x]]) and check_func(datapoints[x] / kwargs['bounds'][x], kwargs['beyond']) and check_func(datapoints[x], kwargs['compare'][x])]
37 else:
38 - return [datapoints[x] for x in xrange(len(datapoints)) if all([datapoints[x], kwargs['bounds'][x]]) and check_func(datapoints[x], kwargs['bounds'][x])]
39 + return [datapoints[x] for x in range(len(datapoints)) if all([datapoints[x], kwargs['bounds'][x]]) and check_func(datapoints[x], kwargs['bounds'][x])]
40
41 def fetch_metrics(self):
42 try:
43 - response = urllib2.urlopen(self.full_url)
44 + response = urllib.request.urlopen(self.full_url)
45
46 if response.code != 200:
47 return None
48 else:
49 return json.loads(response.read())
50 - except urllib2.URLError, TypeError:
51 + except urllib.error.URLError as TypeError:
52 return None
53
54 def generate_output(self, datapoints, *args, **kwargs):
55 @@ -210,7 +210,7 @@ if __name__ == '__main__':
56 warn = float(options.warning)
57 crit = float(options.critical)
58 except ValueError:
59 - print 'ERROR: WARNING or CRITICAL threshold is not a number\n'
60 + print('ERROR: WARNING or CRITICAL threshold is not a number\n')
61 parser.print_help()
62 sys.exit(NAGIOS_STATUSES['UNKNOWN'])
63
64 @@ -234,7 +234,7 @@ if __name__ == '__main__':
65 kwargs['compare'] = [x[0] for x in metric_data[3].get('datapoints', [])][from_slice:]
66
67 if not graphite.has_numbers(kwargs['compare']):
68 - print 'CRITICAL: No compare target output from Graphite!'
69 + print('CRITICAL: No compare target output from Graphite!')
70 sys.exit(NAGIOS_STATUSES['CRITICAL'])
71
72 if graphite.has_numbers(actual) and graphite.has_numbers(kwargs['bounds']):
73 @@ -245,13 +245,13 @@ if __name__ == '__main__':
74 target=target_name)
75
76 else:
77 - print 'CRITICAL: No output from Graphite for target(s): %s' % ', '.join(targets)
78 + print('CRITICAL: No output from Graphite for target(s): %s' % ', '.join(targets))
79 sys.exit(NAGIOS_STATUSES['CRITICAL'])
80 else:
81 for target in metric_data:
82 datapoints = [x[0] for x in target.get('datapoints', []) if isinstance(x[0], Real)]
83 if not graphite.has_numbers(datapoints) and not options.empty_ok:
84 - print 'CRITICAL: No output from Graphite for target(s): %s' % ', '.join(targets)
85 + print('CRITICAL: No output from Graphite for target(s): %s' % ', '.join(targets))
86 sys.exit(NAGIOS_STATUSES['CRITICAL'])
87
88 crit_oob = graphite.check_datapoints(datapoints, check_func, threshold=crit)
89 @@ -265,13 +265,13 @@ if __name__ == '__main__':
90 critical=crit)
91 else:
92 if options.empty_ok and isinstance(metric_data, list):
93 - print 'OK: No output from Graphite for target(s): %s' % ', '.join(targets)
94 + print('OK: No output from Graphite for target(s): %s' % ', '.join(targets))
95 sys.exit(NAGIOS_STATUSES['OK'])
96
97 - print 'CRITICAL: No output from Graphite for target(s): %s' % ', '.join(targets)
98 + print('CRITICAL: No output from Graphite for target(s): %s' % ', '.join(targets))
99 sys.exit(NAGIOS_STATUSES['CRITICAL'])
100
101 - for target, messages in check_output.iteritems():
102 + for target, messages in check_output.items():
103 if messages['CRITICAL']:
104 exit_code = NAGIOS_STATUSES['CRITICAL']
105 elif messages['WARNING']:
106 @@ -281,6 +281,6 @@ if __name__ == '__main__':
107
108 for status_code in ['CRITICAL', 'WARNING', 'OK']:
109 if messages[status_code]:
110 - print '\n'.join(['%s: %s' % (status_code, status) for status in messages[status_code]])
111 + print('\n'.join(['%s: %s' % (status_code, status) for status in messages[status_code]]))
112
113 sys.exit(exit_code)