#!/usr/bin/env python3
########################################################################################
# server_identifier
# This script can be run on standard 45Drives storage servers to determine the system 
# model by gathering hardware infromation.
########################################################################################
import subprocess
import re
import json
import os.path
import os
from datetime import datetime
import sys
import shlex

g_product_lut_idx = {
	"MOBO_MODEL":	0,
	"24I_COUNT":	1,
	"16I_COUNT":	2,
	"CHASSIS_SIZE": 3,
	"ALIAS_STYLE":	4
}

g_chassis_sizes = ["?","AV15","Q30","S45","XL60","F8X1","F8X2","F8X3"]

g_mobo_to_version_lut = {
	"Base": ["X11SSH-CTF","X11SSM-F"],
	"Base-B": ["X11SPL-F"],
	"Enhanced": ["X11SPL-F","X10SRL-F"],
	"Enhanced-S":["X11SPL-F"],
	"Enhanced-AMD":["H11SSL-i"],
	"Turbo": ["X11DPL-i","X10DRL-i"],
	"Turbo-G":["X11SPL-F"],
}

g_product_lut = {
	"Storinator-AV15-Base":				[g_mobo_to_version_lut["Base"],0,0,"AV15","AV15-BASE"],
	"Storinator-AV15-Base-B":			[g_mobo_to_version_lut["Base-B"],0,1,"AV15","STORINATOR"],
	"Storinator-AV15-Enhanced":			[g_mobo_to_version_lut["Enhanced"],0,1,"AV15","STORINATOR"],
	"Storinator-AV15-Enhanced-S":		[g_mobo_to_version_lut["Enhanced-S"],0,1,"AV15","STORINATOR"],
	"Storinator-AV15-Enhanced-AMD":		[g_mobo_to_version_lut["Enhanced-AMD"],0,1,"AV15","STORINATOR"],
	"Storinator-AV15-Turbo":			[g_mobo_to_version_lut["Turbo"],0,1,"AV15","STORINATOR"],
	"Storinator-AV15-Turbo-G":		    [g_mobo_to_version_lut["Turbo-G"],0,1,"AV15","STORINATOR"],

	"Storinator-C8-Base":				[g_mobo_to_version_lut["Base"],0,1,"C8"  ,"STORINATOR"],
	"Storinator-C8-Base-B":				[g_mobo_to_version_lut["Base-B"],0,1,"C8"  ,"STORINATOR"],
	"Storinator-C8-Enhanced":			[g_mobo_to_version_lut["Enhanced"],0,1,"C8"  ,"STORINATOR"],
	"Storinator-C8-Enhanced-S":			[g_mobo_to_version_lut["Enhanced-S"]  ,0,1,"C8"  ,"STORINATOR"],
	"Storinator-C8-Enhanced-AMD":		[g_mobo_to_version_lut["Enhanced-AMD"]  ,0,1,"C8"  ,"STORINATOR"],
	"Storinator-C8-Turbo":				[g_mobo_to_version_lut["Turbo"]  ,0,1,"C8"  ,"STORINATOR"],
	"Storinator-C8-Turbo-G":			[g_mobo_to_version_lut["Turbo-G"]  ,0,1,"C8"  ,"STORINATOR"],

	"Storinator-F8X1-Base-B":			[g_mobo_to_version_lut["Base-B"]  ,1,0,"F8X1","F8"],
	"Storinator-F8X1-Base":				[g_mobo_to_version_lut["Base"]  ,1,0,"F8X1","F8"],
	"Storinator-F8X1-Enhanced":			[g_mobo_to_version_lut["Enhanced"]  ,1,0,"F8X1","F8"],
	"Storinator-F8X1-Enhanced-S":		[g_mobo_to_version_lut["Enhanced-S"]  ,1,0,"F8X1","F8"],
	"Storinator-F8X1-Enhanced-AMD":		[g_mobo_to_version_lut["Enhanced-AMD"]  ,1,0,"F8X1","F8"],
	"Storinator-F8X1-Turbo":			[g_mobo_to_version_lut["Turbo"]  ,1,0,"F8X1","F8"],
	"Storinator-F8X1-Turbo-G":			[g_mobo_to_version_lut["Turbo-G"]  ,1,0,"F8X1","F8"],

	"Storinator-F8X2-Base-B":			[g_mobo_to_version_lut["Base-B"]  ,2,0,"F8X1","F8"],
	"Storinator-F8X2-Base":				[g_mobo_to_version_lut["Base"]  ,2,0,"F8X1","F8"],
	"Storinator-F8X2-Enhanced":			[g_mobo_to_version_lut["Enhanced"]  ,2,0,"F8X1","F8"],
	"Storinator-F8X2-Enhanced-S":		[g_mobo_to_version_lut["Enhanced-S"]  ,2,0,"F8X1","F8"],
	"Storinator-F8X2-Enhanced-AMD":		[g_mobo_to_version_lut["Enhanced-AMD"]  ,2,0,"F8X1","F8"],
	"Storinator-F8X2-Turbo":			[g_mobo_to_version_lut["Turbo"]  ,2,0,"F8X1","F8"],
	"Storinator-F8X2-Turbo-G":			[g_mobo_to_version_lut["Turbo-G"]  ,2,0,"F8X1","F8"],

	"Storinator-F8X3-Base-B":			[g_mobo_to_version_lut["Base-B"]  ,3,0,"F8X1","F8"],
	"Storinator-F8X3-Base":				[g_mobo_to_version_lut["Base"]  ,3,0,"F8X1","F8"],
	"Storinator-F8X3-Enhanced":			[g_mobo_to_version_lut["Enhanced"]  ,3,0,"F8X1","F8"],
	"Storinator-F8X3-Enhanced-S":		[g_mobo_to_version_lut["Enhanced-S"]  ,3,0,"F8X1","F8"],
	"Storinator-F8X3-Enhanced-AMD":		[g_mobo_to_version_lut["Enhanced-AMD"]  ,3,0,"F8X1","F8"],
	"Storinator-F8X3-Turbo":			[g_mobo_to_version_lut["Turbo"]  ,3,0,"F8X1","F8"],
	"Storinator-F8X3-Turbo-G":			[g_mobo_to_version_lut["Turbo-G"]  ,3,0,"F8X1","F8"],
	
	"Storinator-H8-AV15-Base-B":		[g_mobo_to_version_lut["Base-B"]  ,0,1,"AV15","STORINATOR"],
	"Storinator-H8-AV15-Enhanced":		[g_mobo_to_version_lut["Enhanced"]  ,0,1,"AV15","STORINATOR"],
	"Storinator-H8-AV15-Enhanced-S":	[g_mobo_to_version_lut["Enhanced-S"]  ,0,1,"AV15","STORINATOR"],
	"Storinator-H8-AV15-Enhanced-AMD":	[g_mobo_to_version_lut["Enhanced-AMD"]  ,0,1,"AV15","STORINATOR"],
	"Storinator-H8-AV15-Turbo":			[g_mobo_to_version_lut["Turbo"]  ,0,1,"AV15","STORINATOR"],
	"Storinator-H8-AV15-Turbo-G":		[g_mobo_to_version_lut["Turbo-G"]  ,0,1,"AV15","STORINATOR"],

	"Storinator-H8-Q30-Base-B":			[g_mobo_to_version_lut["Base-B"]  ,0,2,"Q30","STORINATOR"],
	"Storinator-H8-Q30-Base":			[g_mobo_to_version_lut["Base"]  ,0,2,"Q30","STORINATOR"],
	"Storinator-H8-Q30-Enhanced":		[g_mobo_to_version_lut["Enhanced"]  ,0,2,"Q30","STORINATOR"],
	"Storinator-H8-Q30-Enhanced-S":		[g_mobo_to_version_lut["Enhanced-S"]  ,0,2,"Q30","STORINATOR"],
	"Storinator-H8-Q30-Enhanced-AMD":	[g_mobo_to_version_lut["Enhanced-AMD"]  ,0,2,"Q30","STORINATOR"],
	"Storinator-H8-Q30-Turbo":			[g_mobo_to_version_lut["Turbo"]  ,0,2,"Q30","STORINATOR"],
	"Storinator-H8-Q30-Turbo-G":		[g_mobo_to_version_lut["Turbo-G"]  ,0,2,"Q30","STORINATOR"],

	"Storinator-H8-S45-Base-B":			[g_mobo_to_version_lut["Base-B"]  ,0,3,"S45","STORINATOR"],
	"Storinator-H8-S45-Base":			[g_mobo_to_version_lut["Base"]  ,0,3,"S45","STORINATOR"],
	"Storinator-H8-S45-Enhanced":		[g_mobo_to_version_lut["Enhanced"]  ,0,3,"S45","STORINATOR"],
	"Storinator-H8-S45-Enhanced-S":		[g_mobo_to_version_lut["Enhanced-S"]  ,0,3,"S45","STORINATOR"],
	"Storinator-H8-S45-Enhanced-AMD":	[g_mobo_to_version_lut["Enhanced-AMD"]  ,0,3,"S45","STORINATOR"],
	"Storinator-H8-S45-Turbo":			[g_mobo_to_version_lut["Turbo"]  ,0,3,"S45","STORINATOR"],
	"Storinator-H8-S45-Turbo-G":		[g_mobo_to_version_lut["Turbo-G"]  ,0,3,"S45","STORINATOR"],

	"Storinator-H8-XL60-Enhanced":		[g_mobo_to_version_lut["Enhanced"]  ,0,4,"XL60","STORINATOR"],
	"Storinator-H8-XL60-Enhanced-S":	[g_mobo_to_version_lut["Enhanced-S"]  ,0,4,"XL60","STORINATOR"],
	"Storinator-H8-XL60-Enhanced-AMD":	[g_mobo_to_version_lut["Enhanced-AMD"]  ,0,4,"XL60","STORINATOR"],
	"Storinator-H8-XL60-Turbo":			[g_mobo_to_version_lut["Turbo"]  ,0,4,"XL60","STORINATOR"],
	"Storinator-H8-XL60-Turbo-G":		[g_mobo_to_version_lut["Turbo-G"]  ,0,4,"XL60","STORINATOR"],

	"Storinator-H16-AV15-Enhanced":		[g_mobo_to_version_lut["Enhanced"]  ,1,0,"AV15" ,"H16"],
	"Storinator-H16-AV15-Enhanced-S":	[g_mobo_to_version_lut["Enhanced-S"]  ,1,0,"AV15" ,"H16"],
	"Storinator-H16-AV15-Enhanced-AMD":	[g_mobo_to_version_lut["Enhanced-AMD"]  ,1,0,"AV15" ,"H16"],
	"Storinator-H16-AV15-Turbo":		[g_mobo_to_version_lut["Turbo"]  ,1,0,"AV15" ,"H16"],
	"Storinator-H16-AV15-Turbo-G":		[g_mobo_to_version_lut["Turbo-G"]  ,1,0,"AV15" ,"H16"],

	"Storinator-H16-AV15-Enhanced-AMD":	[g_mobo_to_version_lut["Enhanced-AMD"]  ,1,0,"AV15" ,"H16"],
	"Storinator-H16-Q30-Enhanced":		[g_mobo_to_version_lut["Enhanced"]  ,1,1,"Q30" ,"H16"],
	"Storinator-H16-Q30-Enhanced-S":	[g_mobo_to_version_lut["Enhanced-S"]  ,1,1,"Q30" ,"H16"],
	"Storinator-H16-Q30-Enhanced-AMD":	[g_mobo_to_version_lut["Enhanced-AMD"]  ,1,1,"Q30" ,"H16"],
	"Storinator-H16-Q30-Turbo":			[g_mobo_to_version_lut["Turbo"]  ,1,1,"Q30" ,"H16"],
	"Storinator-H16-Q30-Turbo-G":		[g_mobo_to_version_lut["Turbo-G"]  ,1,1,"Q30" ,"H16"],
	"Storinator-H16-S45-Enhanced":		[g_mobo_to_version_lut["Enhanced"]  ,1,2,"S45" ,"H16"],
	"Storinator-H16-S45-Enhanced-S":	[g_mobo_to_version_lut["Enhanced-S"]  ,1,2,"S45" ,"H16"],
	"Storinator-H16-S45-Enhanced-AMD":	[g_mobo_to_version_lut["Enhanced-AMD"]  ,1,2,"S45" ,"H16"],
	"Storinator-H16-S45-Turbo":			[g_mobo_to_version_lut["Turbo"]  ,1,2,"S45" ,"H16"],
	"Storinator-H16-S45-Turbo-G":		[g_mobo_to_version_lut["Turbo-G"]  ,1,2,"S45" ,"H16"],
	"Storinator-H16-XL60-Enhanced":		[g_mobo_to_version_lut["Enhanced"]  ,1,3,"XL60","H16"],
	"Storinator-H16-XL60-Enhanced-S":	[g_mobo_to_version_lut["Enhanced-S"]  ,1,3,"XL60","H16"],
	"Storinator-H16-XL60-Enhanced-AMD":	[g_mobo_to_version_lut["Enhanced-AMD"]  ,1,3,"XL60","H16"],
	"Storinator-H16-XL60-Turbo":		[g_mobo_to_version_lut["Turbo"]  ,1,3,"XL60","H16"],
	"Storinator-H16-XL60-Turbo-G":		[g_mobo_to_version_lut["Turbo-G"]  ,1,3,"XL60","H16"],

	"Storinator-H32-Q30-Enhanced":		[g_mobo_to_version_lut["Enhanced"]  ,2,0,"Q30" ,"H32"],
	"Storinator-H32-Q30-Enhanced-S":	[g_mobo_to_version_lut["Enhanced-S"]  ,2,0,"Q30" ,"H32"],
	"Storinator-H32-Q30-Enhanced-AMD":	[g_mobo_to_version_lut["Enhanced-AMD"]  ,2,0,"Q30" ,"H32"],
	"Storinator-H32-Q30-Turbo":			[g_mobo_to_version_lut["Turbo"]  ,2,0,"Q30" ,"H32"],
	"Storinator-H32-Q30-Turbo-G":		[g_mobo_to_version_lut["Turbo-G"]  ,2,0,"Q30" ,"H32"],
	"Storinator-H32-S45-Enhanced":		[g_mobo_to_version_lut["Enhanced"]  ,2,1,"S45" ,"H32"],
	"Storinator-H32-S45-Enhanced-S":	[g_mobo_to_version_lut["Enhanced-S"]  ,2,1,"S45" ,"H32"],
	"Storinator-H32-S45-Enhanced-AMD":	[g_mobo_to_version_lut["Enhanced-AMD"]  ,2,1,"S45" ,"H32"],
	"Storinator-H32-S45-Turbo":			[g_mobo_to_version_lut["Turbo"]  ,2,1,"S45" ,"H32"],
	"Storinator-H32-S45-Turbo-G":		[g_mobo_to_version_lut["Turbo-G"]  ,2,1,"S45" ,"H32"],
	"Storinator-H32-XL60-Enhanced":		[g_mobo_to_version_lut["Enhanced"]  ,2,2,"XL60","H32"],
	"Storinator-H32-XL60-Enhanced-S":	[g_mobo_to_version_lut["Enhanced-S"]  ,2,2,"XL60","H32"],
	"Storinator-H32-XL60-Enhanced-AMD":	[g_mobo_to_version_lut["Enhanced-AMD"]  ,2,2,"XL60","H32"],
	"Storinator-H32-XL60-Turbo":		[g_mobo_to_version_lut["Turbo"]  ,2,2,"XL60","H32"],
	"Storinator-H32-XL60-Turbo-G":		[g_mobo_to_version_lut["Turbo-G"]  ,2,2,"XL60","H32"],

	"Storinator-MI4-Base":				[g_mobo_to_version_lut["Base"]  ,0,0,"MI4"  ,"STORINATOR"],
	"Storinator-MI4-Base-B":			[g_mobo_to_version_lut["Base-B"]  ,0,0,"MI4"  ,"STORINATOR"],
	"Storinator-MI4-Enhanced":			[g_mobo_to_version_lut["Enhanced"]  ,0,0,"MI4"  ,"STORINATOR"],
	"Storinator-MI4-Enhanced-S":		[g_mobo_to_version_lut["Enhanced-S"]  ,0,0,"MI4"  ,"STORINATOR"],
	"Storinator-MI4-Enhanced-AMD":		[g_mobo_to_version_lut["Enhanced-AMD"]  ,0,0,"MI4"  ,"STORINATOR"],
	"Storinator-MI4-Turbo":				[g_mobo_to_version_lut["Turbo"]  ,0,0,"MI4"  ,"STORINATOR"],
	"Storinator-MI4-Turbo-G":			[g_mobo_to_version_lut["Turbo-G"]  ,0,0,"MI4"  ,"STORINATOR"],

	"Storinator-Q30-Base":				[g_mobo_to_version_lut["Base"]  ,0,2,"Q30" ,"STORINATOR"],
	"Storinator-Q30-Base-B":			[g_mobo_to_version_lut["Base-B"]  ,0,2,"Q30" ,"STORINATOR"],
	"Storinator-Q30-Enhanced":			[g_mobo_to_version_lut["Enhanced"]  ,0,2,"Q30" ,"STORINATOR"],
	"Storinator-Q30-Enhanced-S":		[g_mobo_to_version_lut["Enhanced-S"]  ,0,2,"Q30" ,"STORINATOR"],
	"Storinator-Q30-Enhanced-AMD":		[g_mobo_to_version_lut["Enhanced-AMD"]  ,0,2,"Q30" ,"STORINATOR"],
	"Storinator-Q30-Turbo":				[g_mobo_to_version_lut["Turbo"]  ,0,2,"Q30" ,"STORINATOR"],
	"Storinator-Q30-Turbo-G":			[g_mobo_to_version_lut["Turbo-G"]  ,0,2,"Q30" ,"STORINATOR"],

	"Storinator-S45-Base":				[g_mobo_to_version_lut["Base"]  ,0,3,"S45" ,"STORINATOR"],
	"Storinator-S45-Base-B":			[g_mobo_to_version_lut["Base-B"]  ,0,3,"S45" ,"STORINATOR"],
	"Storinator-S45-Enhanced":			[g_mobo_to_version_lut["Enhanced"]  ,0,3,"S45" ,"STORINATOR"],
	"Storinator-S45-Enhanced-S":		[g_mobo_to_version_lut["Enhanced-S"]  ,0,3,"S45" ,"STORINATOR"],
	"Storinator-S45-Enhanced-AMD":		[g_mobo_to_version_lut["Enhanced-AMD"]  ,0,3,"S45" ,"STORINATOR"],
	"Storinator-S45-Turbo":				[g_mobo_to_version_lut["Turbo"]  ,0,3,"S45" ,"STORINATOR"],
	"Storinator-S45-Turbo-G":			[g_mobo_to_version_lut["Turbo-G"]  ,0,3,"S45" ,"STORINATOR"],

	"Storinator-XL60-Enhanced":			[g_mobo_to_version_lut["Enhanced"]  ,0,4,"XL60","STORINATOR"],
	"Storinator-XL60-Enhanced-S":		[g_mobo_to_version_lut["Enhanced-S"]  ,0,4,"XL60","STORINATOR"],
	"Storinator-XL60-Enhanced-AMD":		[g_mobo_to_version_lut["Enhanced-AMD"]  ,0,4,"XL60","STORINATOR"],
	"Storinator-XL60-Turbo":			[g_mobo_to_version_lut["Turbo"]  ,0,4,"XL60","STORINATOR"],
	"Storinator-XL60-Turbo-G":			[g_mobo_to_version_lut["Turbo-G"]  ,0,4,"XL60","STORINATOR"],

	"Stornado-AV15-Enhanced":			[g_mobo_to_version_lut["Enhanced"]  ,0,2,"AV15","STORNADO"],
	"Stornado-AV15-Enhanced-S":			[g_mobo_to_version_lut["Enhanced-S"]  ,0,2,"AV15","STORNADO"],
	"Stornado-AV15-Enhanced-AMD":		[g_mobo_to_version_lut["Enhanced-AMD"]  ,0,2,"AV15","STORNADO"],
	"Stornado-AV15-Turbo":				[g_mobo_to_version_lut["Turbo"]  ,0,2,"AV15","STORNADO"],
	"Stornado-AV15-Turbo-G":			[g_mobo_to_version_lut["Turbo-G"]  ,0,2,"AV15","STORNADO"],

	"Stornado-2U-Base-B":				[g_mobo_to_version_lut["Base-B"]  ,0,2,"2U","2USTORNADO"],
	"Stornado-2U-Enhanced-AMD":			[g_mobo_to_version_lut["Enhanced-AMD"]  ,0,2,"2U","2USTORNADO"],
	"Stornado-2U-Enhanced-S":			[g_mobo_to_version_lut["Enhanced-S"]  ,0,2,"2U","2USTORNADO"],
	"Stornado-2U-Turbo-G":				[g_mobo_to_version_lut["Turbo-G"]  ,0,2,"2U","2USTORNADO"],
	"Stornado-2U-Turbo":				[g_mobo_to_version_lut["Turbo"]  ,0,2,"2U","2USTORNADO"],

	"Destroyinator-AV15-Enhanced":		[g_mobo_to_version_lut["Enhanced"],0,1,"AV15","DESTROYINATOR"],
	"Destroyinator-AV15-Enhanced-S":	[g_mobo_to_version_lut["Enhanced-S"],0,1,"AV15","DESTROYINATOR"],
	"Destroyinator-Q30-Enhanced":		[g_mobo_to_version_lut["Enhanced"]  ,0,2,"Q30","DESTROYINATOR"],
	"Destroyinator-Q30-Enhanced-S":		[g_mobo_to_version_lut["Enhanced-S"]  ,0,2,"Q30","DESTROYINATOR"],
	"Destroyinator-S45-Enhanced":		[g_mobo_to_version_lut["Enhanced"]  ,0,3,"S45","DESTROYINATOR"],
	"Destroyinator-S45-Enhanced-S":		[g_mobo_to_version_lut["Enhanced-S"]  ,0,3,"S45","DESTROYINATOR"],
	"Destroyinator-XL60-Enhanced":		[g_mobo_to_version_lut["Enhanced"]  ,0,4,"XL60","DESTROYINATOR"],
	"Destroyinator-XL60-Enhanced-S":	[g_mobo_to_version_lut["Enhanced-S"]  ,0,4,"XL60","DESTROYINATOR"],

	"Storinator-AV15-VM":				[["VIRTUAL_MACHINE"],0,1,"AV15","STORINATOR"],
	"Storinator-Q30-VM":				[["VIRTUAL_MACHINE"],0,2,"Q30","STORINATOR"],
	"Storinator-S45-VM":				[["VIRTUAL_MACHINE"],0,3,"S45","STORINATOR"],
	"Storinator-XL60-VM":				[["VIRTUAL_MACHINE"],0,4,"XL60","STORINATOR"],

	"Storinator":						[["?"],0,0,"?","STORINATOR"],
	"?":								[["?"],0,0,"?","?"]
}

