This is Interesting: Free Magazines for Graphics designers and webmasters  


Home > Archive > Webmaster forum > June 2005 > Server status script





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Server status script
Charles Sweeney

2005-06-24, 7:32 pm

Okay boys and girls, here's a little PHP script wot I wrote that outputs
the "top" command to your web browser screen.

For those who don't know, it's used on *nix systems and shows a snapshot of
running processes, memory usage, CPU load etc.

<?php

print "<pre>";

system("top -n1 b");

print "</pre>";

?>

"n1" is the number of iterations that the "top" command should run. "b" is
"batch" which allows the output to be written.

--
Charles Sweeney
http://CharlesSweeney.com
Richard

2005-06-25, 7:27 pm

> <?php
> print "<pre>";
> system("top -n1 b");
> print "</pre>";
> ?>


I've tried it, but it only output "<pre></pre>" ?


Els

2005-06-25, 7:27 pm

Richard wrote:

>
> I've tried it, but it only output "<pre></pre>" ?


That's the outcome on my Windows box.
On Unix, I get this:
Warning: system() has been disabled for security reasons in
/home/.....

--
Els http://locusmeus.com/
Sonhos vem. Sonhos vão. O resto é imperfeito.
- Renato Russo -
Charles Sweeney

2005-06-25, 7:27 pm

Richard wrote

>
> I've tried it, but it only output "<pre></pre>" ?


Do you have a *nix operating system?

--
Charles Sweeney
http://CharlesSweeney.com
Charles Sweeney

2005-06-25, 7:27 pm

Els wrote

> Richard wrote:
>
>
> That's the outcome on my Windows box.
> On Unix, I get this:
> Warning: system() has been disabled for security reasons in
> /home/.....


Naturally, if a command is disabled it won't work!

--
Charles Sweeney
http://CharlesSweeney.com
Els

2005-06-25, 7:27 pm

Charles Sweeney wrote:

> Els wrote
>
>
> Naturally, if a command is disabled it won't work!


I know :-)
Was just mentioning it, so Richard can see it does work on a Unix
system, even if it doesn't..

Hmm.. weird sentence <g>

--
Els http://locusmeus.com/
Sonhos vem. Sonhos vão. O resto é imperfeito.
- Renato Russo -
Charles Sweeney

2005-06-25, 7:27 pm

Els wrote

> Was just mentioning it, so Richard can see it does work on a Unix
> system, even if it doesn't..
>
> Hmm.. weird sentence <g>


It's ok, I understand!

--
Charles Sweeney
http://CharlesSweeney.com
newsposting0209@webyourbusiness.com

2005-06-25, 7:27 pm

if system is disabled in php, then use shell script instead as a
cgi-script:

#!/bin/bash
echo "content-type: text/html


<PRE>`top -n1 b`</PRE>";exit 0


(it's also the same number of lines of code - including blanks).

hth

Greg
--
http://www.nod32usa.com

Charles Sweeney

2005-06-25, 7:27 pm

wrote

> Greg


Hey good sir! How the devil are you?!

--
Charles Sweeney
http://CharlesSweeney.com
Els

2005-06-25, 7:27 pm

newsposting0209@webyourbusiness.com wrote:

> if system is disabled in php, then use shell script instead as a
> cgi-script:
>
> #!/bin/bash
> echo "content-type: text/html
>
> <PRE>`top -n1 b`</PRE>";exit 0
>
> (it's also the same number of lines of code - including blanks).
>
> hth


Well, I think using shell is also blocked on the server I tried
system() on :-)

--
Els http://locusmeus.com/
Sonhos vem. Sonhos vão. O resto é imperfeito.
- Renato Russo -
Charles Sweeney

2005-06-25, 7:27 pm

Els wrote

> newsposting0209@webyourbusiness.com wrote:
>
>
> Well, I think using shell is also blocked on the server I tried
> system() on :-)


