def _as_connect(self, cmd=None):
        """
        connect and return value
        """

        _user = None
        _password = None

        if self.options['aspass'] and self.options['asuser']:
            _user = self.options['asuser']
            _password = citrusleaf.hashpassword(self.options['aspass'])

        _conn = citrusleaf.citrusleaf_info(
            self.options['ashost'],
            self.options['asport'],
            cmd,
            _user,
            _password
        )

        if type(_conn) != types.StringType:
            raise base.BlackbirdPluginError(
                'request failed to {ashost}:{asport} ({cmd})'
                ''.format(
                    ashost=self.options['ashost'],
                    asport=self.options['asport'],
                    cmd=cmd
                )
            )

        return _conn
Example #2
0
 def _get_aerospike_conn(self, param):
     r = -1
     try:
         r = citrusleaf.citrusleaf_info(self.config["host"], self.config["port"], param)
     except Exception, e:
         self.log.error("Couldnt connect to aerospike for %s: %s", param, e)
         pass
	def run(self):
		s = self.connect()
		print "Aerospike-Graphite connector started: ", time.asctime(time.localtime())

		while True:
			msg = []
			now = int(time.time())
			r = citrusleaf.citrusleaf_info(CITRUSLEAF_SERVER, CITRUSLEAF_PORT, 'statistics', user, password)
			if (-1 != r):
				lines = []
				for string in r.split(';'):
					if string == "":
						continue

					if string.count('=') > 1:
						continue

					name, value = string.split('=')
					value = value.replace('false', "0")
					value = value.replace('true', "1")
					lines.append("%s.service.%s %s %s" % (GRAPHITE_PATH_PREFIX, name, value, now))
				msg.extend(lines)

			if args.sets:
				r = -1
				try:
					r = citrusleaf.citrusleaf_info(CITRUSLEAF_SERVER, CITRUSLEAF_PORT, 'sets', user, password)
				except:
					pass
				if (-1 != r):
					lines = []
					for string in r.split(';'):
						if len(string) == 0:
							continue
						setList = string.split(':')
						namespace = setList[0]
						sets = setList[1]
						for set_tuple in setList[2:]:
							key, value = set_tuple.split('=')
							lines.append("%s.sets.%s.%s.%s %s %s" % (GRAPHITE_PATH_PREFIX, namespace, sets, key, value, now))
					msg.extend(lines)

			if args.latency:
				r = -1
				if args.latency.startswith('latency:'):
					try:
						r = citrusleaf.citrusleaf_info(CITRUSLEAF_SERVER, CITRUSLEAF_PORT, args.latency, user, password)
					except:
						pass
				else:
					try:
						r = citrusleaf.citrusleaf_info(CITRUSLEAF_SERVER, CITRUSLEAF_PORT, 'latency:', user, password)
					except:
						pass

				if (-1 != r) and not (r.startswith('error')):
					lines = []
					latency_type = ""
					header = []
					for string in r.split(';'):
						if len(string) == 0:
							continue
						if len(latency_type) == 0:
							# Base case
							latency_type, rest = string.split(':', 1)
							header = rest.split(',')
						else:
							val = string.split(',')
							for i in range(1, len(header)):
								name = latency_type + "." + header[i]
								name = name.replace('>', 'over_')
								name = name.replace('ops/sec', 'ops_per_sec')
								value = val[i]
								lines.append("%s.latency.%s %s %s" % (GRAPHITE_PATH_PREFIX , name, value, now))
							# Reset base case
							latency_type = ""
							header = []
					msg.extend(lines)

			if args.namespace:
				r = -1
				try:
					r = citrusleaf.citrusleaf_info(CITRUSLEAF_SERVER, CITRUSLEAF_PORT, 'namespaces', user, password)
				except:
					pass

				if (-1 != r):
					namespaces = filter(None, r.split(';'))
					if len(namespaces) > 0:
						for namespace in namespaces:
							r = -1
							try:
								r = citrusleaf.citrusleaf_info(CITRUSLEAF_SERVER, CITRUSLEAF_PORT, 'namespace/' + namespace, user, password)
							except:
								pass
							if (-1 != r):
								lines = []
								for string in r.split(';'):
									name, value = string.split('=')
									value = value.replace('false', "0")
									value = value.replace('true', "1")
									lines.append(GRAPHITE_PATH_PREFIX + "." + namespace + ".%s %s %s" % (name, value, now))
							msg.extend(lines)

			if args.xdr:
				r = -1
				try:
					r = citrusleaf.citrusleaf_info(CITRUSLEAF_SERVER, CITRUSLEAF_XDR_PORT, 'statistics', user, password)
				except:
					pass
				if (-1 != r):
					lines = []
					for string in r.split(';'):
						if string == "":
							continue

						if string.count('=') > 1:
							continue

						name, value = string.split('=')
						value = value.replace('false', "0")
						value = value.replace('true', "1")
						lines.append("%s.xdr.%s %s %s" % (GRAPHITE_PATH_PREFIX, name, value, now))
					msg.extend(lines)

