Editing
LoadBalancer External Sites: Apache mod balancer
(section)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=Servers= ==modbalancer1== *location: n/a * 10.0.0.50 ==modbalancer2== *location: n/a * 10.0.0.51 == Syncing / Configs == === CRON === * /etc/httpd auto-syncs (unison) every 5 minutes <source> [root@modbalancer1 /]# cat /etc/cron.d/unison SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root HOME=/root/ */5 * * * * root . /root/.bash_profile && /usr/local/bin/sync_configs.sh &> /dev/null </source> <source> #!/bin/bash /usr/bin/unison -terse -batch -auto /etc/httpd/ ssh://root@modbalancer2//etc/httpd/ </source> === Inotify === * Inotify script watches '''/etc/httpd/conf/''' and '''/etc/httpd/conf.d/''' for changes. * If any '''*.conf''' are '''''created / modified / deleted ''''', then -> # Unison will sync modbalancer1 <-> modbalancer2 # Apache will be reloaded on both (ONLY if /etc/rc.d/init.d/configtest is verified) * /etc/rc.local startup <source> #!/bin/sh ## Inotify watcher for HTTPD configs screen -dmS inotify /usr/local/bin/inotify_watcher.pl touch /var/lock/subsys/local </source> <source lang=perl> #!/usr/bin/perl -w use strict; use Linux::Inotify2; use Path::Class; use File::stat; use Data::Dumper; my $debug=1; my $wait=10; ## how many seconds to wait after proccessing an event. This will allow us to group events/updates. (60 seconds default) my @hosts = qw(modbalancer1 modbalancer2 ); ## files must mastch blablabl.$file_ext$ -- for events to fire my @file_ext = qw(conf); ## watched dirs - recursive my @dirs = ("/etc/httpd/conf.d/", "/etc/httpd/conf", ); ## This requires the script from prowl app ## url: http://prowlapp.com/static/prowl.pl my $prowl = {'enabled' => '0', 'script' => '/usr/local/bin/prowl.pl', 'api_key' => '<enter prowl api key>', 'app_name' => 'MOD Balancer', }; ####################################################### Configuration DONE ####################################################### my $inotify = Linux::Inotify2->new or die "unable to create new inotify object: $!"; my $host=`hostname`; ## maybe used for later my $pid=$$; ## on start - lets scan the library ¬ifyProwl('Initializing','program startup'); ## setup initial watch dirs my $dirs =0; my $restart_httpd=0; my @start_list; ## used for notifications my @finish_list; ## used for notifications my @remove_list; ## used for notifications ## Intialize watched dirs - INIT foreach my $drop_dir(@dirs) { &log("Processing subdirs for: $drop_dir",1); my $dir = dir($drop_dir); my $c = 0; $dir->traverse(sub{ my ($child, $cont, $indent) = @_; if ($child->is_dir) { $dirs++; &log("subdir: $child",1); $inotify->watch("$child", IN_CLOSE_WRITE | IN_CREATE | IN_MOVED_TO | IN_DELETE , \&MyWatch); ## in_modify if too verbose.. it grabs any changes to file before final close ## IN_CLOSE_WRITE == after a file is written too/closed ## IN_CREATE == new directories/files ## IN_MOVED_TO == moved directory/files (to watch dirs only) } $cont->($c + 1); }); } ## log/notify status of watched dirs my $init_s = "Watching $dirs directories"; &log($init_s,1); ¬ifyProwl('Initialized',$init_s); ## LOOP to keep watching dirs/files while (1) { $inotify->poll; my $had_event = 0; ## notify on sync start and finish if (@start_list) { my $info = join(', ', @start_list); my $title = 'file Started'; ¬ifyProwl($title,$info); @start_list = (); } if (@finish_list) { my $info = join(', ', @finish_list); my $title = 'file Finished'; ¬ifyProwl($title,$info); @finish_list = (); } if (@remove_list) { ¬ifyProwl('Removed',join(', ', @remove_list)); @remove_list = (); } if ($restart_httpd) { $had_event = 1; $restart_httpd = 0; &restart_HTTPD(); } if ($had_event) { &log("Sleeping '$wait' seconds before processing any new events"); sleep($wait); &log("I\'m awake again - ready to process any new events"); } } sub restart_HTTPD() { &log("Unison sync && Restarting HTTPD"); ¬ifyProwl('unison-HTTPD','sync-restart'); my $out; $out = `/usr/local/bin/sync_configs.sh 2>&1`; ## Verify configs on Node 1 and Node 2 my $failed; foreach my $host (@hosts) { my $check = `ssh $host /etc/init.d/httpd configtest 2>&1`; if ($check !~ /Syntax OK/i) { $failed .= "$host: $check\n"; } } if (!$failed) { ## config passed - reload apache on both foreach my $host (@hosts) { $out .= "$host: "; $out .= `ssh $host -C /etc/init.d/httpd reload 2>&1`; } } else { $out = "HTTPD Config FAIL: cannot reload - $failed"; } &log($out); ¬ifyProwl('unison-HTTPD',$out); } sub MyWatch() { my $event = shift; my $name = $event->fullname; my $file_name = $event->name; my $log = 'unknown'; ## files to skip -- for now, emacs turds if ($file_name =~ /^\..*/i || $file_name =~ /^\#.*/i ) { $log = "SKIPPING: $name [$file_name]"; } ## continue on.. elsif ($event->IN_IGNORED) { ## this is a DIR action ## remove watch - if directory $event->w->cancel; ## cancel watch $log = "DIR $name removed -- cancelling watch"; foreach my $ext (@file_ext) { if ($name =~ /\.$ext$/i) { push (@remove_list,$name); } } } elsif ( $event->IN_DELETE) { ## this is a FILE action ## group deletes together - for notify $log = "FILE: $name removed"; foreach my $ext (@file_ext) { $log = "FILE: $name removed - will notify prowl"; if ($name =~ /\.$ext$/i) { $restart_httpd = 1; ## restart apache - conf file removed push (@remove_list,$name); } } } else { if (-d $name) { $inotify->watch($name, IN_CLOSE_WRITE | IN_CREATE | IN_MOVED_TO , \&MyWatch); $log = "DIR: $name created -- adding to watchlist"; ¬ifyProwl('new',$log); } elsif (-f $name && $event->IN_CREATE) { ## file is created, but has not finished writing - no action needed - just logging $log = "$name is FILE -- in_create called.. waiting for IN_CLOSE_WRITE to process"; foreach my $ext (@file_ext) { ## push new files to start_list (sync started notifications) ## skip notify on this #if ($file_name =~ /\.$ext$/i) { push (@start_list,$name); } } } else { $log = "$name is IN_CLOSE_WRITE" if $event->IN_CLOSE_WRITE; $log = "$name is IN_CREATE" if $event->IN_CREATE; $log = "$name is IN_MOVED_TO" if $event->IN_MOVED_TO; $log = "events for $name have been lost" if $event->IN_Q_OVERFLOW; if ($file_name =~ /^\..*\.\w{5}$/) { &log("$file_name must be rsync - skip it"); } else { ## only update on ext matching @FILES foreach my $ext (@file_ext) { ## push new files to finish_list (sync finished notifications) if ($file_name =~ /\.$ext$/i) { push (@finish_list,"$name"); &log("$name $ext is matched file -- restart httpd"); $restart_httpd = 1; ## restart apache } } if (!$restart_httpd) {&log("$file_name does not match ext of \@files_ext -- skipping httpd restart"); } } } } &log($log); } sub log() { my $msg = shift; my $print= shift; if ($debug || $print) { print localtime() . ": $msg\n"; } system("logger -t $0\[$pid\] \"$msg\""); } sub notifyProwl() { if (defined($prowl->{enabled}) && $prowl->{enabled} == 1) { my ($event,$msg) = @_; my $cmd = sprintf("perl %s -apikey='%s' -application='%s' -event='%s' -notification='%s'",$prowl->{script}, $prowl->{api_key}, $prowl->{app_name}, $event, $msg); my $res = `$cmd`; chomp($cmd); chomp($res); &log("Notify: Prowl Event: $cmd"); &log("Notify: Prowl Result: $res"); } } </source>
Summary:
Please note that all contributions to RARForge may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
RARForge:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Navigation
Home
All Pages
All Files
View Categories
Recent changes
Random page
Edit this menu
Tools
What links here
Related changes
Special pages
Page information