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 |
If you have a router/nat you can just grep for 192.168 or which ever of the four natted adress spaces you use.
Without nat, your isp probably has the same first two octets every time, so grep for what you usually have. |
You could use "getent hosts <hostname>" as well. Even if you use avahi for name resolution. You will still get the localhost IP address for the host you are running on since that is what is in the hosts file.
The freemem value might be useless. There will be memory used for cache that will be freed if the system needs it. Obtaining the load and memory values from a remote host, you probably need to have some kind of client running on those hosts or use ssh to run a command or script that will return the values. And running a client or script to monitor memory and load value will of course reduce the available memory and increase the load. As you would expect. I find it convenient to use public key encryption for ssh. Using that and "AllowHosts" makes it more secure, I you don't need to deal with using expect to deal with authentication. Sometimes that can interfere with pipes and tees in your script. Having usernames and passwords in scripts is best to avoid as well. |
Quote:
That was fairly easy. |
Quote:
Code:
ifconfig $(route | grep default | awk '{print $8}') Ofcourse, if the system is configured such that hostname -i returns the correct value, it avoids the need for all this shenanigans, but as you made clear, this cannot be relied upon on all distributions and setups. This is why I enjoy hanging out on LQ. You can often find interesting insights in threads that you expect to be pretty straightforward on first viewing. Yes... I know, I'm Sad like that. ;) |
Quote:
|
Quote:
Quote:
|
Quote:
|
All times are GMT -5. The time now is 01:42 AM. |