LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-09-2009, 08:30 AM   #1
mmahulo
Member
 
Registered: Nov 2008
Posts: 31

Rep: Reputation: 15
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!
 
Old 01-09-2009, 08:35 AM   #2
Agrouf
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: LFS
Posts: 1,596

Rep: Reputation: 80
What are the remaining elements you are looking for? Can you please sum it up for me?
 
Old 01-09-2009, 09:01 AM   #3
makuyl
Senior Member
 
Registered: Dec 2004
Location: Helsinki
Distribution: Debian Sid
Posts: 1,107

Rep: Reputation: 54
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`

Last edited by makuyl; 01-09-2009 at 12:12 PM.
 
Old 01-09-2009, 09:06 AM   #4
slack12ware
Member
 
Registered: Mar 2008
Location: Deep in the Jango(Africa)
Distribution: Slackware 12, Fedora 8
Posts: 46

Rep: Reputation: 15
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.
 
Old 01-12-2009, 01:08 AM   #5
mmahulo
Member
 
Registered: Nov 2008
Posts: 31

Original Poster
Rep: Reputation: 15
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...
 
Old 01-12-2009, 01:28 AM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,348

Rep: Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749
@slack12ware:

~ = tilde

` = backquote
 
Old 01-12-2009, 02:29 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Here is how I would do:
Code:
#!/bin/bash
host=$(hostname -s)
ip=$(/sbin/ifconfig eth0 | sed -n '/inet /{s/.*addr://;s/ .*//;p}')
mem=$(free -tm | sed -n '/Total/{s/.* //;p}')
load=$(cat /proc/loadavg)
echo $host,$ip,$mem,$load
The hostname command with the -s option prints out the short hostname, that is without the domain name. Strip out the -s option if you want the complete hostname. To extract the data (or better to remove the unwanted parts) from the ifconfig and free output I used sed in alternative to awk or parameter substitution. Note that the total free memory is comprehensive of the swap memory.
 
Old 01-12-2009, 03:44 AM   #8
mmahulo
Member
 
Registered: Nov 2008
Posts: 31

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by colucix View Post
Here is how I would do:
Code:
#!/bin/bash
host=$(hostname -s)
ip=$(/sbin/ifconfig eth0 | sed -n '/inet /{s/.*addr://;s/ .*//;p}')
mem=$(free -tm | sed -n '/Total/{s/.* //;p}')
load=$(cat /proc/loadavg)
echo $host,$ip,$mem,$load
The hostname command with the -s option prints out the short hostname, that is without the domain name. Strip out the -s option if you want the complete hostname. To extract the data (or better to remove the unwanted parts) from the ifconfig and free output I used sed in alternative to awk or parameter substitution. Note that the total free memory is comprehensive of the swap memory.

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

Last edited by mmahulo; 01-12-2009 at 04:19 AM.
 
Old 01-12-2009, 04:41 AM   #9
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
To understand the sed command consider the following:
Code:
$ free -tm
             total       used       free     shared    buffers     cached
Mem:          1011        653        357          0         55        342
-/+ buffers/cache:        255        755
Swap:         2047         36       2010
Total:        3058        689       2368
from this output you want to extract the total memory from the last line. In my example I extracted just the last field "2368". So I tell to sed: for each line containing "Total" remove anything till the last blank space and print out the rest:
Code:
sed -n '/Total/{s/.* //;p}'
where /Total/ is the sed address (the commands inside brackets are executed only for those lines that match the regular expression). The command is the substitution of any number of characters .* followed by a space with nothing (that is remove this part of the line). Take in mind that a dot matches any single character and the asterisk means any number (zero included) of the preceding expression. The p command (separated by semi-colon) is to print what remains of the line after the substitution.

If you want the total memory, swap included you can do something like
Code:
free -tm | sed -n '/Total/{s/Total: *//;s/ .*//;p}'
that is first remove the first part containing Total and the following blank spaces, then remove the last part from the first blank space to the end. Or just simply use awk
Code:
free -tm | awk '/Total/{print $2}'
if you're interested in the physical memory only, just change Total with Mem in the commands above. Or just parse the content of /proc/meminfo (dividing by 1000 if you want the output in Mb)
Code:
awk '/MemTotal/{printf "%d\n",$2/1000}' /proc/meminfo
 
Old 01-12-2009, 05:30 AM   #10
mmahulo
Member
 