def motherboard():
	# search through the output of the command "dmidecode -t 2" for motherboard information
	# example output:
		# # dmidecode 3.2
		# Getting SMBIOS data from sysfs.
		# SMBIOS 3.2.1 present.
		# # SMBIOS implementations newer than version 3.2.0 are not
		# # fully supported by this version of dmidecode.
		
		# Handle 0x0002, DMI type 2, 15 bytes
		# Base Board Information
		#         Manufacturer: Supermicro
		#         Product Name: X11SPL-F
		#         Version: 1.01
		#         Serial Number: ZM18AS011320
		#         Asset Tag: To be filled by O.E.M.
		#         Features:
		#                 Board is a hosting board
		#                 Board is replaceable
		#         Location In Chassis: To be filled by O.E.M.
		#         Chassis Handle: 0x0003
		#         Type: Motherboard
		#         Contained Object Handles: 0

	mobo_dict = {
	"Manufacturer":"?",
	"Product Name":"?",
	"Serial Number":"?"
	}

	mobo = []

	try:
		dmi_result = subprocess.Popen(["dmidecode","-t","2"],stdout=subprocess.PIPE,universal_newlines=True).stdout
	except:
		print("ERROR: dmidecode is not installed")
		exit(1)
	for line in dmi_result:
		for field in mobo_dict.keys():
			regex = re.search("^\s({fld}):\s+(.*)".format(fld=field),line)
			if regex != None:
					mobo_dict[regex.group(1)] = regex.group(2)

	try_fru = False
	for key in mobo_dict.keys():
		if mobo_dict[key] in ["?",""]:
			try_fru = True

	if try_fru:
		try:
			fru_result = subprocess.Popen(["ipmitool","fru"],stdout=subprocess.PIPE,universal_newlines=True).stdout
		except:
			print("ERROR: ipmitool fru failed to return result.")
		for line in fru_result:
			for field in ["Board Mfg", "Board Product", "Board Serial"]:
				regex = re.search("^\s({fld})\s+:\s+(.*)".format(fld=field),line)
				if regex != None:
					if regex.group(1) == "Board Mfg":
						mobo_dict["Manufacturer"] = regex.group(2)
					elif regex.group(1) == "Board Product":
						mobo_dict["Product Name"] = regex.group(2)
					elif regex.group(1) == "Board Serial":
						mobo_dict["Serial Number"] = regex.group(2)
	return mobo_dict

def getDmidecodePCI(server):
	try:
		dmidecode_result = subprocess.Popen(
			["dmidecode", "-t", "9"], stdout=subprocess.PIPE, universal_newlines=True).stdout.read()
	except:
		print("ERROR: dmidecode not installed")
		exit(1)
	pci_slots = []
	rx_pci=re.compile(r"^Handle.*\n(?:.|\n)+?^.*(?:Designation:\s+).*(?:PCIE|SLOT)(.).*(?:.|\n)+?^.*Bus Address:\s+(\S+).*",re.MULTILINE)
	for match in rx_pci.finditer(dmidecode_result):
		pci_slots.append({"ID":match.group(1),"Bus Address":match.group(2)})

	if server["Motherboard"]["Product Name"] in ["EPC621D8A","ROMED8-2T"]:
	# the bus addresses from dmidecode may be invalid. Use lookup table and use a translated address.
		BA_LUT = {
			"EPC621D8A":{
			"ff00:16:02.0":"0000:1c:00.0", #PCIE2
			"ff00:64:00.0":"0000:65:00.0", #PCIE4
			"ff00:64:02.0":"0000:66:00.0", #PCIE3
			"ff00:b2:00.0":"0000:b3:00.0", #PCIE6
			"ff00:b2:02.0":"0000:b4:00.0" #PCIE5
			},
			"ROMED8-2T":{
			"0000:80:00.0":"0000:81:00.0", #PCIE1
			"0000:00:00.0":"0000:01:00.0", #PCIE2
			}
		}

		for slot in pci_slots:
			if "Bus Address" in slot.keys() and slot["Bus Address"] in BA_LUT[server["Motherboard"]["Product Name"]].keys():
				slot["Bus Address"] = BA_LUT[server["Motherboard"]["Product Name"]][slot["Bus Address"]]
	return pci_slots

