Annotation of loncom/init.d/loncontrol, revision 1.24

1.1       harris41    1: #!/usr/bin/perl
1.2       harris41    2: #
1.24    ! albertel    3: # $Id: loncontrol,v 1.23 2004/12/20 14:28:56 matthew Exp $
1.23      matthew     4: #
1.6       harris41    5: # The LearningOnline Network with CAPA
                      6: #
1.21      matthew     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: #
1.2       harris41   29: # Startup script for the LON-CAPA network processes
1.6       harris41   30: #
1.7       harris41   31: 
1.3       harris41   32: # chkconfig: 345 95 5
1.21      matthew    33: # description: LON-CAPA is a "network of knowledge".  It is used to \
1.6       harris41   34: # distribute knowledge resources and instructional management.
1.2       harris41   35: # processnames: lonc, lond, lonsql
                     36: # pidfiles: /home/httpd/perl/logs/lon*.pid
1.7       harris41   37: # config: /etc/httpd/conf/loncapa.conf
1.2       harris41   38: # config: /home/httpd/lonTabs/hosts.tab
                     39: # config: /home/httpd/lonTabs/spare.tab
                     40: 
1.1       harris41   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: 
1.20      matthew    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';
1.22      matthew    50:     my $lond_port = 5663;
                     51:     my $lonhttpd_port = 8080;
1.20      matthew    52: 
                     53: sub firewall_open_port {
                     54:     return if (! &firewall_is_active);
                     55:     if (! `$iptables -L -n 2>/dev/null | grep $fw_chain | wc -l`) { return; }
                     56:     # iptables is running with our chain
                     57:     #
                     58:     # We could restrict the servers allowed to attempt to communicate
                     59:     # here, but the logistics of updating the /home/httpd/lonTabs/host.tab
                     60:     # file are likely to be a problem
1.22      matthew    61:     foreach my $port ($lond_port,$lonhttpd_port) {
                     62:         print "Opening firewall access on port $port.\n";
                     63: 
                     64:         my $firewall_command = 
                     65:             "$iptables -I $fw_chain -p tcp -d 0/0 --dport $port -j ACCEPT";
                     66:         system($firewall_command);
                     67:         my $return_status = $?>>8;
                     68:         if ($return_status == 1) {
                     69:             # Error
                     70:             print "Error opening port.\n";
                     71:         } elsif ($return_status == 2) {
                     72:             # Bad command
                     73:             print "Bad command error opening port.  Command was\n".
                     74:                 "  ".$firewall_command."\n";
                     75:         }
1.20      matthew    76:     }
1.22      matthew    77:     
1.20      matthew    78: }
                     79: 
                     80: sub firewall_is_port_open {
                     81:     # returns 1 if the firewall port is open, 0 if not.
                     82:     #
                     83:     # check if firewall is active or installed
                     84:     return if (! &firewall_is_active);
                     85:     if (`$iptables -L -n 2>/dev/null | grep "tcp dpt:$port"`) { 
                     86:         return 1;
                     87:     } else {
                     88:         return 0;
                     89:     }
                     90: }
                     91: 
                     92: sub firewall_is_active {
                     93:     if (-e '/proc/net/ip_tables_names') {
                     94:         return 1;
                     95:     } else {
                     96:         return 0;
                     97:     }
                     98: }
                     99: 
                    100: sub firewall_close_port {
                    101:     return if (! &firewall_is_active);
1.22      matthew   102:     foreach my $port ($lond_port,$lonhttpd_port) {
                    103:         print "Closing firewall access on port $port.\n";
                    104:         my $firewall_command = 
                    105:             "$iptables -D $fw_chain -p tcp -d 0/0 --dport $port -j ACCEPT";
                    106:         system($firewall_command);
                    107:         my $return_status = $?>>8;
                    108:         if ($return_status == 1) {
                    109:             # Error
                    110:             print "Error closing port.\n";
                    111:         } elsif ($return_status == 2) {
                    112:             # Bad command
                    113:             print "Bad command error closing port.  Command was\n".
                    114:                 "  ".$firewall_command."\n";
                    115:         }
1.20      matthew   116:     }
                    117: }
                    118: 
                    119: } # End firewall variable scope
                    120: 
