Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
running "top" from php on apache2
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Networking & Security
View previous topic :: View next topic  
Author Message
mastabog
n00b
n00b


Joined: 13 Mar 2004
Posts: 59

PostPosted: Mon May 31, 2004 8:02 pm    Post subject: running "top" from php on apache2 Reply with quote

Hey all,

Does anyone know why i can't execute "top" from within php, but i can execute a bunch other shell commands (like "free").

I'm trying to run top with -n 1 -b (meaning for 1 second, and just output for batch) with:
Code:
$output = `top -n 1 -b`;
and it simply doesn't work.

If i try the passthru() function, the exit code is 1 ... Like I said above, i can very well execute "free" with the same:
Code:
$output = `free -k`;
.. and it works flawlessly.

I saw that both free and top files in /usr/bin have the exact same user rights and attributes. I even tried sudo-ing it "sudo /usr/bin/top -n 1 -b" after i corectly set up /etc/sudoers to allow apache to do that (apache ALL=NOPASSWD: /usr/bin/top -n 1 -b).

If this is impossible for some weird reason, what other methods are there to obtain some real-time CPU usage under linux (with the total and user usage like top provided, or at least total)? I know about /proc/loadavg, but that file lists the average usage in the last 1,5 and 10 minutes .. i wanted something more close to 1 seconds like "top" does.

Any ideas? thanks a lot.
Back to top
View user's profile Send private message
mastabog
n00b
n00b


Joined: 13 Mar 2004
Posts: 59

PostPosted: Sat Jun 05, 2004 10:40 pm    Post subject: Reply with quote

Ah, cmon guys ... a lil help here please :)

Oh .. i mention that I did manage to do that (without any prior preparation) on an apache2 server under redhat9.

edit: i have an aswer for the question about other methods to get the cpu % ... parsing /proc/stat (with a delay of 1 second). Still need an aswer t the "top" question because there are other commands that behave the same ...


Last edited by mastabog on Sun Jun 06, 2004 12:39 am; edited 1 time in total
Back to top
View user's profile Send private message
Chris W
l33t
l33t


Joined: 25 Jun 2002
Posts: 972
Location: Brisbane, Australia

PostPosted: Sun Jun 06, 2004 12:26 am    Post subject: Reply with quote

This problem is almost certainly environmental. The following sample script dies:
Code:
<html>
<head>
        <title>Test</title>
</head>
<body>
  <p>
    <?php
      $output=`/usr/bin/top -n 1 -b`;
      echo $output;
    ?>
  </p>
</body>
</html>
with the resulting error in /var/log/apache2/error_log:
Code:
TERM environment variable not set.
Using;
Code:
$output=`TERM=dumb /usr/bin/top -n 1 -b`;
gives better , but by no means pretty, results.

I guess the way that RedHat starts Apache gives it an environment to inherit.
_________________
Cheers,
Chris W
"Common sense: The collection of prejudices acquired by age 18." -- Einstein
Back to top
View user's profile Send private message
mastabog
n00b
n00b


Joined: 13 Mar 2004
Posts: 59

PostPosted: Sun Jun 06, 2004 12:43 am    Post subject: Reply with quote

thanks chris

under redhat9, apache2 runs as "nobody", whereas under gentoo apache2 runs as "apache" which from /etc/passwd has a /bin/false as a shell login.

How can I set/change the environment for a daemon like apache under gentoo so it has a $TERM set? (im far from a linux guru)

edit: under gentoo, the only $_ENV[] variable set in php is PATH .. no TERM or anything else. Will check later on redhat9 (killed that box ...)
Back to top
View user's profile Send private message
Chris W
l33t
l33t


Joined: 25 Jun 2002
Posts: 972
Location: Brisbane, Australia

PostPosted: Sun Jun 06, 2004 12:53 am    Post subject: Reply with quote

There's no really pretty way. Modify the /etc/init.d/apache2 script from:
Code:
        env -i PATH=$PATH /sbin/start-stop-daemon --quiet \
                --start --startas /usr/sbin/apache2 \
                --pidfile ${PIDFILE} -- -k start ${APACHE2_OPTS}
to
Code:

        env -i PATH=$PATH TERM=dumb /sbin/start-stop-daemon --quiet \
                --start --startas /usr/sbin/apache2 \
                --pidfile ${PIDFILE} -- -k start ${APACHE2_OPTS}
The 'env -i' command is responsible for starting Apache with a minimal environment. This is done for security reasons, so there is a potential risk in adding too much to the environment.

If your application will be deployed to another machine then it is possibly best not to rely on the inherited environment and set it directly in your PHP:
Code:
putenv('TERM', 'dumb');
or similar.
_________________
Cheers,
Chris W
"Common sense: The collection of prejudices acquired by age 18." -- Einstein


Last edited by Chris W on Sun Jun 06, 2004 1:01 am; edited 1 time in total
Back to top
View user's profile Send private message
mastabog
n00b
n00b


Joined: 13 Mar 2004
Posts: 59

PostPosted: Sun Jun 06, 2004 1:00 am    Post subject: Reply with quote

thanks again chris ... should have thought about the apache daemon's parameters :)

the thing is that i am building a web frontend to a scalable cluster in php5 and i needed to run some commands from apache (running on the cluster's server), commands that connect to the nodes to get status, etc ...

"top" was just for testing. I'm still having issues with others like wulflogger (which I badly need). I will get my redhat9 machine up and running again and will check what environment was passed to apache and available in php (cus there it works)

again, many thanks
Back to top
View user's profile Send private message
Chris W
l33t
l33t


Joined: 25 Jun 2002
Posts: 972
Location: Brisbane, Australia

PostPosted: Sun Jun 06, 2004 1:02 am    Post subject: Reply with quote

I edited the post above to add a bit and your reply beat me :)
_________________
Cheers,
Chris W
"Common sense: The collection of prejudices acquired by age 18." -- Einstein
Back to top
View user's profile Send private message
mastabog
n00b
n00b


Joined: 13 Mar 2004
Posts: 59

PostPosted: Sun Jun 06, 2004 1:06 am    Post subject: Reply with quote

Heh .. good idea with putenv() (i almost forgot about it). Thanks.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Networking & Security All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum