Unix/Linux Go Back    


Gentoo Gentoo Linux is a versatile and fast, completely free Linux distribution geared towards developers and network professionals.

cpu%/mem% usage, scripting, dzen2: howto learn bash the hard way

Gentoo


Closed    
 
Thread Tools Search this Thread Display Modes
    #8  
Old Unix and Linux 08-05-2008   -   Original Discussion by broli
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
 
Join Date: Mar 2008
Last Activity: 28 March 2011, 6:41 AM EDT
Location: /there/is/only/bin/sh
Posts: 3,653
Thanks: 0
Thanked 11 Times in 9 Posts
Another couple of notes about the use of a temporary file.

First off, if you will never use the first few lines of the temp file, might as well throw them away already at the start.


Code:
top -b -n 1 | tail -n +8 > /tmp/salidatop

Secondly, when you are done, you should remove your temporary file. Better yet, remove it even if you are interrupted.


Code:
trap 'rm -f /tmp/salidatop; exit $?' 0
trap 'exit 127' 1 2 3 5 15

Put those lines near the beginning of the script.

Properly speaking, you should probably use something like mktemp to generate a unique, unpredictable temporary file name. There are security issues with using predictable names, and having a static file name means you can't run two instances of the script at the same time.
Sponsored Links
    #9  
Old Unix and Linux 08-05-2008   -   Original Discussion by broli
broli's Unix or Linux Image
broli broli is offline
Registered User
 
Join Date: Dec 2007
Last Activity: 23 July 2014, 5:32 PM EDT
Location: Argentina
Posts: 219
Thanks: 0
Thanked 0 Times in 0 Posts
[QUOTE=era;302221664]Another couple of notes about the use of a temporary file.

First off, if you will never use the first few lines of the temp file, might as well throw them away already at the start.


Code:
top -b -n 1 | tail -n +8 > /tmp/salidatop

[quote]
i will use those lines to have the iddle% and the amount of memory
Quote:
Originally Posted by era View Post
Secondly, when you are done, you should remove your temporary file. Better yet, remove it even if you are interrupted.


Code:
trap 'rm -f /tmp/salidatop; exit $?' 0
trap 'exit 127' 1 2 3 5 15

Put those lines near the beginning of the script.

Properly speaking, you should probably use something like mktemp to generate a unique, unpredictable temporary file name. There are security issues with using predictable names, and having a static file name means you can't run two instances of the script at the same time.
its by design.

about the security risk, i decided that beeing my personal laptop, always in a secure network, and only the contents of top, that is not thread enough to consider some extra precausions
also, my /tmp is wiped out on poweroff and poweron

the script needs X to run, and the script is run by my user config files (fluxbox startup), so there wont be any other instance.
but deniying the posibilitie is a bad idea.
maybe using $$ in some part of the file can do the trick
and adding the trap to make sure i dont left behind tons of temporal files.
Sponsored Links
    #10  
Old Unix and Linux 08-10-2008   -   Original Discussion by broli
broli's Unix or Linux Image
broli broli is offline
Registered User
 
Join Date: Dec 2007
Last Activity: 23 July 2014, 5:32 PM EDT
Location: Argentina
Posts: 219
Thanks: 0
Thanked 0 Times in 0 Posts
this is what i have now


Code:
      #!/bin/bash

      while :
      do
              top -b -n 1 > /tmp/salidatop

              cpuid=`awk ' NR==3 { print $5 }'  /tmp/salidatop`
              echo -n "^fg(green)CPU $cpuid ** "
              awk 'NR == 8, NR == 12 { printf "[^fg(cyan)%s(^fg(red)%s^fg(green)]--",$12,$9 }' /tmp/salidatop
              echo ">>"
       
              echo -n "^fg(green)MEM ** "
              sort -r -n -k10 /tmp/salidatop | awk 'NR<=4 { printf "[^fg(cyan)%s(^fg(red)%s^fg(green)]--",$12,$10 } '
              echo ">>"
       
              sleep 4
       
      done | dzen2 -ta l -u -l 1 -x 20 -y 710 -w 660 -e 'onstart=lower,uncollapse'

and i dont know how to get the info of the memory.
i need to do this math
(( free + buffers + cached) * 100)/total

but i dont know how to get those fields in one awk (and maybe use the internal math?)
thanks again
    #11  
Old Unix and Linux 08-11-2008   -   Original Discussion by broli
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
 
Join Date: Mar 2008
Last Activity: 28 March 2011, 6:41 AM EDT
Location: /there/is/only/bin/sh
Posts: 3,653
Thanks: 0
Thanked 11 Times in 9 Posts
Something like this maybe.


Code:
#!/bin/sh

while :
do
    top -b -n -1 |
    awk 'NR > 5 { mem[$12] = $10; }
        NR==3 { printf "^fg(green)CPU " $5 " ** "; next; }
        NR==4 { total=$2; free=$6; buffers=$8; next; }
        NR==5 { cached=$8; next; }
        NR==8, NR==12 { printf "[^fg(cyan)" $12 "(^fg(red)" $9 "^fg(green)]--"; next; }
        NR==13 { printf ">>\n"; next; }
        END { ... sort and extract top 4 items from mem array here ...;
            printf "%2.2f\n", (free+buffers+cached)/total*100 }'
    sleep 4
