Output of top command using a cgi/perl script

Hi,

I need to do the following

Step 1: issue the command ps -ef | grep kdesktop
Step 2: Get the PID of process kdesktop from the output of step 1
Step 3: issue top -p <PID in step 2>
Step 4: send all the output generated by Step 3 as an output to a cgi script so that alll the output can be seen from a web page

Please help

-----------------------

Step 1:

[root@xxxxxxxx test]# ps -ef | grep kdesktop
alpha    25334     1  0  2010 ?        00:00:01 kdesktop
root     32366 30982  0 23:59 pts/8    00:00:00 grep kdesktop

Step 2: PID=25334

Step 3: top -p 25334

top - 00:00:48 up 149 days,  6:59,  9 users,  load average: 0.78, 0.57, 0.32
Tasks:   1 total,   0 running,   1 sleeping,   0 stopped,   0 zombie
Cpu(s):  2.8%us,  0.3%sy,  0.0%ni, 95.6%id,  1.0%wa,  0.0%hi,  0.2%si,  0.0%st
Mem:  264354224k total, 140420860k used, 123933364k free,  3379764k buffers
Swap: 16779884k total,   320340k used, 16459544k free, 102230924k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
25334 alpha     15   0  176m  12m 8908 S  0.0  0.0   0:01.84 kdesktop

Step 4: Output of the cgi scritp commandstatus.cgi

<?xml version="1.0"?>
<process>    
<PID>25334</PID>    
<USER>alpha</USER>
........
<COMMAND>kdesktop</COMMAND>
</process>
anshumaEngineeringAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

wilcoxonCommented:
This should do what you want as a single cgi script.  If not, please let me know what you need different.

Your question says you want it displayed in a browser where HTML is natural but your sample shows XML (but your question doesn't specify such).  Is XML output a requirement?  If so, I can modify the script to display XML rather than HTML in the browser.
#!/usr/local/bin/perl

use strict;
use warnings;
use CGI;
use CGI::Carp qw(fatalsToBrowser);

my $q = new CGI;

my $ps = `ps -ef | grep kdesktop`;
my ($user, $pid) = split /\s+/, $ps;

my $top = `top -p $pid`;

print $q->header, $q->start_html,
      $q->table(
          $q->Tr($q->td(['pid',$pid])),
          $q->Tr($q->td(['user',$user]))
          $q->Tr($q->td(['command','kdesktop']))),
      $q->pre($top),
      $q->end_html;

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
anshumaEngineeringAuthor Commented:
Hi Wil,

this is what I get, any ideas why

[root@xxxxxx test]# perl memstatus.cgi
[Sun Apr 10 09:47:37 2011] memstatus.cgi: Scalar found where operator expected at memstatus.cgi line 19, near ")
[Sun Apr 10 09:47:37 2011] memstatus.cgi:           $q"
[Sun Apr 10 09:47:37 2011] memstatus.cgi:       (Missing operator before $q?)
Content-type: text/html

<h1>Software error:</h1>
<pre>syntax error at memstatus.cgi line 19, near &quot;)
          $q&quot;
Execution of memstatus.cgi aborted due to compilation errors.
</pre>
<p>
For help, please send mail to this site's webmaster, giving this error message
and the time and date of the error.

</p>
[Sun Apr 10 09:47:37 2011] memstatus.cgi: syntax error at memstatus.cgi line 19, near ")
[Sun Apr 10 09:47:37 2011] memstatus.cgi:           $q"
[Sun Apr 10 09:47:37 2011] memstatus.cgi: Execution of memstatus.cgi aborted due to compilation errors.
0
wilcoxonCommented:
Sorry.  I missed a comma on line 18.  If you add a comma to the end of the line, it should work.
0
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

anshumaEngineeringAuthor Commented:
thanks, still the code is not giving the correct output

here's the manual run of ps -ef

[root@xxxx test]# ps -ef | grep kdesktop
root      9527  9098  0 17:13 pts/8    00:00:00 grep kdesktop
alpha    25334     1  0  2010 ?        00:00:01 kdesktop

but the output is this

pid 9531
user apache
command kdesktop

also whenever I refresh the cgi file in broswer the PID changes. So something is not right
0
wilcoxonCommented:
Ah.  Try adding "| grep -v grep" to the command in backticks on line 10.  Odds are it is picking up the grep kdesktop command pid rather than the kdesktop command itself and then top returns nothing because the grep is already finished.
0
anshumaEngineeringAuthor Commented:
hi wil, that worked. You rock. Still I am not getting the memory counters etc from the output of line 13 in your code

so either line 13 is not working or else line 20 is not working

so I need to see evrything like VIRT , RES , %cpu etc
0
wilcoxonCommented:
It may be that top is not in the path for your cgi environment.  If you run it from the command line, do you see any top results?  If you see them manually and not in the browser, you may need to either explicitly use the path to top or add the path to top in PATH.
0
wilcoxonCommented:
You can also add this line just after the top call:

die "top failed: $?" if $?;

That should give you the error code of the failure if it is not working (however, it can be less than helpful).
0
anshumaEngineeringAuthor Commented:
I can run top from the command line

Here's the output of the debugging code you asked for

Software error:
top failed: 256 at /var/www/cgi-bin/test/memstatus.cgi line 14.
For help, please send mail to the webmaster (root@localhost), giving this error message and the time and date of the error.
0
wilcoxonCommented:
Hmm.  I thought $? worked for backticks as well as system but maybe not.  256 indicates everything should have worked (if $? does work) - no signal, no coredump, child exited with 0.

What happens if you change the top call to be (think this is correct syntax):

open(TOP, "top -p $pid |") or die "could not open top: $!";
my $top = join '', <TOP>;
close(TOP) or die "could not close top: $!";
0
wilcoxonCommented:
You can run top from the command line.  What output do you get if you run memstatus.cgi from the command line?  Can you post the exact output?  That should show if the problem is with the code itself or with calling top from a cgi script.
0
anshumaEngineeringAuthor Commented:
if I do perl memstatus.cgi then the code just hangs on command line.

Also I tried giving the absolute path of top /usr/bin/top but that doesn't work either. Strange thing is that apache can run one command (ps) but it cannot run top :-(
0
anshumaEngineeringAuthor Commented:
when I do this

open(TOP, "top -p $pid |") or die "could not open top: $!";
my $top = join '', <TOP>;
close(TOP) or die "could not close top: $!";

I get the following output

Software error:

could not close top:  at /var/www/cgi-bin/test/memstatus.cgi line 15.

For help, please send mail to the webmaster (root@localhost), giving this error message and the time and date of the error.
0
wilcoxonCommented:
It's very odd that running memstatus.cgi hangs on the command line.  It should return almost immediately and output some html.  The only reason I can think of would be if your top does not support non-interactive calls.  Can you post the summary section of your man page for top?  Can you do "top -p some_valid_pid > out.txt" and have top exit (not hang) and put the output in out.txt?

Okay.  No helpful error.  Remove the "or die" from the close(TOP) line and see if it works (it should no longer die but it may not have any output still).
0
anshumaEngineeringAuthor Commented:
thanks wilcoxon
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.