def getStorcliInfo(hba_card):
	storcli_path = {
		"SAS9305-16i":"/opt/45drives/tools/storcli64",
		"SAS9305-24i":"/opt/45drives/tools/storcli64",
		"HBA 9405W-16i":"/opt/45drives/tools/storcli64",
		"HBA 9400-16i":"/opt/45drives/tools/storcli64",
		"9600-24i":"/opt/45drives/tools/storcli2",
		"9600-16i":"/opt/45drives/tools/storcli2",
		"9361-16i":"/opt/45drives/tools/storcli64",
		"9361-24i":"/opt/45drives/tools/storcli64"
	}

	jq_filter = {
		"SAS9305-16i": "jq '.Controllers[0].\"Response Data\".\"Version\"'",
		"SAS9305-24i": "jq '.Controllers[0].\"Response Data\".\"Version\"'",
		"HBA 9405W-16i": "jq '.Controllers[0].\"Response Data\".\"Version\"'",
		"HBA 9400-16i": "jq '.Controllers[0].\"Response Data\".\"Version\"'",
		"9600-24i": "jq '.Controllers[0].\"Response Data\".\"Version\"'",
		"9600-16i": "jq '.Controllers[0].\"Response Data\".\"Version\"'",
		"9361-16i": "jq '.Controllers[0].\"Response Data\".\"Version\"'",
		"9361-24i": "jq '.Controllers[0].\"Response Data\".\"Version\"'"
	}
	
	storcli = subprocess.Popen(
		shlex.split("{pth} /c{ctl} show all J".format(pth=storcli_path[hba_card["Model"]],ctl=hba_card["Ctl"])), stdout=subprocess.PIPE, universal_newlines=True)
	storcli.wait()
	jq_command = jq_filter[hba_card["Model"]]
	jq = subprocess.Popen(
			shlex.split(jq_command), stdin=storcli.stdout, stdout=subprocess.PIPE, universal_newlines=True, stderr=subprocess.STDOUT)
	jq.wait()
	jqout,_ = jq.communicate()
	try:
		jq_json = json.loads(jqout)
	except ValueError:
		jq_json =  {}
		
	if jq_json != None:
		hba_card["Firmware Version"] = jq_json.get("Firmware Version","?")
		hba_card["Driver Version"] = jq_json.get("Driver Version","?")
		hba_card["Driver Name"] = jq_json.get("Driver Name","?")

