Red Hat/CentOS hard disk partitioning,formatting & updating fstab with UUID

I came to a situation where I need to partitioning, formatting & updating fstab with UUID of 25 servers each having 26 disks in total. If i am doing it manually, it will take ages to complete.

Here is a script which has FOR loop, ARRAY, COUNTER…etc below are the major tasks

  1. read disk names
  2. create mount points
  3. creating the partition using fdisk (noninteractive way)
  4. formatting with XFS
  5. getting UUID and update in fstab
  6. setting block reservation to 0

 

#!/bin/bash
echo “________HDD__________”
IFS=$’\n’ read -d ” -ra DiskName < <(fdisk -l | grep 800 | egrep -v “GPT|WARNING” | awk ‘{print $2}’ | sed ‘s/://g’)
for i in “${!DiskName[@]}”;do

COUNTER=$(expr $COUNTER + 1)
mkdir -p /data/s$COUNTER
echo -e “o\nn\np\n1\n\n\nw” | fdisk ${DiskName[$i]}
mkfs.xfs $(echo “${DiskName[$i]}1”)
Uuid=$(blkid | grep $(echo “${DiskName[$i]}1”) | awk ‘{print $2}’ | sed ‘s/”//g’)
echo “$Uuid /data/s$COUNTER xfs defaults,noatime 1 2″ >> /etc/fstab
mount /data/s$COUNTER
xfs_io -x -c resblks /data/s$COUNTER

doneIFS=$’\n’ read -d ” -ra ForDiskName < <(fdisk -l | grep 4000 | egrep -v “GPT|WARNING” | awk ‘{print $2}’ | sed ‘s/://g’)
for i in “${!ForDiskName[@]}”;do

ForCOUNTER=$(expr $ForCOUNTER + 1)
mkdir -p /data/$ForCOUNTER
parted -s -a optimal ${ForDiskName[$i]}
mklabel gpt — mkpart primary 512s 100% sleep 2
mkfs.xfs -f $(echo “${ForDiskName[$i]}1”)
Uuid=$(blkid | grep $(echo “${ForDiskName[$i]}1”) | awk ‘{print $2}’ | sed ‘s/”//g’)
echo “$Uuid /data/$ForCOUNTER xfs defaults,noatime 1 2” >> /etc/fstab
mount /data/$ForCOUNTER
xfs_io -x -c resblks /data/$ForCOUNTER

done

NOTE: copy-paste may add some extra Characters, I Am Not an expert in scripting 🙂

Solaris script to generate average Disk I/O based on sar

HI

 

this is a simple script to generate average diskI /O based on sar reports. the output is given as comma separated and you can easily generate graphs by Excel or LibreOffice

 

#!/usr/bin/env bash
#Wrote on 15-08-2016 by Ben George
##################################
# Generate sar Disk IO of current month
##################################

PATH=/usr/xpg4/bin:$PATH

DATEROW=$(read -r date month < <(date ‘+%d %b’); days=(); for (( i = 1; i <= 10#$date; ++i )); do days+=(“$month$i”); done; printf ‘%s,’ “${days[@]}”)
echo “Device,”$DATEROW

lastfile=/var/adm/sa/sa$(date +%d)

for file in /var/adm/sa/sa[0-9][0-9]; do
[[ $file > $lastfile ]] && break
sar -d -f “$file”
done | awk -v RS= -F’\n’ ‘
$1 ~ /Average/ {
for (i = 1; i<=NF; ++i) {
if ($i !~ /vdc/)
continue;
split($i, row, ” “);
a[row[1]]=a[row[1]]”,”row[4] }
}
END {
for (disk in a)
print disk a[disk] }’

 

NOTE: if anyone is getting difficulty reading the script, please mail me i will send the script

Script to Detect un-used/not mounted Storages

simple script to alert unused storage space. This will also tell you active storage path mount points

#!/usr/bin/bash
#created by Ben.T.George
LunId=( `luxadm probe |grep -v “Type:Tape” |grep -v “Node WWN:” | awk -F\/ ‘NR>3{print $4}’|sed ‘s/..$//g’` )
zonelist=( `zoneadm list` )
echo “__________________________________________________________________________________”
zoneadm list -cv
echo “__________________________________________________________________________________”
for luns in “${!LunId[@]}”;do
(echo -e “\x1B[31m ${LunId[$luns]} \x1B[0m”
echo “”
result=$(echo | format “${LunId[$luns]}” 2>&1 |egrep -c ‘(mounted|zpool)’)
if [ $result -gt 0 ]; then
echo -e “\x1B[01;96m ${LunId[$luns]} mounted on: $(mount | grep ${LunId[$luns]} | awk ‘{print $1}’)\x1B[0m”
else
echo -e “\x1B[01;31m ${LunId[$luns]} not mounted \x1B[0m”
fi)&
done

One Line Script to list all LUNS on Solaris 10

Hi All,

 

Here is one line script to list all LUN’s(only external Storage) on solaris 10.

 

root@benvin.net# luxadm probe | grep -v “Node WWN:” | awk -F\/ ‘NR>3{print $4}’|sed ‘s/..$//g’
c3t600D0231000BE73D1C9ED0B453F185C2d0
c3t600D0231000BE73D4A2C042D18A2E641d0
c3t600D0231000BE73D6D42C80D77B8DDF0d0
c3t600D0231000BE73D194D85CA4D102795d0
c3t600D0231000BE73D435D27657C919F6Ed0
c3t600D0231000BE73D5482A3BE75F412FCd0
c3t600D0231000BE73D5619FDD6500CF273d0
c3t600D0231000BE73D37672E311947B18Cd0

Good luck

Copy RSA keys to all Solaris zones & restart ssh service

This is a simple script to copy SSH RSA keys to all standard zones and restarting ssh services on all .

i am not going to explain how to create public RSA keys. Also assume that RSA key is copied to global zone.

usage : create file with script, give exicute permission & run example.sh <ip>

#!/bin/bash
ipaddress=$1
IFS=$’\n’ read -d ” -ra name < <(ssh “$ipaddress” zoneadm list -cv | awk ‘NR > 2{print $4}’ )
IFS=$’\n’ read -d ” -ra state < <(ssh “$ipaddress” zoneadm list | awk ‘NR > 1’ )

for i in “${!name[@]}”;do
ssh $ipaddress mkdir -p “${name[$i]}”/root/.ssh
ssh $ipaddress chmod 700 “${name[$i]}”/root/.ssh
echo “copying RSA keys on : ${state[$i]}”
echo “ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA4T+N21GzKnYgCh9sC72qhOYKjbjHRyp5GqWPDGSfUkhmk8iJRAS2hhcP5lZ1x6fc/cNzqETQ98sjdKm59PB7MSJQ12WMHTc7wtuBdh7+wE0ZlWsjGfEvzCaWIXNUhh2hK2ZUq7XMZ5QR2E+j2uWTJKsZmQyf4
A5z8nLTAtL2CKQamxZskABttpXkxx7gm+yc8coJB8nhOr6Q4KsyDcEmjGAFwbjiBKyefUIiMroP0PtZPI/8EYtwtP/Cm79BFzhJtOl/VGqTvlab+1IQeO54/ztL0gg7C0zTwNTKODTrCNh+nIj6Fk6/CjyX0Qg9xWd7BRr1oUbI5yMvg3wLD66tEw== root@kwtprgateone01
” | ssh $ipaddress “cat >> “${name[$i]}”/root/.ssh/authorized_keys”
ssh $ipaddress chmod 600 “${name[$i]}”/root/.ssh/authorized_keys
echo “changing ssh configuration files on: ${state[$i]}”
ssh $ipaddress “cat ${name[$i]}/root/etc/ssh/sshd_config | sed ‘s/PermitRootLogin no/PermitRootLogin yes/g’ > ${name[$i]}/root/tmp/sshd_config ; cp ${name[$i]}/root/tmp/sshd_config ${name[$i]}/root/e
tc/ssh/sshd_config”
echo “Restarting ssh service on : ${state[$i]}”
ssh $ipaddress “zlogin ${state[$i]} svcadm restart ssh”
done

Solaris disk utilization(including zones) script & sending to mysql

HI

I have been writing scripts to automate my solaris admin tasks. currently i am handling more than 250 sun servers and my primary focus area is solaris nowadays. Somehow linux came to 2nd priority

today i am posting some script to monitor disk utilization of solaris server including zones(containers). And this script will send result to mysql database directly .you need to run this script on global zone and you than set the disk check threshold value also.

To connect to mysql, you need to install mysql-client package. ie simple compile & make. you can download mysql-client from http://dev.mysql.com/downloads/mysql/5.1.html. For compiling this package , you need Compilers and other development tools. You can get all this from sun studio. Install sun studio and include studio in your PATH.

 

#!/bin/bash
#Created By – Ben George – ben@benvin.net
IFS=$’\n’ read -d ” -ra filesystem < <(df -h | egrep -e ‘/dev/dsk|pool’)
IFS=$’\n’ read -d ” -ra zonename < <(zoneadm list)
DATE=$(/usr/bin/date |awk ‘{print $3″-“$2”-“$6}’)
ipaddress=$(ifconfig -a | grep inet | grep -v ‘127.0.0.1’ | awk ‘NR>1{ print $2}’| head -1)
for i in “${!zonename[@]}”;do

if [ “${zonename[$i]}” = “global” ];then
for i in “${!filesystem[@]}”;do
percentage=$(echo “${filesystem[$i]}” | awk ‘{print $5}’ | sed ‘s/%/ /g’)
if [ $percentage -gt 50 ];then
mountpoint=$(echo “${filesystem[$i]}” | awk ‘{print $6}’)
echo $HOSTNAME $ipaddress $mountpoint “:” $percentage”%”
/opt/mysql/bin/mysql –host=172.16.99.182 –user=root –password=Redhat server_daily << EOF
insert into filesystem (hostname,ipaddress,time,filesystem_name,percentage) values(‘$HOSTNAME’,’$ipaddress’,’$DATE’,’$mountpoint’,’$percentage’);
EOF
fi
done
else
echo “${zonename[$i]}”
IFS=$’\n’ read -d ” -ra zonefilesystem < <(zonecfg -z “${zonename[$i]}” info | egrep ‘pool|dir:’ | awk ‘{print $2}’)
for j in “${!zonefilesystem[@]}”;do
echo “${zonefilesystem[$j]}” | while read n;do
zonepercentage=$(zlogin “${zonename[$i]}” df -h “${zonefilesystem[$j]}” | awk ‘NR>1{print $5}’ | sed ‘s/%/ /g’)
if [ $zonepercentage -gt 50 ];then
zone_mountpoint=$(zlogin “${zonename[$i]}” df -h “${zonefilesystem[$j]}” | awk ‘NR>1{print $6}’)
zone_hostname=$(zlogin “${zonename[$i]}” hostname)
zone_ip=$(zlogin “${zonename[$i]}” ifconfig -a | grep inet | grep -v ‘127.0.0.1’ | awk ‘{ print $2}’)
echo $zone_hostname $zone_ip $zone_mountpoint “:” $zonepercentage”%”
/opt/mysql/bin/mysql –host=172.16.99.182 –user=root –password=Redhat server_daily << EOF
insert into filesystem (hostname,ipaddress,time,filesystem_name,percentage) values(‘$zone_hostname’,’$zone_ip’,’$DATE’,’$zone_mountpoint’,’$zonepercentage’);
EOF
fi
done
done
fi
done

 

How to audit solaris commands like rm,mv,cron,zip… as realtime

HI

we faced one issue in production solaris machine like someone/some program is deleting one directory sometimes. I decided to explore more on this and finally found Dtrace , that comes with solaris bundle

 

Dtrace:

DTrace is a comprehensive dynamic tracing framework created by Sun Microsystems for troubleshooting kerneland application problems on production systems in real time. Originally developed for Solaris, it has since been released under the free Common Development and Distribution License (CDDL) and has been ported to several other Unix-like systems.

 

DTrace can be used to get a global overview of a running system, such as the amount of memory, CPU time, filesystem and network resources used by the active processes. It can also provide much more fine-grained information, such as a log of the arguments with which a specific function is being called, or a list of the processes accessing a specific file.

 

Here i am showing some simple script to trace the commands and send output to file. After that we can logrotate that based on size bcoz this will dump huge lines of putput

 

create a file with below script and start that script with nohup, that’s it

 

 

dtrace -qn ‘syscall::unlink*:entry { printf(“%d, %s, %Y, %s\n”, uid, execname, walltimestamp, copyinstr(arg0));}syscall::fsat:entry  /arg0 == 5 / { printf(“%d, %s, %Y ,%s\n”, uid, execname, walltime

stamp, copyinstr(arg2));}’ | tee -a /var/tmp/spora.log