##	Logic to export SIndex Stats to Graphite
##	Since Graphite understands numbers we have used substitutes as below
##	sync_state --
##		synced = 1 & need_sync = 0
##	state --
##		RW = 1 & WO = 0

			if args.sindex:
				r = -1
				try:
					r = citrusleaf.citrusleaf_info(CITRUSLEAF_SERVER, CITRUSLEAF_PORT, 'sindex', user, password)
				except:
					pass
				if (-1 != r):
					indexes = filter(None, r)
					if len(indexes) > 0:
						lines = []
						for index_line in indexes.split(';'):
							if len(index_line) > 0:
								index = dict(item.split("=") for item in index_line.split(":"))

								if (index["sync_state"] == "synced"):
									index["sync_state"] = 1
								elif (index["sync_state"] == "need_sync"):
									index["sync_state"] = 0

								if (index["state"] == "RW"):
									index["state"] = 1
								elif (index["state"] == "WO"):
									index["state"] = 0

								lines.append("%s.sindexes.%s.%s.sync_state %s %s" % (GRAPHITE_PATH_PREFIX, index["ns"], index["indexname"], index["sync_state"], now))
								lines.append("%s.sindexes.%s.%s.state %s %s" % (GRAPHITE_PATH_PREFIX, index["ns"], index["indexname"], index["state"], now))

								r = -1
								try:
									r = citrusleaf.citrusleaf_info(CITRUSLEAF_SERVER, CITRUSLEAF_PORT, 'sindex/' + index["ns"] + '/' + index["indexname"], user, password)
								except:
									pass
								if (-1 != r):
									for string in r.split(';'):
										name, value = string.split('=')
										value = value.replace('false', "0")
										value = value.replace('true', "1")
										lines.append("%s.sindexes.%s.%s.%s %s %s" % (GRAPHITE_PATH_PREFIX, index["ns"], index["indexname"], name, value, now))
						msg.extend(lines)

			nmsg = ''
			#AER-2098 move all non numeric values to numbers
			#check if the val is a float (graphite uses float)
			#if not, break down the non-numeric part of value into numeric
			#this is kind of one way hash but easy to guess
			#leaving the earlier true/false/sync states the way they were done
			#as there is no major gain in moving them to the new format
			for line in msg:
				fields=line.split()
				try:
					float(fields[1])
				except ValueError:
					val = fields[1]
					valstr = ''
					for x in val:
						try:
							int(x)
							valstr += str(x)
						except ValueError:
							# convert [Aa-Zz] into numbers 1-26
							# doing abs() so that non alphanumerics are taken
							# care of example: /
							# ord ('a') + 1 = 96, replacing unnecessary fn call
							valstr += str(abs(ord(x.lower()) - 96))

					fields[1] = valstr
				line = ''
				for f in fields:
					line += f + ' '
				nmsg += line + '\n'
			try:
				s.sendall(nmsg)
			except:
				#Once the connection is broken, we need to reconnect
				print "ERROR: Unable to send to graphite server, retrying connection.."
				sys.stdout.flush()
				s.close()
				s = self.connect()

			time.sleep(INTERVAL)
#
# MAINLINE
#

from time import time
new_time=time()

# Nagios error codes:
# STATE_OK=0
# STATE_WARNING=1
# STATE_CRITICAL=2
# STATE_UNKNOWN=3
# STATE_DEPENDENT=4

r = citrusleaf.citrusleaf_info(arg_host, arg_port, arg_value)
if r == -1:
	print "request to ",arg_host,":",arg_port," returned error"
	# return STATE_CRITICAL
	sys.exit(2)
	
if r == None:
	print "request to ",arg_host,":",arg_port," returned no data"
	# return STATE_UNKNOWN
	sys.exit(3)

stat_line = ""