# print("VERSION:  ",json.dumps(jq_json,indent=4))
# VERSION:   {
	# "Firmware Package Build": "24.22.0-0105",
	# "Firmware Version": "4.740.00-8474",
	# "CPLD Version": "04468-031",
	# "Bios Version": "6.36.00.3_4.19.08.00_0x06180206",
	# "HII Version": "03.25.05.15",
	# "Ctrl-R Version": "5.19-0609",
	# "NVDATA Version": "4.1705.00-00014",
	# "Boot Block Version": "3.10.01.00-0004",
	# "Driver Name": "megaraid_sas",
	# "Driver Version": "07.719.03.00-rh1"
# }
# VERSION:   {
	# "Firmware Package Build": "00.00.00.00",
	# "Firmware Version": "16.00.01.00",
	# "Bios Version": "08.27.00.00_13.00.00.00",
	# "NVDATA Version": "16.00.00.05",
	# "Driver Name": "mpt3sas",
	# "Driver Version": "42.100.00.00"
# }



def hba_lspci(server):
	# determine the model and count of hba cards present in the system
	# by parsing the output of "lspci -d 1000:* -vv -i /opt/45drives/tools/pci.ids"
	# example output:
	#17:00.0 RAID bus controller: Broadcom / LSI Fusion-MPT 24GSAS/PCIe SAS40xx (rev 01)
	#    Subsystem: Broadcom / LSI eHBA 9600-24i Tri-Mode Storage Adapter
	#    Kernel driver in use: mpi3mr
	#    Kernel modules: mpi3mr
	#65:00.0 RAID bus controller: Broadcom / LSI Fusion-MPT 24GSAS/PCIe SAS40xx (rev 01)
	#    Subsystem: Broadcom / LSI eHBA 9600-16i Tri-Mode Storage Adapter
	#    Kernel driver in use: mpi3mr
	#    Kernel modules: mpi3mr

	hba_count = 0
	hba = []
	hba_models = {
		"SAS9305-16i":16,
		"SAS9305-24i":24,
		"HBA 9405W-16i":16,
		"HBA 9400-16i":16,
		"9600-24i":24,
		"9600-16i":16,
		"9361-16i":16,
		"9361-24i":24
	}

	lspci_output = []

	try:
		lspci_result = subprocess.Popen(["lspci", "-d", "1000:*","-vv", "-i", "/opt/45drives/tools/pci.ids"],stdout=subprocess.PIPE,stderr=subprocess.STDOUT,universal_newlines=True).stdout.read()
	except:
		print("ERROR: error running lspci -d 1000:* -vv")
		exit(1)

	hba_drivers = {
		"SAS9305-16i":"mpt3sas",
		"SAS9305-24i":"mpt3sas",
		"HBA 9405W-16i":"mpt3sas",
		"HBA 9400-16i":"mpt3sas",
		"9600-24i":"mpi3mr",
		"9600-16i":"mpi3mr",
		"9361-16i":"megaraid_sas",
		"9361-24i":"megaraid_sas"
	}

	hba_adapters = {
		"SAS9305-16i":"SAS3224 PCI-Express Fusion-MPT SAS-3",
		"SAS9305-24i":"SAS3224 PCI-Express Fusion-MPT SAS-3",
		"HBA 9405W-16i":"SAS3616 Fusion-MPT Tri-Mode I/O Controller Chip (IOC)",
		"HBA 9400-16i":"SAS3416 Fusion-MPT Tri-Mode I/O Controller Chip (IOC)",
		"9600-24i":"Fusion-MPT 24GSAS/PCIe SAS40xx",
		"9600-16i":"Fusion-MPT 24GSAS/PCIe SAS40xx",
		"9361-16i":"MegaRAID SAS-3 3316",
		"9361-24i":"MegaRAID SAS-3 3316"
	}

	hybrid_flag = False

	hba_dict = {
		"Model":"?",
		"Adapter":"?",
		"Bus Address":"?",
		"Drive Connections":0,
		"Kernel Driver":"?",
		"Ctl":0,
		"Firmware Version": "?",
		"Driver Version": "?",
		"Driver Name": "?"
	}

	rx_pci=re.compile(r"^(\w\w:\w\w\.\w).*\n.*(?:(?:(?:^\t).*\n)+^.*)?(9600-16i|9600-24i|SAS9305-16i|SAS9305-24i|HBA 9405W-16i|9361-16i|HBA 9400-16i|9361-24i).*\n",re.MULTILINE)
	ctl = 0
	for match in rx_pci.finditer(lspci_result):
		hba_dict["Model"] = match.group(2)
		hba_dict["Adapter"] = hba_adapters[match.group(2)]
		hba_dict["Bus Address"] = match.group(1)
		hba_dict["Drive Connections"] = hba_models[match.group(2)]
		hba_dict["Kernel Driver"] = hba_drivers[match.group(2)]
		hba_dict["Ctl"] = ctl
		ctl = ctl + 1
		if hba_dict["Drive Connections"] == 24:
			hybrid_flag = True
		hba.append(hba_dict.copy())

	if len(hba) > 0:
		for card in hba:
			getStorcliInfo(card)

	if len(hba) != 0:
		# get list of pci devices including their bus address and slot id
		pci_slots = getDmidecodePCI(server)

		# get a list of pci devices used by system
		sys_bus_path = "/sys/bus/pci/devices"
		try:
			sys_bus_addrs = os.listdir(sys_bus_path)
		except:
			sys_bus_addrs = []

		for pci_slot in pci_slots:
			for card in hba:
				if card["Bus Address"] in pci_slot["Bus Address"]:
					card["PCI Slot"] = pci_slot["ID"]
					if len(sys_bus_addrs) > 0 and pci_slot["Bus Address"] not in sys_bus_addrs:
						# dmidecode gave a bus address that does not match the one used by the system
						for j in range(0,len(sys_bus_addrs)):
							if card["Bus Address"] in sys_bus_addrs[j]:
								# we have found the system bus address that matches the substring
								# address provided by lspci update the card's bus address field
								card["Bus Address"] = sys_bus_addrs[j]
								print("using /sys/bus/pci/devices",sys_bus_addrs[j])
								break
					else:
						# use the bus address provided by dmidecode
						# update the cards bus address to the full format (eg: 0000:01:00.0)
						card["Bus Address"] = pci_slot["Bus Address"]
					
		#sort the final list of hba cards by the Bus Address field. This ensures that
		#the order in which they are stored resembles the lspci output. which has
		#worked reliably in the past.
		hba = sorted(hba, key=lambda k: k['Bus Address']) 

	return hba, hybrid_flag

