Thursday, January 10, 2019

[Solved]: unable to add devices in OpenDCIM

If unable to add device in OpenDCIM, need to find below method to get it resolved.

# mysql -u root -p
# select @@global.sql_mode;
# set sql_mode="";
# SET GLOBAL sql_mode = "";
# service httpd restart

Re-scan Disk after adding disk to virtual machine

Execute below command after add the disk to the machine in vmware.

# ls /sys/class/scsi_host

host 0

# echo "- - -" > /sys/class/scsi_host/host0/scan

# fdisk -l

Friday, July 20, 2018

Create script to get gateway address, DNS, NTP, & Service Status in Linux

Copy the below content and paste it in a file, it will give you the output at /opt/ location, if you wants to change the location you can change.

You have to change the NTP server IP Address in file rest of the things will work on all environment.

#!/bin/bash
rm -rf /opt/ping_result
echo -e "\nTIMESTAMP: `date`" >> /opt/ping_result
echo -e "HOSTNAME: `hostname`"  >> /opt/ping_result
#hostname >> /opt/ping_result
GW=`/sbin/ip route | awk '/default/ { print $3 }'`
checkdns=`cat /etc/resolv.conf | awk '/nameserver/ {print $2}' | awk 'NR == 1 {print; exit}'`
checkntp1=10.200.247.21
checkntp2=10.200.247.22
output=/opt/ping_result
GN='\033[0;32m'
RD='\033[0;31m'
NC='\033[0m'
function pingdns 
{
  tput setaf 6; echo -e "Pinging DNS server in resolv.conf ($checkdns) to check name resolution" >> $output; tput sgr0;
  ping $checkdns -c 4
    if [ $? -eq 0 ]
    then
      tput setaf 6; echo -e "${GN}Primary DNS is Pingable.${NC} Proceeding with NTP Server 1 Check. \n" >> $output; tput sgr0;
    else
      echo -e "Could not establish internet connection to DNS. ${RD}Something may be wrong here.${NC} \n" >> $output >&2
  fi
}
function pingntp1
{
  tput setaf 6; echo -e "Pinging ($checkntp1) to check NTP status"  >> $output; tput sgr0;
  ping $checkntp1 -c 4
    if [ $? -eq 0 ]
    then
      tput setaf 6; echo -e "${GN}NTP Server 1 is Pingable.${NC} Proceeding with NTP Server 2 check. \n" >> $output; tput sgr0;
    else
      echo -e "Could not establish connection to NTP Server 1. ${RD}Something may be wrong here.${NC} \n" >> $output >&2
  fi
}
function pingntp2
{
  tput setaf 6; echo -e "Pinging ($checkntp2) to check NTP status" >> $output; tput sgr0;
  ping $checkntp2 -c 4
    if [ $? -eq 0 ]
    then
      tput setaf 6; echo -e "${GN}NTP Server 2 is Pingable.${NC} \n" >> $output; tput sgr0;
    else
      echo -e "Could not establish connection to NTP Server 2. ${RD}Something may be wrong here.${NC} \n" >> $output >&2
  fi
}
function services1
{
  tput setaf 6; echo -e "${GN}Running Services${NC}" >> $output; tput sgr0;
  service --status-all | grep running >> $output;
    if [ $? -eq 0 ]
    then
      tput setaf 6; echo -e "${GN}Above Given Services are Running.${NC} \n" >> $output; tput sgr0;
    else
      echo -e "${GN}Above Given Services are Running.${NC} \n" >> $output >&2
  fi
}
function services2
{
  tput setaf 6; echo -e "${RD}Stopped Services${NC}" >> $output; tput sgr0;
  service --status-all | grep stopped >> $output;
    if [ $? -eq 0 ]
    then
      tput setaf 6; echo -e "${RD}Above Given Services are Stopped.${NC} \n" >> $output; tput sgr0;
    else
      echo -e "${RD}Above Given Services are Stopped.${NC} \n" >> $output >&2
  fi
}
tput setaf 6; echo -e "\nPinging Defaut gateway ($GW) to check for LAN connectivity">> $output; tput sgr0;
if [ "$GW" = "" ]; then
    tput setaf 1;echo -e "${RD}There is no gateway. Probably disconnected...${NC} \n" >> $output; tput sgr0;