You need to get yourself a proper server!

I think Greg was referring to the script running in shell, not your
access. Try it and find out.

--
Charles Sweeney
http://CharlesSweeney.com
William Tasso

2005-06-25, 7:27 pm

Writing in news:alt.www.webmaster
From the safety of the No thank you cafeteria
Charles Sweeney <me@charlessweeney.com> said:

> Els wrote
>
>
> You need to get yourself a proper server!


ok - proper server in place here. how to enable 'system'?

> I think Greg was referring to the script running in shell, not your
> access. Try it and find out.


how? keep it simple for absolute beginners please.

--
William Tasso
Els

2005-06-25, 7:27 pm

Charles Sweeney wrote:

>
> You need to get yourself a proper server!


No I don't, this server does its job for all my needs (server wise),
so no complaints here.

> I think Greg was referring to the script running in shell, not your
> access. Try it and find out.


Maybe so, but I find it hard to run a script in something that I don't
have access to ;-)

--
Els http://locusmeus.com/
Sonhos vem. Sonhos vão. O resto é imperfeito.
- Renato Russo -
Jerry Stuckle

2005-06-25, 7:27 pm

William Tasso wrote:
> Writing in news:alt.www.webmaster
> From the safety of the No thank you cafeteria
> Charles Sweeney <me@charlessweeney.com> said:
>
>
>
> ok - proper server in place here. how to enable 'system'?
>
>
>
> how? keep it simple for absolute beginners please.
>


Bill,

Look in your php.ini file for a line "disable_functions=...". You
should have a list of functions which are disabled - including, in this
case, system(). Remove "system" from the list.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
William Tasso

2005-06-25, 7:27 pm

Writing in news:alt.www.webmaster
From the safety of the cafeteria
Jerry Stuckle <jstucklex@attglobal.net> said:

> William Tasso wrote:
>
> Bill,
>
> Look in your php.ini file for a line "disable_functions=...". You
> should have a list of functions which are disabled - including, in this
> case, system(). Remove "system" from the list.
>


Thanks, looked in .../apache/php.ini

disable_functions=

has no entries

fwiw: disable_clases= is similarly empty

--
William Tasso
Charles Sweeney

2005-06-25, 7:27 pm

William Tasso wrote

> Writing in news:alt.www.webmaster
> From the safety of the No thank you cafeteria
> Charles Sweeney <me@charlessweeney.com> said:
>
> ok - proper server in place here. how to enable 'system'?


system() is just a PHP function.

http://uk2.php.net/manual/en/function.system.php

AFAIK it's enabled by default. Not sure how one would disable it,
php.ini?


>
> how? keep it simple for absolute beginners please.


Speaking as a beginner, just upload Greg's script as a cgi, give it
executable permission and run it?

--
Charles Sweeney
http://CharlesSweeney.com
Charles Sweeney

2005-06-25, 7:27 pm

William Tasso wrote

> Writing in news:alt.www.webmaster
> From the safety of the cafeteria
> Jerry Stuckle <jstucklex@attglobal.net> said:
>
>
> Thanks, looked in .../apache/php.ini
>
> disable_functions=
>
> has no entries


If system() isn't disabled, then my line of code will work, assuming
it's an OS that uses the "top" command.

--
Charles Sweeney
http://CharlesSweeney.com
Chris Hope

2005-06-25, 7:27 pm

Charles Sweeney wrote:

> William Tasso wrote
>
>
> system() is just a PHP function.
>
> http://uk2.php.net/manual/en/function.system.php
>
> AFAIK it's enabled by default. Not sure how one would disable it,
> php.ini?


You can disable specific functions in PHP (I can't recall off the top of
my head how to do it) and there's also something called "safe mode"
which can be enabled. Safe mode prevents calls to stuff like system()
and exec() and prevents access to files outside the webroot - stuff
like that. A well set up shared hosting server /should/ have safe mode
on but a lot of hosts don't. It's really good in a shared environment
otherwise someone could easily write a script to go nosying around in
other people's directories and find stuff like their database logins
and passwords...