1.11      albertel  121: sub stop_daemon {
1.19      albertel  122:     my ($daemon,$killallname)=@_;
1.11      albertel  123:     my $pidfile="/home/httpd/perl/logs/$daemon.pid";
                    124:     
1.24    ! albertel  125:     printf("%-15s ",$daemon);
1.11      albertel  126:     if (-e $pidfile) {
                    127: 	open(PIDFILE,$pidfile);
                    128: 	my $daemonpid=<PIDFILE>;
                    129: 	chomp($daemonpid);
                    130: 	kill TERM => $daemonpid;
                    131: 	sleep 2;
                    132: 	if (kill 0 => $daemonpid) {
                    133: 	    kill KILL => $daemonpid;
                    134: 	    sleep 2;
                    135: 	    if (kill 0 => $daemonpid) {
1.19      albertel  136: 		print("failed to kill");
1.11      albertel  137: 	    } else {
1.19      albertel  138: 		print("killed");
1.11      albertel  139: 	    }
                    140: 	} else {
1.19      albertel  141: 	    print("stopped");
1.11      albertel  142: 	}
1.19      albertel  143:     } else {
                    144: 	print("not running");
                    145:     }
                    146:     system("killall -q -0 $killallname");
                    147:     if ($? == 0) {
                    148: 	system("killall -q $killallname");
                    149: 	print(", killed off extraneous processes");
1.11      albertel  150:     }
1.24    ! albertel  151:     unlink($pidfile);
1.19      albertel  152:     print("\n");
1.11      albertel  153: }
                    154: 
1.20      matthew   155: 
1.16      albertel  156: if (($command eq "restartold") or ($command eq "reloadold")) {
1.6       harris41  157:     print 'Restarting LON-CAPA'."\n";
                    158:     print 'Ending LON-CAPA client and daemon processes'."\n";
1.24    ! albertel  159:     foreach my $daemon ('lonsql','lond','lonc','lonhttpd','lonmemcached') {
1.19      albertel  160: 	&stop_daemon($daemon,$daemon);
1.11      albertel  161:     }
1.6       harris41  162:     print 'Starting LON-CAPA client and daemon processes (please be patient)'.
                    163: 	"\n";
1.18      albertel  164:     system("su www -c '/home/httpd/perl/loncron --oldlonc --justcheckdaemons'");
1.16      albertel  165: } elsif (($command eq "restart") or ($command eq "reload")) {
1.12      albertel  166:     print 'Restarting LON-CAPA'."\n";
                    167:     print 'Ending LON-CAPA client and daemon processes'."\n";
1.24    ! albertel  168:     foreach my $daemon ('lonsql','lond','lonc','lonhttpd','lonmemcached') {
1.19      albertel  169: 	my $killallname=$daemon;
                    170: 	if ($daemon eq 'lonc') { $killallname='loncnew'; }
                    171: 	&stop_daemon($daemon,$killallname);
1.12      albertel  172:     }
                    173:     print 'Starting LON-CAPA client and daemon processes (please be patient)'.
                    174: 	"\n";
1.18      albertel  175:     system("su www -c '/home/httpd/perl/loncron --justcheckdaemons'");
1.16      albertel  176: } elsif ($command eq "stop") {
1.6       harris41  177:     print 'Stopping LON-CAPA'."\n";
1.24    ! albertel  178:     foreach my $daemon ('lonsql','lond','lonc','lonhttpd','lonmemcached') {
1.19      albertel  179: 	my $killallname=$daemon;
                    180: 	if ($daemon eq 'lonc') { $killallname='loncnew'; }
                    181: 	&stop_daemon($daemon,$killallname);
1.11      albertel  182:     }
1.20      matthew   183:     &firewall_close_port();
1.16      albertel  184: } elsif ($command eq "startold") {
1.20      matthew   185:     &firewall_open_port();
1.6       harris41  186:     print 'Starting LON-CAPA'."\n";
                    187:     print 'Starting LON-CAPA client and daemon processes (please be patient)'.
                    188: 	"\n"; 
1.18      albertel  189:     system("su www -c '/home/httpd/perl/loncron --oldlonc --justcheckdaemons'");
1.16      albertel  190: } elsif ($command eq "start") {
1.20      matthew   191:     &firewall_open_port();
1.12      albertel  192:     print 'Starting LON-CAPA'."\n";
                    193:     print 'Starting LON-CAPA client and daemon processes (please be patient)'.
                    194: 	"\n"; 
1.18      albertel  195:     system("su www -c '/home/httpd/perl/loncron --justcheckdaemons'");
1.16      albertel  196: } elsif ($command eq "status") {
1.1       harris41  197:     $response=`/bin/cat /home/httpd/perl/logs/*.pid 2>&1`;
                    198:     if ($response=~/No such file or directory/) {
1.6       harris41  199: 	print 'LON-CAPA is not running.'."\n";
1.18      albertel  200:     } else {
1.6       harris41  201: 	print 'LON-CAPA is running.'."\n";
1.18      albertel  202: 	system("su www -c '/home/httpd/perl/loncron --justcheckconnections'");
1.1       harris41  203:     }
1.20      matthew   204:     if (! &firewall_is_active) {
                    205:         print 'The iptables firewall is not active'."\n";
                    206:     }
                    207:     if (&firewall_is_port_open()) {
                    208:         print 'The LON-CAPA port is open in firewall.'."\n";
                    209:     } elsif (&firewall_is_active) {
                    210:         print 'The LON-CAPA port is NOT open in running firewall!'."\n";
                    211:     }
1.16      albertel  212: } else {
1.18      albertel  213:     print 'You need to specify one of restart|stop|start|status on the command line.'."\n";
1.1       harris41  214: }

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