Registered: Nov 2008
Posts: 31

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by colucix View Post
To understand the sed command consider the following:
Code:
$ free -tm
             total       used       free     shared    buffers     cached
Mem:          1011        653        357          0         55        342
-/+ buffers/cache:        255        755
Swap:         2047         36       2010
Total:        3058        689       2368
from this output you want to extract the total memory from the last line. In my example I extracted just the last field "2368". So I tell to sed: for each line containing "Total" remove anything till the last blank space and print out the rest:
Code:
sed -n '/Total/{s/.* //;p}'
where /Total/ is the sed address (the commands inside brackets are executed only for those lines that match the regular expression). The command is the substitution of any number of characters .* followed by a space with nothing (that is remove this part of the line). Take in mind that a dot matches any single character and the asterisk means any number (zero included) of the preceding expression. The p command (separated by semi-colon) is to print what remains of the line after the substitution.

If you want the total memory, swap included you can do something like
Code:
free -tm | sed -n '/Total/{s/Total: *//;s/ .*//;p}'
that is first remove the first part containing Total and the following blank spaces, then remove the last part from the first blank space to the end. Or just simply use awk
Code:
free -tm | awk '/Total/{print $2}'
if you're interested in the physical memory only, just change Total with Mem in the commands above. Or just parse the content of /proc/meminfo (dividing by 1000 if you want the output in Mb)
Code:
awk '/MemTotal/{printf "%d\n",$2/1000}' /proc/meminfo

thank you very much sir. now i get the picture. thanks again
 
Old 01-12-2009, 12:09 PM   #11
makuyl
Senior Member
 
Registered: Dec 2004
Location: Helsinki
Distribution: Debian Sid
Posts: 1,107

Rep: Reputation: 54
Quote:
Originally Posted by mmahulo View Post
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...
That one's easy to explain. I told awk to print the fourth column of the fifth line.
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.
 
Old 01-12-2009, 12:35 PM   #12
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,882

Rep: Reputation: 4988Reputation: 4988Reputation: 4988Reputation: 4988Reputation: 4988Reputation: 4988Reputation: 4988Reputation: 4988Reputation: 4988Reputation: 4988Reputation: 4988
IP=$(hostname -i)

may save you all that messing with ifconfig and sed to pull the ip address out.
 
Old 01-12-2009, 03:40 PM   #13
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by GazL View Post
IP=$(hostname -i)

may save you all that messing with ifconfig and sed to pull the ip address out.
On my system it does not display the IP of the eth0 interface, but that one of the localhost, e.g. 127.0.0.1
 
Old 01-12-2009, 04:14 PM   #14
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,882

Rep: Reputation: 4988Reputation: 4988Reputation: 4988Reputation: 4988Reputation: 4988Reputation: 4988Reputation: 4988Reputation: 4988Reputation: 4988Reputation: 4988Reputation: 4988
Quote:
Originally Posted by colucix View Post
On my system it does not display the IP of the eth0 interface, but that one of the localhost, e.g. 127.0.0.1
It should return whatever the hostname resolves to. It works correctly on my system. I think some distros put the hostname on the loopback entry in /etc/hosts in order to ensure that it can always be resolved, especially if they're using a dhcp setup to get an address, so yes, on reflection, that's something to watch out for. Good spot Colucix.

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.
 
Old 01-12-2009, 04:42 PM   #15
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by GazL View Post
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.
Good point. Apparently you have to know a priori which interface to rely on. Unless there is a way to know which is the external interface and which is (are) the internal one(s), other than check the address itself after its extraction, of course. Moreover, there are a lot of different types of network interface. For example having my system at home connected via DSL modem and running OpenSuse I have
Code:
dsl0      Link encap:Point-to-Point Protocol
eth0      Link encap:Ethernet
lo        Link encap:Local Loopback
wlan0     Link encap:Ethernet
wmaster0  Link encap:UNSPEC
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Old/Tough PC. rvijay Linux - General 9 02-02-2005 08:59 AM
Anyone know Cg? How tough is it? R00ts Programming 0 11-11-2004 01:11 PM
Tough One gsibble Linux - General 1 02-01-2004 04:06 PM
Here's a tough one... GameboyHippo Linux - Software 4 10-21-2003 04:35 PM
Ok this one is a tough one so let's see what ya got! antihero Programming 2 08-29-2001 06:32 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:02 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration