How To Count The Number Of Occurrence Of A Given Word In A UNIX File

This is one of the ice breaker questions asked so often in interviews -
How can you count the number of occurrence of a given word in a UNIX file?
Let's say that the following is the sample file. And we need to count the number of occurrence of the word "sample" in it.
/var/root # cat file                                                          
This is a sample file. Frankly, it is very easy to write in this sample file.
I don't intend to have a lot of content in this sample file. Now, I know that
you are wondering what is so special about this sample file. Is there        
a bigger purpose that showing just a sample. Well, relax!                    
For it is just a simple old sample file.  
It is actually a trick question. You will find that most of the guys will simply let you know that they can easily use "grep -c" to figure out the number of occurrence of the word sample in the file. Let's see what happens.
/var/root # grep -c sample file                                              
Isn't that surprising? If you have observed carefully, the word "sample" occurs 6 times. This happens because -c simply counts the first occurrence in a give line. So the first line which actually has two occurrences of the word sample is counted as one.

There is one more way to get the right answer. It is to simply use the "grep -o" switch along with "wc -l", i.e. line count. The "grep -o" switch simply figures out every occurrence of the word in a given file and places it in a new line. So, now you have got as many number of lines in the output as the number of occurrences. Isn't it? Hence, you can simply count the number of lines to figure out the number of occurrence. See for yourself -
/var/root # grep -o sample file | wc -l                                      
Sleek, isn't it?

Google's April Fool, Extraterrestrial Hit Or Real Code Break In Adsense Dashboard ~ 01 April 2014

OK. Today is 1st April 2014. I am not sure if this is an April Fool prank by Google or something really broke in Adsense Code. Or there is a third angle to it as well -
We are not alone in this universe!!
I am not sure which of the above options are correct and I would really like to know if you guys also have a similar issue, prank or extraterrestrial hit - someone just visited my site from Moon and ended up paying $0.01 as well. Check out the "Top Planets and Moon" section in your Google Adsense dashboard / View Planets.

Sounds like a Sci-Fi movie yeah. But, I think it is more likely to be a prank. I even searched internet and found nobody noticing any similar issue. So it is worth a post and hence, worth a question?

Do you guys see anything similar?

How To Switch System Boards In E25K

Switching system board is just a matter of un-configuring existing system board and configuring the new system boards. We are talking about a Sun E25K machine. It has got multiple System Boards and it is very easy to allocate SB to multiple hosts as required.
First of all identify the existing configuration and setup -
# showplatform
# showboards
You will observe that the system boards are allocated to a particular host. Un-configure the System Boards from these hosts -
# cfgadm –c unconfigure SB12  (on host1)
# cfgadm –c unconfigure SB10  (on host2)
Check again to ensure system boards have been configured.
# showplatform -v
Get in touch with the Data Center guy or the Sun/Oracle engineer for physical removal and swapping of boards. Once the engineer confirms that the swapping has been successfully done get back on the SC prompt and reconfigure it for respective hosts -
# cfgadm -c configure SB12 (on host2)
# cfgadm -c configure SB10 (on host1)
Restart the machines and you have successfully swapped E25K uniboards.

Upgrade OS From Solaris 8 To Solaris 10

The following change implementation steps elaborate how one can upgrade Solaris operating system from Solaris8 to Solaris10 using jumpstart procedures. This is divided into two sections. The first section talks about few step and testing before implementation. And the next section elaborates about the actual integration. Its a bit complex, but if you go about it in a step by step fashion - it is a cake walk.

1. Before Implementation Steps and Testing

Mount Solaris10 CDROM at SC level
#mount –F hsfs /dev/dsk/<cdrom path> /mnt