fi
ping $GW -c 4
if [ $? -eq 0 ]
then
  tput setaf 6; echo -e "${GN}Default Gateway Pingable.${NC} Proceeding with internet connectivity check.\n " >> $output; tput sgr0;
  pingdns
  pingntp1
  pingntp2
  services1
  services2
  exit 0
else
  echo -e "${RD}Something is wrong with LAN (Gateway unreachable)${NC} >> /opt/ping_result "
  pingdns
  pingntp1
  pingntp2
  services1
  services2
fi


The output of the file will as mentioned below.



Wednesday, May 30, 2018

Get Complete Virtual Machine inventory of vcenter using vmwarepowercli

Copy below given script and save it as filename.ps1

execute the same script your output of this script will be saved at "c:\vminventory.csv"


You will get below mentioned information from this script.

1.  VMName
2.  IP Address
3.  Domain Name
4.  Real-OS
5.  vCPU
6.  RAM(GB)
7.  Total-HDD(GB)
8.  HDDs(GB)
9.  Datastore
10. Partition/Size
11. Hardware Version
12. PowerState
13. Setting-OS
14. EsxiHost
15. vCenter Server
16. Folder
17. MacAddress
18. VMX
19. VMDK
20. VMTools Status
21. VMTools Version
22. VMTools Version Status
23. VMTools Running Status
24. SnapShots
25. DataCenter
26. vNic
27. PortGroup
28. RDMs


