# Path: amonagent/utils.py
# def split_and_slugify(string, separator=":"):
# 	_string = string.strip().split(separator)
# 	
# 	if len(_string) == 2: # Only key, value
# 		data = {}
# 		key = slugify(unicode(_string[0]))
# 		
# 		try:
# 			if len(_string[1]) > 0:
# 				data[key] = str(_string[1].strip())
# 		except:
# 			pass
# 
# 		return data
# 	
# 	else:
# 		return None
# 
# def to_float(value):
# 	result = value
# 	if not isinstance(value, int):
# 		result = float(value.replace(",", "."))
# 	
# 	return result


import subprocess
import re
import logging
from amonagent.utils import split_and_slugify, to_float

	def check(self):
		data = {}


		try:
			df = subprocess.Popen(['df','-m'], stdout=subprocess.PIPE, close_fds=True).communicate()[0]	
			dfi = subprocess.Popen(['df','-i'], stdout=subprocess.PIPE, close_fds=True).communicate()[0]	
		except:
			log.exception('Unable to collect disk usage metrics.')
			return False


		inodes = self.parse_output(dfi)
		volumes = self.parse_output(df)

		_volume_columns = ('volume', 'total', 'used', 'free', 'percent', 'path')	
		_inode_columns = ('filesystem', 'inodes', 'iused', 'ifree', 'iuse%', 'mounted_on')	

		inode_dict =  map(lambda x: dict(zip(_inode_columns, x)), inodes)
		volumes_dict = map(lambda x: dict(zip(_volume_columns, x)), volumes)
		
		for v in volumes_dict:

			if v['volume'].startswith('/'):

				if any(i['filesystem'] == v['volume'] for i in inode_dict):

					# strip /dev/
					name = v['volume'].replace('/dev/', '')

# Path: amonagent/utils.py
# def split_and_slugify(string, separator=":"):
# 	_string = string.strip().split(separator)
# 	
# 	if len(_string) == 2: # Only key, value
# 		data = {}
# 		key = slugify(unicode(_string[0]))
# 		
# 		try:
# 			if len(_string[1]) > 0:
# 				data[key] = str(_string[1].strip())
# 		except:
# 			pass
# 
# 		return data
# 	
# 	else:
# 		return None
# 
# def to_float(value):
# 	result = value
# 	if not isinstance(value, int):
# 		result = float(value.replace(",", "."))
# 	
# 	return result


# Path: amonagent/modules/core.py
import subprocess
import re
import logging
from amonagent.utils import split_and_slugify, to_float


	def check(self):
		data = {}


		try:
			df = subprocess.Popen(['df','-m'], stdout=subprocess.PIPE, close_fds=True).communicate()[0]	
			dfi = subprocess.Popen(['df','-i'], stdout=subprocess.PIPE, close_fds=True).communicate()[0]	
		except:
			log.exception('Unable to collect disk usage metrics.')
			return False


		inodes = self.parse_output(dfi)
		volumes = self.parse_output(df)

		_volume_columns = ('volume', 'total', 'used', 'free', 'percent', 'path')	
		_inode_columns = ('filesystem', 'inodes', 'iused', 'ifree', 'iuse%', 'mounted_on')	

		inode_dict =  map(lambda x: dict(zip(_inode_columns, x)), inodes)
		volumes_dict = map(lambda x: dict(zip(_volume_columns, x)), volumes)
		
		for v in volumes_dict:

			if v['volume'].startswith('/'):

				if any(i['filesystem'] == v['volume'] for i in inode_dict):

					# strip /dev/
					name = v['volume'].replace('/dev/', '')

					v['percent'] = to_float(v['percent'].replace("%",'')) # Delete the % sign for easier calculation later