[PHP] Monitoring Remote Server Services using php !!!

2006-05-25 Thread Phil Martin

Hi everybody,

 I'm new to the list and also new to php, I hope I can learn many
things from here.
 My first doubt is the following: I'm trying to create a small monitor
system, just to suit my needs in monitoring some services in my application.
I've found some functions that return to me the service by port, name and so
on (getservbyport, getservbyname). What I need is to monitor remote server
services, I mean, lets suppose I have a server 192.168.0.2 with a ssh server
running. I'd like to see the status (up/down) of that server from another
machine, like 192.168.0.1. I don't want to use some monitoring softwares out
there in the web, i know they exist and in fact I use many of them like
nagios, cacti and so on, but I'm planning to do my own small solution.
What I need is some function that asks me a remote IP, port and
protocol as input data and results TRUE/FALSE (any boolean value), just to
see if the service is up. I did that making a function using nmap, but i
don't want to hold that solution to linux, I'd like to use it at other OS.
My function's syntax is like this




  Does anyone know if there is a similar function in PHP ? I'd be very
happy if somebody knows about a function like that.

Thanks in advance

att.
Felipe Martins


Re: [PHP] Monitoring Remote Server Services using php !

2006-05-30 Thread Phil Martin

I did the following, I've followed your advice and wrote my own, that is
listed below. It's a function that has 3 entry arguments, IP, Port and
Protocol. It returns if the Service is UP or DOWN. Take a Look, and see if
we can enhance it's speed.


function servstatus($remote_ip, $remote_ip_port, $remote_ip_proto) {

if ($remote_ip_proto == "tcp") {
   $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

   // Turning off Warning Messages
   $i_error_old=error_reporting();
   error_reporting($i_error_old & ~ (E_WARNING));

   if ($connect = socket_connect($socket, "$remote_ip", "" .
$remote_ip_port . "")) {
   return true;
   socket_set_block($socket);
   socket_close($socket);
   unset($connect);
   } else {
   return false;
   socket_set_nonblock($socket);
   socket_close($socket);
   unset($connect);}

} else {

   // Turning off Warning Messages
   $i_error_old=error_reporting();
   error_reporting($i_error_old & ~ (E_WARNING));

   $socket = fsockopen("udp://$remote_ip", "$remote_ip_port", &$errno,
&$errstr, 2);
   $timeout = 1;

   if (!$socket) {
   return true;
   }

   socket_set_timeout ($socket, $timeout);
   $write = fwrite($socket,"\x00");
   if (!$write) {
   return true;
   }

   $startTime = time();
   $header = fread($socket, 1);
   $endTime = time();
   $timeDiff = $endTime - $startTime;

   if ($timeDiff >= $timeout) {
   fclose($socket);
   return true;
   } else {
   fclose($socket);
   return false;
   }
   }
}


On 5/25/06, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:




On Thu, 25 May 2006, Phil Martin wrote:

> Hi everybody,
>
> I'm new to the list and also new to php, I hope I can learn many
> things from here.
> My first doubt is the following: I'm trying to create a small
monitor
> system, just to suit my needs in monitoring some services in my
application.
> I've found some functions that return to me the service by port, name
and so
> on (getservbyport, getservbyname). What I need is to monitor remote
server
> services, I mean, lets suppose I have a server 192.168.0.2 with a ssh
server
> running. I'd like to see the status (up/down) of that server from
another
> machine, like 192.168.0.1. I don't want to use some monitoring softwares
out
> there in the web, i know they exist and in fact I use many of them like
> nagios, cacti and so on, but I'm planning to do my own small solution.
>What I need is some function that asks me a remote IP, port and
> protocol as input data and results TRUE/FALSE (any boolean value), just
to
> see if the service is up. I did that making a function using nmap, but i
> don't want to hold that solution to linux, I'd like to use it at other
OS.
> My function's syntax is like this
>
>  $ssh_status=service_status (192.168.0.2, 22, tcp);
> if ($ssh_status==TRUE); {
> echo "Service UP";
> } else {
> echo "Service DOWN";
>}
> ?>
>
>
>  Does anyone know if there is a similar function in PHP ? I'd be very
> happy if somebody knows about a function like that.
>
> Thanks in advance
>
> att.
> Felipe Martins
>
>

Why not use php and curl and write your own. Good way to start learning
php. Or you can do like we do and use php and curl to make sure you can
actually get results from the server and use a shell script or php script
on each remote server running from cron every 10/.. minutes that checks
the processes you want monitored and sends an email to a pager if it
detects a problem. That would allow you to not only monitor if a service
is running but also server load, file systems filing up, etc.



[PHP] Executing functions or scripts in parallel

2006-05-30 Thread Phil Martin

Hi everyone,

 I've made a very basic and small function to suit my needs in
monitoring some hosts services. I've noticed that the script is a little bit
slow because of the number of hosts and services to monitor. Is there a way
to execute my function servstatus();  in parallel with every hosts to
increase the speed ?
 Thanks in advance.

[]'s
Felipe Martins


[PHP] Executing functions or scripts in parallel

2006-05-30 Thread Phil Martin

Hi everyone,

 I've made a very basic and small function to suit my needs in
monitoring some hosts services. I've noticed that the script is a little bit
slow because of the number of hosts and services to monitor. Is there a way
to execute my function servstatus();  in parallel with every hosts to
increase the speed ?
 Thanks in advance.

[]'s
Felipe Martins


Re: [PHP] Executing functions or scripts in parallel

2006-05-30 Thread Phil Martin

Sure, sorry about that. I have a function that tells me if the host is DOWN
or UP. I want to run this function in parallel for each host I monitor. The
function is the following:

