for loop not working
by Janani Priya from LinuxQuestions.org on (#4SE27)
here is my code,
#! /bin/bash
for i in $(cat host.txt)
do
hostname=$(hostname -s)
memory=$(free -m | awk 'NR==2{printf "%.2f%%", $3*100/$2 }')
mem_health=$(free -m | awk 'NR==2{ print int($3*100/$2) }')
if [ $mem_health -ge 80 ];
then
mem_health="CRITICAL"
else
mem_health="NORMAL"
fi
DISK=$(df -h | awk '$NF=="/"{printf "%s", $5}')
disk_health=$(df -h | awk '$NF=="/"{printf int($5)}')
if [ $disk_health -ge 80 ]
then
disk_health="CRITICAL"
else
disk_health="NORMAL"
fi
CPU=$(top -bn1 | grep load | awk '{printf "%.2f%%", $(NF-2)}')
CPU_health=$(top -bn1 | grep load | awk '{printf int($(NF-2))}')
if [ $CPU_health -ge 80 ]
then
CPU_health="CRITICAL"
else
CPU_health="NORMAL"
fi
INODE=$(df -h | awk '$NF=="/opt"{printf "%s", $5}')
inode_health=$(df -h | awk '$NF=="/opt"{printf int($5)}')
if [ $inode_health -ge 80 ]
then
inode_health="CRITICAL"
else
inode_health="NORMAL"
fi
df -h > output.txt
printf '%-16s %-10s %-13s %-8s %-13s %-7s %-13s %-7s %-13s %-7s %-13s %-1s\n' "Hostname" "Memory" "MEM_HEALTH" "Disk" "Disk_Health" "CPU" "CPU_Health" "INODE" "inode_health"
printf '%-16s %-10s %-13s %-8s %-13s %-7s %-13s %-7s %-13s %-7s %-13s %-1s\n' "$hostname" "$memory" "$mem_health" "$DISK" "$disk_health" "$CPU" "$CPU_health" "$INODE" "$inode_health"
done
The thing is output gives me the deatils of the server which im currently logged in and not the servers in host.txt file. and also could you pplease help me in getting the output transferred to a csv file?
thank you.


#! /bin/bash
for i in $(cat host.txt)
do
hostname=$(hostname -s)
memory=$(free -m | awk 'NR==2{printf "%.2f%%", $3*100/$2 }')
mem_health=$(free -m | awk 'NR==2{ print int($3*100/$2) }')
if [ $mem_health -ge 80 ];
then
mem_health="CRITICAL"
else
mem_health="NORMAL"
fi
DISK=$(df -h | awk '$NF=="/"{printf "%s", $5}')
disk_health=$(df -h | awk '$NF=="/"{printf int($5)}')
if [ $disk_health -ge 80 ]
then
disk_health="CRITICAL"
else
disk_health="NORMAL"
fi
CPU=$(top -bn1 | grep load | awk '{printf "%.2f%%", $(NF-2)}')
CPU_health=$(top -bn1 | grep load | awk '{printf int($(NF-2))}')
if [ $CPU_health -ge 80 ]
then
CPU_health="CRITICAL"
else
CPU_health="NORMAL"
fi
INODE=$(df -h | awk '$NF=="/opt"{printf "%s", $5}')
inode_health=$(df -h | awk '$NF=="/opt"{printf int($5)}')
if [ $inode_health -ge 80 ]
then
inode_health="CRITICAL"
else
inode_health="NORMAL"
fi
df -h > output.txt
printf '%-16s %-10s %-13s %-8s %-13s %-7s %-13s %-7s %-13s %-7s %-13s %-1s\n' "Hostname" "Memory" "MEM_HEALTH" "Disk" "Disk_Health" "CPU" "CPU_Health" "INODE" "inode_health"
printf '%-16s %-10s %-13s %-8s %-13s %-7s %-13s %-7s %-13s %-7s %-13s %-1s\n' "$hostname" "$memory" "$mem_health" "$DISK" "$disk_health" "$CPU" "$CPU_health" "$INODE" "$inode_health"
done
The thing is output gives me the deatils of the server which im currently logged in and not the servers in host.txt file. and also could you pplease help me in getting the output transferred to a csv file?
thank you.