for arg in arg_stat.split():
	this_stat_line=""
	if arg == "write_stats":
	def run(self):
		s = socket.socket()
		try:
			s.connect((GRAPHITE_SERVER, GRAPHITE_PORT))
		except:
			print "unable to connect to Graphite server on %s:%d" % (GRAPHITE_SERVER, GRAPHITE_PORT)
			sys.exit(-1)
	
		print "Citrusleaf-Graphite connector started: ", time.asctime(time.localtime())
	
		
		while True:
			msg=''
			now = int(time.time())
			r = citrusleaf.citrusleaf_info(CITRUSLEAF_SERVER, CITRUSLEAF_PORT, 'statistics')
			if (-1 != r):
				lines = []
				for string in r.split(';'):
					name, value = string.split('=')
					value = value.replace('false', "0")
					value = value.replace('true', "1")
					lines.append("%s.service.%s %s %s" % (GRAPHITE_PATH_PREFIX, name, value, now))
				msg += '\n'.join(lines)+ '\n'
			if options.sets:
				r=-1
				try:
					r = citrusleaf.citrusleaf_info(CITRUSLEAF_SERVER, CITRUSLEAF_PORT, 'sets')
				except:
					pass
				if (-1 != r):
					lines = []
					for string in r.split(';'):
						if len(string) == 0:
							continue 
						setList = string.split(':')
						namespace = setList[0]
						sets = setList[1]
						for set_tuple in setList[2:]:
							key, value = set_tuple.split('=')
							lines.append("%s.sets.%s.%s.%s %s %s" % (GRAPHITE_PATH_PREFIX, namespace, sets, key, value, now))
					msg += '\n'.join(lines)+'\n'
			if options.latency is not None:
				r=-1
				if options.latency.startswith('latency:'):
					try:
						r = citrusleaf.citrusleaf_info(CITRUSLEAF_SERVER, CITRUSLEAF_PORT, options.latency )
					except:
						pass
				else:
					try:
						r = citrusleaf.citrusleaf_info(CITRUSLEAF_SERVER, CITRUSLEAF_PORT, 'latency:' )
					except:
						pass
						
				if (-1 != r) and not (r.startswith('error')):
					lines = []
					latency_type = ""
					header = []
					for string in r.split(';'):
						if len(string) == 0:
							continue 
						if len(latency_type) == 0:
							# Base case
							latency_type, rest = string.split(':',1)
							header = rest.split(',')
						else:
							val = string.split(',')
							for i in range(1, len(header)):
								name = latency_type + "." + header[i]
								name = name.replace('>', 'over_')
								name = name.replace('ops/sec', 'ops_per_sec')
								value = val[i]
								lines.append("%s.latency.%s %s %s" % (GRAPHITE_PATH_PREFIX ,name, value, now))
							# Reset base case
							latency_type = ""
							header = []
					msg += '\n'.join(lines)+'\n'
			if options.namespace:
				r=-1
				try:
					r = citrusleaf.citrusleaf_info(CITRUSLEAF_SERVER, CITRUSLEAF_PORT, 'namespaces')
				except:
					pass 
				
				if (-1 != r):				
					namespaces = filter(None, r.split(';'))
				
				if len(namespaces) > 0:
					for namespace in namespaces:
						r=-1
						try:
							r = citrusleaf.citrusleaf_info(CITRUSLEAF_SERVER, CITRUSLEAF_PORT, 'namespace/' + namespace)
						except:
							pass
						if (-1 != r):
							lines = []
							for string in r.split(';'):
								name, value = string.split('=')
								value = value.replace('false', "0")
								value = value.replace('true', "1")
								lines.append(GRAPHITE_PATH_PREFIX+"."+namespace + ".%s %s %s" % (name, value, now))
						msg += '\n'.join(lines)+'\n'
						
			if options.xdr:
				r=-1
				try:
					r = citrusleaf.citrusleaf_info(CITRUSLEAF_SERVER, CITRUSLEAF_XDR_PORT, 'statistics')
				except:
					pass
				if (-1 != r):
					lines = []
					for string in r.split(';'):
						name, value = string.split('=')
						value = value.replace('false', "0")
						value = value.replace('true', "1")
						lines.append("%s.xdr.%s %s %s" % (GRAPHITE_PATH_PREFIX, name, value, now))
					msg += '\n'.join(lines)+ '\n'
			s.sendall(msg)
			time.sleep(INTERVAL)