[snip]

--
Chris Hope | www.electrictoolbox.com | www.linuxcdmall.com
Charles Sweeney

2005-06-25, 7:27 pm

Chris Hope wrote

> Charles Sweeney wrote:
>
> You can disable specific functions in PHP (I can't recall off the top of
> my head how to do it)


Yep, Jerry confirmed this. It's in php.ini. I hadn't seen his post when I
made mine.

--
Charles Sweeney
http://CharlesSweeney.com
Chris Hope

2005-06-25, 7:27 pm

Charles Sweeney wrote:

> Chris Hope wrote
>
>
> Yep, Jerry confirmed this. It's in php.ini. I hadn't seen his post
> when I made mine.


Likewise :)

--
Chris Hope | www.electrictoolbox.com | www.linuxcdmall.com
Jerry Stuckle

2005-06-26, 7:25 pm

Chris Hope wrote:
>
>
> You can disable specific functions in PHP (I can't recall off the top of
> my head how to do it) and there's also something called "safe mode"
> which can be enabled. Safe mode prevents calls to stuff like system()
> and exec() and prevents access to files outside the webroot - stuff
> like that. A well set up shared hosting server /should/ have safe mode
> on but a lot of hosts don't. It's really good in a shared environment
> otherwise someone could easily write a script to go nosying around in
> other people's directories and find stuff like their database logins
> and passwords...
>
> [snip]
>


Chris,

I hadn't thought about safe mode - that's a good possibility.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Jerry Stuckle

2005-06-26, 7:25 pm

William Tasso wrote:
> Writing in news:alt.www.webmaster
> From the safety of the cafeteria
> Jerry Stuckle <jstucklex@attglobal.net> said:
>
>
> Thanks, looked in .../apache/php.ini
>
> disable_functions=
>
> has no entries
>
> fwiw: disable_clases= is similarly empty
>


OK, Chris had a good idea with safe_mode. I guess it could also be an
option when you compile PHP - I don't remember offhand, and but I can
check it out later if you'd like.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Jerry Stuckle

2005-06-26, 11:34 pm

William Tasso wrote:
> Writing in news:alt.www.webmaster
> From the safety of the cafeteria
> Jerry Stuckle <jstucklex@attglobal.net> said:
>
>
>
> '../configure' '--prefix=/usr' '--with-apxs=/usr/bin/apxs'
> '--with-config-file-path=/etc/php4/apache' '--enable-memory-limit'
> '--disable-debug' '--with-regex=php' '--disable-rpath'
> '--disable-static' '--with-pic' '--with-layout=GNU'
> '--with-pear=/usr/share/php' '--enable-calendar' '--enable-sysvsem'
> '--enable-sysvshm' '--enable-sysvmsg' '--enable-track-vars'
> '--enable-trans-sid' '--enable-bcmath' '--with-bz2' '--enable-ctype'
> '--with-db4' '--with-iconv' '--enable-exif' '--enable-filepro'
> '--enable-ftp' '--with-gettext' '--enable-mbstring'
> '--with-pcre-regex=/usr' '--enable-shmop' '--enable-sockets'
> '--enable-wddx' '--disable-xml' '--with-expat-dir=/usr' '--with-xmlrpc'
> '--enable-yp' '--with-zlib' '--without-pgsql' '--with-kerberos=/usr'
> '--with-openssl=/usr' '--with-zip=/usr' '--enable-dbx'
> '--with-mime-magic=/usr/share/misc/file/magic.mime'
> '--with-exec-dir=/usr/lib/php4/libexec' '--without-mm'
> '--with-curl=shared,/usr' '--with-dom=shared,/usr'
> '--with-dom-xslt=shared,/usr' '--with-dom-exslt=shared,/usr'
> '--with-zlib-dir=/usr' '--with-gd=shared,/usr' '--enable-gd-native-ttf'
> '--with-jpeg-dir=shared,/usr' '--with-xpm-dir=shared,/usr/X11R6'
> '--with-png-dir=shared,/usr' '--with-freetype-dir=shared,/usr'
> '--with-imap=shared,/usr' '--with-imap-ssl' '--with-ldap=shared,/usr'
> '--with-mcal=shared,/usr' '--with-mhash=shared,/usr'
> '--with-mysql=shared,/usr' '--with-unixODBC=shared,/usr'
> '--with-recode=shared,/usr' '--enable-xslt=shared'
> '--with-xslt-sablot=shared,/usr' '--with-snmp=shared,/usr'
> '--with-sybase-ct=shared,/usr' '--with-ttf=shared,/usr'
> '--with-t1lib=shared,/usr'
>
> no sign of 'system' unless I too missed something.
>


