Skip to content

Commit

Permalink
python3 testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Bryan Irvine committed Jul 17, 2023
1 parent f83c7da commit 486dad2
Show file tree
Hide file tree
Showing 31 changed files with 226 additions and 226 deletions.
18 changes: 9 additions & 9 deletions check_graphite.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import sys
import json
from httplib import HTTPConnection
from http.client import HTTPConnection
from optparse import OptionParser


Expand All @@ -15,15 +15,15 @@ def fetchMetrics(host, port, url):
try:
data = response.read()
except Exception as uhoh:
print "unknown error, possible empty response?: %s" % uhoh
print("unknown error, possible empty response?: %s" % uhoh)
elif response.status == 401:
print "Invalid username or password."
print("Invalid username or password.")
sys.exit(1)
elif response.status == 404:
print "Web service not found."
print("Web service not found.")
sys.exit(1)
else:
print "Web service error (%d): %s" % (response.status, response.reason)
print("Web service error (%d): %s" % (response.status, response.reason))
sys.exit(1)
return data

Expand All @@ -35,7 +35,7 @@ def processResponse(body, nulls):
# for metric, timestamp in data[0]['datapoints']:
# return metric
if not data:
print "no data returned"
print("no data returned")
sys.exit(1)
for metric, timestamp in data[0]['datapoints']:
if nulls and not metric:
Expand Down Expand Up @@ -98,7 +98,7 @@ def makeNagios(metric, warning, critical):
max = cend
else: critical = ''

print "%s - %s|%s=%s;%s;%s;%s;%s; " % (severity, carbonCache, carbonCache, metric, warning, critical, min, max )
print("%s - %s|%s=%s;%s;%s;%s;%s; " % (severity, carbonCache, carbonCache, metric, warning, critical, min, max ))
sys.exit(code)


Expand Down Expand Up @@ -126,10 +126,10 @@ def main():
options, args = parser.parse_args()

if not options.host:
print >> sys.stderr, "You must specify the host."
print("You must specify the host.", file=sys.stderr)
sys.exit(1)
elif not options.url:
print >> sys.stderr, "You must specify the url."
print("You must specify the url.", file=sys.stderr)
sys.exit(1)

global carbonCache
Expand Down
14 changes: 7 additions & 7 deletions cisco-airespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
def fetchOID(host, community, graphiteroot, interface, verbose):

if verbose:
print >> sys.stderr, 'connecting to host: %s using community: %s' % ( host, community )
print('connecting to host: %s using community: %s' % ( host, community ), file=sys.stderr)