def hba():
	# determine the model and count of hba cards present in the system
	# by parsing the output of /opt/45drives/tools/storcli64 show.
	# example output:
		# CLI Version = 007.1017.0000.0000 May 10, 2019
		# Operating system = Linux 4.18.0-240.1.1.el8_3.x86_64
		# Status Code = 0
		# Status = Success
		# Description = None
		# 
		# Number of Controllers = 2
		# Host Name = hoop.45lab.com
		# Operating System  = Linux 4.18.0-240.1.1.el8_3.x86_64
		# StoreLib IT Version = 07.1100.0200.0100
		# StoreLib IR3 Version = 16.06-0
		# 
		# IT System Overview :
		# ==================
		# 
		# ----------------------------------------------------------------------------
		# Ctl Model         AdapterType   VendId DevId SubVendId SubDevId PCI Address 
		# ----------------------------------------------------------------------------
		#   0 HBA 9405W-16i   SAS3616(B0) 0x1000  0xD1    0x1000   0x3080 00:17:00:00 
		#   1 HBA 9405W-16i   SAS3616(B0) 0x1000  0xD1    0x1000   0x3080 00:65:00:00 
		# ----------------------------------------------------------------------------
	hba_count = 0
	hba = []
	try:
		storcli_result = subprocess.Popen(["/opt/45drives/tools/storcli64","show"],stdout=subprocess.PIPE,universal_newlines=True).stdout
	except:
		print("ERROR: storcli is not installed")
		exit(1)
	for line in storcli_result:
		regex = re.search("Number of Controllers = (\d)",line)
		if regex != None:
			hba_count = int(regex.group(1))
			break

	hba_models = {
		"SAS9305-16i":16,
		"SAS9305-24i":24,
		"HBA 9405W-16i":16,
		"HBA 9400-16i": 16
	}

	hba_drivers = {
		"SAS9305-16i":"mpt3sas",
		"SAS9305-24i":"mpt3sas",
		"HBA 9405W-16i":"mpt3sas",
		"HBA 9400-16i":"mpt3sas"
	}

	hba_dict = {
		"Model":"?",
		"Adapter":"?",
		"Bus Address":"?",
		"Drive Connections":0,
		"Kernel Driver":"?"
	}

	hybrid_flag = False

	if hba_count != 0:
		for line in storcli_result:
			for model in hba_models.keys():
				# Model AdapterType VendId DevId SubVendId SubDevId PCIAddress 	
				regex = re.search("(\d)\s+({fld})\s+(\w+).*\w\w:(\w\w:\w\w:\w\w)\s+$".format(fld=model),line)
				if regex != None:
					hba_dict["Model"] = regex.group(2)
					hba_dict["Adapter"] = regex.group(3)
					hba_dict["Bus Address"] = regex.group(4)
					hba_dict["Bus Address"] = hba_dict["Bus Address"][:-3] + ".0"
					hba_dict["Drive Connections"] = hba_models[model]
					hba_dict["Kernel Driver"] = hba_drivers[model]
					hba_dict["Ctl"] = int(regex.group(1))
					if hba_dict["Drive Connections"] == 24:
						hybrid_flag = True
					hba.append(hba_dict.copy())
		
		# get list of pci devices including their bus address and slot id
		pci_slots = getDmidecodePCI()

		# get a list of pci devices used by system
		sys_bus_path = "/sys/bus/pci/devices"
		try:
			sys_bus_addrs = os.listdir(sys_bus_path)
		except:
			sys_bus_addrs = []

		for pci_slot in pci_slots:
			for card in hba:
				if card["Bus Address"] in pci_slot["Bus Address"]:
					card["PCI Slot"] = pci_slot["ID"]
					if len(sys_bus_addrs) > 0 and pci_slot["Bus Address"] not in sys_bus_addrs:
						# dmidecode gave a bus address that does not match the one used by the system
						for j in range(0,len(sys_bus_addrs)):
							if card["Bus Address"] in sys_bus_addrs[j]:
								# we have found the system bus address that matches the substring
								# address provided by storcli64. update the card's bus address field
								card["Bus Address"] = sys_bus_addrs[j]
								print("using /sys/bus/pci/devices",sys_bus_addrs[j])
								break
					else:
						# use the bus address provided by dmidecode
						# update the cards bus address to the full format (eg: 0000:01:00.0)
						card["Bus Address"] = pci_slot["Bus Address"]
					
		#sort the final list of hba cards by the Bus Address field. This ensures that
		#the order in which they are stored resembles the lspci output. which has
		#worked reliably in the past.
		hba = sorted(hba, key=lambda k: k['Bus Address']) 

	return hba, hybrid_flag

