File:  [LON-CAPA] / loncom / init.d / loncontrol
Revision 1.41: download - view: text, annotated - select for diffs
Thu Dec 30 18:40:38 2010 UTC (13 years, 5 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_10_0_RC2, HEAD
- No longer stop and start of firewall rules for existing hosts when loncron
  calls lciptables.
- Additional arg in Firewall::firewall_close_port() - ref to %iphosts
  hash of hosts in cluster.

    1: #!/usr/bin/perl
    2: #
    3: # $Id: loncontrol,v 1.41 2010/12/30 18:40:38 raeburn 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, lonmaxima, lonr
   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: # SuSE chkconfig/insserv info
   41: ### BEGIN INIT INFO
   42: # Provides:       loncapa
   43: # Required-Start: SuSEfirewall2_setup mysql apache2 $network $remote_fs
   44: # Should-Start:   $ALL
   45: # Required-Stop:
   46: # Default-Start:  3 4 5
   47: # Default-Stop:
   48: # Description:    Starts the LON-CAPA services
   49: ### END INIT INFO
   50: 
   51: use strict;
   52: use lib '/home/httpd/lib/perl/';
   53: use LONCAPA::Configuration;
   54: use LONCAPA::Firewall;
   55: use Apache::lonnet;
   56: 
   57: my $command=$ARGV[0]; $command=~s/[^a-z]//g;
   58: 
   59: $ENV{'PATH'}="/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin";
   60: $ENV{'BASH_ENV'}="";
   61: 
   62: sub stop_daemon {
   63:     my ($daemon,$killallname)=@_;
   64:     my $pidfile="/home/httpd/perl/logs/$daemon.pid";
   65:     
   66:     printf("%-15s ",$daemon);
   67:     if (-e $pidfile) {
   68: 	open(PIDFILE,$pidfile);
   69: 	my $daemonpid=<PIDFILE>;
   70: 	chomp($daemonpid);
   71: 	kill TERM => $daemonpid;
   72: 	my $count=0;
   73: 	while ($count++ < 5 && kill(0 => $daemonpid)) {
   74: 	    sleep 1;
   75: 	}
   76: 	if (kill 0 => $daemonpid) {
   77: 	    kill KILL => $daemonpid;
   78: 	    sleep 1;
   79: 	    if (kill 0 => $daemonpid) {
   80: 		print("failed to kill");
   81: 	    } else {
   82: 		print("killed");
   83: 	    }
   84: 	} else {
   85: 	    print("stopped");
   86: 	}
   87:     } else {
   88: 	print("not running");
   89:     }
   90:     system("killall -q -0 $killallname");
   91:     if ($? == 0) {
   92: 	system("killall -q $killallname");
   93: 	print(", killed off extraneous processes");
   94:     }
   95:     unlink($pidfile);
   96:     print("\n");
   97: }
   98: 
   99: sub clean_sockets {
  100:     opendir(SOCKETS,"/home/httpd/sockets/");
  101:     my $perlvarref=&LONCAPA::Configuration::read_conf();
  102:     return if (ref($perlvarref) ne 'HASH');
  103:     while (my $fname=readdir(SOCKETS)) {
  104: 	next if (-d $fname
  105: 		 || $fname=~/(mysqlsock|maximasock|\Q$perlvarref->{'lonSockDir'}\E)/);
  106: 	unlink("/home/httpd/sockets/$fname");
  107:     }
  108: }
  109: 
  110: if ($command eq "restart") {
  111:     print 'Restarting LON-CAPA'."\n";
  112:     print 'Ending LON-CAPA client and daemon processes'."\n";
  113:     foreach my $daemon ('lonsql','lond','lonc','lonmemcached','lonmaxima','lonr') {
  114: 	my $killallname=$daemon;
  115: 	if ($daemon eq 'lonc') { $killallname='loncnew'; }
  116: 	&stop_daemon($daemon,$killallname);
  117:     }
  118:     print 'Starting LON-CAPA client and daemon processes (please be patient)'.
  119: 	"\n";
  120:     system("su www -c '/home/httpd/perl/loncron --justcheckdaemons'");
  121: } elsif (($command eq "stop") || ($command eq 'start') || ($command eq 'status')) {
  122:     my $iptables = &LONCAPA::Firewall::get_pathto_iptables();
  123:     my @fw_chains = &LONCAPA::Firewall::get_fw_chains($iptables);
  124:     my $lond_port = &LONCAPA::Firewall::get_lond_port();
  125:     my %iphost;
  126:     if ($command eq 'stop') {
  127:         print 'Stopping LON-CAPA'."\n";
  128:         foreach my $daemon ('lonsql','lond','lonc','lonmemcached','lonmaxima','lonr') {
  129: 	    my $killallname=$daemon;
  130: 	    if ($daemon eq 'lonc') { $killallname='loncnew'; }
  131: 	    &stop_daemon($daemon,$killallname);
  132:         }
  133:         my $firewall_result = 
  134:             &LONCAPA::Firewall::firewall_close_port($iptables,\@fw_chains,$lond_port,\%iphost,[$lond_port]);
  135:         if ($firewall_result) {
  136:             print "$firewall_result\n";
  137:         }
  138:         &clean_sockets();
  139:     } elsif ($command eq "start") {
  140:         %iphost = &Apache::lonnet::get_iphost();
  141:         my $firewall_result = 
  142:             &LONCAPA::Firewall::firewall_open_port($iptables,\@fw_chains,$lond_port,\%iphost,[$lond_port]);
  143:         if (($firewall_result eq 'ok') || ($firewall_result eq 'inactive firewall')) {
  144:             if ($firewall_result eq 'inactive firewall') {
  145:                 print "WARNING: iptables firewall is currently inactive\n";
  146:             }
  147:             print 'Starting LON-CAPA'."\n";
  148:             print 'Starting LON-CAPA client and daemon processes (please be patient)'.
  149: 	          "\n";
  150:             system("su www -c '/home/httpd/perl/loncron --justcheckdaemons'");
  151:         } else {
  152:             print "Not starting LON-CAPA\n";
  153:             if ($firewall_result eq 'port number unknown') {
  154:                 print "Could not check for status of LON-CAPA port in running firewall - port number unknown.  \n";
  155:             } elsif ($firewall_result) {
  156:                 print "$firewall_result\n";
  157:             }
  158:         }
  159:     } elsif ($command eq "status") {
  160:         %iphost = &Apache::lonnet::get_iphost();
  161:         my $response=`/bin/cat /home/httpd/perl/logs/*.pid 2>&1`;
  162:         if ($response=~/No such file or directory/) {
  163: 	    print 'LON-CAPA is not running.'."\n";
  164:         } else {
  165: 	    print 'LON-CAPA is running.'."\n";
  166: 	    system("su www -c '/home/httpd/perl/loncron --justcheckconnections'");
  167:         }
  168:         if (! &LONCAPA::Firewall::firewall_is_active()) {
  169:             print 'The iptables firewall is not active'."\n";
  170:         }
  171:         if ($lond_port) {
  172:             my $londopen;
  173:             foreach my $fw_chain (@fw_chains) { 
  174:                 my $numopen = &LONCAPA::Firewall::firewall_is_port_open($iptables,$fw_chain,$lond_port,$lond_port,\%iphost);
  175:                 $londopen += $numopen; 
  176:             }
  177:             if ($londopen) {
  178:                 print "The LON-CAPA port ($lond_port) is open in firewall.\n";
  179:             } elsif (&LONCAPA::Firewall::firewall_is_active) {
  180:                 print "The LON-CAPA port ($lond_port) is NOT open in running firewall!\n";
  181:             }
  182:         } else {
  183:             if (&LONCAPA::Firewall::firewall_is_active()) {
  184:                 print "Could not check for status of LON-CAPA port in running firewall - port number unknown.\n";
  185:             } else {
  186:                 print "LON-CAPA port number is unknown, and firewall is not running.\n";
  187:             }
  188:         }
  189:     }
  190: } elsif ($command eq "reload") {
  191:     print 'Reload LON-CAPA config files'."\n";
  192:     system("su www -c '/home/httpd/perl/loncron --justreload'");
  193: } else {
  194:     print "You need to specify one of reload|restart|stop|start|status on the command line.\n";
  195: }

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