William,

Hmmm, I looked through all of this and didn't see anything which would
disable the system() command.

The only times I've seen this disabled is in the php.ini with the
disabled_functions line or enabling safe_mode. But I thought there
might be something in the build configuration; unfortunately I can't see it.

At this point I don't see how the command would be disabled. Have you
asked your hosting company how they did it? It might be interesting.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
William Tasso

2005-06-26, 11:34 pm

Writing in news:alt.www.webmaster
From the safety of the cafeteria
Jerry Stuckle <jstucklex@attglobal.net> said:

[system("top -n1 b");]

> ...
> At this point I don't see how the command would be disabled. Have you
> asked your hosting company how they did it? It might be interesting.


It might except that it's my server. :\

The main task of this server is to provide mail services which it does
very well - I have apache/php/mySql loaded to provide management/service
interface - which also works very well.

I have cobbled together enough information/skills to add a new site in
apache (or apache-ssl) when necessary to install a new interface but my
PHP skills remain close to zer0 - which incidently is not too far behind
my general *n*x skill-set.

I can run the 'top' command (as indicated in the o/p) from the command
line to generate the stats but including it in a PHP file for access via
web returns emptiness.

This is a continuing voyage of discovery for me ... having only
administered Win* systems in anger since VMS went out of vogue.
Fortunately, networking (well tcp/ip) principals are the same regardless
of platform. As with all learning: the faster you run, the steeper the
curve.

Perhaps there's an entry generated in an error log that I haven't found
yet. Certainly there's no errors on the page.

--
William Tasso
William Tasso

2005-06-26, 11:34 pm

Writing in news:alt.www.webmaster
From the safety of the cafeteria
Jerry Stuckle <jstucklex@attglobal.net> said:

> ...
> At this point I don't see how the command would be disabled...



ok - the journey continues ....

system("top -n1 b", $rv); returns blank but a return-value of: 1

system('ls', $retval); returns the expected list but a return-value of: 0

'system' appears to be functioning as it should - I read this as a
permissions (or location) issue for the 'top' command. Am I right? - any
clues?

--
William Tasso
Charles Sweeney

2005-06-26, 11:35 pm

William Tasso wrote

> Perhaps there's an entry generated in an error log that I haven't
> found yet. Certainly there's no errors on the page.


In your php.ini you should be able to see if you have error logging
turned on, and the name and location of the error file.

This line:

display_errors = Off

Toggles error display on the screen. I have it set to "Off". Don't
want the world to see my mistakes! The other option (as you might have
guessed!) is "On".

The errors go into the Apache error log, mine is here:

/usr/local/apache/logs/error_log

--
Charles Sweeney
http://CharlesSweeney.com
William Tasso

2005-06-26, 11:35 pm

Writing in news:alt.www.webmaster
From the safety of the No thank you cafeteria
Charles Sweeney <me@charlessweeney.com> said:

> ...
> upload Greg's script as a cgi,


I created it in the web root as <filename>.cgi - is that what you mean?