#Add-PSSnapin vmware.vimautomation.core
Connect-Viserver    
#####################################  
 ## http://kunaludapi.blogspot.com  
 ## Version: 1  
 ## Tested this script on  
 ##  1) Powershell v3  
 ##  2) Powercli v5.5  
 ##  3) Vsphere 5.x  
 ####################################
 function Get-VMinventory {  
 function Get-RDMDisk {  
   [CmdletBinding()]  
   param (  
     [Parameter(Mandatory=$True)]  
     [string[]]$VMName  
     )  
         $RDMInfo = Get-VM -Name $VMName | Get-HardDisk -DiskType RawPhysical, RawVirtual  
         $Result = foreach ($RDM in $RDMInfo) {  
          "{0}/{1}/{2}/{3}"-f ($RDM.Name), ($RDM.DiskType),($RDM.Filename), ($RDM.ScsiCanonicalName)     
         }  
         $Result -join (", ")  
 }  
 function Get-vNicInfo {  
   [CmdletBinding()]  
   param (  
     [Parameter(Mandatory=$True)]  
     [string[]]$VMName  
     )  
         $vNicInfo = Get-VM -Name $VMName | Get-NetworkAdapter  
         $Result = foreach ($vNic in $VnicInfo) {  
           "{0}={1}"-f ($vnic.Name.split("")[2]), ($vNic.Type)  
         }  
         $Result -join (", ")  
 }  
 function Get-InternalHDD {  
   [CmdletBinding()]  
   param (  
     [Parameter(Mandatory=$True)]  
     [string[]]$VMName  
     )  
         $VMInfo = Get-VMGuest -VM $VMName # (get-vm $VMName).extensiondata  
         $InternalHDD = $VMInfo.ExtensionData.disk   
         $result = foreach ($vdisk in $InternalHDD) {  
           "{0}={1}GB/{2}GB"-f ($vdisk.DiskPath), ($vdisk.FreeSpace /1GB -as [int]),($vdisk.Capacity /1GB -as [int])  
         }  
         $result -join (", ")  
 }  
   foreach ($vm in (get-vm)) {  
     $props = @{'VMName'=$vm.Name;  
           'IP Address'= $vm.Guest.IPAddress -join ","; #$VM.ExtensionData.Summary.Guest.IpAddress  
           'PowerState'= $vm.PowerState;  
           'Domain Name'= ($vm.ExtensionData.Guest.Hostname -split '\.')[1,2] -join '.';            
           'vCPU'= $vm.NumCpu;  
           'RAM(GB)'= $vm.MemoryGB;  
           'Total-HDD(GB)'= $vm.ProvisionedSpaceGB -as [int];  
           'HDDs(GB)'= ($vm | get-harddisk | select-object -ExpandProperty CapacityGB) -join " + "            
           'Datastore'= (Get-Datastore -vm $vm) -split ", " -join ", ";  
           'Partition/Size' = Get-InternalHDD -VMName $vm.Name  
           'Real-OS'= $vm.guest.OSFullName;  
           'Setting-OS' = $VM.ExtensionData.summary.config.guestfullname;  
           'EsxiHost'= $vm.VMHost;  
           'vCenter Server' = ($vm).ExtensionData.Client.ServiceUrl.Split('/')[2].trimend(":443")  
           'Hardware Version'= $vm.Version;  
           'Folder'= $vm.folder;  
           'MacAddress' = ($vm | Get-NetworkAdapter).MacAddress -join ", ";  
           'VMX' = $vm.ExtensionData.config.files.VMpathname;  
           'VMDK' = ($vm | Get-HardDisk).filename -join ", ";  
           'VMTools Status' = $vm.ExtensionData.Guest.ToolsStatus;  
           'VMTools Version' = $vm.ExtensionData.Guest.ToolsVersion;  
           'VMTools Version Status' = $vm.ExtensionData.Guest.ToolsVersionStatus;  
           'VMTools Running Status' = $vm.ExtensionData.Guest.ToolsRunningStatus;  
           'SnapShots' = ($vm | get-snapshot).count;  
           'DataCenter' = $vm | Get-Datacenter;  
           'vNic' = Get-VNICinfo -VMName $vm.name;  
           'PortGroup' = ($vm | Get-NetworkAdapter).NetworkName -join ", ";  
           'RDMs' = Get-RDMDisk -VMName $VM.name  
           #'Department'= ($vm | Get-Annotation)[0].value;  
           #'Environment'= ($vm | Get-Annotation)[1].value;  
           #'Project'= ($vm | Get-Annotation)[2].value;  
           #'Role'= ($vm | Get-Annotation)[3].value;  
           }  
     $obj = New-Object -TypeName PSObject -Property $Props  
     Write-Output $obj | select-object -Property 'VMName', 'IP Address', 'Domain Name', 'Real-OS', 'vCPU', 'RAM(GB)', 'Total-HDD(GB)' ,'HDDs(GB)', 'Datastore', 'Partition/Size', 'Hardware Version', 'PowerState', 'Setting-OS', 'EsxiHost', 'vCenter Server', 'Folder', 'MacAddress', 'VMX', 'VMDK', 'VMTools Status', 'VMTools Version', 'VMTools Version Status', 'VMTools Running Status', 'SnapShots', 'DataCenter', 'vNic', 'PortGroup', 'RDMs' # 'Folder', 'Department', 'Environment' 'Environment'  
   }  
 }  
 Get-VMinventory 

 $obj | Sort VMName | Export-Csv -Path "c:\vminventory.csv"

Sunday, May 27, 2018

VMware script to get CPU core details.

Save the given file in machine and give extension ".ps1"

$myCol = @()
Foreach ($cluster in Get-Cluster)
    {
        Foreach($vmhost in ($cluster | Get-VMHost))
        {
            Foreach($vm in (Get-VM -Location $vmhost)){
                $VMView = $vm | Get-View
                $VMSummary = "" | Select ClusterName,HostName,VMName,VMSockets,VMCores,CPUSockets,CPUCores
                $VMSummary.ClusterName = $cluster.Name
                $VMSummary.HostName = $vmhost.Name
                $VMSummary.VMName = $vm.Name
                $VMSummary.VMSockets = $VMView.Config.Hardware.NumCpu
                $VMSummary.VMCores = $VMView.Config.Hardware.NumCoresPerSocket
                $VMSummary.CPUSockets = $vmhost.ExtensionData.Hardware.CpuInfo.NumCpuPackages
                $VMSummary.CPUCores = $vmhost.ExtensionData.Hardware.CpuInfo.NumCpuCores
                $myCol += $VMSummary
            }
        }
    }
