File:  [LON-CAPA] / loncom / lchttpdlogs
Revision 1.2: download - view: text, annotated - select for diffs
Thu Mar 28 15:16:38 2013 UTC (11 years, 9 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_12_X, version_2_11_X, version_2_11_5_msu, version_2_11_5, version_2_11_4_uiuc, version_2_11_4_msu, version_2_11_4, version_2_11_3_uiuc, version_2_11_3_msu, version_2_11_3, version_2_11_2_uiuc, version_2_11_2_msu, version_2_11_2_educog, version_2_11_2, version_2_11_1, version_2_11_0_RC3, version_2_11_0_RC2, version_2_11_0_RC1, version_2_11_0, HEAD
- Apache log files have different names from other distros in Debian and
  Ubuntu.

#!/usr/bin/perl
#
# The Learning Online Network with CAPA
#
# $Id: lchttpdlogs,v 1.2 2013/03/28 15:16:38 raeburn Exp $
#
# Copyright Michigan State University Board of Trustees
#
# This file is part of the LearningOnline Network with CAPA (LON-CAPA).
#
# LON-CAPA is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# LON-CAPA is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with LON-CAPA; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# /home/httpd/html/adm/gpl.txt
#
# http://www.lon-capa.org/
#
#  lchttpdlogs - LONC-CAPA setuid script to tail Apache access and error logs
#         called by loncron, output to /home/httpd/html/lonstatus/index.html.
#

use strict;
use lib '/home/httpd/lib/perl/';
use LONCAPA::Configuration;
use HTML::Entities;

# ---------------------------------------------------------------- Exit codes
# Exit codes.
# ( (0,"ok"),
# (1,"User ID mismatch.  This program must be run as user 'www'"),
#
# ----------------------------------------------------------- Initializations
# Security
$ENV{'PATH'}='/bin/:/usr/bin:/usr/local/sbin:/home/httpd/perl'; #Nullify path
                                                                # information
delete @ENV{qw(IFS CDPATH ENV BASH_ENV)}; # nullify potential taints

# Do not print error messages.
my $noprint=1;

print "In lchttpdlogs\n" unless $noprint;

# --------------------------- Make sure this process is running from user=www
my $wwwid=getpwnam('www');
if ($wwwid != $<) {
    print("User ID mismatch.  This program must be run as user 'www'\n")
        unless $noprint;
    &Exit(1);
}

# ------------------ Read configuration files; determine  distro and protocol  

my $perlvarref=LONCAPA::Configuration::read_conf('loncapa.conf');
my $lonhost;
if (ref($perlvarref) eq 'HASH') {
    $lonhost = $perlvarref->{'lonHostID'};
}
undef($perlvarref);

# ------------------------- tail error_log and access_log (or ssl_ equivalents) 

my $distro;
my $protocol = 'http';

if (open(DSH,"/home/httpd/perl/distprobe |")) {
    $distro = <DSH>;
    close(DSH);
}
if (open(HOSTS,">/home/httpd/perl/hosts.tab")) {
    while (my $configline=<HOSTS>) {
        next if ($configline =~ /^(\^|\#|\s*$)/x);
        chomp($configline);
        my ($id,$domain,$role,$name,$prot,$intdom)=split(/:/,$configline);
        if ($id eq $lonhost) {
            if ($prot eq 'https') {
                $protocol = $prot;   
            }
            last;
        }
    }
    close(HOSTS);
}

&EnableRoot();
my $result = &check_httpd_logs($distro,$protocol);
print $result;

# ----------------------------------------------------------------- Exit script
print "lchttpdlogs Exiting\n" unless $noprint;
&DisableRoot;
&Exit(0);

sub EnableRoot {
    if ($wwwid==$>) {
        ($<,$>)=($>,$<);
        ($(,$))=($),$();
    } else {
        # root capability is already enabled
    }
    return $>;
}

sub DisableRoot {
    if ($wwwid==$<) {
        ($<,$>)=($>,$<);
        ($(,$))=($),$();
    } else {
        # root capability is already disabled
    }
}

sub check_httpd_logs {
    my ($distro,$protocol) = @_;
    my $text;
    my $logpath = '/var/log/httpd';
    my $access_log_file = 'access_log';
    my $error_log_file = 'error_log';
    if ($distro =~ /^(debian|ubuntu)/) {
        $logpath = '/var/log/apache2';
        $access_log_file = 'access.log';
        $error_log_file = 'error.log';
    } elsif ($distro =~ /^suse/) {
        $logpath = '/var/log/apache2';
    } elsif ($distro =~ /^sles(\d+)/) {
        if ($1 >= 10) {
            $logpath = '/var/log/apache2';
        } else {
            $logpath = '/var/log/apache';
        }
    }
    if ($protocol eq 'https') {
        $access_log_file = 'ssl_'.$access_log_file;
        $error_log_file = 'ssl_'.$error_log_file;
    }

    $text='<hr /><a name="httpd" />'."<h2>httpd</h2><h3>Access Log</h3>\n<pre>\n";

    if (open(AFH,"tail -n25 $logpath/$access_log_file |")) {
        while (my $line=<AFH>) { 
            $text .= &encode_entities($line,'<>&"');
        }
        close(AFH);
    }

    $text .= "</pre>\n<h3>Error Log</h3>\n<pre>\n";

    if (open(EFH,"tail -n25 $logpath/$error_log_file |")) {
        while (my $line=<EFH>) {
            $text .= $line;
        }
        close(EFH);
        $text .= "</pre>\n";
    }
    return $text;
}

sub Exit {
    my ($code) = @_;
    print "Exiting with status $code\n" unless $noprint;
    exit $code;
}

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>