Take backup of all configuration files
# ./opt/SUNWexplo/bin/explorer
# psrinfo
# prtconf -pv | grep -i boot
# psrinfo
# prtconf | grep -i mem
# metastat | grep – i State
# mkdir /var/tmp/CONFIG.`hostname`
# cd /var/tmp/CONFIG.`hostname`
# df -k > df_k.out
# prtvtoc -h /dev/dsk/c0t0d0s2 > prtvtoc.c1t0d0.out
# prtvtoc -h /dev/dsk/c0t1d0s2 > prtvtoc.c1t1d0.out
# swap -l > swap.out
# metadb -i > metadb.out
# mount -v > mount.out
# metastat > metastat.out
# metastat -p > metastat-p.out
# metadb -i > metadb.out
# cp -p /etc/vfstab  vfstab.todays_date
# cp -p /etc/system system.todays_date
# cp -p /kernel/drv/sd.conf sd.conf.orig
# cp -p /etc/path_to_inst path_to_inst.orig
# showrev -p > showrev.out
# /usr/platform/`uname -m`/sbin/prtdiag -v > prtdiag.out
# eeprom > eeprom.out
# inq > inq.out
# netstat -rn > netstat.out
# iostat -En > iostat.out
# ifconfig -a > ifconfig.out
# kstat -p > kstat.out
# ps -ef > ps.ef.out
# who -rb > who.out
# uptime > uptime.out
# dumpadm > dumpadm.log.todays-date
# ptree -a > ptree.out

Copy the solaris imae from DVD to server SC
# cp –r /cdrom/cdrom0/s0/Solaris_2.7/Misc/* /export/install/jumpstart 

Create jumpstart server /export/install/config/server_name

Create profile
# vi profile
install_type             upgrade
root_device              c1t0d0s0
backup_media             remote_filesystem
#partitioning    explicit
# Mount Locations are fixed.
# Skip the following Slices:
# 0: Root
# 4: A separate /var partion
# 5: A un-used partition for whatever
# 7: For SDS metadbase
#filesys c1t0d0s0   16384    / logging
#filesys c1t0d0s1  16384    swap
#filesys c1t0d0s3   16384    /var logging
#filesys c1t0d0s4  16384   /var/crash
#filesys c1t0d0s5  free   unnamed
#filesys c1t0d0s7  256   unnamed

