s0cke
23.03.08, 01:43
das ist doch dieses ovh check out tool: http://forum.ovh.de/showthread.php?t=3241
#!/usr/bin/perl -Tw use strict; use Socket; use POSIX 'setsid'; use File::stat; $|=1; my $OCO_VERSION="oco-1.15"; my $LOGFILE="/var/log/oco/oco.log"; &daemonize; #get hostname my $hostname=`/bin/hostname`; chomp ($hostname); # use port 79 my $port = 79; my $proto = getprotobyname('tcp'); # create a socket, make it reusable socket(SERVER, PF_INET, SOCK_STREAM, $proto) or die "socket: $!"; setsockopt(SERVER, SOL_SOCKET, SO_REUSEADDR, 1) or die "setsock: $!"; # grab a port on this machine my $paddr = sockaddr_in($port, INADDR_ANY); # bind to a port, then listen bind(SERVER, $paddr) or die "bind: $!"; listen(SERVER, SOMAXCONN) or die "listen: $!"; print localtime(time)." oco server started on port $port\n"; # accepting a connection my $client_addr; my ($errormessage, $status, $localip, $myaddr, $mysockaddr, $answer); while ($client_addr = accept(CLIENT, SERVER)) { #set autoflush to socket select((select(CLIENT), $| = 1)[0]); #get local IP on which client make connection $mysockaddr = getsockname(CLIENT); ($port, $myaddr) = unpack_sockaddr_in($mysockaddr); $localip=inet_ntoa($myaddr); # find out who connected my ($client_port, $client_ip) = sockaddr_in($client_addr); my $client_ipnum = inet_ntoa($client_ip); my $client_host = gethostbyaddr($client_ip, AF_INET); if (! defined ($client_host)) { $client_host=$client_ipnum;} #get status of machine $status=get_status({ errormessage => \$errormessage }); # set answer if ($status >= 300) { $answer="$status KO $localip $hostname $OCO_VERSION $errormessage"; } else { $answer="$status OK $localip $hostname $OCO_VERSION $errormessage"; } # don't print logs unless debugging #print localtime(time)." oco connection: $client_host:$client_ipnum:$client_port $hostname:$localip:$port answer: $answer\n"; print CLIENT $answer."\n"; #read one line eval { local $SIG{"ALRM"} = sub { die "timeout\n" }; alarm 1; my $ok =; alarm 0; }; # print CLIENT $answer."\n"; close CLIENT; } sub get_status { my $args = shift; my $PREFIX="/usr/local/oco/"; my $CHECK_STATE="result/"; my ($file, @dirContent, @data, $errormess, $allOK, $errorCode, $isfile); $errorCode=200; $allOK = 1; $errormess = ""; $isfile=0; opendir(DIR,$PREFIX.$CHECK_STATE); @dirContent=readdir(DIR); closedir(DIR); foreach $file (@dirContent){ if ($file !~ m/^\./) { $isfile=1; #check time of file modification, if it is < 15 min. if ((stat($PREFIX.$CHECK_STATE.$file)->mtime)>=(time-(15*60))) { #read content of result file open (FILE, $PREFIX.$CHECK_STATE.$file) || die ("cannot open file".$PREFIX.$CHECK_STATE.$file); @data= ; chomp(@data); close (FILE); #if errorCode >=300 => not OK :( if ($data[0] >= 300) { $allOK=0; } #if errorCode > 200 => add check to error message if ($data[0] > "200") { $errormess.=",".$data[0]."-".$file; } #if this errorCode > max errorCode => max errorCode = this errorCode if ($data[0] > $errorCode) { $errorCode=$data[0]; } } else { #result file is too old, something is wrong with check, set errorCode to '398' $allOK=0; $errorCode="398"; $errormess.=",398-".$file; } } } #there is no files :( very bad if ($isfile==0) { $errorCode=399; $errormess="399-no_results"; } #if errorCode=200 => All is OK (we don't need error message) if ($errorCode==200){ $errormess=""; } else { $errormess=~s/^,//; } ${$args->{errormessage}}=$errormess; return $errorCode; } exit; sub daemonize { my ($i,$pid); print "Launching oco server daemon, logs are in $LOGFILE\n"; #security $ENV{PATH}="/bin:/usr/bin"; chdir("/"); close STDIN; if ( !defined( my $pid=fork() )) { die "Impossible to fork\n"; } elsif ($pid >0) { #it's dad, byebye. exit 0; } #it's the kid #on crée un nouvelle session setsid(); #close stdin (to avoid break when closing terminal) open STDIN, "/dev/null"; #you can redirect all to /dev/null, but you will have no warning in futur open STDOUT, ">>$LOGFILE" or die "Unable to open $LOGFILE: $!\n"; open STDERR, ">&".fileno(STDOUT) or die "Unable to redirect STDERR to STDOUT: $!\n"; }