# SNinfo.ctl: Collects Serial Numbers # $Id: SNinfo.ctl,v 1.14 2015/08/31 14:01:46 RDA Exp $ # ARCS: $Header: /home/cvs/cvs/RDA_8/src/scripting/lib/collect/EXPLORER/SNinfo.ctl,v 1.14 2015/08/31 14:01:46 RDA Exp $ # # Change History # 20150828 JGS Restrict SMS serial retrieval. =head1 NAME EXPLORER:SNinfo - Collects Serial Numbers =head1 DESCRIPTION This module collects serial numbers from a series of sources. The following macros are available: =cut use Explorer use Xml # Make the module persistent and share macros keep @SHARE_MACROS var @SHARE_MACROS = ('get_all_serials',\ 'get_asradm_serial','get_backup_eeprom_serial',\ 'get_bios_serial','get_cst_serial','get_dmidecode_serial',\ 'get_eeprom_serial','get_explorer_serial',\ 'get_fserial','get_ipmi_serial',\ 'get_lshal_serial','get_lshal_smbios_serial',\ 'get_lshal_system_serial','get_prtconf_serial',\ 'get_prtdiag_serial','get_prtpicl_serial',\ 'get_smbios_serial','get_sms_serial',\ 'get_stclient_serial','get_virtinfo_serial') =head2 get_all_serials() Returns a reference to a hash with the serial numbers found. =cut macro get_all_serials {var $ser = {} debug ' Inside SNinfo, gathering asradm serial number' if ?get_asradm_serial() var $ser->{'asradm'} = last debug ' Inside SNinfo, gathering BIOS serial number' if ?get_bios_serial() var $ser->{'BIOS'} = last debug ' Inside SNinfo, gathering CST serial number' if ?get_cst_serial() var $ser->{'CST'} = last debug ' Inside SNinfo, gathering dmidecode serial number' if ?get_dmidecode_serial() var $ser->{'dmidecode'} = last debug ' Inside SNinfo, gathering EEPROM serial number' if ?get_eeprom_serial() var $ser->{'EEPROM'} = last debug ' Inside SNinfo, gathering EEPROM backup serial number' if ?get_backup_eeprom_serial() var $ser->{'Backup EEPROM'} = last debug ' Inside SNinfo, gathering serial number from Explorer configuration' if ?get_explorer_serial() var $ser->{'Explorer'} = last debug ' Inside SNinfo, gathering the serial number on Fujitsu systems' if ?get_fserial() var $ser->{'fserial'} = first debug ' Inside SNinfo, gathering IPMI serial number' if ?get_ipmi_serial() var $ser->{'IPMI'} = last debug ' Inside SNinfo, gathering lshal smbios serial number' if ?get_lshal_smbios_serial() var $ser->{'lshal smbios'} = last debug ' Inside SNinfo, gathering lshal system serial number' if ?get_lshal_system_serial() var $ser->{'lshal system'} = last debug ' Inside SNinfo, gathering prtconf serial number' if ?get_prtconf_serial() var $ser{'prtconf'} = last debug ' Inside SNinfo, gathering prtdiag serial number' if ?get_prtdiag_serial() var $ser->{'prtdiag'} = last debug ' Inside SNinfo, gathering prtpicl serial number' if ?get_prtpicl_serial() var $ser->{'prtpicl'} = last debug ' Inside SNinfo, gathering smbios serial number' if ?get_smbios_serial() var $ser->{'smbios'} = last debug ' Inside SNinfo, gathering SMS serial number' if ?get_sms_serial() var $ser->{'SMS'} = last debug ' Inside SNinfo, gathering stclient (service tag) serial number' if ?get_stclient_serial() var $ser->{'stclient'} = last debug ' Inside SNinfo, gathering virtinfo serial number' if ?get_virtinfo_serial() var $ser->{'virtinfo'} = last return $ser } =head2 get_asradm_serial() Gets and returns the F serial number. =cut macro get_asradm_serial {if grepCommand('/usr/sbin/asradm list 2>/dev/null',\ '^System\s+Id\s+(\w+)\s*$','1f') return first return undef } =head2 get_backup_eeprom_serial() Gets and returns the serial number from EEPROM backup. =cut macro get_backup_eeprom_serial {if grepFile('/etc/default/SUNWsneep','^ChassisSerialNumber\s+(\w+)','1f') return first return undef } =head2 get_bios_serial() Gets and returns the the BIOS serial number using F (Windows only). =cut macro get_bios_serial {if or(isWindows(),isCygwin()) return second(@lin = command('wmic bios get serialnumber 2>/dev/null')) return undef } =head2 get_cst_serial() Gets and returns the the CST serial number. =cut macro get_cst_serial {if grepFile('/var/opt/SUNWcst/user_data','serial.*system\s+(\w+)','1fi') return first return undef } =head2 get_dmidecode_serial() Gets and returns the F serial number. =cut macro get_dmidecode_serial {var $flg = false if ?$[XPL]->has_dmidecode {loop $lin (grepCommand({cmd=>[quote(last),'2>/dev/null'],\ tag=>'_sn_dmidecode_@EXPLORER_XPLR'},\ 'DMI type \d+,|^\s*Serial Number:','i')) {if $flg {if match($lin,'^\s*Serial Number:\s+(\w+)',true) return first return undef } if match($lin,'DMI type 1,',true) var $flg = true } } return undef } =head2 get_eeprom_serial() Gets and returns the serial number from EEPROM. =cut macro get_eeprom_serial {if grepCommand({cmd=>'/usr/sbin/eeprom nvramrc 2>/dev/null',\ tag=>'_sn_eeprom_@EXPLORER_XPLR'},\ '^(nvramrc=)?\."\s*ChassisSerialNumber\s+(\w+)\s*"\s+cr','2f') return first return undef } =head2 get_explorer_serial() Gets and returns the serial number from the Explorer configuration. =cut macro get_explorer_serial {if ?testDir('d',${ENV.EXP_CONFIG}) var $fil = catFile(last,'default','explorer') elsif ${OS.solaris} var $fil = cond(expr('<',get_osv(),11),'/etc/opt/SUNWexplo/default/explorer',\ '/etc/explorer/default/explorer') if ?$fil {var $pat = concat('^EXP_SERIAL_',command(${CMD.HOSTID:'hostid'}),'="(\w+)"$') if grepFile($fil,$pat,'1') return first } return undef } =head2 get_fserial() Gets and returns the serial number on Fujitsu systems. =cut macro get_fserial {if ?testFile('fx',catFile(nvl(get_pkg_base('FJSVmadm'),'/opt'),\ 'FJSVmadm','sbin','serialid')) {if grepCommand({cmd=>lastTestCommand(),\ tag=>'_sn_serialid_@EXPLORER_XPLR'},\ 'serialid\S*\s+(\w+)','1f') return first } return undef } =head2 get_ipmi_serial() Gets and returns the IPMI serial number. =cut macro get_ipmi_serial {# Locate ipmitool if !?$pgm = $[XPL]->has_ipmitool return undef # Parse the IPMI data var ($ipm,$typ) = ({}) loop $lin(command({cmd=>[quote($pgm),'-I bmc fru 2>/dev/null'],\ inc=>4,\ tag=>'_sn_ipmitool_I_bmc_fru_@EXPLORER_XPLR'})) {if match($lin,'Chassis Type') {if !?$typ var $typ = 'misc' } elsif match($lin,'FRU.*Desc.*:(.*)$') var $typ = check(first,\ '\s+.*ID 0[)]', 'ILOM',\ '\s*[Mm][Bb](\.[Ff][Rr][Uu])?\s+', 'motherboard',\ '([\s\/]*SYS\s+)|(\s*System\s+FRU\s+)','system',\ '\s+MIDPLANE\s+.*ID', 'midplane',\ $typ) elsif match($lin,'^$') var $typ = undef next !?$typ if match($lin,'\s*([^:]+):([^:]+)') {var ($key,$val) = last next !match($key,'(Product\s+Version|(Chassis|Product)\s+Serial)') var $val = trim($val) next !??$val next match($val,'^0[-0]*$') if match($val,'^\d{4}[A-Z]{3}-(\w+)$') var $val = first next match($val,'^[0-9a-fA-F]{2}$') $val = replace($val,'\s+','_',1) if match($key,'Chassis\s+Serial') var $ipm->{concat($typ,'_chassis')} = $val elsif match($key,'Product\s+Serial') var $ipm->{concat($typ,'_product')} = $val elsif match($key,'Product\s+Version') var $ipm->{concat($typ,'_version')} = $val } } # Retrieve the serial if exists($ipm->{'ILOM_version'}) {if match($ipm->{'ILOM_version'},'^3\.') {if exists($ipm->{'system_product'}) return $ipm->{'system_product'} if exists($ipm->{'system_chassis'}) return $ipm->{'system_chassis'} if exists($ipm->{'midplane_product'}) return $ipm->{'midplane_product'} if exists($ipm->{'midplane_chassis'}) return $ipm->{'midplane_chassis'} } else {if exists($ipm->{'midplane_product'}) return $ipm->{'midplane_product'} if exists($ipm->{'midplane_chassis'}) return $ipm->{'midplane_chassis'} if exists($ipm->{'system_product'}) return $ipm->{'system_product'} if exists($ipm->{'system_chassis'}) return $ipm->{'system_chassis'} } } if exists($ipm->{'motherboard_product'}) return $ipm->{'motherboard_product'} if exists($ipm->{'motherboard_chassis'}) return $ipm->{'motherboard_chassis'} if exists($ipm->{'misc_product'}) return $ipm->{'misc_product'} if exists($ipm->{'misc_chassis'}) return $ipm->{'misc_chassis'} return undef } =for stopwords smbios =head2 get_lshal_serial($key) Gets and returns the specified F serial number. =head2 get_lshal_smbios_serial() Gets and returns the smbios F serial number. =head2 get_lshal_system_serial() Gets and returns the system F serial number. =cut macro get_lshal_serial {if !?$tbl {var $tbl = {} keep $tbl if ?nvl(testFile('x','/usr/bin/lshal'),\ testFile('x','/usr/sbin/lshal')) {loop $lin (grepCommand(last,\ '^\s*(smbios\.system|system\.hardware)\.serial\s+=')) {if match($lin,"^\s*([\.\w]+)\s+=\s+'([^']+)") var $tbl->{first} = second } } } return $tbl->{$arg[0]} } macro get_lshal_smbios_serial return get_lshal_serial('smbios.system.serial') macro get_lshal_system_serial return get_lshal_serial('system.hardware.serial') =head2 get_prtconf_serial() Gets and returns the F serial number. =cut macro get_prtconf_serial {loop $lin (grepCommand({cmd=>'/usr/sbin/prtconf -vp 2>/dev/null',\ tag=>'_sn_prtconf_vp_@EXPLORER_XPLR'},\ '^\s+(chassis-sn|system-board-serial#):\s*\x27?(\w+)\x27?','2f')) {var $ser = last break !match($ser,'[a-zA-Z0-9]+') break compare('gt',length($ser),10) break match($ser,'^501') return $ser } return undef } =head2 get_prtdiag_serial() Gets and returns the F serial number. =cut macro get_prtdiag_serial {# Test that we are in a global zone if ?testFile('fx','/sbin/zonename') {if !grepCommand(lastTestCommand(),'^global$','f') return undef } # Extract the serial number var ($hwn) = command([${CMD.UNAME:'uname'},' -m']) var ($hwn) = match($hwn,'(\S+)') if ?testFile('x','/usr/sbin/prtconf') var $sys = nvl(first(grepCommand('/usr/sbin/prtconf','^SUNW,','f')),\ first(grepCommand('/usr/sbin/prtconf','^CYRS,','f'))) loop $cmd ('/usr/sbin/prtdiag',\ catFile('/usr','platform',$hwn,'sbin','prtdiag'),\ catFile('/usr','platform',$sys,'sbin','prtdiag'),\ catFile('/usr','kvm','prtdiag')) {next !?testFile('fx',$cmd) var $skp = 0 loop $lin (command({cmd=>[lastTestCommand(),'-v 2>/dev/null'],\ inc=>6,\ tag=>'_sn_prtdiag_v_@EXPLORER_XPLR'})) {if match($lin,'^Chassis\s+Serial\s+Number\s*$') next $skp = 1 next !$skp if match($lin,'^-+\s*$') next $skp = 2 if expr('==',$skp,2) {if match($lin,'^(\w+)\s*$') return first var $skp = 0 } } break } return undef } =head2 get_prtpicl_serial() Gets and returns the F serial number. =cut macro get_prtpicl_serial {var ($nmi) = command([${CMD.UNAME:'uname'},' -i']) var ($nmi) = match($nmi,'([^\s]+)') loop $rec (['/usr/sbin/prtpicl','_sn_prtpicl_v_@EXPLORER_XPLR'],\ [catFile('/usr','platform',$nmi,'sbin','prtpicl')]) {next !?testFile('fx',$rec->[0]) if grepCommand({cmd=>[lastTestCommand(),'-v 2>/dev/null'],\ inc=>20,\ tag=>$rec->[1]},\ ':chassis-sn\s*(\w+)','f1') return first } return undef } =head2 get_smbios_serial() Gets and returns the F serial number. =cut macro get_smbios_serial {loop $cmd ('/usr/sbin/smbios','/sbin/smbios','/usr/bin/smbios') {next !?testFile('fx',$cmd) var %arg = ('_sn_smbios_t_1_@EXPLORER_XPLR'=>'-t 1',\ '_sn_smbios_t_3_@EXPLORER_XPLR'=>'-t 3',\ '_sn_smbios_@EXPLORER_XPLR' =>'') loop $tag (keys(%arg)) {var $sta = 0 loop $lin (grepCommand({cmd=>[lastTestCommand(),$arg{$tag}],\ tag=>$tag},\ '(^ID.*SIZE|SMB_TYPE_(SYSTEM|CHASSIS)|Serial Number:)')) {if match($lin,'^ID.*SIZE') next $sta = 1 next expr('<',$sta,1) if match($lin,'SMB_TYPE_(SYSTEM|CHASSIS)') next $sta = 2 next expr('<',$sta,2) var ($ser) = match($lin,'Serial Number:\s*(.*?)\s*$') var $ser = replace($ser,'([Nn]ot [Aa]vail|[Tt]o [Bb]e).*$') var $ser = replace($ser,'([Nn]one|[^:\s]*:([^:\s]*:){4,})\S*') if !match(field('\s',0,$ser),'^(0[\-0]*)?$') return $ser } } } return undef } =head2 get_sms_serial() Gets and returns the SMS serial number. =cut macro get_sms_serial {if ?testFile('fx','/opt/SUNWSMS/bin/showplatform') {$usr = nvl(isUser(${T_SMS_USER},true),'sms-svc') if grepFile('/etc/passwd',concat('^',verbatim($usr),':'),'f') {if grepCommand({cmd=>concat('su - ',quote($usr),' -c "',lastTestCommand(),\ ' -p csn" 2>/dev/null'),\ tag=>'_sn_showplatform_p_csn_@EXPLORER_XPLR'},\ '^Chassis Serial Number\s*:\s*(\w+)','1f') return first } } loop $lin (grepFile('/var/opt/SUNWSMS/SMS/.pcd/platform_info',\ '(version [124-9])|(^(\|[^|]*)*\|(\w+)\s*$)')) {break match($lin,'version [124-9]') if match($lin,'^(\|[^|]*)*\|(\w+)\s*$') return second } return undef } =head2 get_stclient_serial() Gets and returns the F (service tags) serial number. =cut macro get_stclient_serial {if ?testFile('fx',catFile(\ nvl(get_pkg_base('SUNWservicetagu','usr','bin'),'/usr/bin'),'stclient')) {var $xml = xmlLoadCommand({cmd=>[lastTestCommand(),'-E'],\ tag=>'_sn_stclient_E_@EXPLORER_XPLR'}) return $xml->find('agent/system_info/serial_number')->get_data() } return undef } =head2 get_virtinfo_serial() Gets and returns the F serial number. =cut macro get_virtinfo_serial {if grepCommand({cmd=>'/usr/sbin/virtinfo -s 2>/dev/null',\ tag=>'_sn_virtinfo_a_@EXPLORER_XPLR'},\ '^Chassis\s+serial#:\s+(\w+)\s*$','1f') return first return undef } =head1 SEE ALSO L =head1 COPYRIGHT NOTICE Copyright (c) 2002, 2016, Oracle and/or its affiliates. All rights reserved. =head1 TRADEMARK NOTICE Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners. =cut