diff --git a/MANIFEST.in b/MANIFEST.in index f9bd145..a90fe37 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1 +1,2 @@ include requirements.txt +include main.conf.example diff --git a/conf/main.conf.example b/main.conf.example similarity index 100% rename from conf/main.conf.example rename to main.conf.example diff --git a/rt_server_client/ToolBox/base/__init__.py b/rt_server_client/ToolBox/base/__init__.py index 746f36f..29a31b9 100644 --- a/rt_server_client/ToolBox/base/__init__.py +++ b/rt_server_client/ToolBox/base/__init__.py @@ -17,11 +17,18 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +from __future__ import print_function +import sys +from .. import colors + class Debug: """Debug Class""" def __init__(self,args): - if args.debug_mode: - self.debug_enable = True + if args: + if args.debug_mode: + self.debug_enable = True + else: + self.debug_enable = False else: self.debug_enable = False @@ -30,3 +37,15 @@ def print_message(self,message): if self.debug_enable: print("[DEBUG] " + str(message)) + +def pwrn(*objs): + """Print warning messages""" + print(colors.term.YELLOW + 'WARNING: ', *objs, end=colors.term.NOC+"\n", file=sys.stderr) + +def perr(*objs): + """Print error messages""" + print(colors.term.RED + 'ERROR: ', *objs, end=colors.term.NOC+"\n", file=sys.stderr) + +def pok(*objs): + """Print error messages""" + print(colors.term.GREEN + 'SUCCESS: ', *objs, end=colors.term.NOC+"\n", file=sys.stderr) diff --git a/rt_server_client/ToolBox/colors/__init__.py b/rt_server_client/ToolBox/colors/__init__.py new file mode 100644 index 0000000..fc1facd --- /dev/null +++ b/rt_server_client/ToolBox/colors/__init__.py @@ -0,0 +1,20 @@ +class term: + '''Colors class for terminal + Just use term.NOC,WHITE,GREEN...''' + NOC = '\033[0m' + WHITE = '\033[1m' + GREEN = '\033[92m' + RED = '\033[91m' + YELLOW = '\033[93m' + BLUE = '\033[94m' + AZURE = '\033[96m' + BLACK = '\033[90m' + PINK = '\033[95m' + BGREEN = '\033[42m' + BRED = '\033[41m' + BYELLOW = '\033[43m' + BBLUE = '\033[44m' + BAZURE = '\033[46m' + BBLACK = '\033[40m' + BPINK = '\033[45m' + diff --git a/rt_server_client/ToolBox/init/__init__.py b/rt_server_client/ToolBox/init/__init__.py new file mode 100644 index 0000000..eda2dff --- /dev/null +++ b/rt_server_client/ToolBox/init/__init__.py @@ -0,0 +1,8 @@ +# Database inicialization +# +import rtapi + +def run(): + """Database inicialization. Create some necessary objects in database""" + pass + diff --git a/rt_server_client/sysinfo/__init__.py b/rt_server_client/sysinfo/__init__.py new file mode 100644 index 0000000..a0c7fb9 --- /dev/null +++ b/rt_server_client/sysinfo/__init__.py @@ -0,0 +1,264 @@ +# SysInfo part of the rt-server-client +# Here we gather all the information about the system +import fcntl +import socket +import struct +import sys +import os +import commands +import platform +import time +import datetime +import re +import random +import shutil +import argparse +import ipaddr +from ..ToolBox import base, net + +class SysInfo(): + """ + SysInfo Class will gather and contain all the system information + """ + + def __init__(self, args=False, init_run="no"): + self.information = { + 'network': { + 'lldp': {}, + }, + } + self.debug = base.Debug(args) + + def getHwAddr(self, ifname): + s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + info = fcntl.ioctl(s.fileno(), 0x8927, struct.pack('256s', ifname[:15])) + return ''.join(['%02x' % ord(char) for char in info[18:24]]) + + def GetVirtualServers(self, virt='xen'): + """Create list of virtual servers""" + + if virt == 'xen': + command = 'xm list' + index = 0 + elif virt == 'qemu': + command = 'virsh list --all' + index = 1 + else: + print('Unsupported virtualization') + sys.exit(1) + + output = commands.getoutput(command) + virtuals = [] + + for line in output.splitlines()[2:]: + virtual = line.split()[index] + virtuals.append(virtual) + + self.debug.print_message("Virtual servers: "+str(virtuals)) + + return virtuals + + def DiscoverAll(self): + self.DiscoverNetworking() + self.DiscoverSystem() + + def DiscoverNetworking(self): + # Get interface list into interfaces list and filter docker interfaces + device_list = net.get_interfaces() + self.information['network']['device_list'] = device_list + self.debug.print_message("Device list: "+str(device_list)) + + # Get ip address for each interface + # Get connections from lldp + # Empty list for connections + self.information['network']['interface_connections'] = [] + self.information['network']['interfaces_ips'] = [] + self.information['network']['interfaces_ips6'] = [] + + # Network Interfaces LLDP Connections + for interface in device_list: + self.debug.print_message("Processing interface " + interface) + # Default values + self.infromation['network']['lldp']['switch_name'] = '' + self.infromation['network']['lldp']['switch_port'] = '' + # Get ip addresses + self.infromation['network']['interfaces_ips'].append(net.get_ip4_addr(interface)) + self.debug.print_message("IPv4: "+str(self.information['network']['interfaces_ips'])) + + self.information['network']['interfaces_ips6'].append(net.get_ip6_addr(interface)) + self.debug.print_message("IPv6: "+str(self.information['network']['interfaces_ips6'])) + + # Get lldp + lldp_output = commands.getoutput('lldpctl -f keyvalue ' + interface) + + #Test if it's juniper or Force10, otherwise skip this discovery becouse unsupported device + # For JUniper + if lldp_output.find('Juniper') > -1: + for line in lldp_output.split('\n'): + if line.find('lldp.'+interface+'.chassis.name') > -1: + self.information['network']['lldp']['switch_name'] = line.split('=')[1] + elif line.find('lldp.'+interface+'.port.descr') > -1: + self.information['network']['lldp']['switch_port'] = line.split('=')[1] + # Others cisco like + else: + for line in lldp_output.split('\n'): + if line.find('lldp.'+interface+'.chassis.name') > -1: + self.information['network']['lldp']['switch_name'] = line.split('=')[1] + elif line.find('lldp.'+interface+'.port.ifname') > -1: + self.information['network']['lldp']['switch_port'] = line.split('=')[1] + + #add connection to list + connection = [switch_name, switch_port] + self.debug.print_message("Connection: "+str(connection)) + self.information['network']['interface_connections'].append(connection) + + # Get Drac IP + management_ip_commands = ['omreport chassis remoteaccess config=nic' , 'ipmitool lan print'] + self.information['network']['drac_ip'] = '' + for mgmcommand in management_ip_commands: + output = commands.getstatusoutput(mgmcommand) + try: + self.information['network']['drac_ip'] = re.findall('[0-9]{0,3}\.[0-9]{0,3}\.[0-9]{0,3}\.[0-9]{0,3}',output[1])[0] + break + except: + pass + self.debug.print_message("Drac IP: "+ self.information['network']['drac_ip']) + + def DiscoverSystem(self): + """ Get all system information """ + # Get service tag + # Server type, model + self.information['product_name'] = commands.getoutput('get-bios-ident -s -m') + self.debug.print_message("Product name: "+self.information['product_name']) + + self.information['service_tag'] = commands.getoutput('get-bios-ident -s -t') + self.debug.print_message("Service Tag: "+self.information['service_tag']) + + self.information['vendor'] = commands.getoutput('get-bios-ident -s -v') + self.debug.print_message("Vendor: "+ self.information['vendor']) + + # Check for virtualization + if not os.path.isdir('/proc/xen'): + # Not xen, check libvirtd + if os.path.isfile('/usr/sbin/libvirtd'): + # It's KVM/QEMU Hypervisor + self.information['server_type_id'] = 4 + self.information['hypervisor'] = "yes" + self.information['virtual_servers'] = self.GetVirtualServers(virt='qemu') + self.debug.print_message("Server is hypervisor") + self.debug.print_message("Virtuals: "+str(self.information['virtual_servers'])) + elif vendor == 'QEMU': + # Looks like server but, QEMU vendor + self.information['server_type_id'] = 1504 + self.information['hypervisor'] = "no" + self.debug.print_message("Server is virtual (QEMU)") + # Wait, exit normaly when supervisor on QEMU + if self.init_run == "yes": + self.debug.print_message("It's QEMU virtual on Supervisor, exiting.") + sys.exit(0) + else: + # It's server + self.information['server_type_id'] = 4 + self.information['hypervisor'] = "no" + self.debug.print_message("Server is physical normal server") + else: + if not os.path.isfile('/proc/xen/xenbus'): + self.information['server_type_id'] = 1504 + self.information['hypervisor'] = "no" + self.debug.print_message("Server is virtual (XEN)") + else: + self.debug.print_message("Server is hypervisor") + self.information['server_type_id'] = 4 + self.information['hypervisor'] = "yes" + self.information['virtual_servers'] = self.GetVirtualServers() + self.debug.print_message("Virtuals: "+str(self.information['virtual_servers'])) + + + self.information['hostname'] = platform.node() + + # Workaround for VPS and servicetag + if self.information['server_type_id'] == 1504: + self.information['service_tag'] = "VPS-"+ self.information['hostname'] + self.debug.print_message("VPS Service Tag override: "+ self.information['service_tag']) + else: + self.information['service_tag'] = commands.getoutput('get-bios-ident -s -t') + + self.debug.print_message("Hostname: "+ self.information['hostname']) + + # CPU information + # Read /proc/cpuinfo into variable + cpu_proc = open('/proc/cpuinfo') + file_c = cpu_proc.read() + cpu_proc.close() + + # Get number of logical CPUs + self.information['cpu_logical_num'] = file_c.count('processor') + self.debug.print_message("Logical CPU num: "+str(self.information['cpu_logical_num'])) + + # Get CPU Model Name + self.information['cpu_model_name'] = re.sub(' +',' ',re.findall('model name.*',file_c)[0].split(': ')[1]) + self.debug.print_message("CPU model name: "+ self.information['cpu_model_name']) + + # Physical CPU information + lscpu_output = commands.getstatusoutput('lscpu') + if lscpu_output[0] == 0: + lscpu = lscpu_output[1] + try: + self.information['cpu_num'] = int(re.findall('CPU socket.*',lscpu)[0].split(':')[1].strip()) + except: + self.information['cpu_num'] = int(re.findall('Socket\(s\).*',lscpu)[0].split(':')[1].strip()) + self.information['cpu_cores'] = int(re.findall('Core\(s\) per socket.*',lscpu)[0].split(':')[1].strip()) + self.information['cpu_mhz'] = int(re.findall('CPU MHz.*',lscpu)[0].split(':')[1].strip().split('.')[0]) + else: + self.information['cpu_num'] = "" + self.information['cpu_cores'] = "" + self.information['cpu_mhz'] = "" + + self.debug.print_message("CPU NUM, CPU Cores, CPU Mhz: %s, %s, %s" % (self.information['cpu_num'], self.information['cpu_cores'], self.information['cpu_mhz'])) + + # Get Memory info + meminfo = open('/proc/meminfo') + file_c = meminfo.read() + meminfo.close() + self.information['memory_mb'] = int(file_c.split()[1]) / 1024 + self.debug.print_message("Memory MB: %s" % (str(self.information['memory_mb']))) + + # OS, type, release + self.information['os_distribution'], self.information['os_version'], self.information['os_codename'] = platform.dist() + + # Stupid debian, empty os_codename + if self.information['os_codename'] == '': + for line in commands.getoutput('lsb_release -a').split('\n'): + if line.find('Codename:') > -1: + self.information['os_codename'] = line.split()[1] + self.debug.print_message("os_dist, os_version, os_codename: " + str([self.information['os_distribution'], self.information['os_version'], self.information['os_codename']])) + + # Get label from Drac (display) + output = commands.getstatusoutput('omreport chassis frontpanel') + + if output[0] == 0: + try: + line = re.findall('LCD Line 1.*',output[1])[0] + label = line.split(' ')[4] + update_label = "yes" + except: + update_label = "no" + label = "" + else: + update_label = "no" + label = "" + self.debug.print_message("label, update_label: %s %s" % (label, update_label)) + + # If hostname and label not match, try to configure LCD + if self.information['hostname'] != label: + if commands.getstatusoutput('omconfig chassis frontpanel config=custom lcdindex=1 text="' + self.information['hostname'] + '"')[0] == 0: + label = self.information['hostname'] + update_label = "yes" + self.debug.print_message("label, update_label: %s %s" % (label, update_label)) + self.information['label'] = label + self.information['update_label'] = update_label + + # Get Kernel version + self.information['kernel_version'] = platform.release() + self.debug.print_message("Kernel: "+ self.information['kernel_version']) + diff --git a/scripts/system-info b/scripts/system-info index 36b9e1c..68f84c6 100755 --- a/scripts/system-info +++ b/scripts/system-info @@ -49,15 +49,10 @@ import re import random import shutil import argparse -#FOR MAC -import fcntl -import socket -import struct -#END - import ipaddr import MySQLdb from rt_server_client.ToolBox import net, base +from rt_server_client.ToolBox import init as database_init import rtapi config_path = "/etc/rt-server-client/" @@ -76,6 +71,7 @@ parser = argparse.ArgumentParser( parser.add_argument("-f", dest="force_init", default=False, action="store_true", help="Force initialization, automaticly rewrite hostname") parser.add_argument("-d", dest="debug_mode", default=False, action="store_true", help="Debug mode") +parser.add_argument("--init", dest="init_mode", default=False, action="store_true", help="Inicialization of objects in DB") args = parser.parse_args() ## @@ -100,243 +96,15 @@ config.readfp(config_file) # Close config config_file.close() -#FOR MAC -def getHwAddr(ifname): - s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) - info = fcntl.ioctl(s.fileno(), 0x8927, struct.pack('256s', ifname[:15])) - return ''.join(['%02x' % ord(char) for char in info[18:24]]) -#END - - -def GetVirtualServers(virt='xen'): - """Create list of virtual servers""" - - if virt == 'xen': - command = 'xm list' - index = 0 - elif virt == 'qemu': - command = 'virsh list --all' - index = 1 - else: - print('Unsupported virtualization') - sys.exit(1) - - output = commands.getoutput(command) - virtuals = [] - - for line in output.splitlines()[2:]: - virtual = line.split()[index] - virtuals.append(virtual) - - debug.print_message("Virtual servers: "+str(virtuals)) - - return virtuals - - # Sleep for some seconds. When running on 200 hosts in same second, local dns should have a problem with it if not args.debug_mode: time.sleep(random.randint(1,12)) -# Get interface list into interfaces list and filter docker interfaces -device_list = net.get_interfaces() -debug.print_message("Device list: "+str(device_list)) - -# Get ip address for each interface -# Get connections from lldp -# Empty list for connections -interface_connections = [] -interfaces_ips = [] -interfaces_ips6 = [] - -# Get service tag -# Server type, model -product_name = commands.getoutput('get-bios-ident -s -m') -debug.print_message("Product name: "+product_name) - -service_tag = commands.getoutput('get-bios-ident -s -t') -debug.print_message("Service Tag: "+service_tag) - -vendor = commands.getoutput('get-bios-ident -s -v') -debug.print_message("Vendor: "+vendor) - -# Check for virtualization -if not os.path.isdir('/proc/xen'): - # Not xen, check libvirtd - if os.path.isfile('/usr/sbin/libvirtd'): - # It's KVM/QEMU Hypervisor - server_type_id = 4 - hypervisor = "yes" - virtual_servers = GetVirtualServers(virt='qemu') - debug.print_message("Server is hypervisor") - debug.print_message("Virtuals: "+str(virtual_servers)) - elif vendor == 'QEMU': - # Looks like server but, QEMU vendor - server_type_id = 1504 - hypervisor = "no" - debug.print_message("Server is virtual (QEMU)") - # Wait, exit normaly when supervisor on QEMU - if init_run == "yes": - debug.print_message("It's QEMU virtual on Supervisor, exiting.") - sys.exit(0) - else: - #Je to server - server_type_id = 4 - hypervisor = "no" - debug.print_message("Server is physical normal server") -else: - if not os.path.isfile('/proc/xen/xenbus'): - #Je to virtual - server_type_id = 1504 - hypervisor = "no" - debug.print_message("Server is virtual (XEN)") - else: - #Je to hypervisor - server_type_id = 4 - hypervisor = "yes" - virtual_servers = GetVirtualServers() - debug.print_message("Server is hypervisor") - debug.print_message("Virtuals: "+str(virtual_servers)) - - -hostname = platform.node() - -# Workaround for VPS and servicetag -if server_type_id == 1504: - service_tag = "VPS-"+hostname - debug.print_message("VPS Service Tag override: "+service_tag) -else: - service_tag = commands.getoutput('get-bios-ident -s -t') - -debug.print_message("Hostname: "+hostname) - -# CPU information -# Read /proc/cpuinfo into variable -cpu_proc = open('/proc/cpuinfo') -file_c = cpu_proc.read() -cpu_proc.close() - -# Get number of logical CPUs -cpu_logical_num = file_c.count('processor') -debug.print_message("Logical CPU num: "+str(cpu_logical_num)) - -# Get CPU Model Name -cpu_model_name = re.sub(' +',' ',re.findall('model name.*',file_c)[0].split(': ')[1]) -debug.print_message("CPU model name: "+cpu_model_name) - -# Physical CPU information -lscpu_output = commands.getstatusoutput('lscpu') -if lscpu_output[0] == 0: - lscpu = lscpu_output[1] - try: - cpu_num = int(re.findall('CPU socket.*',lscpu)[0].split(':')[1].strip()) - except: - cpu_num = int(re.findall('Socket\(s\).*',lscpu)[0].split(':')[1].strip()) - cpu_cores = int(re.findall('Core\(s\) per socket.*',lscpu)[0].split(':')[1].strip()) - cpu_mhz = int(re.findall('CPU MHz.*',lscpu)[0].split(':')[1].strip().split('.')[0]) -else: - cpu_num = "" - cpu_cores = "" - cpu_mhz = "" - -debug.print_message("CPU NUM, CPU Cores, CPU Mhz: %s, %s, %s" % (cpu_num, cpu_cores, cpu_mhz)) - -# Get Memory info -meminfo = open('/proc/meminfo') -file_c = meminfo.read() -meminfo.close() -memory_mb = int(file_c.split()[1]) / 1024 -debug.print_message("Memory MB: %s" % (str(memory_mb))) - -# Network Interfaces LLDP Connections -for interface in device_list: - debug.print_message("Processing interface " + interface) - # Default values - switch_name = '' - switch_port = '' - # Get ip addresses - interfaces_ips.append(net.get_ip4_addr(interface)) - debug.print_message("IPv4: "+str(interfaces_ips)) - - interfaces_ips6.append(net.get_ip6_addr(interface)) - debug.print_message("IPv6: "+str(interfaces_ips6)) - - # Get lldp - lldp_output = commands.getoutput('lldpctl -f keyvalue ' + interface) - - #Test if it's juniper or Force10, otherwise skip this discovery becouse unsupported device - # For JUniper - if lldp_output.find('Juniper') > -1: - for line in lldp_output.split('\n'): - if line.find('lldp.'+interface+'.chassis.name') > -1: - switch_name = line.split('=')[1] - elif line.find('lldp.'+interface+'.port.descr') > -1: - switch_port = line.split('=')[1] - # Others cisco like - else: - for line in lldp_output.split('\n'): - if line.find('lldp.'+interface+'.chassis.name') > -1: - switch_name = line.split('=')[1] - elif line.find('lldp.'+interface+'.port.ifname') > -1: - switch_port = line.split('=')[1] - - #add connection to list - connection = [switch_name, switch_port] - debug.print_message("Connection: "+str(connection)) - interface_connections.append(connection) - - -# OS, type, release -os_distribution, os_version, os_codename = platform.dist() - -# Stupid debian, empty os_codename -if os_codename == '': - for line in commands.getoutput('lsb_release -a').split('\n'): - if line.find('Codename:') > -1: - os_codename = line.split()[1] -debug.print_message("os_dist, os_version, os_codename: " + str([os_distribution, os_version, os_codename])) - -# Get Drac IP -management_ip_commands = ['omreport chassis remoteaccess config=nic' , 'ipmitool lan print'] -drac_ip = '' -for mgmcommand in management_ip_commands: - output = commands.getstatusoutput(mgmcommand) - try: - drac_ip = re.findall('[0-9]{0,3}\.[0-9]{0,3}\.[0-9]{0,3}\.[0-9]{0,3}',output[1])[0] - break - except: - pass -debug.print_message("Drac IP: "+drac_ip) - - -# Get label from Drac (display) -output = commands.getstatusoutput('omreport chassis frontpanel') - -if output[0] == 0: - try: - line = re.findall('LCD Line 1.*',output[1])[0] - label = line.split(' ')[4] - update_label = "yes" - except: - update_label = "no" - label = "" -else: - update_label = "no" - label = "" -debug.print_message("label, update_label: %s %s" % (label, update_label)) - - -# If hostname and label not match, try to configure LCD -if hostname != label: - if commands.getstatusoutput('omconfig chassis frontpanel config=custom lcdindex=1 text="' + hostname + '"')[0] == 0: - label = hostname - update_label = "yes" -debug.print_message("label, update_label: %s %s" % (label, update_label)) - -# Get Kernel version -kernel_version = platform.release() -debug.print_message("Kernel: "+kernel_version) - -## Main Database part +############################################################ +## ## +## Main Database part ## +## ## +############################################################ # Create connection to database try: @@ -352,304 +120,270 @@ except MySQLdb.Error as e: debug.print_message("Initializing RT Api object") rtobject = rtapi.RTObject(db) +# Initialization mode +if args.init_mode: + debug.print_message("Running rt-server-client initialization") + if database_init.run(rtobject): + base.perr("Initialization process failed") + sys.exit(1) + else: + base.pok("Initialization process done" + sys.exit(0) +# Prepare server_object and gather information about the system +server_object = SysInfo(args) +server_object.DiscoverAll() +debug.print_message("Discovered info from sysinfo class:" + str(server_object.information)) + +## +## Here insertion into database starts +## debug.print_message("STARTING MAIN DATABASE INSERTION/UPDATE") -if not rtobject.ObjectExistST(service_tag): +if not rtobject.ObjectExistST(server_object.information['service_tag']): # # service tag not exist - # + # debug.print_message("Service tag not exist in database") - if not rtobject.ObjectExistName(hostname): - # hostname not exist = insert new server Object - rtobject.AddObject(hostname,server_type_id,service_tag,label) - object_id = rtobject.GetObjectId(hostname) - debug.print_message("Added new object to database %s with ID %s" % (str([hostname,server_type_id,service_tag,label]), str(object_id))) - - # Insert attributes, OS info and waranty - #get id of OS - searchstring = os_distribution+"%"+os_codename - os_id = rtobject.GetDictionaryId(searchstring) - attr_id = rtobject.GetAttributeId("SW type") - debug.print_message("SW Type, searching for: %s , Found: %s, Updating: %s" %(searchstring, str(os_id), str(attr_id))) - if os_id != None: - rtobject.InsertAttribute(object_id,server_type_id,attr_id,"NULL",os_id,hostname) - debug.print_message("Inserting OS attribute: obj_id=%d srv_type_id=%d attr_id=%d os_id=%d hostname=%s" % (object_id,server_type_id,attr_id,os_id,hostname)) - else: - debug.print_message("OS id not found in racktables") - - - if server_type_id == 4: - #get id of HW - words = product_name.split() - searchstring = vendor+"%"+"%".join(str(x) for x in words) - hw_id = rtobject.GetDictionaryId(searchstring) - #insert os and hw info - attr_id = rtobject.GetAttributeId("HW type") - debug.print_message("HW Type, searching for: %s , Found: %s, Updating: %s" %(searchstring, str(hw_id), str(attr_id))) - if hw_id != None: - rtobject.InsertAttribute(object_id,server_type_id,attr_id,"NULL",hw_id,hostname) - debug.print_message("Inserting HW attribute: obj_id=%d srv_type_id=%d attr_id=%d hw_id=%d hostname=%s" % (object_id,server_type_id,attr_id,hw_id,hostname)) - else: - debug.print_message("HW id not found in racktables") - - - # Insert CPU and Memory Information - debug.print_message("Inserting CPU attribute information") - if cpu_model_name != "": - rtobject.InsertAttribute(object_id,server_type_id,rtobject.GetAttributeId("CPU Model"),cpu_model_name,"NULL",hostname) - if cpu_logical_num != "": - rtobject.InsertAttribute(object_id,server_type_id,rtobject.GetAttributeId("CPUs Logical"),"NULL",cpu_logical_num,hostname) - if cpu_num != "": - rtobject.InsertAttribute(object_id,server_type_id,rtobject.GetAttributeId("CPUs"),"NULL",cpu_num,hostname) - if cpu_cores != "": - rtobject.InsertAttribute(object_id,server_type_id,rtobject.GetAttributeId("Cores per CPU"),"NULL",cpu_cores,hostname) - if cpu_mhz != "": - rtobject.InsertAttribute(object_id,server_type_id,rtobject.GetAttributeId("CPU, MHz"),"NULL",cpu_mhz,hostname) - if memory_mb != "": - rtobject.InsertAttribute(object_id,server_type_id,rtobject.GetAttributeId("RAM Mem, MB"),"NULL",memory_mb,hostname) - - - if hypervisor == "yes": - searchstring = "Hypervisor" - attid_hyp = rtobject.GetAttributeId(searchstring) - rtobject.InsertAttribute(object_id,server_type_id,attid_hyp,'NULL',1501,hostname) - debug.print_message("Hypervisor, searching for: %s , Found: %s" %(searchstring, str(attid_hyp))) - if len(virtual_servers) != 0: - rtobject.CleanVirtuals(object_id,virtual_servers) - for virtual_name in virtual_servers: - virtual_id = rtobject.GetObjectId(virtual_name) - if virtual_id != None: - debug.print_message("Virtual Linking id:%s with %s(%s)" %(str(object_id), str(virtual_id), str(virtual_name))) - rtobject.LinkVirtualHypervisor(object_id,virtual_id) - - - if len(drac_ip) != 0: - rtobject.UpdateNetworkInterface(object_id,'drac') - rtobject.InterfaceAddIpv4IP(object_id,'drac',drac_ip) - debug.print_message("Inserting Drac IP") - - - for device in device_list: - #Add/update network interface - port_id = rtobject.UpdateNetworkInterface(object_id,device) - #Add network connections - if interface_connections[device_list.index(device)] != '': - rtobject.LinkNetworkInterface(object_id,device,interface_connections[device_list.index(device)][0],interface_connections[device_list.index(device)][1]) - #Add IPv4 ips - if interfaces_ips[device_list.index(device)] != '': - for ip in interfaces_ips[device_list.index(device)]: - rtobject.InterfaceAddIpv4IP(object_id,device,ip) - #Add IPv6 ips - if interfaces_ips6[device_list.index(device)] != '': - for ip in interfaces_ips6[device_list.index(device)]: - rtobject.InterfaceAddIpv6IP(object_id,device,ip) - - # Set Installation Date - rtobject.InsertAttribute(object_id,server_type_id,rtobject.GetAttributeId("Installation Date"),"NULL",int(time.time()),hostname) - rtobject.InsertLog(object_id, "Server-Audit insert instalation date") - debug.print_message("Inserting installation date: "+str(time.time())) - - + if not rtobject.ObjectExistName(server_object.information['hostname']): + # hostname not exist & ST not exist = insert new server Object + rtobject.AddObject( + server_object.information['hostname'], + server_object.information['server_type_id'], + server_object.information['service_tag'], + server_object.information['label'] + ) + object_id = rtobject.GetObjectId(server_object.information['hostname']) + debug.print_message("Added new object to database %s with ID %s" % ( + str([ + server_object.information['hostname'], + server_object.information['server_type_id'], + server_object.information['service_tag'], + server_object.information['label'] + ]), + str(object_id) + )) else: # # hostname exist, what to do? # - print("Hostname %s already exist. I'm using service tag: %s" % (hostname,service_tag)) - debug.print_message("Hostname %s already exist. My Service Tag is %s" % (hostname,service_tag)) + print("Hostname %s already exist but with different service tag. I'm using service tag: %s" % ( + server_object.information['hostname', + server_object.information['service_tag'] + )) + debug.print_message("Hostname %s already exist. My Service Tag is %s" % ( + server_object.information['hostname', + server_object.information['service_tag'] + )) + sys.exit(1) else: # # service tag exist # #check if hostname is the same - if (rtobject.ObjectExistName(hostname)) or (args.force_init) : + if (rtobject.ObjectExistName(server_object.information['hostname'])) or (args.force_init) : if args.force_init: debug.print_message("Force initialization enabled") debug.print_message("Updating hostname") - rtobject.UpdateObjectName(rtobject.GetObjectId(rtobject.GetObjectNameByAsset(service_tag)), hostname) + rtobject.UpdateObjectName(rtobject.GetObjectId(rtobject.GetObjectNameByAsset(server_object.information['service_tag'])), server_object.information['hostname']) else: # hostname exist and service tag is same, update info debug.print_message("Hostname already in DB, service tag is matching. Updating information in DB") - if rtobject.ObjectExistSTName(hostname, service_tag): - object_id = rtobject.GetObjectId(hostname) + if rtobject.ObjectExistSTName(server_object.information['hostname'], server_object.information['service_tag']): + object_id = rtobject.GetObjectId(server_object.information['hostname']) object_label = rtobject.GetObjectLabel(object_id) debug.print_message("Object id is: %s and label %s" %(str(object_id), object_label)) else: - print("Can't do it. Hostname %s and service-tag %s already exist, but does not belong to the same server"% (hostname, service_tag)) - debug.print_message("Can't do it. Hostname %s and service-tag %s already exist, but does not belong to the same server"% (hostname, service_tag)) + print("Can't do it. Hostname %s and service-tag %s already exist, but does not belong to the same server"% ( + server_object.information['hostname'], + server_object.information['service_tag'] + )) + debug.print_message("Can't do it. Hostname %s and service-tag %s already exist, but does not belong to the same server"% ( + server_object.information['hostname'], + server_object.information['service_tag'] + )) sys.exit(1) # Update Zaloha attribute if args.force_init: debug.print_message("Updating Zaloha attribute") - rtobject.InsertAttribute(object_id,server_type_id,rtobject.GetAttributeId("Zaloha"),'NULL',1500,hostname) - - - #Update label if it's not same - if update_label == "yes": - if label != object_label: - rtobject.UpdateObjectLabel(object_id, label) - logstring = "Label changed %s -> %s" % (object_label,label) - rtobject.InsertLog(object_id,logstring) - debug.print_message("Updating old label ( %s ) with new ( %s )" %(object_label, label)) - else: - debug.print_message("Label is same, skip update") - - # Set Installation date if it's blank - result = rtobject.GetAttributeValue(object_id, rtobject.GetAttributeId("Installation Date")) - if result == None: - rtobject.InsertAttribute(object_id,server_type_id,rtobject.GetAttributeId("Installation Date"),"NULL",int(time.time()),hostname) - rtobject.InsertLog(object_id, "Server-Audit insert instalation date") - debug.print_message("Installation date is blank, setting actual time") - - # Insert attributes, OS info and waranty - #get id of OS - searchstring = os_distribution+"%"+os_codename - os_id = rtobject.GetDictionaryId(searchstring) - attr_id = rtobject.GetAttributeId("SW type") - debug.print_message("SW Type, searching for: %s , Found: %s, Updating: %s" %(searchstring, str(os_id), str(attr_id))) - if os_id != None: - rtobject.InsertAttribute(object_id,server_type_id,attr_id,"NULL",os_id,hostname) - debug.print_message("Updating SW Type") - - #Insert kernel version atributes - if kernel_version != "": - attr_id = rtobject.GetAttributeId("Kernel") - kernel_id=rtobject.GetDictionaryId(kernel_version) - if kernel_id == None: - #Have to insert kernel version into dictionary - debug.print_message("Inseting new kernel version to dictionary: %s"% str(kernel_version)) - # kernels dictionary ahs ID 10003 - rtobject.InsertDictionaryValue(10003,str(kernel_version)) - kernel_id=rtobject.GetDictionaryId(kernel_version) - #log kernel change, and update RT - if rtobject.GetAttributeValue(object_id, attr_id) != None: - last_kernel_id = rtobject.GetAttributeValue(object_id, attr_id)[1] - last_kernel_version = rtobject.GetDictionaryValueById(last_kernel_id) - else: - last_kernel_id = "" - last_kernel_version = "" - if last_kernel_id != kernel_id: - logstring = "Kernel changed %s -> %s" % (last_kernel_version,kernel_version) - rtobject.InsertLog(object_id,logstring) - rtobject.InsertAttribute(object_id,server_type_id,attr_id,"NULL",kernel_id,hostname) - - if server_type_id == 4: - #get id of HW - words = product_name.split() - searchstring = vendor+"%"+"%".join(str(x) for x in words) - hw_id = rtobject.GetDictionaryId(searchstring) - #insert os and hw info - attr_id = rtobject.GetAttributeId("HW type") - debug.print_message("HW Type, searching for: %s , Found: %s, Updating: %s" %(searchstring, str(hw_id), str(attr_id))) - if hw_id != None: - rtobject.InsertAttribute(object_id,server_type_id,attr_id,"NULL",hw_id,hostname) - debug.print_message("Updating HW Type") - - - # Insert CPU and Memory Information - debug.print_message("Updating CPU information attributes") - if cpu_model_name != "": - rtobject.InsertAttribute(object_id,server_type_id,rtobject.GetAttributeId("CPU Model"),cpu_model_name,"NULL",hostname) - if cpu_logical_num != "": - rtobject.InsertAttribute(object_id,server_type_id,rtobject.GetAttributeId("CPUs Logical"),"NULL",cpu_logical_num,hostname) - if cpu_num != "": - rtobject.InsertAttribute(object_id,server_type_id,rtobject.GetAttributeId("CPUs"),"NULL",cpu_num,hostname) - if cpu_cores != "": - rtobject.InsertAttribute(object_id,server_type_id,rtobject.GetAttributeId("Cores per CPU"),"NULL",cpu_cores,hostname) - if cpu_mhz != "": - rtobject.InsertAttribute(object_id,server_type_id,rtobject.GetAttributeId("CPU, MHz"),"NULL",cpu_mhz,hostname) - if memory_mb != "": - rtobject.InsertAttribute(object_id,server_type_id,rtobject.GetAttributeId("RAM Mem, MB"),"NULL",memory_mb,hostname) - - if hypervisor == "yes": - searchstring = "Hypervisor" - attid_hyp = rtobject.GetAttributeId(searchstring) - rtobject.InsertAttribute(object_id,server_type_id,attid_hyp,'NULL',1501,hostname) - debug.print_message("Hypervisor, searching for: %s , Found: %s" %(searchstring, str(attid_hyp))) - if len(virtual_servers) != 0: - rtobject.CleanVirtuals(object_id,virtual_servers) - for virtual_name in virtual_servers: - virtual_id = rtobject.GetObjectId(virtual_name) - if virtual_id != None: - debug.print_message("Virtual Linking id:%s with %s(%s)" %(str(object_id), str(virtual_id), str(virtual_name))) - rtobject.LinkVirtualHypervisor(object_id,virtual_id) - else: - print("ERROR: Virtual (%s) exist on hypervisor but not in racktables database" % (virtual_name)) - - - - if len(drac_ip) != 0: - rtobject.UpdateNetworkInterface(object_id,'drac') - drac_list = [drac_ip] - rtobject.CleanIPAddresses(object_id,drac_list,'drac') - rtobject.InterfaceAddIpv4IP(object_id,'drac',drac_ip) - debug.print_message("Updating Drac IP") - - - # Clean unused interfaces - rtobject.CleanUnusedInterfaces(object_id, device_list) - debug.print_message("Cleaning unused interfaces") - - for device in device_list: - #Add/update network interface - port_id = rtobject.UpdateNetworkInterface(object_id,device) - #Add network connections - if interface_connections[device_list.index(device)] != '': - debug.print_message("Processing %s, %s, %s" % (device, interface_connections[device_list.index(device)][0], interface_connections[device_list.index(device)][1])) - rtobject.LinkNetworkInterface(object_id,device,interface_connections[device_list.index(device)][0],interface_connections[device_list.index(device)][1]) - #Add IPv4 ips - if interfaces_ips[device_list.index(device)] != '': - rtobject.CleanIPAddresses(object_id,interfaces_ips[device_list.index(device)],device) - for ip in interfaces_ips[device_list.index(device)]: - rtobject.InterfaceAddIpv4IP(object_id,device,ip) - #Add IPv6 ips - if interfaces_ips6[device_list.index(device)] != '': - rtobject.CleanIPv6Addresses(object_id,interfaces_ips6[device_list.index(device)],device) - for ip in interfaces_ips6[device_list.index(device)]: - rtobject.InterfaceAddIpv6IP(object_id,device,ip) - - # Update motd from comment And Tags - comment = rtobject.GetObjectComment(object_id) - debug.print_message("Getting object comment: \n"+str(comment)) - - #Prepare tag comment - tags_list = rtobject.GetObjectTags(object_id) - debug.print_message("Retrieving TAG list: "+str(tags_list)) - tag_array={} - comment_tag='' - for tag_row in tags_list: - #tag_array[tag_row[0]] - if not tag_array.get(tag_row[0]): - tag_array[tag_row[0]] = [] - tag_array[tag_row[0]].append(tag_row[1]) - - for key in tag_array.keys(): - if key: - comment_tag = comment_tag + key + ": " - for tag in tag_array[key]: - comment_tag = comment_tag + tag + " " - comment_tag = comment_tag + "\n" - debug.print_message("Tags\n"+comment_tag) - - if comment == None: - comment = "" - # copy template to motd, if no exist make tamplate from motd - try: - shutil.copy2(motd_file_original, motd_file) - except IOError : - shutil.copy2(motd_file,motd_file_original) - - debug.print_message("Updating MOTD file") - motd_open_file = open(motd_file, "a") - motd_open_file.write("\n\033[34m--- tags ---\033[33m\n\n" + comment_tag + "\033[0m") - motd_open_file.write("\n\033[34m--- comment ---\033[33m\n\n" + comment + "\033[0m\n") - motd_open_file.close() + rtobject.InsertAttribute( + object_id, + server_object.information['server_type_id'], + rtobject.GetAttributeId("Zaloha"), + 'NULL', + 1500, + server_object.information['hostname'] + ) else: #Hostname is different - print("Service tag %s already exist, but hostname is different." % service_tag) - debug.print_message("Service tag %s already exist, but hostname is different." % service_tag) - -db.close() + print("Service tag %s already exist, but hostname is different." % server_object.information['service_tag']) + debug.print_message("Service tag %s already exist, but hostname is different." % server_object.information['service_tag']) + sys.exit(1) +# +# Update Object information +# +#Update label if it's not same +if server_object.information['update_label'] == "yes": + if server_object.information['label'] != object_label: + rtobject.UpdateObjectLabel(object_id, server_object.information['label']) + logstring = "Label changed %s -> %s" % (object_label,server_object.information['label']) + rtobject.InsertLog(object_id,logstring) + debug.print_message("Updating old label ( %s ) with new ( %s )" %(object_label, server_object.information['label'])) + else: + debug.print_message("Label is same, skip update") + +# Set Installation date if it's blank +result = rtobject.GetAttributeValue(object_id, rtobject.GetAttributeId("Installation Date")) +if result == None: + rtobject.InsertAttribute( + object_id, + server_object.information['server_type_id'], + rtobject.GetAttributeId("Installation Date"), + "NULL", + int(time.time()), + server_object.information['hostname']) + rtobject.InsertLog(object_id, "Server-Audit insert instalation date") + debug.print_message("Installation date is blank, setting actual time") + +# OS System Information +searchstring = server_object.informationp'os_distribution'] + "%" + server_object.information['os_codename'] +os_id = rtobject.GetDictionaryId(searchstring) +attr_id = rtobject.GetAttributeId("SW type") +debug.print_message("SW Type, searching for: %s , Found: %s, Updating: %s" %(searchstring, str(os_id), str(attr_id))) +if os_id != None: + debug.print_message("Inserting OS attribute: obj_id=%d srv_type_id=%d attr_id=%d os_id=%d hostname=%s" % ( + object_id, + server_object.information['server_type_id'], + attr_id, + os_id, + server_object.information['hostname'] + )) + rtobject.InsertAttribute( + object_id, + server_object.information['server_type_id'], + attr_id, + os_id, + server_object.information['hostname'] + ) +else: + debug.print_message("OS id not found in racktables") + + +if server_object.information['server_type_id'] == 4: + #get id of HW + words = server_object.information['product_name'].split() + searchstring = server_object.information['vendor'] + "%" + "%".join(str(x) for x in words) + hw_id = rtobject.GetDictionaryId(searchstring) + #insert os and hw info + attr_id = rtobject.GetAttributeId("HW type") + debug.print_message("HW Type, searching for: %s , Found: %s, Updating: %s" %(searchstring, str(hw_id), str(attr_id))) + if hw_id != None: + rtobject.InsertAttribute( + object_id, + server_object.information['server_type_id'], + attr_id, + "NULL", + hw_id,hostname + ) + debug.print_message("Inserting HW attribute: obj_id=%d srv_type_id=%d attr_id=%d hw_id=%d hostname=%s" % ( + object_id, + server_object.information['server_type_id'], + attr_id, + "NULL", + hw_id,hostname + )) + else: + debug.print_message("HW id not found in racktables") +#TODO + # Insert CPU and Memory Information + debug.print_message("Inserting CPU attribute information") + if cpu_model_name != "": + rtobject.InsertAttribute(object_id,server_type_id,rtobject.GetAttributeId("CPU Model"),cpu_model_name,"NULL",hostname) + if cpu_logical_num != "": + rtobject.InsertAttribute(object_id,server_type_id,rtobject.GetAttributeId("CPUs Logical"),"NULL",cpu_logical_num,hostname) + if cpu_num != "": + rtobject.InsertAttribute(object_id,server_type_id,rtobject.GetAttributeId("CPUs"),"NULL",cpu_num,hostname) + if cpu_cores != "": + rtobject.InsertAttribute(object_id,server_type_id,rtobject.GetAttributeId("Cores per CPU"),"NULL",cpu_cores,hostname) + if cpu_mhz != "": + rtobject.InsertAttribute(object_id,server_type_id,rtobject.GetAttributeId("CPU, MHz"),"NULL",cpu_mhz,hostname) + if memory_mb != "": + rtobject.InsertAttribute(object_id,server_type_id,rtobject.GetAttributeId("RAM Mem, MB"),"NULL",memory_mb,hostname) + + if hypervisor == "yes": + searchstring = "Hypervisor" + attid_hyp = rtobject.GetAttributeId(searchstring) + rtobject.InsertAttribute(object_id,server_type_id,attid_hyp,'NULL',1501,hostname) + debug.print_message("Hypervisor, searching for: %s , Found: %s" %(searchstring, str(attid_hyp))) + if len(virtual_servers) != 0: + rtobject.CleanVirtuals(object_id,virtual_servers) + for virtual_name in virtual_servers: + virtual_id = rtobject.GetObjectId(virtual_name) + if virtual_id != None: + debug.print_message("Virtual Linking id:%s with %s(%s)" %(str(object_id), str(virtual_id), str(virtual_name))) + rtobject.LinkVirtualHypervisor(object_id,virtual_id) + + if len(drac_ip) != 0: + debug.print_message("Inserting Drac IP") + rtobject.UpdateNetworkInterface(object_id,'drac') + rtobject.InterfaceAddIpv4IP(object_id,'drac',drac_ip) + +for device in device_list: + #Add/update network interface + port_id = rtobject.UpdateNetworkInterface(object_id,device) + #Add network connections + if interface_connections[device_list.index(device)] != '': + rtobject.LinkNetworkInterface(object_id,device,interface_connections[device_list.index(device)][0],interface_connections[device_list.index(device)][1]) + #Add IPv4 ips + if interfaces_ips[device_list.index(device)] != '': + for ip in interfaces_ips[device_list.index(device)]: + rtobject.InterfaceAddIpv4IP(object_id,device,ip) + #Add IPv6 ips + if interfaces_ips6[device_list.index(device)] != '': + for ip in interfaces_ips6[device_list.index(device)]: + rtobject.InterfaceAddIpv6IP(object_id,device,ip) + +# Set Installation Date +debug.print_message("Inserting installation date: "+str(time.time())) +rtobject.InsertAttribute(object_id,server_type_id,rtobject.GetAttributeId("Installation Date"),"NULL",int(time.time()),hostname) +rtobject.InsertLog(object_id, "Server-Audit insert instalation date") + +# Update motd from comment And Tags +comment = rtobject.GetObjectComment(object_id) +debug.print_message("Getting object comment: \n"+str(comment)) + +#Prepare tag comment +tags_list = rtobject.GetObjectTags(object_id) +debug.print_message("Retrieving TAG list: "+str(tags_list)) +tag_array={} +comment_tag='' +for tag_row in tags_list: + #tag_array[tag_row[0]] + if not tag_array.get(tag_row[0]): + tag_array[tag_row[0]] = [] + tag_array[tag_row[0]].append(tag_row[1]) + +for key in tag_array.keys(): + if key: + comment_tag = comment_tag + key + ": " + for tag in tag_array[key]: + comment_tag = comment_tag + tag + " " + comment_tag = comment_tag + "\n" + debug.print_message("Tags\n"+comment_tag) + +if comment == None: + comment = "" +# copy template to motd, if no exist make tamplate from motd +try: + shutil.copy2(motd_file_original, motd_file) +except IOError : + shutil.copy2(motd_file,motd_file_original) + +debug.print_message("Updating MOTD file") +motd_open_file = open(motd_file, "a") +motd_open_file.write("\n\033[34m--- tags ---\033[33m\n\n" + comment_tag + "\033[0m") +motd_open_file.write("\n\033[34m--- comment ---\033[33m\n\n" + comment + "\033[0m\n") +motd_open_file.close()