def serial_check():
	#### OLD SERIAL ######################################
	# FRU Device Description : Builtin FRU Device (ID 0)
	# Chassis Type          : Unspecified
	# Chassis Part Number   : N/A
	# Chassis Serial        : N/A
	# Board Mfg Date        : Tue Dec  1 05:04:00 2020
	# Board Mfg             : Supermicro
	# Board Product         : X11SPL-F
	# Board Serial          : 1238383213
	# Board Part Number     : N/A
	# Product Manufacturer  : 45Drives
	# Product Name          : Storinator
	# Product Part Number   : S45
	# Product Version       : 5.0
	# Product Serial        : 1234-1
	# Product Asset Tag     : N/A

	#### NEW SERIAL #####################################
	# FRU Device Description : Builtin FRU Device (ID 0)
	# Chassis Type          : Rack Mount Chassis
	# Chassis Part Number   : AV15
	# Chassis Serial        : 13371337-1
	# Board Mfg Date        : Tue Dec  1 05:04:00 2020
	# Board Mfg             : Supermicro
	# Board Product         : X11SPL-F
	# Board Serial          : ZM18AS011320
	# Board Part Number     : X11SPL-F
	# Product Manufacturer  : 45Drives
	# Product Name          : Stornado-AV15-Enhanced
	# Product Part Number   : AV15
	# Product Version       : (Enhanced)
	# Product Serial        : 13371337-1
	# Product Asset Tag     : IPMIPASSWD
	serial_fields = [
		"Chassis Type",
		"Chassis Part Number",
		"Chassis Serial",
		"Board Mfg Date",
		"Board Mfg",
		"Board Product",
		"Board Serial",
		"Board Part Number",
		"Product Manufacturer",
		"Product Name",
		"Product Part Number",
		"Product Version",
		"Product Serial",
		"Product Asset Tag"
	]

	serial_result = {
		"Chassis Type":"?",
		"Chassis Part Number":"?",
		"Chassis Serial":"?",
		"Board Mfg Date":"?",
		"Board Mfg":"?",
		"Board Product":"?",
		"Board Serial":"?",
		"Board Part Number":"?",
		"Product Manufacturer":"?",
		"Product Name":"?",
		"Product Part Number":"?",
		"Product Version":"?",
		"Product Serial":"?",
		"Product Asset Tag":"?"
	}

	try:
		ipmi_test = subprocess.run(["ipmitool","fru"],stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL).returncode
	except:
		print("ERROR: ipmitool is not installed")
		exit(1)
	
	if ipmi_test:
		# ipmitool fru command has returned a standard error. This can happen on a virtual machine
		print("/opt/45drives/tools/server_identifier: ipmitool fru command failed.")
		print("/opt/45drives/tools/server_identifier: !! WARNING !! Cannot determine server type automatically using non-standard hardware.")
		return serial_result

	
	ipmi_result = subprocess.Popen(["ipmitool","fru"],stdout=subprocess.PIPE,universal_newlines=True).stdout
	for line in ipmi_result:
		for field in serial_fields:
			regex = re.search("({fld})\s+:\s+(\S+)".format(fld=field),line)
			if regex != None:
				serial_result[regex.group(1)] = regex.group(2)
	return serial_result

def determine_model(mobo_model_str,hba_dict_lst,chassis_size_str):
	# Use the detected hardware to identify whe model of storinator if it was serialised using the old 
	# serialization tool.
	hba_16i_count = 0
	hba_24i_count = 0

	model = "?"
	for card in hba_dict_lst:
		if card["Drive Connections"] == 16:
			hba_16i_count += 1
		elif card["Drive Connections"] == 24:
			hba_24i_count += 1

	for sys_type in g_product_lut.keys():
		if (mobo_model_str in g_product_lut[sys_type][g_product_lut_idx["MOBO_MODEL"]] and
			g_product_lut[sys_type][g_product_lut_idx["24I_COUNT"]] == hba_24i_count and
			g_product_lut[sys_type][g_product_lut_idx["16I_COUNT"]] == hba_16i_count and
			g_product_lut[sys_type][g_product_lut_idx["CHASSIS_SIZE"]] == chassis_size_str):
				model = sys_type

	if model == "?":
		if mobo_model_str != "?":
			print("/opt/45drives/tools/server_identifier: !! WARNING !! " + mobo_model_str + " Motherboard is not supported for Automatic Identification.")
		print("/opt/45drives/tools/server_identifier: !! WARNING !! Automatic Identification failed. ")
		print("/opt/45drives/tools/server_identifier: Setting Model to \"Storinator(Generic)\"")
		# TODO: ADD A MANUAL SELECTION TOOL HERE
		model = "Storinator"

	return model

def update_json_file(server,scan_time):
	old_file = None
	old_server = None
	json_dir = "/etc/45drives/server_info"

	#make a directory to store the server info files
	if not os.path.isdir(json_dir):
		print("/opt/45drives/tools/server_identifier: Server Info Directory Created:  ("+json_dir+")")
		os.makedirs(json_dir)

	# load the existing server_info.json file in as a json object.
	if os.path.exists(json_dir+"/server_info.json") and os.path.isfile(json_dir+"/server_info.json"):
		old_file = open(json_dir+"/server_info.json","r")
		try:
			old_server = json.load(old_file)
		except Exception as e:
			print("/opt/45drives/tools/server_identifier: Overwriting "+json_dir+"server_info.json with scan results.")
			old_server = None
		old_file.close()

	if old_server != None:
		# json object loaded in successfully
		if "Edit Mode" not in old_server.keys():
			old_server["Edit Mode"] = False

		if old_server != server and server["Model"] != "?" and not old_server["Edit Mode"]:
			# the hardware configuration has changed since last time
			# back up the existing server_info_file
			backup_file = open(json_dir+"/server_info_backup_"+scan_time+".json","w")
			backup_file.write(json.dumps(old_server,indent=4))
			backup_file.close()

			print("/opt/45drives/tools/server_identifier: Hardware configuration has changed since last scan.")
			print("                              A backup has been created in ("+json_dir+"/server_info_backup_"+scan_time+".json)")
			
			new_file = open(json_dir+"/server_info.json","w")
			new_file.write(json.dumps(server,indent=4))
			new_file.write("\n")
			new_file.close()
			print("--------------------------------------------------------------------------------")
			print(json_dir+"/server_info_backup_"+scan_time+".json)")
			print("--------------------------------------------------------------------------------")
			print(json.dumps(old_server,indent=4))
			print("--------------------------------------------------------------------------------")
			print(json_dir+"/server_info.json:")
			print("--------------------------------------------------------------------------------")
			print(json.dumps(server,indent=4))

		elif old_server != server and old_server["Edit Mode"]:
			print("/opt/45drives/tools/server_identifier: Hardware configuration has changed since last scan.")
			print("                              \"Edit Mode\" is enabled. ("+json_dir+"/server_info.json)")
			print("                              Results of this scan will not be saved.")

		if old_server["Edit Mode"]:
			print("--------------------------------------------------------------------------------")
			print(json_dir+"/server_info.json:")
			print("--------------------------------------------------------------------------------")
			print(json.dumps(old_server,indent=4))
			print("--------------------------------------------------------------------------------")
			print("Scan Results: (\"Edit Mode\":true i.e. server_info.json will not be modified)")
			print("--------------------------------------------------------------------------------")
			print(json.dumps(server,indent=4))
		else:
			print("--------------------------------------------------------------------------------")
			print("Scan Results: ")
			print("--------------------------------------------------------------------------------")
			print(json.dumps(server,indent=4))


	elif server["Model"] != "?":
		# this is the first time that this script was run on the system successfully
		new_file = open(json_dir+"/server_info.json","w")
		new_file.write(json.dumps(server,indent=4))
		new_file.write("\n")
		new_file.close()
		print("/opt/45drives/tools/server_identifier: Server Info File Created:  ("+json_dir+"/server_info.json)")
		print("--------------------------------------------------------------------------------")
		print(json_dir+"/server_info.json:")
		print("--------------------------------------------------------------------------------")
		print(json.dumps(server,indent=4))
	
	else:
		# this was unsuccessful. Write the info file for the first time to store the result.
		print("/opt/45drives/tools/server_identifier: Placeholder Server Info File Created:  ("+json_dir+"/server_info.json)")
		new_file = open(json_dir+"/server_info.json","w")
		new_file.write(json.dumps(server,indent=4))
		new_file.write("\n")
		new_file.close()
		print("--------------------------------------------------------------------------------")
		print(json_dir+"/server_info.json:")
		print("--------------------------------------------------------------------------------")
		print(json.dumps(server,indent=4))

	if server["Model"] == "?":
		print("/opt/45drives/tools/server_identifier: !! WARNING !!")
		print("                              Unable to determine server model automatically.")
		print("                              Server Info File Path:  ("+json_dir+"/server_info.json)")
		print("                              You can edit the server_info file manually and run dmap again if using non-standard hardware.")
		print("                              If using standard 45Drives server hardware. Serialization should be performed before running dmap.")

