File:  [LON-CAPA] / loncom / init.d / loncontrol
Revision 1.27: download - view: text, annotated - select for diffs
Sat Jan 28 11:21:42 2006 UTC (18 years, 3 months ago) by albertel
Branches: MAIN
CVS tags: version_2_3_X, version_2_3_2, version_2_3_1, version_2_3_0, version_2_2_X, version_2_2_99_1, version_2_2_99_0, version_2_2_2, version_2_2_1, version_2_2_0, version_2_1_X, version_2_1_99_3, version_2_1_99_2, version_2_1_99_1, version_2_1_99_0, version_2_1_3, HEAD
- suse puts it in /usr/sbin

    1: #!/usr/bin/perl
    2: #
    3: # $Id: loncontrol,v 1.27 2006/01/28 11:21:42 albertel Exp $
    4: #
    5: # The LearningOnline Network with CAPA
    6: #
    7: # Copyright Michigan State University Board of Trustees
    8: #
    9: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
   10: #
   11: # LON-CAPA is free software; you can redistribute it and/or modify
   12: # it under the terms of the GNU General Public License as published by
   13: # the Free Software Foundation; either version 2 of the License, or
   14: # (at your option) any later version.
   15: #
   16: # LON-CAPA is distributed in the hope that it will be useful,
   17: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   18: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   19: # GNU General Public License for more details.
   20: #
   21: # You should have received a copy of the GNU General Public License
   22: # along with LON-CAPA; if not, write to the Free Software
   23: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   24: #
   25: # /home/httpd/html/adm/gpl.txt
   26: #
   27: # http://www.lon-capa.org/
   28: #
   29: # Startup script for the LON-CAPA network processes
   30: #
   31: 
   32: # chkconfig: 345 95 5
   33: # description: LON-CAPA is a "network of knowledge".  It is used to \
   34: # distribute knowledge resources and instructional management.
   35: # processnames: lonc, lond, lonsql
   36: # pidfiles: /home/httpd/perl/logs/lon*.pid
   37: # config: /etc/httpd/conf/loncapa.conf
   38: # config: /home/httpd/lonTabs/hosts.tab
   39: # config: /home/httpd/lonTabs/spare.tab
   40: 
   41: $command=$ARGV[0]; $command=~s/[^a-z]//g;
   42: 
   43: $ENV{'PATH'}="/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin";
   44: $ENV{'BASH_ENV'}="";
   45: 
   46: { # Firewall variable scoping
   47:     # Firewall code is based on the code in FC2 /etc/init.d/ntpd
   48:     my $fw_chain = 'RH-Firewall-1-INPUT';
   49:     my $iptables = '/sbin/iptables';
   50:     if (! -e $iptables) {
   51: 	$iptables = '/usr/sbin/iptables';
   52: 	if (! -e $iptables) {
   53: 	    print("Unable to find iptables command\n");
   54: 	}
   55:     }
   56:     my $lond_port = 5663;
   57:     my $lonhttpd_port = 8080;
   58: 
   59: sub firewall_open_port {
   60:     return if (! &firewall_is_active);
   61:     if (! `$iptables -L -n 2>/dev/null | grep $fw_chain | wc -l`) { return; }
   62:     # iptables is running with our chain
   63:     #
   64:     # We could restrict the servers allowed to attempt to communicate
   65:     # here, but the logistics of updating the /home/httpd/lonTabs/host.tab
   66:     # file are likely to be a problem
   67:     foreach my $port ($lond_port,$lonhttpd_port) {
   68:         print "Opening firewall access on port $port.\n";
   69: 
   70:         my $firewall_command = 
   71:             "$iptables -I $fw_chain -p tcp -d 0/0 --dport $port -j ACCEPT";
   72:         system($firewall_command);
   73:         my $return_status = $?>>8;
   74:         if ($return_status == 1) {
   75:             # Error
   76:             print "Error opening port.\n";
   77:         } elsif ($return_status == 2) {
   78:             # Bad command
   79:             print "Bad command error opening port.  Command was\n".
   80:                 "  ".$firewall_command."\n";
   81:         }
   82:     }
   83:     
   84: }
   85: 
   86: sub firewall_is_port_open {
   87:     # returns 1 if the firewall port is open, 0 if not.
   88:     #
   89:     # check if firewall is active or installed
   90:     return if (! &firewall_is_active);
   91:     if (`$iptables -L -n 2>/dev/null | grep "tcp dpt:$port"`) { 
   92:         return 1;
   93:     } else {
   94:         return 0;
   95:     }
   96: }
   97: 
   98: sub firewall_is_active {
   99:     if (-e '/proc/net/ip_tables_names') {
  100:         return 1;
  101:     } else {
  102:         return 0;
  103:     }
  104: }
  105: 
  106: sub firewall_close_port {
  107:     return if (! &firewall_is_active);
  108:     foreach my $port ($lond_port,$lonhttpd_port) {
  109:         print "Closing firewall access on port $port.\n";
  110:         my $firewall_command = 
  111:             "$iptables -D $fw_chain -p tcp -d 0/0 --dport $port -j ACCEPT";
  112:         system($firewall_command);
  113:         my $return_status = $?>>8;
  114:         if ($return_status == 1) {
  115:             # Error
  116:             print "Error closing port.\n";
  117:         } elsif ($return_status == 2) {
  118:             # Bad command
  119:             print "Bad command error closing port.  Command was\n".
  120:                 "  ".$firewall_command."\n";
  121:         }
  122:     }
  123: }
  124: 
  125: } # End firewall variable scope
  126: 
  127: sub stop_daemon {
  128:     my ($daemon,$killallname)=@_;
  129:     my $pidfile="/home/httpd/perl/logs/$daemon.pid";
  130:     
  131:     printf("%-15s ",$daemon);
  132:     if (-e $pidfile) {
  133: 	open(PIDFILE,$pidfile);
  134: 	my $daemonpid=<PIDFILE>;
  135: 	chomp($daemonpid);
  136: 	kill TERM => $daemonpid;
  137: 	sleep 1;
  138: 	if (kill 0 => $daemonpid) {
  139: 	    kill KILL => $daemonpid;
  140: 	    sleep 1;
  141: 	    if (kill 0 => $daemonpid) {
  142: 		print("failed to kill");
  143: 	    } else {
  144: 		print("killed");
  145: 	    }
  146: 	} else {
  147: 	    print("stopped");
  148: 	}
  149:     } else {
  150: 	print("not running");
  151:     }
  152:     system("killall -q -0 $killallname");
  153:     if ($? == 0) {
  154: 	system("killall -q $killallname");
  155: 	print(", killed off extraneous processes");
  156:     }
  157:     unlink($pidfile);
  158:     print("\n");
  159: }
  160: 
  161: 
  162: if (($command eq "restartold") or ($command eq "reloadold")) {
  163:     print 'Restarting LON-CAPA'."\n";
  164:     print 'Ending LON-CAPA client and daemon processes'."\n";
  165:     foreach my $daemon ('lonsql','lond','lonc','lonhttpd','lonmemcached') {
  166: 	&stop_daemon($daemon,$daemon);
  167:     }
  168:     print 'Starting LON-CAPA client and daemon processes (please be patient)'.
  169: 	"\n";
  170:     system("su www -c '/home/httpd/perl/loncron --oldlonc --justcheckdaemons'");
  171: } elsif ($command eq "restart") {
  172:     print 'Restarting LON-CAPA'."\n";
  173:     print 'Ending LON-CAPA client and daemon processes'."\n";
  174:     foreach my $daemon ('lonsql','lond','lonc','lonhttpd','lonmemcached') {
  175: 	my $killallname=$daemon;
  176: 	if ($daemon eq 'lonc') { $killallname='loncnew'; }
  177: 	&stop_daemon($daemon,$killallname);
  178:     }
  179:     print 'Starting LON-CAPA client and daemon processes (please be patient)'.
  180: 	"\n";
  181:     system("su www -c '/home/httpd/perl/loncron --justcheckdaemons'");
  182: } elsif ($command eq "stop") {
  183:     print 'Stopping LON-CAPA'."\n";
  184:     foreach my $daemon ('lonsql','lond','lonc','lonhttpd','lonmemcached') {
  185: 	my $killallname=$daemon;
  186: 	if ($daemon eq 'lonc') { $killallname='loncnew'; }
  187: 	&stop_daemon($daemon,$killallname);
  188:     }
  189:     &firewall_close_port();
  190: } elsif ($command eq "startold") {
  191:     &firewall_open_port();
  192:     print 'Starting LON-CAPA'."\n";
  193:     print 'Starting LON-CAPA client and daemon processes (please be patient)'.
  194: 	"\n"; 
  195:     system("su www -c '/home/httpd/perl/loncron --oldlonc --justcheckdaemons'");
  196: } elsif ($command eq "start") {
  197:     &firewall_open_port();
  198:     print 'Starting LON-CAPA'."\n";
  199:     print 'Starting LON-CAPA client and daemon processes (please be patient)'.
  200: 	"\n"; 
  201:     system("su www -c '/home/httpd/perl/loncron --justcheckdaemons'");
  202: } elsif ($command eq "reload") {
  203:     print 'Reload LON-CAPA config files'."\n";
  204:     system("su www -c '/home/httpd/perl/loncron --justreload'");
  205: } elsif ($command eq "status") {
  206:     $response=`/bin/cat /home/httpd/perl/logs/*.pid 2>&1`;
  207:     if ($response=~/No such file or directory/) {
  208: 	print 'LON-CAPA is not running.'."\n";
  209:     } else {
  210: 	print 'LON-CAPA is running.'."\n";
  211: 	system("su www -c '/home/httpd/perl/loncron --justcheckconnections'");
  212:     }
  213:     if (! &firewall_is_active) {
  214:         print 'The iptables firewall is not active'."\n";
  215:     }
  216:     if (&firewall_is_port_open()) {
  217:         print 'The LON-CAPA port is open in firewall.'."\n";
  218:     } elsif (&firewall_is_active) {
  219:         print 'The LON-CAPA port is NOT open in running firewall!'."\n";
  220:     }
  221: } else {
  222:     print 'You need to specify one of restart|stop|start|status on the command line.'."\n";
  223: }

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