Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'GeekTool - Useful and fun info on the desktop' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
GeekTool - Useful and fun info on the desktop
Authored by: GalainHH on Jul 07, '04 05:54:38AM

Does anybody know, how to Display a particular piece of the top output?

[code]top -l 1 | fgrep "CPU usage"[/code] displays the whole line, and additionally the idle value is wrong: it shows 0,0 %

I would like a display of a single value: current total CPU Usage.

Does anybody know the code?

GalainHH



[ Reply to This | # ]
GeekTool - Useful and fun info on the desktop
Authored by: chris_on_hints on Jul 07, '04 06:13:22AM

Can i suggest using MenuMeters? It does CPU, disk, network and memory useage in the menu bar... very nicely too.

sorry that i cant help with the 'top' command - grep just makes my head hurt!!



[ Reply to This | # ]
GeekTool - Useful and fun info on the desktop
Authored by: uff on Jul 07, '04 08:13:39AM

top -l 1 | fgrep "CPU usage" | awk '{print "cpu usage: ", $8}'
or $9 if you want the sys cpu usage%

[ Reply to This | # ]
GeekTool - Useful and fun info on the desktop
Authored by: robg on Jul 07, '04 08:15:29AM
This worked for me:
top -l 1 | grep 'CPU usage' | cut -c 33-80
The cut -c 33-80 bit displays only columns 33 to 80 of the grep result; 33 is where the phrase "CPU usage" starts, and 80 is about at the end of the line...

I tested with GeekTool, and it's working quite nicely -- though I do use MenuMeters, as suggested in another post...

-rob.

[ Reply to This | # ]
Does not work correctly
Authored by: GalainHH on Jul 08, '04 05:50:49AM
Thanks for the replies:

1) I can?t use another menubar item, my menubar is overcrowded: Meteorologist, ABMenu, Konfabulator, Timbuktu, Menu Calendar, Bluetooth PrefMenu, AppleScript Menu, Airport PrefMenu, Date and Time

I need a second Menubar!

Konfabulator has a widget, which makes the Job that Geektool should do in future, as I dont like Konfabulator and have only a demo Version runnig

2) top -l 1 | fgrep "CPU usage" | awk '{print "cpu usage: ", $8}'

Does only show the current User CPU Usage, not the sum of User and System Usage

3) top -l 1 | grep 'CPU usage' | cut -c 33-80

Shows 3 Values: The Idle Value is always 0,0, as the sum of User and CPU usage is always 100%

I think this is because top itself produces a high cpu usage, which is measured at the time, when invoked.

Sounds a bit confusing: A command, that shall display the CPU usage produces so much cpu load, that the result is falsified

GalainHH

[ Reply to This | # ]
Does not work correctly
Authored by: ToMaToTaChi on Jul 08, '04 06:48:28PM
just needs a slight modification :)
 top -dl2 | grep sage | cut -c 33-88 | tail -1 
Oh, but that wasn't good enough nope nope. :)

top -dl2 | grep "sage" | cut -c 44-71 | tail -1 | awk '{ printf "CPU Time\nUsed: %04.2f%%\nIdle: %04.2f%%\n", (100 - $5), $5; }'
Well, I liked that fine, it could be a little more fun
So:

top -dl2 | grep "sage" | cut -c 44-71 | tail -1 | awk '{
  pe = 30;
  pa = "[]";
  pb = "--";

  z[0] = 100 - $5;
  z[1] = $5;
  z[2] = "Used";
  z[3] = "Idle";

  for( foo = 0; foo < 2; foo++ )
  {
    zzz = ( pe * z[foo] / 100) + 0;
    zzy = ( pe - zzz) + 0;
    zza = "\000";
    zzb = "\000";
    
    for( bar = 0; bar < zzz; bar++ )
      { zza = zza pa }
    for( bar = 0; bar < zzy; bar++ )
      { zzb = zzb pb }
    printf "%s %04.2f%% %s%s\n", z[foo+2], z[foo], zza, zzb; 
  }
}'

note, geektool does not appreciate line brakes; strip them prior to pasting. I posted like this for the sake of legibility -- it's down right painful to read all clumped on a single line.

-- one thing that bummed me out was geektool did not like my echo -e \033[##m coloration, inversion, etc. I had the thing looking pretty spiffy in terminal.app running bash. Bah. all yellow and red, it's pretty :D

Otherwise i'm having too much fun with this thing!



[ Reply to This | # ]
Exactly what I wanted
Authored by: GalainHH on Jul 09, '04 02:47:27AM

Thanks a lot,

your second code line does exactly what I wanted.

Thank you
GalainHH



[ Reply to This | # ]
Does not work correctly
Authored by: Eravau on Jul 08, '04 06:50:12PM
To get around the "top always being the process hog" problem, I have my script run top with two samples. By the second sample, top has generally calmed down a little and isn't being the processor hog that it always is on launch. My script spits out the Uptime, Current Load Average, Total CPU Use (user + system), and the current CPU Hog Process. Take it and modify to taste:

#!/usr/bin/perl

$top_cmd     = "top -o cpu -l 2 -s 3 -n 1";
$uptime_cmd  = "uptime";
@top         = `$top_cmd`;
$uptime      = `$uptime_cmd`;

$count = scalar @top;

foreach $line_no (10..$count) {
        $line     = $top[$line_no];
        if ($line =~ /Load Avg.\s+(\d+\.\d+)\,\s+(\d+\.\d+)\,\s+(\d+\.\d+)\s+CPU usage.\s+(\d+\.\d+)\% user,\s+(\d+\.\d+)\% sys,\s+(\d+\.\d+)\% idle/) {
                $load_avg = $1;
                $load_rec = $2;
                $load_now = $3;
                $cpu_usr  = $4;
                $cpu_sys  = $5;
                $cpu_idl  = $6;
                $cpu_use  = $cpu_usr + $cpu_sys;
        }
        elsif ($line =~ /PID/) {
                $find_hog = 1;
        }
        elsif ($find_hog) {
                if ($line =~ /\s?\d+\s+(\S+(\s\S+)?)\s+(\d+.\d+)\S\s+\d+.\d\d.\d+/) {
                        $cpu_hog_name = $1;
                        $cpu_hog_prct = $3;
                }
        }
}
if ($uptime =~ /up\s+(\d+)\sday[s]?.\s+(\d+).(\d\d).\s+\d\susers/) {
        $days_up      = $1;
        $hours_up     = $2;
        $mins_up      = $3;
        $formatted_up = $days_up . "d:" . $hours_up . "h:" . $mins_up . "m";
}

print "Up Time:         $formatted_up\n";
print "Current Load:    $load_now\n";
print "Total CPU Use:   $cpu_use\%\n";
print "CPU Hog Process: $cpu_hog_name \@ $cpu_hog_prct\%\n";

exit;
There are a few other variables in there if you'd like to pull those out and display them as well. I just didn't really want to see them myself.

[ Reply to This | # ]