Показать сообщение отдельно

  #89  
Старый 11.06.2007, 01:39
REDsaratov
Участник форума
Регистрация: 17.12.2006
Сообщений: 191
Провел на форуме:
415254

Репутация: 75
По умолчанию

Этот скрипт сохраняет информацию о посетителях вашего сайта (дата, время,host name, IP-адрес и т.д.),затем он отсылает вам эту информацию по E-mail.

PHP код:
#! /usr/local/bin/perl


#                              mail-log.cgi
#
#                    Version 2.03   --   27 Jan 1997
#                   http://www.all-yours.net/scripts/
#             Perl (release 4 or 5) script for Unix servers


#     Written by Walter Soldierer of All-Yours Internet Solutions
#               (hermes@all-yours.net), one of the authors of
#
#                          THE DIGITAL POSTCARD
#
#           A FREE service to increase your web site's exposure!
#                            Need more hits?
#            Check it out at http://www.all-yours.net/postcard/


# This script saves access date+time, the visitor's host name, their IP
# address and browser type, and the refering page (if any) to a log file
# on your server (default name = logfile.txt). This file will be automatically
# mailed to your mailbox. You can change the mailing interval according
# to your needs (default value is every 50 accesses to the page to be
# logged). The script will autocreate the logfile, if it does not exist.


# ----- BEGIN INSTALLATION INSTRUCTIONS -----
#  - Save this script to a file mail-log.cgi
#  - Cut installation instructions (optional)
#  - Change the first script line, if your server's Perl executable
#    is not located in /usr/local/bin/perl
#  - Customize the settings below (*SETTINGS*)
#    * Change e-mail address for $recipient
#    * Set $log_page to 1 if you also want to log the page that
#      called this script. Recommended in case you want to log accesses
#      to more than one HTML page.
#    * Set $max_entries to your needs, if you want to have more or less
#      than 50 log entries mailed to your mailbox each time.
#    * Change path/filename for log file, if necessary
#      (default = logfile.txt in your cgi directory)
#    * Change values for $check_host and $interval, if necessary
#    * Change filename for host/time file, if necessary (default = hosttime.txt)
#  - ASCII-upload mail-log.cgi to your cgi directory (cgi-bin, cgi-local...)
#    Make it executable (chmod mail-log.cgi 755)
#  - add the following line (Server Sde Include) to the page(s) to be logged
#    (after the <body> tag):
#    <!--#exec cgi="/your-cgi-directory/mail-log.cgi"-->
#    HTML pages with Server Side Include tags often need to have the
#    file extension ".shtml" !!!! (don't forget to update
#    all links to this page(s) accordingly)
#    (You should test the script with a test.shtml page first)
#  - Load the logged page(s) with your browser.
#  - View your new logfile
#    It should look something like this:
#    
#    Time: 05/30/96 14:44:07 EDT
#    Host: your.host.and.domain
#    Addr: 123.45.67.89
#    With: Mozilla/1.22 (compatible; MSIE 2.0; Windows 95)
#    Page: test.shtml
#    From: [no entry here, if you accessed your page directly (otherwise Yahoo etc.)]
#
#    If the script doesn't work and you want to contact us, please check
#    the following first:
#    - did you upload the script in ASCII mode (*not* binary)?
#    - did you chmod the script 755
#    - does your server support the "exec cgi" Server Side Include?
#    - did you rename the html file to .shtml
#    - did you check the path to your server's Perl executable?
#
#    If you still have a problem and you want us to help you, we need to know:
#     - a detailed description of your problem
#     - your script's settings section
#     - html code and file name for the page to be logged
#     - script related error messages from your server's error log file
# ----- END INSTALLATION INSTRUCTIONS -----



# -----------------------------------------------------------------------
# *SETTINGS* to customize this script:

# Your email address (within single quotes!):
$recipient 'xxxx@yyyy.com';

# Do you want to log the .shtml page's file name?
# (recommended when more than one poage is logged)
# 1 = the .shtml page that called this script will be logged
# 0 = the .shtml page that called this script will not be logged
$log_page 1;

# Do you want to log your visitor's IP number?
# The script attemps to call a domain name server in order to resolve
# IP addresses (such as 132.45.323.5) to a host name (such as yourdomain.com)
# However, resolving IP addressses doesn't work 100 per cent so you
# might also want to log rhe IP addresses.
# 1 = IP addresses will be logged
# 0 = IP addresses will be discarded
$log_IP 1;