statsTable = {
'sysInterfaceStatPktsIn': '1.3.6.1.4.1.3375.2.1.2.4.4.3.1.2',
Expand Down Expand Up @@ -62,20 +62,20 @@ def fetchOID(host, community, graphiteroot, interface, verbose):
result = [x[0] for x in snmp.get(vars)]
result = float(x)
if verbose:
print >> sys.stderr, '%s %s = %s' % (vs.val, type, result)
print('%s %s = %s' % (vs.val, type, result), file=sys.stderr)
#currentTime = time.time()
datapoint = '%s.%s.%s' % (graphiteroot, vs.val.replace('.', '-'), type)
package.append((datapoint, (currentTime, result)))
except Exception as uhoh:
print >> sys.stderr, "could not get oid: %s" % uhoh
print("could not get oid: %s" % uhoh, file=sys.stderr)
#sys.exit(1)

return package, currentTime, result


def makePickle(datapoint, currentTime, data, verbose, debug):
if debug:
print >> sys.stderr, 'storing pickle in \'data.p\''
print('storing pickle in \'data.p\'', file=sys.stderr)
fh = open('data.p', 'wb')
pickle.dump(package, fh)
sys.exit()
Expand All @@ -85,16 +85,16 @@ def makePickle(datapoint, currentTime, data, verbose, debug):
def sendPickle(carbonServer, carbonPort, shippingPackage, verbose):
packageSize = struct.pack('!L', len(shippingPackage))
if verbose:
print >> sys.stderr, 'connecting to carbon server: %s on port: %s' % ( carbonServer, carbonPort )
print('connecting to carbon server: %s on port: %s' % ( carbonServer, carbonPort ), file=sys.stderr)
try:
s = socket.socket()
s.connect((carbonServer, carbonPort))
s.sendall(packageSize)
s.sendall(shippingPackage)
if verbose:
print >> sys.stderr, 'sending pickle...'
print('sending pickle...', file=sys.stderr)
except Exception as uhoh:
print "Could not connect to carbon server: %s" % uhoh
print("Could not connect to carbon server: %s" % uhoh)
sys.exit(1)

def main():
Expand Down
44 changes: 22 additions & 22 deletions codahale_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import sys
from base64 import b64encode
from optparse import OptionParser
import urllib2, httplib
import urllib.request, urllib.error, urllib.parse, http.client
import json

socket.setdefaulttimeout(30.0)
Expand All @@ -23,17 +23,17 @@ def processResponse(data,graphiteRoot,pickleport):
timestamp = time.time()
output = ([])

if options.verbose: print >> sys.stderr, data
if options.verbose: print(data, file=sys.stderr)
d = json.loads(data)

try:
# Step through JSON objects and sub objects and sub objects.
for everyone, two in d.iteritems():
for everyone, two in d.items():
if type(two).__name__=='dict':
for attr, value in two.items():
for attr, value in list(two.items()):
if type(value).__name__=='dict':
try:
for left, right in value.items():
for left, right in list(value.items()):
if not ((type(right).__name__ == "float") or (type(right).__name__ == "int")): continue
# strip unicode stuff
if '.' in everyone:
Expand All @@ -60,7 +60,7 @@ def processResponse(data,graphiteRoot,pickleport):
output.append((blah, (timestamp,two)))
# probably not needed any longer
except KeyError:
print >> sys.stderr, "Critical: Key not found: %s" % resource
print("Critical: Key not found: %s" % resource, file=sys.stderr)
sys.exit(1)

finally:
Expand All @@ -82,17 +82,17 @@ def processResponse(data,graphiteRoot,pickleport):
sys.exit(0)


class HTTPSClientAuthHandler(urllib2.HTTPSHandler):
class HTTPSClientAuthHandler(urllib.request.HTTPSHandler):
def __init__(self, key, cert):
urllib2.HTTPSHandler.__init__(self)
urllib.request.HTTPSHandler.__init__(self)
self.key = key
self.cert = cert

def https_open(self, req):
return self.do_open(self.getConnection, req)

def getConnection(self, host, timeout=300):
return httplib.HTTPSConnection(host, key_file=self.key, cert_file=self.cert)
return http.client.HTTPSConnection(host, key_file=self.key, cert_file=self.cert)

if __name__ == '__main__':

Expand Down Expand Up @@ -132,11 +132,11 @@ def getConnection(self, host, timeout=300):
options, args = parser.parse_args()

if not options.host:
print >> sys.stderr, "Critical: You must specify the host."
print("Critical: You must specify the host.", file=sys.stderr)
sys.exit(1)

if not options.url:
print >> sys.stderr, "You must specify a URL."
print("You must specify a URL.", file=sys.stderr)
sys.exit(1)
else:
url = options.url
Expand All @@ -153,30 +153,30 @@ def getConnection(self, host, timeout=300):
# default to use SSL if the port is 443
if options.usingssl or options.port == '443':
if not options.key:
from httplib import HTTPSConnection
from http.client import HTTPSConnection
try:
connection = HTTPSConnection(options.host, options.port)
connection.request("GET", url, None, headers)
except:
print >> sys.stderr, "Unable to make HTTPS connection to https://%s:%s%s" % ( options.host, options.port, url )
print("Unable to make HTTPS connection to https://%s:%s%s" % ( options.host, options.port, url ), file=sys.stderr)
sys.exit(1)
else:
import urllib2
from httplib import HTTPSConnection
opener = urllib2.build_opener(HTTPSClientAuthHandler(options.key, options.client))
import urllib.request, urllib.error, urllib.parse
from http.client import HTTPSConnection
opener = urllib.request.build_opener(HTTPSClientAuthHandler(options.key, options.client))
connectString = "https://%s:%s%s" % (options.host, options.port, options.url)
try:
response = opener.open(connectString)
except:
print >> sys.stderr, "Could not connect to %s" % connectString
print("Could not connect to %s" % connectString, file=sys.stderr)
sys.exit(2)
else:
from httplib import HTTPConnection
from http.client import HTTPConnection
try:
connection = HTTPConnection(options.host, options.port)
connection.request("GET", url, None, headers)
except Exception as e:
print >> sys.stderr, "Unable to make HTTP connection to http://%s:%s%s because: %s" % ( options.host, options.port, url, e )
print("Unable to make HTTP connection to http://%s:%s%s because: %s" % ( options.host, options.port, url, e ), file=sys.stderr)
sys.exit(1)

graphiteRoot = "%s.%s" % ( options.graphiteRoot, options.host )
Expand All @@ -189,11 +189,11 @@ def getConnection(self, host, timeout=300):
if returnCode == 200:
processResponse(response.read(),graphiteRoot,options.pickleport)
elif returnCode == 401:
print "Invalid username or password."
print("Invalid username or password.")
sys.exit(1)
elif returnCode == 404:
print "404 not found."
print("404 not found.")
sys.exit(1)
else:
print "Web service error %: " % returnCode #, (None if not response.reason else response.reason) )
print("Web service error %: " % returnCode) #, (None if not response.reason else response.reason) )
sys.exit(1)
14 changes: 7 additions & 7 deletions emcisilon_disk_perf_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
def fetchOID(host, community, graphiteroot, emcDisk, verbose):

if verbose:
print >> sys.stderr, 'connecting to host: %s using community: %s' % ( host, community )
print('connecting to host: %s using community: %s' % ( host, community ), file=sys.stderr)

statsTable = {
'diskperfOpsPerSecond': '1.3.6.1.4.1.12124.2.2.52.1.3',
Expand Down Expand Up @@ -55,20 +55,20 @@ def fetchOID(host, community, graphiteroot, emcDisk, verbose):
result = [x[0] for x in snmp.get(vars)]
result = float(x)
if verbose:
print >> sys.stderr, '%s %s = %s' % (vs.val, type, result)
print('%s %s = %s' % (vs.val, type, result), file=sys.stderr)
#currentTime = time.time()
datapoint = '%s.%s.%s' % (graphiteroot, vs.val.replace(' ',''), type)
package.append((datapoint, (currentTime, result)))
except Exception as uhoh:
print >> sys.stderr, "could not get oid: %s" % uhoh
print("could not get oid: %s" % uhoh, file=sys.stderr)
#sys.exit(1)

return package, currentTime, result


def makePickle(datapoint, currentTime, data, verbose, debug):
if debug:
print >> sys.stderr, 'storing pickle in \'data.p\''
print('storing pickle in \'data.p\'', file=sys.stderr)
fh = open('data.p', 'wb')
pickle.dump(package, fh)
sys.exit()
Expand All @@ -78,16 +78,16 @@ def makePickle(datapoint, currentTime, data, verbose, debug):
def sendPickle(carbonServer, carbonPort, shippingPackage, verbose):
packageSize = struct.pack('!L', len(shippingPackage))
if verbose:
print >> sys.stderr, 'connecting to carbon server: %s on port: %s' % ( carbonServer, carbonPort )
print('connecting to carbon server: %s on port: %s' % ( carbonServer, carbonPort ), file=sys.stderr)
try:
s = socket.socket()
s.connect((carbonServer, carbonPort))
s.sendall(packageSize)
s.sendall(shippingPackage)
if verbose:
print >> sys.stderr, 'sending pickle...'
print('sending pickle...', file=sys.stderr)
except Exception as uhoh:
print "Could not connect to carbon server: %s" % uhoh
print("Could not connect to carbon server: %s" % uhoh)
sys.exit(1)

def main():
Expand Down
14 changes: 7 additions & 7 deletions emcisilon_fan_speed.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
def fetchOID(host, community, graphiteroot, emcFan, verbose):

if verbose:
print >> sys.stderr, 'connecting to host: %s using community: %s' % ( host, community )
print('connecting to host: %s using community: %s' % ( host, community ), file=sys.stderr)

statsTable = {
'fanSpeed': '1.3.6.1.4.1.12124.2.53.1.4'
Expand Down Expand Up @@ -53,20 +53,20 @@ def fetchOID(host, community, graphiteroot, emcFan, verbose):
result = [x[0] for x in snmp.get(vars)]
result = float(x)
if verbose:
print >> sys.stderr, '%s %s = %s' % (vs.val, type, result)
print('%s %s = %s' % (vs.val, type, result), file=sys.stderr)
#currentTime = time.time()
datapoint = '%s.%s.%s' % (graphiteroot, vs.val.replace(' ',''), type)
package.append((datapoint, (currentTime, result)))
except Exception as uhoh:
print >> sys.stderr, "could not get oid: %s" % uhoh
print("could not get oid: %s" % uhoh, file=sys.stderr)
#sys.exit(1)

return package, currentTime, result


def makePickle(datapoint, currentTime, data, verbose, debug):
if debug:
print >> sys.stderr, 'storing pickle in \'data.p\''
print('storing pickle in \'data.p\'', file=sys.stderr)
fh = open('data.p', 'wb')
pickle.dump(package, fh)
sys.exit()
Expand All @@ -76,16 +76,16 @@ def makePickle(datapoint, currentTime, data, verbose, debug):
def sendPickle(carbonServer, carbonPort, shippingPackage, verbose):
packageSize = struct.pack('!L', len(shippingPackage))
if verbose:
print >> sys.stderr, 'connecting to carbon server: %s on port: %s' % ( carbonServer, carbonPort )
print('connecting to carbon server: %s on port: %s' % ( carbonServer, carbonPort ), file=sys.stderr)
try:
s = socket.socket()
s.connect((carbonServer, carbonPort))
s.sendall(packageSize)
s.sendall(shippingPackage)
if verbose:
print >> sys.stderr, 'sending pickle...'
print('sending pickle...', file=sys.stderr)
except Exception as uhoh:
print "Could not connect to carbon server: %s" % uhoh
print("Could not connect to carbon server: %s" % uhoh)
sys.exit(1)

def main():
Expand Down
Loading

0 comments on commit 486dad2

Please sign in to comment.