Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Im getting forgien characters when I email the output from "top" 1

Status
Not open for further replies.

john99999

Instructor
Apr 29, 2005
73
0
0
US
When I email the output of top to my self it contains a bunch of forgien characters that dont appear if I just output it to the screen. How can I get it not to show thos characters? IE start with "top" and not "[?7ltop"

[?7ltop - 09:56:47 up 6 days, 5:38, 2 users, load average: 0.60, 0.61, 0.62
Tasks: 261 total, 1 running, 232 sleeping, 0 stopped, 28 zombie
Cpu(s): 3.5% us, 1.0% sy, 0.1% ni, 90.5% id, 4.6% wa, 0.0% hi, 0.2% si
Mem:  1034712k total, 1009904k used, 24808k free, 80224k buffers
Swap: 2096440k total, 52184k used, 2044256k free, 470088k cached
 
They're escape codes possibly for an Ansi terminal, you could try changing your terminal type, or look at a module to get the info for you. Do you have SNMP installed on the server

Alternatively you could use a few regexes to parse out the control sequences

This might be what you're looking for, an ANSI parser

HTH
--Paul


cigless ...
 
Thanks, I installed that but I getting this error:
# ./load.pl
Can't locate object method "new" via package "Image::ANSI::parser" (perhaps you forgot to load "Image::ANSI::parser"?) at ./loadlog.pl line 11.

Im my code I have:
$top2=qx[top -n1];
my $parser = Image::ANSI::parser->new;
$top = $parser->parse( string => $top2 );

Do you know why Im getting that error?
 
Fixed it, I added
use Image::ANSI::parser;

But now it says:
Use of uninitialized value in split at /usr/lib/perl5/site_perl/5.8.3/Image/ANSI/Parser.pm line 139.
Can't call method "attr" on an undefined value at /usr/lib/perl5/site_perl/5.8.3/Image/ANSI/Parser.pm line 389.

Any ideas?
 
Parsers can be fun to play with, but you could always run
Code:
$top2=qx[top -n1 -b];
to suppress the control characters, and stop messing about with parsers altogether...
 
Thanks that did it!

But it gives like 500 processes, is there a way to get it to return just 50?
 
Nope. But you could
Code:
my @top2 = qx[top -n1 -b];
and take the 1st 50 lines of the array, or even pipe the output of top to head (you'll have to rtfm for head syntax, I don't have access to a manpage for it just now)
 
Found it:
Code:
my $top2 = qx[top -n1 -b | head -50];
You might have to tweak the '50' a bit to account for heading lines...
 
This is working ok, when I run the script through the commandline, but through a cronjob, it does not return anything for top? Why will this work through commandline and not through cron?
 
privs, what uid is the cron job running under?

cigless ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top