function servstatus($remote_ip, $remote_ip_port, $remote_ip_proto) {

if ($remote_ip_proto == "tcp") {
   $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

   // Turning Off Warning Messages
   $i_error_old=error_reporting();
   error_reporting($i_error_old & ~ (E_WARNING));

   if ($connect = socket_connect($socket, "$remote_ip", "" .
$remote_ip_port . "")) {
   return true;
   socket_set_block($socket);
   socket_close($socket);
   unset($connect);
   } else {
   return false;
   socket_set_nonblock($socket);
   socket_close($socket);
   unset($connect);}

} else {

   // Turning off Warning Messages
   $i_error_old=error_reporting();
   error_reporting($i_error_old & ~ (E_WARNING));

   $socket = fsockopen("udp://$remote_ip", "$remote_ip_port", &$errno,
&$errstr, 2);
   $timeout = 1;

   if (!$socket) {
   return true;
   }

   socket_set_timeout ($socket, $timeout);
   $write = fwrite($socket,"\x00");
   if (!$write) {
   return true;
   }

   $startTime = time();
   $header = fread($socket, 1);
   $endTime = time();
   $timeDiff = $endTime - $startTime;

   if ($timeDiff >= $timeout) {
   fclose($socket);
   return true;
   } else {
   fclose($socket);
   return false;
   }
   }
}


Thanks in advance

Felipe Martins

On 5/30/06, Dave Goodchild <[EMAIL PROTECTED]> wrote:




On 30/05/06, Phil Martin <[EMAIL PROTECTED]> wrote:
>
> Hi everyone,
>
>   I've made a very basic and small function to suit my needs in
> monitoring some hosts services. I've noticed that the script is a little
> bit
> slow because of the number of hosts and services to monitor. Is there a
> way
> to execute my function servstatus();  in parallel with every hosts to
> increase the speed ?
>   Thanks in advance.
>
> []'s
> Felipe Martins
>
>
Can you show us the function?

--
http://www.web-buddha.co.uk

dynamic web programming from Reigate, Surrey UK (php, mysql, xhtml, css)

look out for project karma, our new venture, coming soon!



Re: [PHP] Executing functions or scripts in parallel

2006-05-30 Thread Phil Martin

Many parts of this script were found at the following site:
http://www.sitepoint.com/article/udp-portscanning-php

This script isn't totally mine. I've just adapted it to my needs and it is
working quite well, telling me when a service is up or down ... read the
article for further details.

Anyway, my question was about concurrent script running. My script is
working the way I needed it to and all I need now is a way to fasten it. I
still think it's a little bit slow by many aspects, maybe because I'm not a
programmer and I've written a code that is not so good or fast, or there is
no way to have all my servers services checked in parallel.


Thanks in advance.

Felipe Martins

On 5/30/06, Jochem Maas <[EMAIL PROTECTED]> wrote:


Phil Martin wrote:
> Sure, sorry about that. I have a function that tells me if the host is
DOWN
> or UP. I want to run this function in parallel for each host I monitor.
The
> function is the following:
>
> function servstatus($remote_ip, $remote_ip_port, $remote_ip_proto) {
>
> if ($remote_ip_proto == "tcp") {
>$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
>
>// Turning Off Warning Messages
>$i_error_old=error_reporting();
>error_reporting($i_error_old & ~ (E_WARNING));
>
>if ($connect = socket_connect($socket, "$remote_ip", "" .
> $remote_ip_port . "")) {
>return true;

the following 3 statements will never be run.

>socket_set_block($socket);
>socket_close($socket);
>unset($connect);
>} else {
>return false;

the following 3 statements will never be run.

>socket_set_nonblock($socket);
>socket_close($socket);
>unset($connect);}

my guess is you know even less about sockets, especially
blocking/non-blocking
than I do!

>
> } else {

why are you making the distinction between udp and tcp in this way?

>
>// Turning off Warning Messages
>$i_error_old=error_reporting();
>error_reporting($i_error_old & ~ (E_WARNING));
>
>$socket = fsockopen("udp://$remote_ip", "$remote_ip_port",
&$errno,
> &$errstr, 2);
>$timeout = 1;
>
>if (!$socket) {

if you don't get the socket why does that mean the service is up?

>return true;
>}
>
>socket_set_timeout ($socket, $timeout);
>$write = fwrite($socket,"\x00");
>if (!$write) {

again if you can't write why does that mean the service is up?

>return true;
>}
>
>$startTime = time();
>$header = fread($socket, 1);
>$endTime = time();
>$timeDiff = $endTime - $startTime;
>
>if ($timeDiff >= $timeout) {

why does a time measurement mean the service is up?

>    fclose($socket);
>return true;
>} else {
>fclose($socket);
>return false;
>}
>}
> }
>
>
> Thanks in advance
>
> Felipe Martins
>
> On 5/30/06, Dave Goodchild <[EMAIL PROTECTED]> wrote:
>
>>
>>
>>
>> On 30/05/06, Phil Martin <[EMAIL PROTECTED]> wrote:
>> >
>> > Hi everyone,
>> >
>> >   I've made a very basic and small function to suit my needs in
>> > monitoring some hosts services. I've noticed that the script is a
>> little
>> > bit
>> > slow because of the number of hosts and services to monitor. Is there
a
>> > way
>> > to execute my function servstatus();  in parallel with every hosts to
>> > increase the speed ?
>> >   Thanks in advance.
>> >
>> > []'s
>> > Felipe Martins
>> >
>> >
>> Can you show us the function?
>>
>> --
>> http://www.web-buddha.co.uk
>>
>> dynamic web programming from Reigate, Surrey UK (php, mysql, xhtml,
css)
>>
>> look out for project karma, our new venture, coming soon!
>>
>