$myCol | Export-csv reports.csv

Now connect vcenter using vmware powercli and execute the script.


Another method to get done this is follow below mentioned file save and execute the same to get the data.


Don't forget to change the given lines with your scenario.



  • $vcenter = vc01.vcdx56.com
  • $vcenteruser = vcdx56\magnus”
  • $vcenterpw = “not secret”
  • $reportdir = c:\vSpherereport
  • $filename = c:\vSphere$date.html
  • $basefile = c:\vSpherereportCPU-oversubscription.html

 # The script will calculate the ESXi host CPU core to VM vCPU oversubscription and create a HTML report

#
# Version 1.0 Magnus Andersson RTS
#————————————————
# Start of script parameters section
#
# vCenter Server configuration
$vcenter = “vcenter-ip-address“
$vcenteruser = “Administrator@vsphere.local“
$vcenterpw = "password"
#
$date=get-date -uformat %Y-%m-%d
#
# Define report directory
$reportdir = "c:\temp\vSpherereport"
#
# Define file name
$filename = "c:\temp\vSphere$date.html"
#
# Define base file
$basefile = "c:\temp\vSpherereportCPU-oversubscription.html"
#
# End of script parameter section
#—————————————— #
#
# Connect to vCenter Server
connect-viserver $vcenter -User $vcenteruser -Password $vcenterpw
#
function Get-Cluster-CPUMem {
$table = @()
foreach($clust in (get-cluster | Sort-Object Name)) {
$esx = $clust | Get-VMHost
$clu = "" | select Cluster, "ESXi host num CPU core", "VM vCPU", "VM vCPU on", "Percent of vCPU on"
$vms = $clust | get-vm
$clu.Cluster = $clust.Name
$clu."ESXi host num CPU core" = ($clust | Get-vmhost | Measure-Object -Sum NumCPU).Sum
$clu."VM vCPU" = ($vms | Measure-Object -Sum NumCPU).Sum
$clu."VM vCPU on" = ($vms | ?{$_.PowerState -eq "PoweredOn"} | Measure-Object -Sum NumCPU).Sum
$clu."Percent of vCPU on" = "{0:P0}" -f ($clu."VM vCPU on" / $clu."ESXi host num CPU core")
$table += $clu
}
$clu = "" | select Cluster, "ESXi host num CPU core", "VM vCPU", "VM vCPU on", "Percent of vCPU on"
$clu.Cluster = "Total"
$clu."ESXi host num CPU core" = ($table | Measure-Object -property "ESXi host num CPU core" -sum).sum
$clu."VM vCPU" = ($table | Measure-Object -property "VM vCPU" -sum).sum
$clu."VM vCPU on" = ($table | Measure-Object -property "VM vCPU on" -sum).sum
$clu."Percent of vCPU on" = "{0:P0}" -f ($clu."VM vCPU on" / $clu."ESXi host num CPU core")
$table += $clu
$table
}
$a = "<style>"
$a = $a + "BODY{background-color:white;}"
$a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
$a = $a + "TH{border-width: 1px;padding: 4px;border-style: solid;border-color: black;}"
$a = $a + "TD{border-width: 1px;padding: 4px;border-style: solid;border-color: black;text-align:center;}"
$a = $a + "</style>"
Get-Cluster-CPUMem | ConvertTo-HTML -head $a -Title "CPU oversubscription report $date" | Out-File $filename
Remove-Item $basefile
Copy-Item $filename $basefile
if ($filename -like "*-01.*") {
Copy-Item $filename $reportdir
}
Remove-item $filename