Create sysidcfg file
#vi sysidcfg
network_interface=ce0  {hostname=server_name

Create rules file
#vi rules
hostname server_name - profile -

Create /etc/ethers
#vi /etc/ethers
<server_name macaddess>  server_name

Share OS Image and Directory
#vi /etc/dfs/dfstab
/usr/sbin/share -F nfs -o ro,anon=0 /export/install

Start NFS service
#showmount –e

Add server_name as a client. This will copy the CDROM contents in to the /export/install directory
#cd /cdrom/cdrom0/s0/Solaris_2.7/Tools
# ./setup_install_server /export/install
# ./setup_install_server -b /export/install sun4u
#cd /cdrom/cdrom0/s0/Solaris_2.7/Tools
#./add_install_client -e <Client MAC Address> -s -c server_name sun4u

Create rules.ok file

2. Implementation Steps

Sanitory reboot the system
#shutdown -y -g0 -i6
c0t0d0s0 ===>root disk
c0t1d0s0 ===>root mirror

Detach all mirror devices
c1t0d0=rootdisk,   c1t1d0=rootmirror #metadetach  d200 d240 detaching root mirror
#metadetach  d201 d241 detaching swap mirror
#metadetach d203 d243 detaching var mirror
#metadetach d204 d244 detaching opt mirror
#installboot /usr/platform/`uname -i`/lib/fs/ufs/bootblk /dev/rdsk/c0t1d0s0 install the bootblock onto the rootmirror device

Shutdown all the applications and shutdown the server
#shutdown –y –g0 –i0

Boot with mirror disk
ok>boot rootmitrror

Shutdown the server again
#shutdown –y –g0 –i0

Boot with network
ok>boot net - install

After successfull upgradation check /etc/vfstab
#df –k
#uname –a
#netstat –nr
#ifconfg –a
#metastat |egrep “main|need|sync”

Test the applications with application support team

Attach all root mirror devices to root devices
#metattach d200 d220
#metattach d201 d221
#metattach d203 d223
#metattach d204 d224

Check for meta devices and ensure syncing stands completed.

How To Find port_wwn And node_wwn Of A Storage Device

Once the storage device is provisioned to UNIX boxes a storage engineer typically looks for the port_wwn and node_wwn numbers of the HBA to correctly slice and dice the storage device for your server needs. The problem with the situation is that most of the UNIX engineers expect to see the device and then figure out its details. However, the device is not made available to run-level-3 unless it is correctly configured by the storage engineers.
But there is something that a UNIX engineer can do to figure out the port_wwn and node_wwn numbers. Once the HBA device is connected these identification numbers are available in single user mode.

{0} ok probe-scsi-all
Cannot Init Link.
Cannot Init Link.
MPT Version 1.05, Firmware Version
Target 0
Unit 0   Disk     SEAGATE ST914603SSUN146G0768    286739329 Blocks, 146 GB
  SASAddress 5000c5000aca0491  PhyNum 0
Target 1
Unit 0   Disk     SEAGATE ST914603SSUN146G0768    286739329 Blocks, 146 GB
  SASAddress 5000c5000aca06b1  PhyNum 1
  Unit 0   Removable Read Only device    TSSTcorpCD/DVDW TS-T632ASR03
{0} ok cd /pci@0/pci@0/pci@9/SUNW,emlxs@0
{0} ok pwd
{0} ok .properties
assigned-addresses       82120010 00000000 0b000000 00000000 00002000
                         82120018 00000000 0b002000 00000000 00002000
                         81120020 00000000 00004000 00000000 00000100
                         82120030 00000000 0b040000 00000000 00040000
port_wwn                 10 00 00 00 c9 96 ad 38
node_wwn                 20 00 00 00 c9 96 ad 38
alternate-reg            01120020 00000000 00000000 00000000 00000100
reg                      00120000 00000000 00000000 00000000 00000000
                         03120010 00000000 00000000 00000000 00001000
                         03120018 00000000 00000000 00000000 00000100
                         02120030 00000000 00000000 00000000 00020000
compatible               pci10df,fc20
clock-frequency          02625a00
#size-cells              00000000
#address-cells           00000002
copyright                Copyright (c) 2000-2006 Emulex
model                    LPe11000-S
name                     SUNW,emlxs
device_type              scsi-fcp
manufacturer             Emulex
fcode-version            1.50a9
fcode-rom-offset         0000c000
interrupts               00000001
cache-line-size          00000010
class-code               000c0400
subsystem-id             0000fc21
subsystem-vendor-id      000010df
revision-id              00000002
device-id                0000fc20
vendor-id                000010df
{0} ok
And there you go. Its right in front of you eyes. Communicate the same and make their life a little easier.

IO Wait Issues With Oracle Database

The following log illustrates a typical server hung / database hung state when multiple read writes are on going on the database. A simple command lsof - list of open files can help you figure it out. You will observe that the same process id is writing / reading from multiple locations, of huge sizes and the system overall running at snails pace.
unix_server1# lsof /ora/data
oracle    701 oracle  256u  VREG 273,58002    3727360 12302 /ora/data/RPRO1P/RPRO1Pcntl01.dbf
oracle    701 oracle  257u  VREG 273,58002    3727360 12303 /ora/data/RPRO1P/RPRO1Pcntl02.dbf
oracle    701 oracle  258u  VREG 273,58002    3727360 12304 /ora/data/RPRO1P/RPRO1Pcntl03.dbf
oracle    701 oracle  259u  VREG 273,58002  262152192 12308 /ora/data/RPRO1P/RPRO1Psystem01.dbf
oracle    701 oracle  260u  VREG 273,58002  157294592 12309 /ora/data/RPRO1P/RPRO1Ptools01.dbf
oracle    701 oracle  261u  VREG 273,58002 1048584192 12307 /ora/data/RPRO1P/RPRO1Preqpro_tbl01.dbf
oracle    701 oracle  262u  VREG 273,58002 1048584192 12306 /ora/data (/dev/vx/dsk/app-dg/vol3)
oracle    701 oracle  263u  VREG 273,58002   52436992 12305 /ora/data/RPRO1P/RPRO1Pgenuser.dbf
oracle    795 oracle  256u  VREG 273,58002  524296192 12297 /ora/data/CQST1P/CQST1Psystem01.dbf
oracle    795 oracle  257u  VREG 273,58002  209723392 12298 /ora/data/CQST1P/CQST1Ptools01.dbf
oracle    795 oracle  258u  VREG 273,58002   52436992 12293 /ora/data/CQST1P/CQST1Pcqmaster_tbl01.dbf
oracle    795 oracle  259u  VREG 273,58002   52436992 12292 /ora/data/CQST1P/CQST1Pcqmaster_idx01.dbf
oracle    795 oracle  260u  VREG 273,58002   52436992 12296 /ora/data/CQST1P/CQST1Pgenuser.dbf
oracle    795 oracle  261u  VREG 273,58002    1662976 12289 /ora/data/CQST1P/CQST1Pcntl01.dbf
oracle    795 oracle  262u  VREG 273,58002    1662976 12290 /ora/data/CQST1P/CQST1Pcntl02.dbf
oracle    795 oracle  263u  VREG 273,58002    1662976 12291 /ora/data/CQST1P/CQST1Pcntl03.dbf
oracle   1971 oracle  256u  VREG 273,58002  524296192  6260 /ora/data/CADT1D/CADT1Dsystem01.dbf
oracle   1971 oracle  257u  VREG 273,58002  209723392  6263 /ora/data/CADT1D/CADT1Dtools01.dbf
oracle   1971 oracle  258u  VREG 273,58002  524296192  6261 /ora/data/CADT1D/CADT1Dcqmaster_tbl01.dbf
You know now, when it's time to call your DBA.

Essential PreCheck Before Every System Turnover

Before doing any turnover it is very important to prepare for any eventuality and that is why you should be prepared with backout steps and procedures. However, to successfully backout you must be aware of the current state of the system. I wrote a small handy script with a bunch of commands that helps me everytime to grab the current state of the system before I go in for any change. Put  all of them in a script -

# mkdir /var/tmp/PRE_CHANGE_data.`hostname`
# cd /var/tmp/PRE_CHANGE_data.`hostname`
# /usr/sbin/metastat > metastat.txt
# /usr/sbin/metastat -p  > metastat-p.txt
# /usr/sbin/metadb > metadb.txt
# df -k > df-k.txt
# for x in `/usr/sbin/metastat d0 | grep No | awk '{print $1}'`; do  /usr/sbin/prtvtoc /dev/dsk/$x > prtvtoc.$x.txt; done
# /usr/sbin/eeprom | sort > eeprom.txt
# /usr/local/bin/inq > inq.txt
# cp -ip /etc/path_to_inst path_to_inst.txt
# /usr/bin/netstat -rnv > netstat-rnv.txt
# /usr/platform/sun4u/sbin/prtdiag -v > prtdiag-v.txt
# cp -ip /etc/system system.txt
# cp -ip /etc/vfstab vfstab.txt
# /usr/sbin/mount > mount.txt
# /usr/sbin/dumpadm > dumpadm.txt
# ifconfig -a > ifconfig-a.txt
# swap -l > swap-l.txt
# kstat -p ce:::"/^link_/" > kstat-p_ce.txt
# ps -ef > ps-ef.txt
# netstat -a > netstat-a.txt
# netstat -an > netstat-an.txt
# ptree > ptree.txt
# showrev -p > showrev-p.txt
# egrep "^TZ" /etc/default/init > TZ.txt
# zdump -v `egrep "^TZ" /etc/default/init | cut -d= -f2` | grep 2008 > zdump_2008.txt
# uptime > uptime.txt
# who -r > who-r.txt
# cp -p /var/spool/cron/crontabs/root crontab_root.txt
# /usr/sbin/vxprint > vxprint.txt
# /usr/sbin/vxdisk list > vxdisk_list.txt
# /usr/sbin/vxdg list > vxdg_list.txt
# cp -p /kernel/drv/scsi_vhci.conf kernel-drv-scsi_vhci.conf
# ps -ef|grep pmon > pmon.txt
# metastat -p d0 > d0.txt
# prtconf -pv |grep -i boot > prtconf_boot.txt
# echo|format > format.txt
# /opt/VRTSvcs/bin/hastatus -summ > hastatus.txt

Feel free to add your own commands to the list and please do share it with us all. This may be different for different flavors of UNIX.