done | dzen2 --options

I haven't finished the mem part but it's not too complex; you should be able to implement the remaining part of the awk script and sort and pick the output lines you want by searching the forums a bit. The mawk manual page has an example of how to implement a simple sort in awk.

My top prints "Mem: total used free buffers" followed by "Swap: total used free cached" on lines 4 and 5; I'm not entirely sure which of the "free" and "total" fields you want, but extracting the fields you want (provided you are sure which ones you want) should be similarly straightforward. I've put in placeholders for those. A complication is that the output has a human-readable suffix like "k" which I imagine might vary, so properly you should parse that before doing the math on the resulting numbers.

Last edited by era; 08-11-2008 at 04:20 AM..
Sponsored Links
    #12  
Old Unix and Linux 08-11-2008   -   Original Discussion by broli
broli's Unix or Linux Image
broli broli is offline
Registered User
 
Join Date: Dec 2007
Last Activity: 23 July 2014, 5:32 PM EDT
Location: Argentina
Posts: 219
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by era View Post
Something like this maybe.


Code:
#!/bin/sh

while :
do
    top -b -n -1 |
    awk 'NR > 5 { mem[$12] = $10; }
        NR==3 { printf "^fg(green)CPU " $5 " ** "; next; }
        NR==4 { total=$2; free=$6; buffers=$8; next; }
        NR==5 { cached=$8; next; }
        NR==8, NR==12 { printf "[^fg(cyan)" $12 "(^fg(red)" $9 "^fg(green)]--"; next; }
        NR==13 { printf ">>\n"; next; }
        END { ... sort and extract top 4 items from mem array here ...;
            printf "%2.2f\n", (free+buffers+cached)/total*100 }'
    sleep 4
done | dzen2 --options

I haven't finished the mem part but it's not too complex; you should be able to implement the remaining part of the awk script and sort and pick the output lines you want by searching the forums a bit. The mawk manual page has an example of how to implement a simple sort in awk.

My top prints "Mem: total used free buffers" followed by "Swap: total used free cached" on lines 4 and 5; I'm not entirely sure which of the "free" and "total" fields you want, but extracting the fields you want (provided you are sure which ones you want) should be similarly straightforward. I've put in placeholders for those. A complication is that the output has a human-readable suffix like "k" which I imagine might vary, so properly you should parse that before doing the math on the resulting numbers.
yeah, this thing is growing faster than my speed to read awk info :P
i will look at that and run some tests
the problem, is that all this work only o get a simple percentage of free mem, is growing in complexity and footprint
to a point where i dont know if its worth the problem
maybe i can make use of an external program to give me that info.
maybe doit myself in C? .... (actually, stole the related code of wmmem )
Sponsored Links
    #13  
Old Unix and Linux 08-12-2008   -   Original Discussion by broli
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
 
Join Date: Mar 2008
Last Activity: 28 March 2011, 6:41 AM EDT
Location: /there/is/only/bin/sh
Posts: 3,653
Thanks: 0
Thanked 11 Times in 9 Posts
A single awk script is probably way more efficient than the multiple awks and sorts and echos and what not you had before, that's why I'm proposing it.

If you can fork the code from top to give you exactly the output you want, it will of course be more efficient still, but IMHO probably not worth the programmer effort.

Maybe if you could add a module to top to produce XML or CSV or something else more machine-readable, that could perhaps be worth it.
Sponsored Links
    #14  
Old Unix and Linux 08-12-2008   -   Original Discussion by broli
broli's Unix or Linux Image
broli broli is offline
Registered User
 
Join Date: Dec 2007
Last Activity: 23 July 2014, 5:32 PM EDT
Location: Argentina
Posts: 219
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by era View Post
A single awk script is probably way more efficient than the multiple awks and sorts and echos and what not you had before, that's why I'm proposing it.

If you can fork the code from top to give you exactly the output you want, it will of course be more efficient still, but IMHO probably not worth the programmer effort.

Maybe if you could add a module to top to produce XML or CSV or something else more machine-readable, that could perhaps be worth it.
i see that using a single awk would be faster, but im still working to uderstand how to doit.

eventually i hope my script is a while loop (needed only for dzen) and an awk calling a script file with the -f parameter.

and what you mean by "if you could add a module to top"
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Should I learn bash scripting or is it going obsolete? KalEl Shell Programming and Scripting 9 10-31-2010 07:53 PM
HOWTO - Total memory and CPU usage ... without top? newbie_01 UNIX for Dummies Questions & Answers 2 09-21-2010 01:16 AM
Any book to learn perl scripting vkvishwakarma6 Shell Programming and Scripting 5 02-24-2010 02:05 AM
Help- To learn shell scripting tj23 Shell Programming and Scripting 2 06-20-2008 07:10 AM
newbie: way to learn more about server's resource usage blakekr UNIX for Dummies Questions & Answers 2 05-20-2008 12:43 AM



All times are GMT -4. The time now is 09:04 PM.