# Number of logged visits mailed to your mailbox at a time:
$max_entries 50;

# Change logfile name if necessary:
$logfile 'logfile.txt';

# server's sendmail directory:
$mailprogam '/usr/lib/sendmail';

# If you set $check_host = "1" the script attempts not
# to log multiple page reloads.
# When a page was accessed from the same host by using the same
# browser, this visit will only be logged after the time 
# interval specified in $interval.
# Example: You set $interval to 1800 (seconds). If there was no
# access from a different host in the meantime, a visitor from the
# same host who uses the same browser will only be logged when s/he
# accesses your page 30 minutes after visitor one.
# In other words: If someone reloads the page many times, in most cases
# s/he needs to wait 30 minutes after each reload to make your log file
# grow. When you set $check_host to 0 even the reloads will be logged.
$check_host 1;  # set to zero if you don't like this feature

# If you set the $check_host variable to 1, specify a time interval to
# ignore page reloads
$interval 600;   # seconds!

# If you set the $check_host variable to 1, change the name of the file
# that temporarily stores host and time information (if necessary)
$hostfile 'hostfile.txt';




# don't change anything past this line unless you know what you are doing
# -----------------------------------------------------------------------

# create a date+time string
$shortdate = `date +"%D %T %Z"`; 
chop ($shortdate);

# Some of Perl's network info functions required here
($part1,$part2,$part3,$part4)=split(/\./,$ENV{REMOTE_ADDR});
$IP_adr=pack("C4",$part1,$part2,$part3,$part4);
(
$host_name)=(gethostbyaddr("$IP_adr"2));

print 
"Content-type: text/plain\n\n";

if (
$check_host) {
   
# read host and time info from last visitor
   
if (-"$hostfile") {
      
open(HOST,"$hostfile");
      
$hostline = <HOST>;
      
chop($hostline) if $hostline =~ /\n$/;
      
close(HOST);
      (
$old_time,$old_number,$old_page,$old_browser) = split(/\|/,$hostline);
   }

   
# save host and time info and check if this is a page reload
   
open(HOST,">$hostfile");
   
$seconds time;
   print 
HOST "$seconds\|$ENV{REMOTE_ADDR}\|$ENV{'DOCUMENT_URI'}\|$ENV{'HTTP_USER_AGENT'}";
   
close(HOST);

   if (
time $old_time $interval
   
&& $ENV{REMOTE_ADDReq $old_number
   
&& $ENV{'DOCUMENT_URI'eq $old_page
   
&& $ENV{'HTTP_USER_AGENT'eq $old_browser) {
      exit;  
# probably same visitor, so exit
   
}
}

# open log file for output and append new log data
open (LOGFILE">>$logfile");
print 
LOGFILE "Time: $shortdate\n";
print 
LOGFILE "From: $ENV{'HTTP_REFERER'}\n";
print 
LOGFILE "IP  : $ENV{REMOTE_ADDR}\n" if $log_IP;
print 
LOGFILE "Host: $host_name\n";
print 
LOGFILE "With: $ENV{'HTTP_USER_AGENT'}\n";
print 
LOGFILE "Page: $ENV{'DOCUMENT_URI'}\n" if $log_page;
print 
LOGFILE "\n";
close (LOGFILE);

# open log file for input and count log entries
open (LOGFILE$logfile);
@
entries = <LOGFILE>;
close (LOGFILE);
$log_rows 7;
$log_rows-- unless $log_page;
$log_rows-- unless $log_IP;
$log_count = @entries/$log_rows;

# if number of logs >= max. number of logs, mail file and delete it
if ($log_count >= $max_entries) {
   
open (MAIL"|$mailprogam $recipient") || die "Can't open $mailprogam!\n";
   print 
MAIL "Subject: Mail-log File\n\n";
   print 
MAIL "@entries\n";
   
close MAIL;

   
unlink $logfile;
}

# end of script 

Скрипт выполняет множество функций для администрирования Unix сервера.

PHP код:
#!/usr/bin/perl5
#
# +---------------------------------------------------------------------+
# | sysdaemon                                                           |
# | Written By   : Trans-Euro I.T Ltd                                   |
# | Written On   : July 27th 1997                                       |
# |                                                                     |
# | Purpose        System monitor                                       |
# |                Reads the contents of :-                             |
# |                /usr/local/etc/sysdaemon/daemontab                   |
# |                the daemon names present are scanned for the results |
# |                being written back to :-                             |
# |                /var/log/sysmon.REPORT                               |
# |               If a failure occurs the appropriate support team are  |
# |               contacted by email.. also if any filestore partition  |
# |               reaches 99%, a pager message can also be sent.        |
# |               This daemon replaces sysmon, which was written in     |
# |               korn shall/bash.                                      |
# |               On recieving a kill -HUP signal the daemon will       |
# |               re-read its config file.                              |
# |               The definable nature of this daemon means...          |
# |               There is NO user changeable code.                     |  
# +---------------------------------------------------------------------+
# | (c) 1997 This script is the intellectual property of                |
# | Trans-Euro I.T Ltd                                                  |
# +---------------------------------------------------------------------+
use Config;

$VERSION="1.40";
$PROGRAM="sysdaemon";

$hup=0;

$pidfile="/var/run/sysdaemon.pid";
$tab="/usr/local/etc/sysdaemon/daemontab";
$report="/var/log/sysmon.REPORT";
$date=get_date();
$time=get_time();
$node=`uname -n`;
$daemon_name="";
$status="";
$work1="/tmp/sysdaemon1.$$";
$work2="/tmp/sysdaemon2.$$";
$mailprog='/usr/sbin/sendmail';
$support="support\@svr1.marketrends.net";
$filesystem="";
$capacity="";
$fstat="";

@
daemontable="";
@
proc="";
@
fstab="";

format SYSDAEMON =
@<<<<<<<<<<<<<<<<<< : 
daemon is @<<<<<<<<<<<<<<<<<<<<<<<<:Status is @<<<<
$time                           $daemon_name                        $status 
.

format SYSDAEMONHUP =
@<<<<<<<<<<<<<<<<<< : 
HUP re-reading @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$time                                $tab
.

format SYSDAEMONFS =
@<<<<<<<<<<<<<<<<<< : @<<<<<<<<<<<<<<<<<<< : @<<<<       : @<<<<
$time                 $filesystem            $capacity     $fstat
.

unless ( -"$tab") { die "$tab is either missing or empty !!\n";}
                 
read_daemontab();
        
unless (fork) {
 
unless (fork) {
   
sleep1 until getppid == 1;

# +---------------------------------------------------------------------+
# | Main Daemon control loop                                            |
# +---------------------------------------------------------------------+

   
write_pid();
   
write_report_header();
       
   while ( ! 
$daemon_control) {

         
# +---------------------+
         # | Catch signals here! |
         # +---------------------+

         
$SIG{HUP} = \&hup;
         if (
$hup) { read_daemontab();$hup=0;}

         
# +---------------------+
         # | main process loop   |
         # +---------------------+

         
$SIG{HUP} = \&hup;
        
         
$date=get_date();
         
$time=get_time();
         
gather_process_info();
         
scan_process();          
         
check_filestore();

         
unlink($work1);
         
unlink($work2);
         
sleep 300;
                              }
            
 exit 
0;
               }
exit 
0;
              }

# +---------------------------------------------------------------------+
# | Sub programs                                                        |
# +---------------------------------------------------------------------+

sub hup {
  
$hup=1;
 }

# +---------------------------------------------------------------------+

sub read_daemontab {

if (
$hup eq "1") { log_hup();}

open(T,"$tab") || die "Cannot open $tab!\n";
@
daemontable=<T>;
close(T);

foreach 
$EOF (@daemontable) {
   
$_=$EOF;
   
s/\012//g;
   
$EOF=$_;}
}

# +---------------------------------------------------------------------+

sub write_pid {
open(PID,">$pidfile") || die "Cannot open $pidfile!!\n";
print 
PID $$;
close(PID);
}

# +---------------------------------------------------------------------+

sub get_date {
my $date=`date`;
$_=$date;
s/\012//g;
$date=$_;
return 
$date;
}

# +---------------------------------------------------------------------+

sub write_report_header {
open(REP,">$report")|| die "Cannot open $report!!\n";
print 
REP "--------------------------------------------------------\n";
print 
REP "$PROGRAM : $VERSION starts at $date\n";
print 
REP "Trans-Euro I.T (c) 1997.\n";
print 
REP "Written by Stephen Martin\n";
print 
REP "--------------------------------------------------------\n";
close(REP);


# +---------------------------------------------------------------------+

sub get_time {
my $time=`date +"%D : %T"`;
$_=$time;
s/\012//g;
$time=$_;
return 
$time;
}

# +---------------------------------------------------------------------+

sub gather_process_info {
open(WORK1,"|ps -ax > $work1")|| die "Cannot gather process info!\n";
close(WORK1);
open(WORK1,"$work1")|| die "Cannot read process workfile!\n";
@
proc=<WORK1>;
close(WORK1);
}

# +---------------------------------------------------------------------+

sub scan_process {
foreach 
$dproc (@daemontable){
   (
$daemon,$daemon_name)=split(/:/,$dproc);
   if ( 
grep /$daemon/, @proc ) {log_daemon_status("$daemon_name","up");}
   else { 
log_daemon_status("$daemon_name","down");
          
mail_support("$daemon_name");}
                             } 
   
}

# +---------------------------------------------------------------------+

sub log_daemon_status {
    
my ($daemon_name,$state) = @_;
    
$status=$state;
    
open(SYSDAEMON,">>$report")|| die "Cannot log results to $result!\n";
    
write SYSDAEMON;
    
close(SYSDAEMON);
}

# +---------------------------------------------------------------------+

sub log_hup {
    
open(SYSDAEMONHUP,">>$report")|| die "Cannot log HUP call to $result!\n";
    
write SYSDAEMONHUP;
    
close(SYSDAEMONHUP);
}

# +---------------------------------------------------------------------+

sub mail_support {

     
my ($d_name) = @_;
 
     
$d_name;

     
open (MAIL"| $mailprog $support") || die "Can't open $mailprog!\n";
     print 
MAIL "To: $support\n";
     print 
MAIL "From:$PROGRAM\n";
     print 
MAIL "Subject:$PROGRAM : $d_name failure\n";
     print 
MAIL  "------------------------------------------------------\n\n";
     print 
MAIL "$PROGRAM has detected $d_name missing on $node";
     
close (MAIL);
}

# +---------------------------------------------------------------------+

sub check_filestore {
local $line,$d1,$d2,$d3,$d4,$fs;
open(DF,"|df > $work2")||die "Cannot df!\n";
close(DF);

open(DFW,"$work2")||die "Cannot open $work2!\n";
    while(<
DFW>) {
                  @
fstab=<DFW>; 
                 }
close(DFW);

foreach 
$line (@fstab) {
         (
$d1,$d2,$d3,$d4,$lim,$fs)=split(/\ {1,}/,$line);
             if ( 
$lim gt "98%" ) { 
                  
log_fs("$fs","$lim","Full");
                  
mail_support_fs("$fs","$lim","Full");
                  } else { 
log_fs("$fs","$lim","Ok")};
                      }                       

}

# +---------------------------------------------------------------------+

sub log_fs {
    
my ($l1,$l2,$l3) = @_;
    
$fstat="$l3";
    
$filesystem=$l1;
    
$capacity=$l2;
open(SYSDAEMONFS,">>$report") || die "Cannot open $report to log fs full\n";
write SYSDAEMONFS;
close(SYSDAEMONFS);
}

# +---------------------------------------------------------------------+


sub mail_support_fs {

    
my ($l1,$l2,$l3) = @_;
    
$fstat="$l3";
    
$filesystem=$l1;
    
$capacity=$l2;

    
open (MAIL"| $mailprog $support") || die "Can't open $mailprog!\n";
    print 
MAIL "To: $support\n";
    print 
MAIL "From:$PROGRAM\n";
    print 
MAIL "Subject:$PROGRAM : Filestore $node Full $filesystem\n";
    print 
MAIL  "------------------------------------------------------\n\n";
    print 
MAIL "$PROGRAM has detected that the filesystem $filesystem";
    print 
MAIL "on $node has become full.\n";
    print 
MAIL "$filesystem is currently at $capacity.\n";
    
close (MAIL);


Последний раз редактировалось REDsaratov; 11.06.2007 в 01:53..
 
Ответить с цитированием