Tough script
hi
i want to write a script in ksh that will print the comma delimited list of Host,IP,FreeMem,averageLoad. This is what i tried and i get lost: cd /etc host=`cat HOSTNAME` cd .. I_P=`/sbin/ifconfig` IP=${I_P#*eth0 * inet addr:} # delete everything to the left of inet addr: IP=${IP%% *} # delete from the right till you find the last value that variable IP now holds echo "Host, IP" echo $host , $IP TotMem=`free -tm` Tot_Mem=${TotMem#*Total: } #Tot_Mem=${$1% *} echo $Tot_Mem the above script acheives to get the Host and the IP address. Please help with the remaining elements as to how i can get them. your assistance is much appreciated. thanks! |
What are the remaining elements you are looking for? Can you please sum it up for me?
|
You could experiment with these:
echo `hostname` echo `ifconfig eth0|awk 'NR==2 {print $2}'|sed 's/^.....//'` echo `free -t|awk 'NR==5 {print $4}'` echo `cat /proc/loadavg` Edit, oops, for a comma delimited something like: echo `hostname`,`ifconfig eth0|awk 'NR==2 {print $2}'|sed 's/^.....//'`,\ `free -t|awk 'NR==5 {print $4}'`,`cat /proc/loadavg` |
for averageLoad: do
echo `uname -a | cut -d ' ' -fX` run uname on your box and check the column number loadtime avarage is in and put it where "X" is in the above command. for freememory do: echo `cat /proc/meminfo | grep MemFree | cut -d ' ' -f2` play around with the value for 2 above note the (`) they are tilda not commas. Good luck sorry cant be of too much help aint a guru, I myself experiment and mess around a little before I come up with real scripts and am not not my box right now. |
Thanks everyone.
and, makuyl is it possible to use another method except using awk as in you reply post: echo `free -t|awk 'NR==5 {print $4}'`. Or could you at least explain to me in a nutshell as to what the statement actually means. I haven't got the grips of AWK as yet. Please... |
@slack12ware:
~ = tilde ` = backquote |
Here is how I would do:
Code:
#!/bin/bash |
Quote:
thanks colucix i really wish to understand what the following means and right now i don't. can you please break it down for me: '/Total/{s/.* //;p}'. Another thing is that I want total memory and not only free memory. thanks |
To understand the sed command consider the following:
Code:
$ free -tm Code:
sed -n '/Total/{s/.* //;p}' If you want the total memory, swap included you can do something like Code:
free -tm | sed -n '/Total/{s/Total: *//;s/ .*//;p}' Code:
free -tm | awk '/Total/{print $2}' Code:
awk '/MemTotal/{printf "%d\n",$2/1000}' /proc/meminfo |
Quote:
thank you very much sir. now i get the picture. thanks again |
Quote:
I didn't mean you should use those echo lines as I wrote them, just wanted to give you some ideas for a possible solution. Much cleaner to use variables like you and colucix already did. |
IP=$(hostname -i)
may save you all that messing with ifconfig and sed to pull the ip address out. |
Quote:
|
Quote:
It's an interesting problem. If you can't rely on 'hostname -i', then you have to parse the output of ifconfig, but then how do you decide which interface to report should you encounter a system with multiple interfaces. |
Quote:
Code:
dsl0 Link encap:Point-to-Point Protocol |
All times are GMT -5. The time now is 12:20 AM. |