> give it
> executable permission and run it?


What is 'executable permission'? Guessing chmod, but I've no idea what
value, current profile is: -rw-r--r-- and the owner is root.

--
William Tasso
Charles Sweeney

2005-06-26, 11:35 pm

William Tasso wrote

> Writing in news:alt.www.webmaster
> From the safety of the cafeteria
> Jerry Stuckle <jstucklex@attglobal.net> said:
>
>
>
> ok - the journey continues ....
>
> system("top -n1 b", $rv); returns blank but a return-value of: 1
>
> system('ls', $retval); returns the expected list but a return-value
> of: 0
>
> 'system' appears to be functioning as it should - I read this as a
> permissions (or location) issue for the 'top' command. Am I right? -
> any clues?


Long shot.

Not sure if you need a Content-Type header. Try this after the <?php
line:

header("Content-Type: text/plain; charset=us-ascii");

The above was used by Toby in his uptime script.

I would try it with and without the <pre>s.

As you say, the system() function is working, so it's a puzzle as to why
"top" doesn't seem to output.

--
Charles Sweeney
http://CharlesSweeney.com
Mark Parnell

2005-06-26, 11:35 pm

Previously in alt.www.webmaster, William Tasso <SpamBlocked@tbdata.com>
said:

> What is 'executable permission'? Guessing chmod, but I've no idea what
> value, current profile is: -rw-r--r-- and the owner is root.


chmod 744 filename.cgi

That will make it -rwxr--r-- (owner can read, write and execute, all
others can read).

(Hint: man chmod)

--
Mark Parnell
http://www.clarkecomputers.com.au
William Tasso

2005-06-26, 11:35 pm

Writing in news:alt.www.webmaster
From the safety of the No thank you cafeteria
Charles Sweeney <me@charlessweeney.com> said:

> ...
> The errors go into the Apache error log ...


aha: error is "TERM environment variable not set."

--
William Tasso
William Tasso

2005-06-26, 11:35 pm

Writing in news:alt.www.webmaster
From the safety of the cafeteria
Mark Parnell <webmaster@clarkecomputers.com.au> said:

> Previously in alt.www.webmaster, William Tasso <SpamBlocked@tbdata.com>
> said:
>
>
> chmod 744 filename.cgi
>
> That will make it -rwxr--r-- (owner can read, write and execute, all
> others can read).


ok - done that

http://....cgi still invokes a file download dialogue

> (Hint: man chmod)


yes - I've seen the output before :/

--
William Tasso
Charles Sweeney

2005-06-26, 11:35 pm

William Tasso wrote

> ok - the journey continues ....


Try this, Mr T. It achieves the same thing (outputting the "top"
command to screen) but goes about it in a different way. The output is
written to a file. PHP reads the file, then outputs it.

Please see my comments after this.

Code:

<?php

$command = "top -n1 b > ";

$path_to_file = "/home/user/public_html/includes/";

$output_file = "top_output";

system($command . $path_to_file . $output_file);

$output = file($path_to_file . $output_file);

$screen_content = "";

foreach($output as $line)
{

$screen_content = $screen_content . $line . "<br>";

}

print "<pre>";

print $screen_content;

print "</pre>";

?>

Comments:

Change $path_to_file to suit. The directory you name here must be
write-able. CHMOD 777 would ensure this.

--
Charles Sweeney
http://CharlesSweeney.com
Charles Sweeney

2005-06-26, 11:35 pm

William Tasso wrote

> Writing in news:alt.www.webmaster
> From the safety of the cafeteria
> Mark Parnell <webmaster@clarkecomputers.com.au> said:
>
<SpamBlocked@tbdata.com>[color=darkred]
what[color=darkred]
>
> ok - done that
>
> http://....cgi still invokes a file download dialogue
>
>
> yes - I've seen the output before :/