def vm_check(mobo_dict):
	return (mobo_dict["Manufacturer"] == "?" and mobo_dict["Product Name"] == "?" and mobo_dict["Serial Number"] == "?")

def old_serial(serial_result):
	for field in serial_result.keys():
		if serial_result[field] == "N/A":
			return True
	return False

def vm_passthrough(server):
	server["Model"] = "Storinator-{cs}-VM".format(cs=g_chassis_sizes[len(server["HBA"])])
	server["Chassis Size"] = g_chassis_sizes[len(server["HBA"])]
	server["Motherboard"]["Serial Number"] = "VIRTUAL_MACHINE"
	server["Motherboard"]["Manufacturer"] = "VIRTUAL_MACHINE"
	server["Motherboard"]["Product Name"] = "VM_MOTHERBOARD"
	server["Serial"] = "VIRTUAL_MACHINE"
	print("/opt/45drives/tools/server_identifier: Virtual Machine with HBA Pass Through Detected.")
	print("                              Setting Model to \"{m}\", and Chassis Size to \"{c}\"".format(m=server["Model"],c=server["Chassis Size"]))
	try:
		lspci_result = subprocess.Popen(["lspci"],stdout=subprocess.PIPE,universal_newlines=True, stderr=subprocess.PIPE).stdout
	except:
		print("ERROR: lspci command failed")
		exit(1)
	for hba_card in server["HBA"]:
		for line in lspci_result:
			regex = re.search("^({addr}).*{adap}".format(addr=hba_card["Bus Address"],adap=hba_card["Adapter"]),line)
			if regex != None:
				hba_card["Bus Address"] = "0000:" + regex.group(1)

def edit_mode_check():
	old_file = None
	old_server = None
	json_dir = "/etc/45drives/server_info"

	#make a directory to store the server info files
	if not os.path.isdir(json_dir):
		print("/opt/45drives/tools/server_identifier: Server Info Directory Created:  ("+json_dir+")")
		os.makedirs(json_dir)

	# load the existing server_info.json file in as a json object.
	if os.path.exists(json_dir+"/server_info.json") and os.path.isfile(json_dir+"/server_info.json"):
		old_file = open(json_dir+"/server_info.json","r")
		try:
			old_server = json.load(old_file)
		except Exception as e:
			print("/opt/45drives/tools/server_identifier: Error loading data from " + json_dir + "/server_info.json")
			print("Error Message: ",e)
			if input("/opt/45drives/tools/server_identifier: Would you like to overwrite existing file with new scan results? (y/n):") == "n":
				print("/opt/45drives/tools/server_identifier: Make the necessary adjustments to "+ json_dir + "/server_info.json and try again.")
				old_file.close()
				sys.exit(1)
			old_server = None
		old_file.close()

	if old_server != None:
		# json object loaded in successfully
		if "Edit Mode" not in old_server.keys():
			old_server["Edit Mode"] = False
		return old_server["Edit Mode"]
	return False

def get_os():
	os_release_path = "/etc/os-release"
	os_release_fields = {
		"NAME":"?",
		"VERSION_ID":"?"
		}
	if os.path.isfile(os_release_path):
		os_release_file = open(os_release_path,"r")
		os_release_lines = os_release_file.read().splitlines()
		os_release_file.close()
		for line in os_release_lines:
			for field in os_release_fields.keys():
				regex = re.search("^({fld})=".format(fld=field) + '\"(.+?)\"',line)
				if regex != None:
					os_release_fields[regex.group(1)] = regex.group(2)

	return os_release_fields["NAME"],os_release_fields["VERSION_ID"]


def main():
	server = {
		"Motherboard":"?",
		"HBA":[],
		"Hybrid":False,
		"Serial":"?",
		"Model":"?",
		"Alias Style":"?",
		"Chassis Size":"?",
		"VM":False,
		"Edit Mode":False,
		"OS NAME": "?",
		"OS VERSION_ID": "?"
	}
	# get current time
	current_day = datetime.today()
	current_time = datetime.now()
	scan_time = current_day.strftime("%Y_%m_%d_") + current_time.strftime("%H_%M")
	server["Edit Mode"] = edit_mode_check()
	serial_result = {}
	
	server["Motherboard"] = motherboard()
	#server["HBA"], server["Hybrid"] = hba() <- depreciated..
	server["HBA"], server["Hybrid"] = hba_lspci(server)
	server["VM"] = vm_check(server["Motherboard"])
	if not server["VM"]:
		serial_result = serial_check()
		server["Serial"] = serial_result["Product Serial"].upper()
		server["Chassis Size"] = serial_result["Product Part Number"].upper()
		server["Model"] = determine_model(server["Motherboard"]["Product Name"],server["HBA"],server["Chassis Size"]) if old_serial(serial_result) else serial_result["Product Name"]

	if server["Model"] == "?":
		if len(server["HBA"]) > 0 and server["VM"]:
			vm_passthrough(server) 			# VM with hba pass through detected, update server accordingly

	server["Alias Style"] = g_product_lut[server["Model"]][g_product_lut_idx["ALIAS_STYLE"]]
	
	# This is added to use only a single chassis size for the Stornado
	if "Product Part Number" in serial_result.keys():
		if serial_result["Product Part Number"].upper() == "F32":
			server["Alias Style"] = "STORNADO"
			server["Chassis Size"] = "AV15"

	# get OS NAME and OS VERSION_ID
	server["OS NAME"], server["OS VERSION_ID"] = get_os()
	update_json_file(server,scan_time)

	# warn user if improper number of HBA cards are detected.
	if len(server["HBA"]) != g_product_lut[server["Model"]][g_product_lut_idx["24I_COUNT"]] + g_product_lut[server["Model"]][g_product_lut_idx["16I_COUNT"]]:
		print("/opt/45drives/tools/server_identifier: !! WARNING !!")
		print("                              Quantity of HBA Cards detected does not match the quantity expected")
		print("                              for a \"{m}\". Drive aliasing may not work as desired. ".format(m=server["Model"]))
	exit(0)

if __name__ == "__main__":
	main()