cgi isn't my bag, but I think you will need to put it in your cgi
directory, like /cgi-bin. Then get it http://.../cgi-bin/blah.cgi

--
Charles Sweeney
http://CharlesSweeney.com
Charles Sweeney

2005-06-26, 11:35 pm

William Tasso wrote

> Writing in news:alt.www.webmaster
> From the safety of the No thank you cafeteria
> Charles Sweeney <me@charlessweeney.com> said:
>
>
> aha: error is "TERM environment variable not set."


Don't know what that means!

Just had a thought. You can have more than one php.ini file. phpinfo
will give you the location of the one in use in your case.

--
Charles Sweeney
http://CharlesSweeney.com
Mark Parnell

2005-06-26, 11:35 pm

Previously in alt.www.webmaster, William Tasso <SpamBlocked@tbdata.com>
said:
> Mark Parnell <webmaster@clarkecomputers.com.au> said:
>
>
> http://....cgi still invokes a file download dialogue


Is the web server the owner of the file? (Sorry, only just jumped into
this thread partway through - didn't know what the original issue was).
My guess is that you would probably need to make it executable for all
users, so:

chmod 755 filename.cgi

That way anyone can execute it, but only the owner (root?) can modify
it.

--
Mark Parnell
http://www.clarkecomputers.com.au
William Tasso

2005-06-26, 11:35 pm

Writing in news:alt.www.webmaster
From the safety of the No thank you cafeteria
Charles Sweeney <me@charlessweeney.com> said:

> William Tasso wrote
>
>
> Don't know what that means!


Well - I think it means it doesn't know where to direct the output ...
which would indicate it is trying to run. Incidentally I did try adding
the path to 'top' but it made no difference.

> Just had a thought. You can have more than one php.ini file. phpinfo
> will give you the location of the one in use in your case.


I have the right (only) php.ini file :)

--
William Tasso
William Tasso

2005-06-26, 11:35 pm

Writing in news:alt.www.webmaster
From the safety of the cafeteria
Mark Parnell <webmaster@clarkecomputers.com.au> said:


> ...
> chmod 755 filename.cgi
>
> That way anyone can execute it, but only the owner (root?) can modify
> it.


ok - sounds plausible. still invokes download dialogue.


--
William Tasso
Mark Parnell

2005-06-26, 11:35 pm

Previously in alt.www.webmaster, William Tasso <SpamBlocked@tbdata.com>
said:
> Writing in news:alt.www.webmaster
> From the safety of the cafeteria


Shhh! Don't tell my boss I'm in the cafeteria, or it won't be safe any
more, will it? ;-)

> Mark Parnell <webmaster@clarkecomputers.com.au> said:
>
>
> ok - sounds plausible.


I though so. ;-)

> still invokes download dialogue.


We have now officially reached the end of my (limited) knowledge of such
matters. :-(

What mime type is it being sent with?

--
Mark Parnell
http://www.clarkecomputers.com.au
William Tasso

2005-06-26, 11:35 pm

Writing in news:alt.www.webmaster
From the safety of the cafeteria
Mark Parnell <webmaster@clarkecomputers.com.au> said:


> ...
> What mime type is it being sent with?


Script:

#!/bin/bash
echo "content-type: text/html

<PRE>`top -n1 b`</PRE>";exit 0


Headers:

01. Date: Mon, 27 Jun 2005 01:22:10 GMT
02. Server: Apache/1.3.33 (Debian GNU/Linux) PHP/4.3.10-15
03. Last-Modified: Sun, 26 Jun 2005 23:54:52 GMT
04. ETag: "400e033-4c-42bf404c"
05. Accept-Ranges: bytes
06. Content-Length: 76
07. Keep-Alive: timeout=15, max=100
08. Connection: Keep-Alive
09. Content-Type: application/x-shellscript


--
William Tasso
Sponsored Links


Copyright 2003 - 2008 forum4designers.com  Software forum  Computer Hardware reviews