]> git.8kb.co.uk Git - slony-i/slony_failover/commitdiff
Initial commit
authorglyn <glyn@8kb.co.uk>
Tue, 6 May 2014 14:58:49 +0000 (15:58 +0100)
committerglyn <glyn@8kb.co.uk>
Tue, 6 May 2014 14:58:49 +0000 (15:58 +0100)
.gitignore [new file with mode: 0644]
LICENCE [new file with mode: 0644]
README.md [new file with mode: 0644]
failover_log_database.sql [new file with mode: 0644]
init.debian [new file with mode: 0644]
slony_failover.conf [new file with mode: 0644]
slony_failover.conf.minimal [new file with mode: 0644]
slony_failover.pl [new file with mode: 0644]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..ff37810
--- /dev/null
@@ -0,0 +1,2 @@
+.DS_Store
+Thumb.db
diff --git a/LICENCE b/LICENCE
new file mode 100644 (file)
index 0000000..94a9ed0
--- /dev/null
+++ b/LICENCE
@@ -0,0 +1,674 @@
+                    GNU GENERAL PUBLIC LICENSE
+                       Version 3, 29 June 2007
+
+ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
+ Everyone is permitted to copy and distribute verbatim copies
+ of this license document, but changing it is not allowed.
+
+                            Preamble
+
+  The GNU General Public License is a free, copyleft license for
+software and other kinds of works.
+
+  The licenses for most software and other practical works are designed
+to take away your freedom to share and change the works.  By contrast,
+the GNU General Public License is intended to guarantee your freedom to
+share and change all versions of a program--to make sure it remains free
+software for all its users.  We, the Free Software Foundation, use the
+GNU General Public License for most of our software; it applies also to
+any other work released this way by its authors.  You can apply it to
+your programs, too.
+
+  When we speak of free software, we are referring to freedom, not
+price.  Our General Public Licenses are designed to make sure that you
+have the freedom to distribute copies of free software (and charge for
+them if you wish), that you receive source code or can get it if you
+want it, that you can change the software or use pieces of it in new
+free programs, and that you know you can do these things.
+
+  To protect your rights, we need to prevent others from denying you
+these rights or asking you to surrender the rights.  Therefore, you have
+certain responsibilities if you distribute copies of the software, or if
+you modify it: responsibilities to respect the freedom of others.
+
+  For example, if you distribute copies of such a program, whether
+gratis or for a fee, you must pass on to the recipients the same
+freedoms that you received.  You must make sure that they, too, receive
+or can get the source code.  And you must show them these terms so they
+know their rights.
+
+  Developers that use the GNU GPL protect your rights with two steps:
+(1) assert copyright on the software, and (2) offer you this License
+giving you legal permission to copy, distribute and/or modify it.
+
+  For the developers' and authors' protection, the GPL clearly explains
+that there is no warranty for this free software.  For both users' and
+authors' sake, the GPL requires that modified versions be marked as
+changed, so that their problems will not be attributed erroneously to
+authors of previous versions.
+
+  Some devices are designed to deny users access to install or run
+modified versions of the software inside them, although the manufacturer
+can do so.  This is fundamentally incompatible with the aim of
+protecting users' freedom to change the software.  The systematic
+pattern of such abuse occurs in the area of products for individuals to
+use, which is precisely where it is most unacceptable.  Therefore, we
+have designed this version of the GPL to prohibit the practice for those
+products.  If such problems arise substantially in other domains, we
+stand ready to extend this provision to those domains in future versions
+of the GPL, as needed to protect the freedom of users.
+
+  Finally, every program is threatened constantly by software patents.
+States should not allow patents to restrict development and use of
+software on general-purpose computers, but in those that do, we wish to
+avoid the special danger that patents applied to a free program could
+make it effectively proprietary.  To prevent this, the GPL assures that
+patents cannot be used to render the program non-free.
+
+  The precise terms and conditions for copying, distribution and
+modification follow.
+
+                       TERMS AND CONDITIONS
+
+  0. Definitions.
+
+  "This License" refers to version 3 of the GNU General Public License.
+
+  "Copyright" also means copyright-like laws that apply to other kinds of
+works, such as semiconductor masks.
+
+  "The Program" refers to any copyrightable work licensed under this
+License.  Each licensee is addressed as "you".  "Licensees" and
+"recipients" may be individuals or organizations.
+
+  To "modify" a work means to copy from or adapt all or part of the work
+in a fashion requiring copyright permission, other than the making of an
+exact copy.  The resulting work is called a "modified version" of the
+earlier work or a work "based on" the earlier work.
+
+  A "covered work" means either the unmodified Program or a work based
+on the Program.
+
+  To "propagate" a work means to do anything with it that, without
+permission, would make you directly or secondarily liable for
+infringement under applicable copyright law, except executing it on a
+computer or modifying a private copy.  Propagation includes copying,
+distribution (with or without modification), making available to the
+public, and in some countries other activities as well.
+
+  To "convey" a work means any kind of propagation that enables other
+parties to make or receive copies.  Mere interaction with a user through
+a computer network, with no transfer of a copy, is not conveying.
+
+  An interactive user interface displays "Appropriate Legal Notices"
+to the extent that it includes a convenient and prominently visible
+feature that (1) displays an appropriate copyright notice, and (2)
+tells the user that there is no warranty for the work (except to the
+extent that warranties are provided), that licensees may convey the
+work under this License, and how to view a copy of this License.  If
+the interface presents a list of user commands or options, such as a
+menu, a prominent item in the list meets this criterion.
+
+  1. Source Code.
+
+  The "source code" for a work means the preferred form of the work
+for making modifications to it.  "Object code" means any non-source
+form of a work.
+
+  A "Standard Interface" means an interface that either is an official
+standard defined by a recognized standards body, or, in the case of
+interfaces specified for a particular programming language, one that
+is widely used among developers working in that language.
+
+  The "System Libraries" of an executable work include anything, other
+than the work as a whole, that (a) is included in the normal form of
+packaging a Major Component, but which is not part of that Major
+Component, and (b) serves only to enable use of the work with that
+Major Component, or to implement a Standard Interface for which an
+implementation is available to the public in source code form.  A
+"Major Component", in this context, means a major essential component
+(kernel, window system, and so on) of the specific operating system
+(if any) on which the executable work runs, or a compiler used to
+produce the work, or an object code interpreter used to run it.
+
+  The "Corresponding Source" for a work in object code form means all
+the source code needed to generate, install, and (for an executable
+work) run the object code and to modify the work, including scripts to
+control those activities.  However, it does not include the work's
+System Libraries, or general-purpose tools or generally available free
+programs which are used unmodified in performing those activities but
+which are not part of the work.  For example, Corresponding Source
+includes interface definition files associated with source files for
+the work, and the source code for shared libraries and dynamically
+linked subprograms that the work is specifically designed to require,
+such as by intimate data communication or control flow between those
+subprograms and other parts of the work.
+
+  The Corresponding Source need not include anything that users
+can regenerate automatically from other parts of the Corresponding
+Source.
+
+  The Corresponding Source for a work in source code form is that
+same work.
+
+  2. Basic Permissions.
+
+  All rights granted under this License are granted for the term of
+copyright on the Program, and are irrevocable provided the stated
+conditions are met.  This License explicitly affirms your unlimited
+permission to run the unmodified Program.  The output from running a
+covered work is covered by this License only if the output, given its
+content, constitutes a covered work.  This License acknowledges your
+rights of fair use or other equivalent, as provided by copyright law.
+
+  You may make, run and propagate covered works that you do not
+convey, without conditions so long as your license otherwise remains
+in force.  You may convey covered works to others for the sole purpose
+of having them make modifications exclusively for you, or provide you
+with facilities for running those works, provided that you comply with
+the terms of this License in conveying all material for which you do
+not control copyright.  Those thus making or running the covered works
+for you must do so exclusively on your behalf, under your direction
+and control, on terms that prohibit them from making any copies of
+your copyrighted material outside their relationship with you.
+
+  Conveying under any other circumstances is permitted solely under
+the conditions stated below.  Sublicensing is not allowed; section 10
+makes it unnecessary.
+
+  3. Protecting Users' Legal Rights From Anti-Circumvention Law.
+
+  No covered work shall be deemed part of an effective technological
+measure under any applicable law fulfilling obligations under article
+11 of the WIPO copyright treaty adopted on 20 December 1996, or
+similar laws prohibiting or restricting circumvention of such
+measures.
+
+  When you convey a covered work, you waive any legal power to forbid
+circumvention of technological measures to the extent such circumvention
+is effected by exercising rights under this License with respect to
+the covered work, and you disclaim any intention to limit operation or
+modification of the work as a means of enforcing, against the work's
+users, your or third parties' legal rights to forbid circumvention of
+technological measures.
+
+  4. Conveying Verbatim Copies.
+
+  You may convey verbatim copies of the Program's source code as you
+receive it, in any medium, provided that you conspicuously and
+appropriately publish on each copy an appropriate copyright notice;
+keep intact all notices stating that this License and any
+non-permissive terms added in accord with section 7 apply to the code;
+keep intact all notices of the absence of any warranty; and give all
+recipients a copy of this License along with the Program.
+
+  You may charge any price or no price for each copy that you convey,
+and you may offer support or warranty protection for a fee.
+
+  5. Conveying Modified Source Versions.
+
+  You may convey a work based on the Program, or the modifications to
+produce it from the Program, in the form of source code under the
+terms of section 4, provided that you also meet all of these conditions:
+
+    a) The work must carry prominent notices stating that you modified
+    it, and giving a relevant date.
+
+    b) The work must carry prominent notices stating that it is
+    released under this License and any conditions added under section
+    7.  This requirement modifies the requirement in section 4 to
+    "keep intact all notices".
+
+    c) You must license the entire work, as a whole, under this
+    License to anyone who comes into possession of a copy.  This
+    License will therefore apply, along with any applicable section 7
+    additional terms, to the whole of the work, and all its parts,
+    regardless of how they are packaged.  This License gives no
+    permission to license the work in any other way, but it does not
+    invalidate such permission if you have separately received it.
+
+    d) If the work has interactive user interfaces, each must display
+    Appropriate Legal Notices; however, if the Program has interactive
+    interfaces that do not display Appropriate Legal Notices, your
+    work need not make them do so.
+
+  A compilation of a covered work with other separate and independent
+works, which are not by their nature extensions of the covered work,
+and which are not combined with it such as to form a larger program,
+in or on a volume of a storage or distribution medium, is called an
+"aggregate" if the compilation and its resulting copyright are not
+used to limit the access or legal rights of the compilation's users
+beyond what the individual works permit.  Inclusion of a covered work
+in an aggregate does not cause this License to apply to the other
+parts of the aggregate.
+
+  6. Conveying Non-Source Forms.
+
+  You may convey a covered work in object code form under the terms
+of sections 4 and 5, provided that you also convey the
+machine-readable Corresponding Source under the terms of this License,
+in one of these ways:
+
+    a) Convey the object code in, or embodied in, a physical product
+    (including a physical distribution medium), accompanied by the
+    Corresponding Source fixed on a durable physical medium
+    customarily used for software interchange.
+
+    b) Convey the object code in, or embodied in, a physical product
+    (including a physical distribution medium), accompanied by a
+    written offer, valid for at least three years and valid for as
+    long as you offer spare parts or customer support for that product
+    model, to give anyone who possesses the object code either (1) a
+    copy of the Corresponding Source for all the software in the
+    product that is covered by this License, on a durable physical
+    medium customarily used for software interchange, for a price no
+    more than your reasonable cost of physically performing this
+    conveying of source, or (2) access to copy the
+    Corresponding Source from a network server at no charge.
+
+    c) Convey individual copies of the object code with a copy of the
+    written offer to provide the Corresponding Source.  This
+    alternative is allowed only occasionally and noncommercially, and
+    only if you received the object code with such an offer, in accord
+    with subsection 6b.
+
+    d) Convey the object code by offering access from a designated
+    place (gratis or for a charge), and offer equivalent access to the
+    Corresponding Source in the same way through the same place at no
+    further charge.  You need not require recipients to copy the
+    Corresponding Source along with the object code.  If the place to
+    copy the object code is a network server, the Corresponding Source
+    may be on a different server (operated by you or a third party)
+    that supports equivalent copying facilities, provided you maintain
+    clear directions next to the object code saying where to find the
+    Corresponding Source.  Regardless of what server hosts the
+    Corresponding Source, you remain obligated to ensure that it is
+    available for as long as needed to satisfy these requirements.
+
+    e) Convey the object code using peer-to-peer transmission, provided
+    you inform other peers where the object code and Corresponding
+    Source of the work are being offered to the general public at no
+    charge under subsection 6d.
+
+  A separable portion of the object code, whose source code is excluded
+from the Corresponding Source as a System Library, need not be
+included in conveying the object code work.
+
+  A "User Product" is either (1) a "consumer product", which means any
+tangible personal property which is normally used for personal, family,
+or household purposes, or (2) anything designed or sold for incorporation
+into a dwelling.  In determining whether a product is a consumer product,
+doubtful cases shall be resolved in favor of coverage.  For a particular
+product received by a particular user, "normally used" refers to a
+typical or common use of that class of product, regardless of the status
+of the particular user or of the way in which the particular user
+actually uses, or expects or is expected to use, the product.  A product
+is a consumer product regardless of whether the product has substantial
+commercial, industrial or non-consumer uses, unless such uses represent
+the only significant mode of use of the product.
+
+  "Installation Information" for a User Product means any methods,
+procedures, authorization keys, or other information required to install
+and execute modified versions of a covered work in that User Product from
+a modified version of its Corresponding Source.  The information must
+suffice to ensure that the continued functioning of the modified object
+code is in no case prevented or interfered with solely because
+modification has been made.
+
+  If you convey an object code work under this section in, or with, or
+specifically for use in, a User Product, and the conveying occurs as
+part of a transaction in which the right of possession and use of the
+User Product is transferred to the recipient in perpetuity or for a
+fixed term (regardless of how the transaction is characterized), the
+Corresponding Source conveyed under this section must be accompanied
+by the Installation Information.  But this requirement does not apply
+if neither you nor any third party retains the ability to install
+modified object code on the User Product (for example, the work has
+been installed in ROM).
+
+  The requirement to provide Installation Information does not include a
+requirement to continue to provide support service, warranty, or updates
+for a work that has been modified or installed by the recipient, or for
+the User Product in which it has been modified or installed.  Access to a
+network may be denied when the modification itself materially and
+adversely affects the operation of the network or violates the rules and
+protocols for communication across the network.
+
+  Corresponding Source conveyed, and Installation Information provided,
+in accord with this section must be in a format that is publicly
+documented (and with an implementation available to the public in
+source code form), and must require no special password or key for
+unpacking, reading or copying.
+
+  7. Additional Terms.
+
+  "Additional permissions" are terms that supplement the terms of this
+License by making exceptions from one or more of its conditions.
+Additional permissions that are applicable to the entire Program shall
+be treated as though they were included in this License, to the extent
+that they are valid under applicable law.  If additional permissions
+apply only to part of the Program, that part may be used separately
+under those permissions, but the entire Program remains governed by
+this License without regard to the additional permissions.
+
+  When you convey a copy of a covered work, you may at your option
+remove any additional permissions from that copy, or from any part of
+it.  (Additional permissions may be written to require their own
+removal in certain cases when you modify the work.)  You may place
+additional permissions on material, added by you to a covered work,
+for which you have or can give appropriate copyright permission.
+
+  Notwithstanding any other provision of this License, for material you
+add to a covered work, you may (if authorized by the copyright holders of
+that material) supplement the terms of this License with terms:
+
+    a) Disclaiming warranty or limiting liability differently from the
+    terms of sections 15 and 16 of this License; or
+
+    b) Requiring preservation of specified reasonable legal notices or
+    author attributions in that material or in the Appropriate Legal
+    Notices displayed by works containing it; or
+
+    c) Prohibiting misrepresentation of the origin of that material, or
+    requiring that modified versions of such material be marked in
+    reasonable ways as different from the original version; or
+
+    d) Limiting the use for publicity purposes of names of licensors or
+    authors of the material; or
+
+    e) Declining to grant rights under trademark law for use of some
+    trade names, trademarks, or service marks; or
+
+    f) Requiring indemnification of licensors and authors of that
+    material by anyone who conveys the material (or modified versions of
+    it) with contractual assumptions of liability to the recipient, for
+    any liability that these contractual assumptions directly impose on
+    those licensors and authors.
+
+  All other non-permissive additional terms are considered "further
+restrictions" within the meaning of section 10.  If the Program as you
+received it, or any part of it, contains a notice stating that it is
+governed by this License along with a term that is a further
+restriction, you may remove that term.  If a license document contains
+a further restriction but permits relicensing or conveying under this
+License, you may add to a covered work material governed by the terms
+of that license document, provided that the further restriction does
+not survive such relicensing or conveying.
+
+  If you add terms to a covered work in accord with this section, you
+must place, in the relevant source files, a statement of the
+additional terms that apply to those files, or a notice indicating
+where to find the applicable terms.
+
+  Additional terms, permissive or non-permissive, may be stated in the
+form of a separately written license, or stated as exceptions;
+the above requirements apply either way.
+
+  8. Termination.
+
+  You may not propagate or modify a covered work except as expressly
+provided under this License.  Any attempt otherwise to propagate or
+modify it is void, and will automatically terminate your rights under
+this License (including any patent licenses granted under the third
+paragraph of section 11).
+
+  However, if you cease all violation of this License, then your
+license from a particular copyright holder is reinstated (a)
+provisionally, unless and until the copyright holder explicitly and
+finally terminates your license, and (b) permanently, if the copyright
+holder fails to notify you of the violation by some reasonable means
+prior to 60 days after the cessation.
+
+  Moreover, your license from a particular copyright holder is
+reinstated permanently if the copyright holder notifies you of the
+violation by some reasonable means, this is the first time you have
+received notice of violation of this License (for any work) from that
+copyright holder, and you cure the violation prior to 30 days after
+your receipt of the notice.
+
+  Termination of your rights under this section does not terminate the
+licenses of parties who have received copies or rights from you under
+this License.  If your rights have been terminated and not permanently
+reinstated, you do not qualify to receive new licenses for the same
+material under section 10.
+
+  9. Acceptance Not Required for Having Copies.
+
+  You are not required to accept this License in order to receive or
+run a copy of the Program.  Ancillary propagation of a covered work
+occurring solely as a consequence of using peer-to-peer transmission
+to receive a copy likewise does not require acceptance.  However,
+nothing other than this License grants you permission to propagate or
+modify any covered work.  These actions infringe copyright if you do
+not accept this License.  Therefore, by modifying or propagating a
+covered work, you indicate your acceptance of this License to do so.
+
+  10. Automatic Licensing of Downstream Recipients.
+
+  Each time you convey a covered work, the recipient automatically
+receives a license from the original licensors, to run, modify and
+propagate that work, subject to this License.  You are not responsible
+for enforcing compliance by third parties with this License.
+
+  An "entity transaction" is a transaction transferring control of an
+organization, or substantially all assets of one, or subdividing an
+organization, or merging organizations.  If propagation of a covered
+work results from an entity transaction, each party to that
+transaction who receives a copy of the work also receives whatever
+licenses to the work the party's predecessor in interest had or could
+give under the previous paragraph, plus a right to possession of the
+Corresponding Source of the work from the predecessor in interest, if
+the predecessor has it or can get it with reasonable efforts.
+
+  You may not impose any further restrictions on the exercise of the
+rights granted or affirmed under this License.  For example, you may
+not impose a license fee, royalty, or other charge for exercise of
+rights granted under this License, and you may not initiate litigation
+(including a cross-claim or counterclaim in a lawsuit) alleging that
+any patent claim is infringed by making, using, selling, offering for
+sale, or importing the Program or any portion of it.
+
+  11. Patents.
+
+  A "contributor" is a copyright holder who authorizes use under this
+License of the Program or a work on which the Program is based.  The
+work thus licensed is called the contributor's "contributor version".
+
+  A contributor's "essential patent claims" are all patent claims
+owned or controlled by the contributor, whether already acquired or
+hereafter acquired, that would be infringed by some manner, permitted
+by this License, of making, using, or selling its contributor version,
+but do not include claims that would be infringed only as a
+consequence of further modification of the contributor version.  For
+purposes of this definition, "control" includes the right to grant
+patent sublicenses in a manner consistent with the requirements of
+this License.
+
+  Each contributor grants you a non-exclusive, worldwide, royalty-free
+patent license under the contributor's essential patent claims, to
+make, use, sell, offer for sale, import and otherwise run, modify and
+propagate the contents of its contributor version.
+
+  In the following three paragraphs, a "patent license" is any express
+agreement or commitment, however denominated, not to enforce a patent
+(such as an express permission to practice a patent or covenant not to
+sue for patent infringement).  To "grant" such a patent license to a
+party means to make such an agreement or commitment not to enforce a
+patent against the party.
+
+  If you convey a covered work, knowingly relying on a patent license,
+and the Corresponding Source of the work is not available for anyone
+to copy, free of charge and under the terms of this License, through a
+publicly available network server or other readily accessible means,
+then you must either (1) cause the Corresponding Source to be so
+available, or (2) arrange to deprive yourself of the benefit of the
+patent license for this particular work, or (3) arrange, in a manner
+consistent with the requirements of this License, to extend the patent
+license to downstream recipients.  "Knowingly relying" means you have
+actual knowledge that, but for the patent license, your conveying the
+covered work in a country, or your recipient's use of the covered work
+in a country, would infringe one or more identifiable patents in that
+country that you have reason to believe are valid.
+
+  If, pursuant to or in connection with a single transaction or
+arrangement, you convey, or propagate by procuring conveyance of, a
+covered work, and grant a patent license to some of the parties
+receiving the covered work authorizing them to use, propagate, modify
+or convey a specific copy of the covered work, then the patent license
+you grant is automatically extended to all recipients of the covered
+work and works based on it.
+
+  A patent license is "discriminatory" if it does not include within
+the scope of its coverage, prohibits the exercise of, or is
+conditioned on the non-exercise of one or more of the rights that are
+specifically granted under this License.  You may not convey a covered
+work if you are a party to an arrangement with a third party that is
+in the business of distributing software, under which you make payment
+to the third party based on the extent of your activity of conveying
+the work, and under which the third party grants, to any of the
+parties who would receive the covered work from you, a discriminatory
+patent license (a) in connection with copies of the covered work
+conveyed by you (or copies made from those copies), or (b) primarily
+for and in connection with specific products or compilations that
+contain the covered work, unless you entered into that arrangement,
+or that patent license was granted, prior to 28 March 2007.
+
+  Nothing in this License shall be construed as excluding or limiting
+any implied license or other defenses to infringement that may
+otherwise be available to you under applicable patent law.
+
+  12. No Surrender of Others' Freedom.
+
+  If conditions are imposed on you (whether by court order, agreement or
+otherwise) that contradict the conditions of this License, they do not
+excuse you from the conditions of this License.  If you cannot convey a
+covered work so as to satisfy simultaneously your obligations under this
+License and any other pertinent obligations, then as a consequence you may
+not convey it at all.  For example, if you agree to terms that obligate you
+to collect a royalty for further conveying from those to whom you convey
+the Program, the only way you could satisfy both those terms and this
+License would be to refrain entirely from conveying the Program.
+
+  13. Use with the GNU Affero General Public License.
+
+  Notwithstanding any other provision of this License, you have
+permission to link or combine any covered work with a work licensed
+under version 3 of the GNU Affero General Public License into a single
+combined work, and to convey the resulting work.  The terms of this
+License will continue to apply to the part which is the covered work,
+but the special requirements of the GNU Affero General Public License,
+section 13, concerning interaction through a network will apply to the
+combination as such.
+
+  14. Revised Versions of this License.
+
+  The Free Software Foundation may publish revised and/or new versions of
+the GNU General Public License from time to time.  Such new versions will
+be similar in spirit to the present version, but may differ in detail to
+address new problems or concerns.
+
+  Each version is given a distinguishing version number.  If the
+Program specifies that a certain numbered version of the GNU General
+Public License "or any later version" applies to it, you have the
+option of following the terms and conditions either of that numbered
+version or of any later version published by the Free Software
+Foundation.  If the Program does not specify a version number of the
+GNU General Public License, you may choose any version ever published
+by the Free Software Foundation.
+
+  If the Program specifies that a proxy can decide which future
+versions of the GNU General Public License can be used, that proxy's
+public statement of acceptance of a version permanently authorizes you
+to choose that version for the Program.
+
+  Later license versions may give you additional or different
+permissions.  However, no additional obligations are imposed on any
+author or copyright holder as a result of your choosing to follow a
+later version.
+
+  15. Disclaimer of Warranty.
+
+  THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
+APPLICABLE LAW.  EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
+HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
+OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
+THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
+IS WITH YOU.  SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
+ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
+
+  16. Limitation of Liability.
+
+  IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
+WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
+THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
+GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
+USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
+DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
+PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
+EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
+SUCH DAMAGES.
+
+  17. Interpretation of Sections 15 and 16.
+
+  If the disclaimer of warranty and limitation of liability provided
+above cannot be given local legal effect according to their terms,
+reviewing courts shall apply local law that most closely approximates
+an absolute waiver of all civil liability in connection with the
+Program, unless a warranty or assumption of liability accompanies a
+copy of the Program in return for a fee.
+
+                     END OF TERMS AND CONDITIONS
+
+            How to Apply These Terms to Your New Programs
+
+  If you develop a new program, and you want it to be of the greatest
+possible use to the public, the best way to achieve this is to make it
+free software which everyone can redistribute and change under these terms.
+
+  To do so, attach the following notices to the program.  It is safest
+to attach them to the start of each source file to most effectively
+state the exclusion of warranty; and each file should have at least
+the "copyright" line and a pointer to where the full notice is found.
+
+    <one line to give the program's name and a brief idea of what it does.>
+    Copyright (C) <year>  <name of author>
+
+    This program 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 3 of the License, or
+    (at your option) any later version.
+
+    This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
+
+Also add information on how to contact you by electronic and paper mail.
+
+  If the program does terminal interaction, make it output a short
+notice like this when it starts in an interactive mode:
+
+    <program>  Copyright (C) <year>  <name of author>
+    This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
+    This is free software, and you are welcome to redistribute it
+    under certain conditions; type `show c' for details.
+
+The hypothetical commands `show w' and `show c' should show the appropriate
+parts of the General Public License.  Of course, your program's commands
+might be different; for a GUI interface, you would use an "about box".
+
+  You should also get your employer (if you work as a programmer) or school,
+if any, to sign a "copyright disclaimer" for the program, if necessary.
+For more information on this, and how to apply and follow the GNU GPL, see
+<http://www.gnu.org/licenses/>.
+
+  The GNU General Public License does not permit incorporating your program
+into proprietary programs.  If your program is a subroutine library, you
+may consider it more useful to permit linking proprietary applications with
+the library.  If this is what you want to do, use the GNU Lesser General
+Public License instead of this License.  But first, please read
+<http://www.gnu.org/philosophy/why-not-lgpl.html>.
diff --git a/README.md b/README.md
new file mode 100644 (file)
index 0000000..7f21728
--- /dev/null
+++ b/README.md
@@ -0,0 +1,118 @@
+#Slony-I Failover / Switchover 
+
+Perl script to assist with performing switchover and failover of replication sets 
+in PostgreSQL databases replicated using Slony-I.
+
+The script can be run in interactive mode to suggest switchover or failover and
+will create a slonik script to perform the suggested action.
+
+It's hard to put together a script for all situations as different Slony 
+configurations can have different complexities (hence the existance of slonik), 
+but this script is intended to be used for building and running slonik scripts 
+to move all sets from one node to another.
+
+There is also an autofailover mode which will sit and poll each node and perform
+a failover of failed nodes.  This mode should be assumed as experimental, as 
+there can be guite a few decisions to be made when failing over different setups.
+
+##Example usage
+
+Launch interactive mode with command line parameters:
+
+```bash
+$ ./slony_failover.pl -h localhost -db TEST -cl test_replication
+```
+
+Launch with configuration file, a minimal configuration file would be:
+
+    slony_database_host = localhost
+    slony_database_name = TEST
+    slony_cluster_name = test_replication
+
+```bash
+$ ./slony_failover.pl -f slony_failover.conf
+```
+Run as a daemon in debian:
+
+```bash
+$ sudo cp init.debian /etc/init.d/slony_failover
+$ cp slony_failover.conf /var/slony/failover/slony_failover.conf
+$ sudo chmod +x /etc/init.d/slony_failover
+$ sudo update-rc.d slony_failover start 99 2 3 4 5 . stop 24 0 1 6 
+$ sudo invoke-rc.d slony_failover start
+```
+
+##Command line parameters
+
+```bash
+$ ./failover.pl [options]
+```
+
+|Switch    |
+|----------|------------------------------------------
+|-f        |Read all configuration from config file
+|-h        |Host running PostgreSQL instance to read state of Slony-I cluster from
+|-p        |Port of above PostgreSQL database instance
+|-db       |Name of above PostgreSQL database instance 
+|-cl       |Name of Slony-I cluster
+|-u        |User to connect to above PostgreSQL database instance
+|-P        |Password for above user (Use .pgpass instead where possible)
+|-i        |Print information about slony cluster and exit
+
+##Configuration file parameters
+
+| Section     | Parameter                                    | Type                          | Default                         | Comment
+|:------------|:-------------------------------------------- |:------------------------------|:--------------------------------|:-----------------------------------
+| General     |**lang**                                      | en/fr                         | *'en'*                          | The language to print messages in, currently only english and french
+| General     |**prefix_directory**                          | /full/path/to/directory       | *'/tmp/slony_failovers'*        | Working directory for script to generate slonik scripts and log files
+| General     |**separate_working_directory**                | boolean                       | *'true'*                        | Append a separate working directory to the prefix_directory for each run
+| General     |**slonik_path**                               | /full/path/to/bin/directory   | *<null>*                        | Slonik binary if not in current path
+| General     |**pid_filename**                              | /path/to/pidfile              | *'/var/run/slony_failover.pid'* | Pid file to use when running in autofailover mode
+| General     |**enable_try_blocks**                         | boolean                       | *false*                         |    Write slonik script with try blocks where possible to aid error handling
+| General     |**lockset_method**                            | single/multiple               | *'multiple'*                    | Write slonik script that locks all sets
+| General     |**pull_aliases_from_comments**                | boolean                       | *false*                         | If true, script will pull text from between parentheses in comments fields
+|             |                                              |                               |                                     | and use to generate (possibly) meaningful aliases for nodes and sets.
+| General     |**log_line_prefix**                           | text                          | *<null>*                        | Prefix to add to log lines, special values:
+|             |                                              |                               |                                 |     %p = process ID
+|             |                                              |                               |                                 |     %t = timestamp without milliseconds
+|             |                                              |                               |                                 |     %m = timestamp with milliseconds
+| General     |**failover_offline_subscriber_only**          | boolean                       | *false*                         | If set to true any subscriber only nodes that are unavailable at the time 
+|             |                                              |                               |                                 | of failover will also be failed over.  If false any such nodes will be 
+|             |                                              |                               |                                 | excluded from the preamble and not failed over, however this may be problematic
+|             |                                              |                               |                                 | especially in the case where the most up to date node is the unavailable one.
+| General     |**drop_failed_nodes**                         | boolean                       | *false*                         | After failover automatically drop the failed nodes.
+| Slon Config |**slony_database_host**                       | IP Address/Hostname           | *<null>*                        | PostgreSQL Hostname of database to read Slony configuration from
+| Slon Config |**slony_database_port**                       | integer                       | *5432*                          | PostgreSQL Port of database to read Slony configuration from
+| Slon Config |**slony_database_name**                       | name                          | *<null>*                        | PostgreSQL database name to read Slony configuration from 
+| Slon Config |**slony_database_user**                       | username                      | *'slony'*                       | Username to use to connect when reading Slony configuration
+| Slon Config |**slony_database_password**                   | password                      | *''*                            | Recommended to leave blank and use .pgpass file
+| Slon Config |**slony_cluster_name**                        | name                          | *<null>*                        | Name of Slony-I cluster to read configuration for 
+| Logging     |**enable_debugging**                          | boolean                       | *false*                         | Enable printing of debug messages to stdout
+| Logging     |**log_filename**                              | base file name                | *'failover.log'*                | File name to use for script process logging, special values as per strftime spec
+| Logging     |**log_to_postgresql**                         | boolean                       | *false*                         | Store details of failover script runs in a postgresql database
+| Logging     |**log_database_host**                         | IP Address/Hostname           | *<null>*                        | PostgreSQL Hostname of logging database
+| Logging     |**log_database_port**                         | integer                       | *<null>*                        | PostgreSQL Port of logging database
+| Logging     |**log_database_name**                         | name                          | *<null>*                        | PostgreSQL database name of logging database
+| Logging     |**log_database_user**                         | username                      | *<null>*                        | Username to use to connect when logging to database
+| Logging     |**log_database_password**                     | password                      | *''*                            | Recommended to leave blank and use .pgpass file
+| Autofailover|**enable_autofailover**                       | boolean                       | *'false'*                       | Rather than interactive mode sit and watch the cluster state for failed
+|             |                                              |                               |                                 | origin/forwarding nodes; upon detection trigger automated failover.
+|             |                                              |                               |                                 |
+| Autofailover|**autofailover_forwarding_providers**         | boolean                       | *'false'*                       | If true a failure of a pure forwarding provider will also trigger failover
+| Autofailover|**autofailover_config_any_node**              | boolean                       | *'true'*                        | After reading the initial cluster configuration, subsequent reads of the configuration 
+|             |                                              |                               |                                 | will use conninfo read from sl_subscribe to read from any node.
+| Autofailover|**autofailover_poll_interval**                | integer                       | 500                             | How often to check for failure of nodes (milliseconds)
+| Autofailover|**autofailover_node_retry**                   | integer                       | 2                               | When failure is detected, retry this many times before initiating failover
+| Autofailover|**autofailover_sleep_time*                    | integer                       | 1000                            | Interval between retries (milliseconds)
+
+Changes
+-------
+
+* 08/04/2012 - Hash together some ideas for interactive failover perl script
+* 04/11/2012 - Experiment with different use of try blocks (currently can't use multiple lock sets indide try)
+* 13/04/2014 - Update to work differently for Slony 2.2+
+* 05/05/2014 - Experiment with autofailover ideas
+
+Licence
+-------
+See the LICENCE file.
\ No newline at end of file
diff --git a/failover_log_database.sql b/failover_log_database.sql
new file mode 100644 (file)
index 0000000..557f05c
--- /dev/null
@@ -0,0 +1,15 @@
+CREATE USER failover_log_user WITH PASSWORD 'failover_log_password';
+
+DROP TABLE IF EXISTS public.failovers;
+CREATE TABLE public.failovers
+(
+  id bigserial NOT NULL PRIMARY KEY,
+  applied timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
+  reason varchar,
+  exit_code integer,
+  results text,
+  script text,
+  cluster_name varchar
+);
+GRANT SELECT, INSERT ON TABLE public.failovers TO failover_log_user;
+GRANT SELECT, UPDATE ON TABLE public.failovers_id_seq TO failover_log_user;
diff --git a/init.debian b/init.debian
new file mode 100644 (file)
index 0000000..17df393
--- /dev/null
@@ -0,0 +1,53 @@
+#!/bin/bash
+
+### BEGIN INIT INFO
+# Provides:          slony_failover
+# Required-Start:    $network $local_fs $remote_fs $syslog
+# Required-Stop:     $network $local_fs $remote_fs $syslog
+# Default-Start:     2 3 4 5
+# Default-Stop:      0 1 6
+# Short-Description: Start slony_failover daemon at boot time
+# Description:       Enable slony_failover service.
+### END INIT INFO
+
+NAME=slony_failover
+USER=root
+DAEMON=/var/slony/failover/$NAME.pl
+CFGFILE=/var/slony/failover/$NAME.conf
+DAEMON_OPTS="-f $CFGFILE"
+PIDFILE=/var/run/$NAME.pid
+# note: SSD is required only at startup of the daemon.
+SSD=`which start-stop-daemon`
+ENV="env -i LANG=C PATH=/bin:/usr/bin:/usr/local/bin:/usr/local/pgsql/bin"
+
+trap "" 1
+
+case "$1" in
+  start)
+    echo -n "Starting server: $NAME"
+    su $USER -c "$ENV $SSD --start --pidfile $PIDFILE --background --quiet --exec $DAEMON -- $DAEMON_OPTS"
+    ;;
+
+  stop)
+    echo -n "Stopping server: $NAME"
+    su $USER -c "$SSD --stop --pidfile $PIDFILE"
+    ;;
+
+  restart)
+    $0 stop
+    $0 start
+    ;;
+
+  *)
+    echo "Usage: /etc/init.d/$NAME {start|stop|restart}"
+    exit 1
+    ;;
+esac
+
+if [ $? -eq 0 ]; then
+    echo .
+    exit 0
+else
+    echo " failed"
+    exit 1
+fi
diff --git a/slony_failover.conf b/slony_failover.conf
new file mode 100644 (file)
index 0000000..177f365
--- /dev/null
@@ -0,0 +1,89 @@
+#------------------------------------------------------------------------------
+# General behaviour
+#------------------------------------------------------------------------------
+lang = en                                   # Language for script strings
+                                            # currently only en/fr
+
+prefix_directory = /usr/failovers           # Script working directory
+separate_working_directory = true           # Creates and appends a separate working
+                                            # directory to the prefix_directory
+                                            # for each script run
+
+pid_filename = /var/run/slony_failover.pid  # If the pid file name starts with a
+                                            # forward slash then taken as absoloute
+                                            # othewise appended to prefix_directory
+
+slonik_path = /usr/local/pgsql/bin          # Slonik binary if not in current path
+
+enable_try_blocks = false                   # Group operations inside try blocks when
+                                            # doing a move set; only works with 'single'
+                                            # lockset_method
+
+lockset_method = multiple                   # Used for MOVE SET
+                                            # multiple = lock all sets and move together
+                                            # single = lock and move each set at a time
+
+pull_aliases_from_comments = false          # Try to generate aliases in the
+                                            # slonik scripts based on comments within
+                                            # sl_node/set comments in parentheses
+
+log_line_prefix = [%t PID=%p]               # Special values:
+                                            #   %p = process ID
+                                            #   %t = timestamp without milliseconds
+                                            #   %m = timestamp with milliseconds
+
+failover_offline_subscriber_only = true     # If set to true any subscriber only nodes
+                                            # that are unavailable at the time of failover
+                                            # will also be failed over.  If false any such
+                                            # nodes will be excluded from the preamble and
+                                            # not failed over, however this may be problematic
+                                            # especially in the case where the most up to date
+                                            # node is the unavailable one.
+
+drop_failed_nodes = true                    # Drop all failed nodes immediately
+
+#------------------------------------------------------------------------------
+# Database node to read initial Slony-I configuration from
+#------------------------------------------------------------------------------
+slony_database_host = localhost
+slony_database_port = 5434
+slony_database_name = TEST
+slony_database_user = slony
+slony_database_password =                   # Recommended to leave blank and use .pgpass
+slony_cluster_name = test_replication
+
+
+#------------------------------------------------------------------------------
+# Logging and debugging
+#------------------------------------------------------------------------------
+enable_debugging = false
+log_filename = failover-%Y-%m-%d.log        # If the log file name starts with a 
+                                            # forward slash then taken as absoloute
+                                            # othewise appended to prefix_directory
+                                            # Special values as per strftime spec
+            
+
+log_to_postgresql = false                   # Save log n a PostgreSQL table (table def in failover_log_database.sql)
+log_database_host = localhost
+log_database_port = 5432
+log_database_name = TEST_FAILOVERS
+log_database_user = failover_log_user
+log_database_password =                     # Recommended to leave blank and use .pgpass
+
+#------------------------------------------------------------------------------
+# Autofailover
+#------------------------------------------------------------------------------
+enable_autofailover = false                 # Rather than interactive mode sit and
+                                            # watch the cluster state for failed
+                                            # origin/forwarding nodes; upon detection
+                                            # trigger automated failover.
+
+autofailover_forwarding_providers = true    # Also trigger failover on forwarding providers
+
+autofailover_config_any_node = true         # After reading the initial cluster configuration,
+                                            # subsequent reads of the configuration will
+                                            # use conninfo to read from any node.
+
+autofailover_poll_interval = 50             # Cluster state poll interval (milliseconds)
+autofailover_node_retry = 2                 # Retries before initiating failover
+autofailover_sleep_time = 5000              # Interval between retries (milliseconds)
diff --git a/slony_failover.conf.minimal b/slony_failover.conf.minimal
new file mode 100644 (file)
index 0000000..e8a5f71
--- /dev/null
@@ -0,0 +1,5 @@
+# Database node to read Slony-I configuration from
+slony_database_host = localhost
+slony_database_name = TEST
+slony_cluster_name = test_replication
+
diff --git a/slony_failover.pl b/slony_failover.pl
new file mode 100644 (file)
index 0000000..377dfaa
--- /dev/null
@@ -0,0 +1,2178 @@
+#!/usr/bin/perl
+
+# Script:      failover.pl
+# Copyright:   08/04/2012: v1.0.1 Glyn Astill <glyn@8kb.co.uk>
+# Requires:    Perl 5.10.1+, Data::UUID, File::Slurp
+#               PostgreSQL 9.0+ Slony-I 2.0+
+#
+# This script is a command-line utility to manage switchover and failover
+# of replication sets in Slony-I clusters.
+#
+# This script 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 3 of the License, or
+# (at your option) any later version.
+#
+# This script 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 this script.  If not, see <http://www.gnu.org/licenses/>.
+
+use strict;
+use warnings;
+use DBI;
+use Getopt::Long qw/GetOptions/;
+use Data::UUID;
+use File::Slurp;
+use v5.10.1;
+use sigtrap 'handler' => \&sigExit, 'HUP', 'INT','ABRT','QUIT','TERM';
+use Time::HiRes qw/usleep gettimeofday/;
+use POSIX qw/strftime/;
+use Config qw/%Config/;
+
+use constant false => 0;
+use constant true  => 1;
+
+my $g_script_version = '1.0.1';
+my $g_debug = false;
+my $g_pidfile = '/var/run/slony_failover.pid';
+my $g_pid_written = false;
+my $g_lang = 'en';
+my $g_prefix = '/tmp/slony_failovers';
+my $g_separate_working = true;
+my $g_log_prefix = '%t';
+my $g_log_to_db = false;
+my $g_logdb_name;
+my $g_logdb_host;
+my $g_logdb_port;
+my $g_logdb_user;
+my $g_logdb_pass;
+my $g_slonikpath;
+my $g_use_try_blocks = false;
+my $g_lockset_method = 'multiple';
+my $g_logfile = 'failover.log';
+my $g_input;
+my $g_reason;
+my $g_script;
+my $g_node_from;
+my $g_node_to;
+my $g_clname;
+my $g_dbname;
+my $g_dbhost;
+my $g_dbport = 5432;
+my $g_dbconninfo;
+my $g_dbpass = '';
+my $g_dbuser = 'slony';
+my $g_node_count;
+my $g_available_node_count;
+my $g_critical_node_count;
+my $g_subs_follow_origin = false;
+my $g_use_comment_aliases = false;
+my @g_cluster;                      # Array refs of node info.  In hindsight this should have been a hash, should be fairly simple to switch.
+my @g_failed;
+my @g_sets;
+my @g_lags;
+my $g_result;
+my $g_version;
+my $g_failover_method = 'old';
+my $g_resubscribe_method = 'subscribe';
+my $g_failover = false;
+my $g_fail_subonly = false;
+my $g_drop_failed = false;
+my $g_autofailover = false;
+my $g_autofailover_poll_interval = 500;
+my $g_autofailover_retry = 2;
+my $g_autofailover_retry_sleep = 1000;
+my $g_autofailover_provs = false;
+my $g_autofailover_config_any = true;
+my @g_unresponsive;
+my %g_backups;
+my $g_pid = $$;
+
+
+my %message = (
+'en' => {
+    'usage'                            => q{-h <host> -p <port> -db <database> -c <cluster name> -u <username> -P <password> (Password option not recommended; use pgpass instead)},
+    'title'                            => q{Slony-I failover script version $1},
+    'cluster_fixed'                    => q{Aborting failover action: all origin nodes now responsive},
+    'cluster_failed'                   => q{Found $1 failed nodes, sleeping for $2ms before retry $3 of $4},
+    'load_cluster'                     => q{Getting a list of database nodes...}, 
+    'load_cluster_fail'                => q{Unable to read cluster configuration $1}, 
+    'load_cluster_success'             => q{Loaded Slony-I v$1 cluster "$2" with $3 nodes read from node at $4:$5/$6}, 
+    'lag_detail'                       => q{Current node lag information from configuration node:},
+    'script_settings'                  => q{Using $1 batches of lock set, $2 FAILOVER and $3},
+    'generated_script'                 => q{Generated script "$1"},
+    'autofailover_init'                => q{Entering autofailover mode},
+    'autofailover_init_cnf'            => q{Slony configuration will be read from $1 node},
+    'autofailover_init_pol'            => q{Polling every $1ms},
+    'autofailover_init_ret'            => q{Failed nodes will be retried $1 times with $2ms sleep},
+    'autofailover_init_set'            => q{Failed forwarding providers $1 be failed over},
+    'autofailover_load_cluster'        => q{$1 Slony-I v$2 cluster "$3" with $4 nodes},
+    'autofailover_proceed'             => q{Proceeding with failover:},
+    'autofailover_detail'              => q{Failed node: $1, Backup node: $2},
+    'autofailover_halt'                => q{Unable to perform any failover for $1 failed nodes},
+    'autofailover_check_sub'           => q{Checking subscriber node $1},
+    'autofailover_check_sub_fail'      => q{Unable to check subscriber node $1},
+    'autofailover_promote_find'        => q{Finding most up to date subscriber to all sets ($1) on unresponsive node $2},
+    'autofailover_promote_found'       => q{Using previously found most up to date subscriber to all sets ($1) on unresponsive node $2},
+    'autofailover_promote_skip'        => q{No failover required for unresponsive node $1 as it is neither the origin or an active forwarder of any sets},
+    'autofailover_promote_fail'        => q{Could not find suitable backup node for promotion},
+    'autofailover_node_detail'         => q{Node $1 is $2 and provides sets $3 at $4 lag},
+    'autofailover_promote_best'        => q{Best node for promotion is node $1 seq = $2},
+    'autofailover_unresponsive'        => q{Detected unresponsive provider node: $1},
+    'autofailover_unresponsive_subonly'=> q{Detected unresponsive subscriber only node: $1},
+    'interactive_head_id'              => q{ID},
+    'interactive_head_name'            => q{Name},
+    'interactive_head_status'          => q{Status},
+    'interactive_head_providers'       => q{Provider IDs},
+    'interactive_head_config'          => q{Configuration},
+    'interactive_detail_1'             => q{Origin for sets: },
+    'interactive_detail_2'             => q{Providing sets: },
+    'interactive_detail_3'             => q{Subscriptions: },
+    'interactive_choose_node'          => q{Please choose the node to move all sets $1:},
+    'interactive_confirm'              => q{You chose to move sets $1 node $2 ($3). Is this correct [y/n]? },
+    'interactive_action'               => q{Best course of action is most likely to do a "$1". Do you wish to continue [y/n]?},
+    'interactive_surrender'            => q{Uable to determine best course of action},
+    'interactive_write_script'         => q{Writing a script to $1 node $2 to $3},
+    'interactive_check_nodes'          => q{Checking availability of database nodes...},
+    'interactive_continue'             => q{Do you wish to continue [y/n]?},
+    'interactive_preserve'             => q{Preserve subscription paths to follow the origin node (choose no if unsure) [y/n]?},
+    'interactive_aliases'              => q{Generate aliases based on sl_node/set comments in parentheses (choose no if unsure) [y/n]?},
+    'interactive_summary'              => q{Summary of nodes to be passed to failover:},
+    'interactive_node_info'            => q{Node : $1 ($2) $3 (conninfo $4)},
+    'interactive_run_script'           => q{Would you like to run this script now [y/n]?},
+    'interactive_running'              => q{Running the script now. This may take some time; please be patient!},
+    'interactive_reason'               => q{Please enter a brief reson for taking this action: },
+    'interactive_failover_detail_1'    => q{Before you go any further please consider the impact of a full failover:},
+    'interactive_failover_detail_2'    => q{The node you are failing over from will cease to participate in the cluster permanently until it is rebuild & subscribed},
+    'interactive_failover_detail_3'    => q{If the outage is temporary (i.e. network/power/easily replaceable hardware related) consider waiting it out},
+    'interactive_failover_detail_4'    => q{This type of failover is likely to be more a business decision than a technical one},
+    'info_all_nodes_available'         => q{INFO: All nodes are available},
+    'info_req_nodes_available'         => q{INFO: $1 of $2 nodes are available. No unavailable nodes are subscribed to the old origin},
+    'wrn_node_unavailable'             => q{WARNING: Node $1 unavailable},
+    'wrn_req_unavailable'              => q{WARNING: Old origin node ($1) is available, however $2 subscribers are unavailable},
+    'wrn_not_tested'                   => q{WARNING: Script not tested with Slony-I v$1},
+    'note_multiple_try'                => q{NOTICE: Cannot lock multiple sets within try blocks in version $1 dropping back to single sets},
+    'note_reshape_cluster'             => q{NOTICE: Either drop the failed subscribers or bring them back up, then retry to MOVE SET},
+    'dbg_generic'                      => q{DEBUG: $1},
+    'dbg_cluster'                      => q{DEBUG: NodeID $1/ProvIDs $2/Conninfo $3/OrigSets $4/NodeName $5/ProvTree $6/Active $7/FwdSets $8/ActSubSets $9},
+    'dbg_resubscribe'                  => q{DEBUG: Checking possibility to resubscribe set $1 ($2) to node $3 ($4) which pulls $5 ($6) from $7 ($8)},
+    'dbg_failover_method'              => q{DEBUG: Failover method is $1},
+    'dbg_cluster_load'                 => q{DEBUG: Loading cluster configuration from $1},
+    'dbg_cluster_good'                 => q{DEBUG: Cluster state good},
+    'dbg_autofailover_check'           => q{DEBUG: Checking node $1 ($2) role is $3 (conninfo: $4)},
+    'dbg_autofailover_active_check'    => q{DEBUG: Initiate active check of $1 node $2},
+    'dbg_slonik_script'                => q{DEBUG: Running slonik script $1},
+    'err_generic'                      => q{ERROR: $1},
+    'err_no_database'                  => q{ERROR: Please specify a database name},
+    'err_no_cluster'                   => q{ERROR: Please specify a slony cluster name},
+    'err_no_host'                      => q{ERROR: Please specify a host},
+    'err_no_config'                    => q{ERROR: No valid config found},
+    'err_fail_config'                  => q{ERROR: Failed to load configuration},
+    'err_write_fail'                   => q{ERROR: Could not write to $1 "$2"},
+    'err_read_fail'                    => q{ERROR: Could not read from $1 "$2"},
+    'err_unlink_fail'                  => q{ERROR: Could not delete $1 "$2"},
+    'err_mkdir_fail'                   => q{ERROR: Unable to create $1 directory "$2"},
+    'err_execute_fail'                 => q{ERROR: Could not execute $1 "$2"},
+    'err_inactive'                     => q{ERROR: Node $1 is not active (state = $2)},
+    'err_cluster_empty'                => q{ERROR: Loaded cluster contains no nodes}, 
+    'err_cluster_offline'              => q{ERROR: Loaded cluster contains no reachable nodes}, 
+    'err_cluster_lone'                 => q{ERROR: Loaded cluster contains only 1 node}, 
+    'err_not_origin'                   => q{ERROR: Node $1 is not the origin of any sets},
+    'err_not_provider'                 => q{ERROR: Node $1 is not a provider of any sets},
+    'err_not_provider_sets'            => q{ERROR: Node $1 does not provide the sets required: need ($2) but provides ($3)},
+    'err_no_configuration'             => q{ERROR: Could not read configuration for node $1},
+    'err_must_enter_node_id'           => q{ERROR: You must enter a node id},
+    'err_not_a_node_id'                => q{ERROR: I have no knowledge of a node $1},
+    'err_same_node'                    => q{ERROR: Cant move from and to the same node},
+    'err_node_offline'                 => q{ERROR: $1 node ($2) is not available},
+    'err_incomplete_preamble'          => q{ERROR: Incomplete preamble},
+    'err_running_slonik'               => q{ERROR: Could not run slonik: $1},
+    'err_pgsql_connect'                => q{ERROR: Cannot connect to postgres server},
+    'slonik_output'                    => q{SLONIK: $1},
+    'exit_noaction'                    => q{Exiting, no action has been taken},
+    'exit'                             => q{Exited by $1}
+    },
+'fr' => {
+    'usage'                            => q{-h <host> -p <port> -db <database> -c <cluster name> -u <username> -P <password> (Option mot de passe pas recommandé; utiliser pgpass place)},
+    'title'                            => q{Slony-I failover (basculement) version de script $1},
+    'cluster_fixed'                    => q{Abandon de l'action de basculement: tous les noeuds d'origine maintenant sensible},
+    'cluster_failed'                   => q{Trouvé $1 Ã©choué noeuds, couchage pour $2 ms avant réessayer $3 de $4},
+    'load_cluster'                     => q{Obtenir une liste de noeuds de base de donnees...},
+    'load_cluster_fail'                => q{Impossible de lire la configuration du cluster $1},
+    'load_cluster_success'             => q{Chargé Slony-I v$1 groupe "$2" avec $3 noeuds lire Ã  partir du noeud Ã  $4:$5/$6},
+    'lag_detail'                       => q{Current informations noeud de décalage Ã  partir du noeud de configuration:},
+    'script_settings'                  => q{Utilisation de $1 lots de système de verrouillage, $2 FAILOVER et $3},
+    'generated_script'                 => q{Script généré "$1"},
+    'autofailover_init'                => q{Entrer dans le mode de autofailover},
+    'autofailover_init_cnf'            => q{Configuration Slony sera lu Ã  partir de $1 noeud},
+    'autofailover_init_pol'            => q{Vérifier toutes les $1ms},
+    'autofailover_init_ret'            => q{Noeuds défaillants seront rejugés $1 fois avec $2 ms sommeil},
+    'autofailover_init_set'            => q{Fournisseurs d'expédition Ã©choué $1 Ãªtre Ã©choué sur},
+    'autofailover_load_cluster'        => q{$1 Slony-I v$2 grappe "$3" avec $4 noeuds},
+    'autofailover_proceed'             => q{De procéder Ã  failover:},
+    'autofailover_detail'              => q{Noeud défaillant: $1, noeud de sauvegarde: $2},
+    'autofailover_halt'                => q{Noeuds Impossible d'effectuer une failover pour $1 Ã©choué},
+    'autofailover_check_sub'           => q{Vérification noeud abonné $1},
+    'autofailover_check_sub_fail'      => q{Impossible de vérifier noeud abonné $1},
+    'autofailover_promote_find'        => q{Trouver plus Ã  jour abonné Ã  tous les jeux ($1) sur le noeud ne répond pas $2},
+    'autofailover_promote_found'       => q{Utilisation précédemment trouvé plus Ã  jour abonné Ã  tous les jeux ($1) sur le noeud ne répond pas $2},
+    'autofailover_promote_skip'        => q{Pas de failover requis pour le noeud ne répond pas $1 car il n'est ni l'origine ou un transitaire active de tous les jeux},
+    'autofailover_promote_fail'        => q{Impossible de trouver le noeud de sauvegarde approprié pour la promotion},
+    'autofailover_node_detail'         => q{Noeud $1 est $2 et fournit des ensembles $3 Ã  $4 retard},
+    'autofailover_promote_best'        => q{Meilleur noeud pour la promotion est noeud $1 suivants = $2},
+    'autofailover_unresponsive'        => q{Noeud ne répond pas détecté: $1},
+    'autofailover_unresponsive_subonly'=> q{Abonné ne répond pas détecté seulement de noeud: $1},
+    'interactive_head_name'            => q{Nom},
+    'interactive_head_status'          => q{Statut},
+    'interactive_head_providers'       => q{Fournisseur IDs},
+    'interactive_detail_1'             => q{Origine pour les jeux: },
+    'interactive_detail_2'             => q{Fournir des ensembles: },
+    'interactive_detail_3'             => q{Abonnements: },
+    'interactive_choose_node'          => q{S'il vous plaît choisissez le noeud Ã  déplacer tous les ensembles $1:},
+    'interactive_confirm'              => q{Vous avez choisi de passer ensembles $1 noeud $2 ($3). Est-ce correct [o/n]? },
+    'interactive_action'               => q{Meilleur plan d'action est le plus susceptible de faire une $1. Voulez-vous continuer [o/n]?},
+    'interactive_surrender'            => q{Uable pour déterminer le meilleur plan d'action},
+    'interactive_write_script'         => q{Rédaction d'un script Ã  $1 $2 noeud Ã  $3},
+    'interactive_check_nodes'          => q{Vérification de la disponibilité des noeuds de base de donnees...},
+    'interactive_continue'             => q{Voulez-vous continuer [o/n]?},
+    'interactive_preserve'             => q{Préserver les chemins de souscription Ã  suivre le noeud d'origine (ne pas choisir en cas de doute) [o/n]?},
+    'interactive_aliases'              => q{Générer des alias sur la base de sl_node / set commentaires entre parenthèses (ne pas choisir en cas de doute) [o/n]?},
+    'interactive_summary'              => q{Résumé des noeuds Ã  passer Ã  failover:},
+    'interactive_node_info'            => q{Noeud : $1 ($2) $3 (conninfo $4)},
+    'interactive_run_script'           => q{Voulez-vous exécuter ce script maintenant [o/n]?},
+    'interactive_running'              => q{L'exécution du script maintenant. Cela peut prendre un certain temps; s'il vous plaît Ãªtre patient!},
+    'interactive_reason'               => q{S'il vous plaît entrer une brève reson pour cette action: },
+    'interactive_failover_detail_1'    => q{Avant d'aller plus loin s'il vous plaît envisager l'impact d'un failover (basculement) complet:},
+    'interactive_failover_detail_2'    => q{Le noeud vous ne parviennent pas au-dessus de cesse de participer au groupe de façon permanente jusqu'à ce qu'il soit Ã  reconstruire et souscrit},
+    'interactive_failover_detail_3'    => q{Si la panne est temporaire (c.-à-réseau / alimentation / facilement remplaçable matériel connexe) envisager d'attendre dehors},
+    'interactive_failover_detail_4'    => q{Ce type de failover est susceptible d'être plus une décision d'affaires que technique},
+    'info_all_nodes_available'         => q{INFO: Tous les noeuds sont disponibles},
+    'info_req_nodes_available'         => q{INFO: $1 of $2 noeuds sont disponibles. Pas de noeuds indisponibles sont souscrites Ã  l'ancienne origine},
+    'wrn_node_unavailable'             => q{ATTENTION: Noeud $1 disponible},
+    'wrn_req_unavailable'              => q{ATTENTION: Noeud Old origine ($1) est disponible, mais $2 abonnés ne sont pas disponibles},
+    'wrn_not_tested'                   => q{ATTENTION: Script pas testé avec Slony-I v$1},
+    'note_multiple_try'                => q{AVIS: Vous ne pouvez pas verrouiller plusieurs ensembles dans des blocs try dans la version $1 de retomber Ã  des jeux simples},
+    'note_reshape_cluster'             => q{AVIS: Vous devez supprimer les abonnés défaillants ou les ramener, puis réessayez Ã  MOVE SET},
+    'err_generic'                      => q{ERREUR: $1},
+    'err_no_database'                  => q{ERREUR: S'il vous plaît spécifier un base de donnees nom},
+    'err_no_cluster'                   => q{ERREUR: S'il vous plaît indiquez un nom de cluster slony},
+    'err_no_host'                      => q{ERREUR: S'il vous plaît spécifier un hôte},
+    'err_no_config'                    => q{ERREUR: Aucune configuration valide n'a Ã©té trouvée},
+    'err_fail_config'                  => q{ERREUR: Impossible de charger la configuration},
+    'err_write_fail'                   => q{ERREUR: Impossible d'écrire dans $1 "$2"},
+    'err_read_fail'                    => q{ERREUR: Impossible de lire $1 "$2"},
+    'err_unlink_fail'                  => q{ERREUR: Impossible de supprimer $1 "$2"},
+    'err_mkdir_fail'                   => q{ERREUR: Impossible de créer $1 répertoire "$2"},
+    'err_execute_fail'                 => q{ERREUR: Impossible d'exécuter $1 "$2"},
+    'err_inactive'                     => q{ERREUR: Noeud $1 n'est pas active (état = $2)},
+    'err_cluster_empty'                => q{ERREUR: Groupe chargé contient pas de noeuds},
+    'err_cluster_offline'              => q{ERREUR: Groupe chargé contient pas de noeuds accessibles},
+    'err_cluster_lone'                 => q{ERRRUE: Groupe chargé ne contient que 1 noeud},
+    'err_not_origin'                   => q{ERREUR: NÅ“ud $1 n'est pas Ã  l'origine de tous les jeux},
+    'err_not_provider'                 => q{ERREUR: Noeud $1 n'est pas un fournisseur de tous les jeux},
+    'err_not_provider_sets'            => q{ERREUR: Noeud $1 ne fournit pas les ensembles nécessaires: le besoin ($2), mais fournit ($3)},
+    'err_no_configuration'             => q{ERREUR: Impossible de lire la configuration pour le noeud $1},
+    'err_must_enter_node_id'           => q{ERREUR: Vous devez entrer un id de noeud},
+    'err_not_a_node_id'                => q{ERREUR: Je n'ai pas connaissance d'un $1 de noeud},
+    'err_same_node'                    => q{ERREUR: Cant déplacer depuis et vers le même noeud},
+    'err_node_offline'                 => q{ERREUR: $1 noeud ($2) n'est pas disponible},
+    'err_incomplete_preamble'          => q{ERREUR: Préambule incomplète},
+    'err_running_slonik'               => q{ERREUR: Ne pouvait pas courir slonik: $1},
+    'err_pgsql_connect'                => q{ERREUR: Impossible de se connecter au serveur postgres},
+    'slonik_output'                    => q{SLONIK: $1},
+    'exit_noaction'                    => q{Quitter, aucune action n'a Ã©té prise},
+    'exit'                             => q{Quitter par $1}
+    }
+);
+
+
+# Setup date variables
+my ($g_year, $g_month, $g_day, $g_hour, $g_min, $g_sec) = (localtime(time))[5,4,3,2,1,0];
+my $g_date = sprintf ("%02d:%02d:%02d on %02d/%02d/%04d", $g_hour, $g_min, $g_sec, $g_day, $g_month+1, $g_year+1900);
+
+# Handle command line options
+Getopt::Long::Configure('no_ignore_case');
+use vars qw{%opt};
+die lookupMsg('usage') unless GetOptions(\%opt, 'host|H=s', 'port|p=i', 'dbname|db=s', 'clname|cl=s', 'dbuser|u=s', 'dbpass|P=s', 'cfgfile|f=s', 'infoprint|I', ) and keys %opt and ! @ARGV;
+
+# Read configuration
+if (defined($opt{cfgfile})) {
+    unless (getConfig($opt{cfgfile})) {
+        println(lookupMsg('err_no_config'));
+        exit(1);
+    }
+}
+else {
+    if (defined($opt{dbname})) {
+        $g_dbname = $opt{dbname};
+    }
+    if (defined($opt{clname})) {
+        $g_clname = $opt{clname};
+    }
+    if (defined($opt{host})) {
+        $g_dbhost = $opt{host};
+    }
+    if (defined($opt{port})) {
+        $g_dbport = $opt{port};
+    }
+    if (defined($opt{dbuser})) {
+        $g_dbuser = $opt{dbuser};
+    }
+    if (defined($opt{dbpass})) {
+        $g_dbpass = $opt{dbpass};
+    }
+}
+
+# Fill in any missing values with defaults or display message and die
+if (!defined($g_dbname)) {
+    println(lookupMsg('err_no_database'));
+    die lookupMsg('usage');
+}
+if (!defined($g_clname)) {
+    println(lookupMsg('err_no_cluster'));
+    die lookupMsg('usage');
+}
+if (!defined($g_dbhost)) {
+    println(lookupMsg('err_no_host'));
+    die lookupMsg('usage');
+}
+
+
+# Build conninfo from supplied datbase name/host/port
+$g_dbconninfo = "dbname=$g_dbname;host=$g_dbhost;port=$g_dbport";
+
+if (!defined($opt{infoprint})) {
+    # Check prefix directory and create if not present
+    unless(-e $g_prefix or mkdir $g_prefix) {
+        println(lookupMsg('err_mkdir_fail', 'prefix', $g_prefix));
+        exit(2);
+    }
+
+    if ($g_separate_working) {
+        if ($g_prefix !~ m/\/$/) {
+            $g_prefix .= "/";
+        }
+
+        # Get a uuid for working directory
+        $g_prefix .= getUUID($g_date);
+
+        # Create a working directory and setup log file
+        unless(-e $g_prefix or mkdir $g_prefix) {
+            println(lookupMsg('err_mkdir_fail', 'work', $g_prefix));
+        }
+    }
+}
+
+# Set postgres path if provided
+if (defined($g_slonikpath) && ($g_slonikpath ne "")) {
+    $ENV{PATH} .= ":$g_slonikpath";
+}
+
+# Check if autofailover is enabled, if so check configuration and enter autofailover mode
+if (($g_autofailover) && !defined($opt{infoprint})) {
+
+    # Write out a PID file
+    if (writePID($g_prefix, $g_logfile, $g_log_prefix, $g_pidfile)) {
+        $g_pid_written = true;
+    }
+    else {
+        cleanExit(1, "system");
+    }
+    
+    # Go into endless loop for autofailover
+    autoFailover($g_dbconninfo, $g_clname, $g_dbuser, $g_dbpass, $g_prefix, $g_logfile, $g_log_prefix);
+}
+
+# Read slony configuration and output some basic information
+eval {
+    #local $| = 1;
+    println(lookupMsg('load_cluster', $g_prefix));
+    ($g_node_count, $g_version) = loadCluster($g_dbconninfo, $g_clname, $g_dbuser, $g_dbpass, $g_prefix, $g_logfile, $g_log_prefix);
+};
+if ($@) {
+    println(lookupMsg('load_cluster_fail', 'from supplied configuration'));
+    cleanExit(2, "system");
+}
+
+if (defined($opt{infoprint})) {
+    println(lookupMsg('load_cluster_success', $g_version, $g_clname, $g_node_count, $g_dbhost, $g_dbport, $g_dbname) . ":");
+    chooseNode("info", undef, undef, undef, 0);
+    exit(0);
+}
+else {
+    printlog($g_prefix,$g_logfile,$g_log_prefix,"*"x68 . "\n* ");
+    printlogln($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('title', $g_script_version));
+    printlogln($g_prefix,$g_logfile,$g_log_prefix,"*"x68);
+}
+
+if ($g_node_count <= 0) {
+    printlogln($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('err_cluster_empty'));
+    cleanExit(3, "system");
+}
+elsif (substr($g_version,0,1) < 2) {
+    printlogln($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('wrn_not_tested', $g_version));
+}
+else {
+    printlogln($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('load_cluster_success', $g_version, $g_clname, $g_node_count, $g_dbhost, $g_dbport, $g_dbname));
+    printlogln($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('script_settings', $g_lockset_method, $g_failover_method, uc($g_resubscribe_method)));
+}
+
+# Output lag information between each node and node configuration was read from
+if (loadLag($g_dbconninfo, $g_clname, $g_dbuser, $g_dbpass, $g_prefix, $g_logfile, $g_log_prefix) > 0) {
+    printlogln($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('lag_detail'));
+    foreach (@g_lags) {
+        printlogln($g_prefix,$g_logfile,$g_log_prefix,"\t$_");
+    }
+    printlog($g_prefix,$g_logfile,$g_log_prefix,"\n");
+}
+
+# Prompt user to choose nodes to move sets from / to
+$g_node_from = chooseNode("from", $g_prefix, $g_logfile, $g_log_prefix, 0);
+if ($g_node_from == 0) {
+    cleanExit(4, "user");
+}
+elsif ($g_node_from == -1) {
+    cleanExit(5, "system");
+}
+
+$g_node_to = chooseNode("to", $g_prefix, $g_logfile, $g_log_prefix, $g_node_from);
+if ($g_node_to == 0) {
+    cleanExit(6, "user");
+}
+elsif ($g_node_to == -1) {
+    cleanExit(7, "system");
+}
+elsif ($g_node_from == $g_node_to) {
+    printlogln($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('err_same_node'));
+    cleanExit(8, "system");
+}
+
+# Check nodes are available and decide on action to take
+printlogln($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('interactive_check_nodes'));
+($g_available_node_count, $g_critical_node_count) = checkNodes($g_clname, $g_dbuser, $g_dbpass, $g_node_from, $g_node_to, $g_prefix, $g_logfile, $g_log_prefix);
+
+if ($g_available_node_count <= 0) {
+    printlogln($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('err_cluster_offline'));
+    cleanExit(9, "system");
+}
+elsif ($g_critical_node_count == -1) {
+    printlogln($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('err_node_offline', 'Target new origin', $g_node_to));
+    cleanExit(10, "system");
+}
+elsif ($g_critical_node_count == -2) {
+    printlogln($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('err_node_offline', 'Old origin', $g_node_from));
+    printlog($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('interactive_action', 'FAILOVER'));
+    $g_failover = true;
+}
+elsif ($g_critical_node_count == 0) {
+    if ($g_node_count == $g_available_node_count) {
+        printlogln($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('info_all_nodes_available'));
+    }
+    else {
+        printlogln($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('info_req_nodes_available', $g_available_node_count, $g_node_count));
+    }
+    printlog($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('interactive_action', 'MOVE SET'));
+}
+elsif ($g_critical_node_count > 0) {
+    printlogln($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('wrn_req_unavailable', $g_node_from, $g_critical_node_count));
+    printlogln($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('note_reshape_cluster'));
+    printlogln($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('exit_noaction'));
+    cleanExit(11, "user");
+}
+else {
+    printlog($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('interactive_surrender'));
+    cleanExit(12, "system");
+}
+$g_input = <>;
+chomp($g_input);
+if ($g_input !~ /^[Y|O]$/i) {
+    printlogln($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('exit_noaction'));
+    cleanExit(13, "user");
+}
+
+if (!$g_use_comment_aliases) {
+    printlog($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('interactive_aliases'));
+    $g_input = <>;
+    chomp($g_input);
+    if ($g_input =~ /^[Y|O]$/i) {
+        $g_use_comment_aliases = true;
+    }
+}
+
+if ($g_failover) {
+    printlogln($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('interactive_summary'));
+
+    foreach (@g_failed) {
+        printlogln($g_prefix,$g_logfile,$g_log_prefix,"\t" . lookupMsg('interactive_node_info',$_->[0],($_->[4] // "unnamed"),(defined($_->[9]) ? "providing sets $_->[9]" : "sole subscriber"), $_->[2])); 
+    }
+
+    printlogln($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('interactive_failover_detail_1'));
+    printlogln($g_prefix,$g_logfile,$g_log_prefix,"\t" . lookupMsg('interactive_failover_detail_2'));
+    printlogln($g_prefix,$g_logfile,$g_log_prefix,"\t" . lookupMsg('interactive_failover_detail_3'));
+    printlogln($g_prefix,$g_logfile,$g_log_prefix,"\t" . lookupMsg('interactive_failover_detail_4'));
+
+    printlog($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('interactive_reason'));
+    $g_reason = <>;
+    printlog($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('interactive_continue'));
+    $g_input = <>;
+    chomp($g_input);
+    if ($g_input !~ /^[Y|O]$/i) {
+        printlogln($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('exit_noaction'));
+        cleanExit(14, "user");
+    }
+
+    printlogln($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('interactive_write_script', 'failover from', $g_node_from, $g_node_to));
+    $g_script = writeFailover($g_prefix, $g_dbconninfo, $g_clname, $g_dbuser, $g_dbpass, $g_node_from, $g_node_to, $g_subs_follow_origin, $g_use_comment_aliases, $g_logfile, $g_log_prefix);    
+}
+else {
+    printlog($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('interactive_preserve'));
+    $g_input = <>;
+    chomp($g_input);
+    if ($g_input =~ /^[Y|O]$/i) {
+        $g_subs_follow_origin = true;
+    }
+
+    printlog($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('interactive_reason'));
+    $g_reason = <>;
+
+    printlogln($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('interactive_write_script', 'move all sets provided by', $g_node_from, $g_node_to));
+    $g_script = writeMoveSet($g_prefix, $g_dbconninfo, $g_clname, $g_dbuser, $g_dbpass, $g_node_from, $g_node_to, $g_subs_follow_origin, $g_use_comment_aliases, $g_logfile, $g_log_prefix);    
+}
+
+# Complete and run script if required
+if (-e $g_script) {
+    printlogln($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('generated_script', $g_script));
+    printlog($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('interactive_run_script', $g_script));
+    $g_input = <>;
+    chomp($g_input);
+    if ($g_input =~ /^[Y|O]$/i) {
+        printlogln($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('interactive_running'));
+        unless (runSlonik($g_script, $g_prefix, $g_logfile, $g_log_prefix)) {
+            printlogln($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('err_execute_fail', 'slonik script', $g_script));
+        }
+    }
+    else {
+        printlogln($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('exit_noaction'));
+    }
+}
+else {
+    printlogln($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('err_read_fail', 'slonik script', $g_script));
+    cleanExit(15, "system");
+}
+
+cleanExit(0, "script completion");
+
+###########################################################################################################################################
+
+sub cleanExit {
+    my $exit_code = shift;
+    my $type = shift;
+
+    printlogln($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('exit', $type));
+
+    if ($g_log_to_db) {    
+        eval {
+           logDB("dbname=$g_logdb_name;host=$g_logdb_host;port=$g_logdb_port", $g_logdb_user, $g_logdb_pass, $exit_code, $g_reason, $g_prefix, $g_logfile, $g_log_prefix, $g_clname, $g_script);
+        };
+    }
+
+    if ($g_pid_written) {
+        removePID($g_prefix, $g_logfile, $g_log_prefix, $g_pidfile);
+    }
+
+    exit($exit_code);
+}
+
+sub sigExit {
+    cleanExit(100,'signal');    
+}
+
+sub checkNodes {
+    my $clname = shift;
+    my $dbuser = shift;
+    my $dbpass = shift;
+    my $from = shift;
+    my $to = shift;
+    my $prefix = shift;
+    my $logfile = shift;
+    my $log_prefix = shift;
+
+    my $dsn;
+    my $dbh;
+    my $sth;
+    my $query;
+    my $result_count = 0;
+    my $critical_count = 0;
+
+    my @subsets;
+    my @origsets;
+
+    undef @g_failed;
+    undef @g_unresponsive;
+    undef %g_backups;
+
+    foreach (@g_cluster) {
+        if ($_->[0] == $from) {
+            @origsets = split(',', $_->[3]); 
+            last;
+        }
+    }
+
+    foreach (@g_cluster) {
+        if ($g_debug) {
+            printlogln($prefix,$logfile,$log_prefix,lookupMsg('dbg_cluster', $_->[0],($_->[1] // "<NONE>"),$_->[2],($_->[3] // "<NONE>"),$_->[4],($_->[5] // "<NONE>") . "(" . ($_->[8] // "<NONE>") . ")",$_->[6],($_->[7] // "<NONE>"),($_->[9] // "<NONE>") . " (" . ($_->[10] // "<NONE>") . ")"));
+        }
+            
+        $dsn = "DBI:Pg:$_->[2];";
+        eval {
+            $dbh = DBI->connect($dsn, $dbuser, $dbpass, {RaiseError => 1});
+            $query = "SELECT count(*) FROM pg_namespace WHERE nspname = ?";
+            $sth = $dbh->prepare($query);
+            $sth->bind_param(1, "_" . $clname);
+            $sth->execute();
+        
+            $result_count = $result_count+$sth->rows;        
+
+            $sth->finish;
+            $dbh->disconnect();
+
+        };
+        if ($@) {
+            # Critical count will be -1 if the new origin is down, -2 if the old origin is down or positive if subscribers to sets on old origin are down.
+            printlogln($prefix,$logfile,$log_prefix,lookupMsg('wrn_node_unavailable', $_->[0]));
+            if ($g_debug) {
+                printlogln($prefix,$logfile,$log_prefix,lookupMsg('dbg_generic', $@));
+            }
+            if ($_->[0] == $to) {
+                $critical_count = -1;        
+            }
+            elsif ($_->[0] == $from) {
+                $critical_count = -2;        
+            }
+            else {
+                foreach my $subprov (split(';', $_->[5])) {
+                    my ($node, $setlist) = (split('->', $subprov)) ;
+                    $node =~ s/n//g;
+                    $setlist =~ s/(\)|\(|s)//g;
+                    @subsets = (split(',', $setlist));
+
+                    if (($critical_count >= 0) && (checkSubscribesAnySets(\@origsets, \@subsets))) {
+                        $critical_count++;    
+                    }
+                }
+            }
+            # Only push nodes with active subscribers to sets into failed list unless explicitly told to
+            if (($g_fail_subonly) || (defined($_->[9]))) {
+                push(@g_failed, \@$_);
+                $g_backups{$_->[0]} = $to;
+            }
+            push(@g_unresponsive, \@$_);
+        }    
+        
+    }
+    return ($result_count, $critical_count);
+}
+
+sub loadCluster {
+    my $dbconninfo = shift;
+    my $clname = shift;
+    my $dbuser = shift;
+    my $dbpass = shift;
+    my $prefix = shift;
+    my $logfile = shift;
+    my $log_prefix = shift;
+
+    my $dsn;
+    my $dbh;
+    my $sth;
+    my $query;
+    my $version;
+    my $qw_clname;
+    undef @g_cluster;
+
+    if ($g_debug) {
+        printlogln($prefix,$logfile,$log_prefix,lookupMsg('dbg_cluster_load', $dbconninfo));
+    }
+
+    $dsn = "DBI:Pg:$dbconninfo;";
+    eval {
+        $dbh = DBI->connect($dsn, $dbuser, $dbpass, {RaiseError => 1});
+        $qw_clname = $dbh->quote_identifier("_" . $clname);
+
+        $query = "SELECT $qw_clname.getModuleVersion()";
+        $sth = $dbh->prepare($query);
+        $sth->execute();
+        ($version) = $sth->fetchrow; 
+        $sth->finish;
+
+        $query = "WITH z AS (
+                SELECT a.no_id, b.sub_provider AS no_prov,
+                    COALESCE(c.pa_conninfo,(SELECT pa_conninfo FROM $qw_clname.sl_path WHERE pa_server = $qw_clname.getlocalnodeid(?) LIMIT 1)) AS no_conninfo,
+                    array_to_string(array(SELECT set_id FROM $qw_clname.sl_set WHERE set_origin = a.no_id ORDER BY set_id),',') AS origin_sets,
+                    string_agg(CASE WHEN b.sub_receiver = a.no_id AND b.sub_forward AND b.sub_active THEN b.sub_set::text END, ',' ORDER BY b.sub_set) AS prov_sets,
+                    coalesce(trim(replace(substring(a.no_comment from '\\\\((.+)\\\\)'), ' ','')), 'node' || a.no_id) AS no_name,
+                    'n' || b.sub_provider || '->(' || string_agg(CASE WHEN b.sub_receiver = a.no_id THEN 's' || b.sub_set END,',' ORDER BY b.sub_set,',') || ')' AS sub_tree,
+                    coalesce(trim(replace(substring(d.no_comment from '\\\\((.+)\\\\)'), ' ','')), 'node' || b.sub_provider, '')
+                    || '->(' || string_agg(CASE WHEN b.sub_receiver = a.no_id THEN coalesce(trim(translate(e.set_comment, ' ,->', '____')), 'set' || b.sub_set) END,',' ORDER BY b.sub_set) || ')' AS sub_tree_name,
+                    CASE " . ((substr($version,0,3) >= 2.2) ? "WHEN a.no_failed THEN 'FAILED' " : "") . "WHEN a.no_active THEN 'ACTIVE' ELSE 'INACTIVE' END AS no_status,
+                    array_to_string(array(SELECT DISTINCT sub_set::text FROM $qw_clname.sl_subscribe WHERE sub_provider = a.no_id AND sub_active ORDER BY sub_set),',') AS prov_sets_active,
+                    string_agg(CASE WHEN b.sub_receiver = a.no_id THEN b.sub_set::text END,',' ORDER BY b.sub_set,',') AS sub_sets    
+                FROM $qw_clname.sl_node a
+                LEFT OUTER JOIN $qw_clname.sl_subscribe b ON a.no_id = b.sub_receiver
+                LEFT OUTER JOIN $qw_clname.sl_path c ON c.pa_server = a.no_id AND c.pa_client = $qw_clname.getlocalnodeid(?)
+                LEFT OUTER JOIN $qw_clname.sl_node d ON b.sub_provider = d.no_id
+                LEFT OUTER JOIN $qw_clname.sl_set e ON b.sub_set = e.set_id
+                GROUP BY b.sub_provider, a.no_id, a.no_comment, c.pa_conninfo, d.no_comment, a.no_active
+                ORDER BY a.no_id
+                )
+                SELECT no_id,
+                    nullif(string_agg(no_prov::text, ',' ORDER BY no_prov),'') AS no_provs,
+                    no_conninfo,
+                    nullif(string_agg(origin_sets::text, ',' ORDER BY origin_sets),'') AS origin_sets,
+                    no_name,
+                    nullif(string_agg(sub_tree, ';' ORDER BY sub_tree),'') AS no_sub_tree,
+                    no_status,
+                    nullif(string_agg(prov_sets::text, ',' ORDER BY prov_sets),'') AS prov_sets,
+                    nullif(string_agg(sub_tree_name, ';' ORDER BY sub_tree_name),'') AS no_sub_tree_name,
+                    nullif(string_agg(prov_sets_active::text, ',' ORDER BY prov_sets_active),'') AS prov_sets_active,
+                    nullif(string_agg(sub_sets::text, ',' ORDER BY sub_sets),'') AS no_subs
+                FROM z GROUP BY no_id, no_conninfo, no_name, no_status";
+        $sth = $dbh->prepare($query);
+
+        $sth->bind_param(1, "_" . $clname);
+        $sth->bind_param(2, "_" . $clname);
+
+        $sth->execute();
+
+        while (my @node = $sth->fetchrow) { 
+            push(@g_cluster,  \@node);
+        }
+
+        $sth->finish;
+
+        $dbh->disconnect();
+    };
+    if ($@) {
+        if ($g_debug) {
+            printlogln($prefix,$logfile,$log_prefix,lookupMsg('dbg_generic', $@));
+        }
+        die lookupMsg('err_pgsql_connect');
+    }
+    else {
+        if (substr($version,0,1) < 2) {
+            printlogln($prefix,$logfile,$log_prefix,lookupMsg('wrn_not_tested', $version));
+        }
+        if (($g_use_try_blocks) && ($g_lockset_method eq 'multiple') && (substr($version,0,3) <= 9.9)) {
+            # It's currently not possible to lock multiple sets at a time within a try block (v2.2.2), leave the logic in and set a high version number for now.
+            printlogln($prefix,$logfile,$log_prefix, lookupMsg('note_multiple_try', $version));
+            $g_lockset_method = 'single';
+        }
+        if (substr($version,0,3) >= 2.2) {
+            $g_failover_method = 'new';
+            $g_resubscribe_method = 'resubscribe';
+        }
+    }
+
+    return (scalar(@g_cluster), $version);
+}
+
+sub loadSets {
+    my $dbconninfo = shift;
+    my $clname = shift;
+    my $nodenumber = shift;
+    my $dbuser = shift;
+    my $dbpass = shift;
+    my $prefix = shift;
+    my $logfile = shift;
+    my $log_prefix = shift;
+    
+    my $dsn;
+    my $dbh;
+    my $sth;
+    my $query;
+    my $qw_clname;
+
+    @g_sets = ();
+    
+    $dsn = "DBI:Pg:$dbconninfo;";
+    eval {
+        $dbh = DBI->connect($dsn, $dbuser, $dbpass, {RaiseError => 1});
+        $qw_clname = $dbh->quote_identifier("_" . $clname);
+        $query = "SELECT set_id, trim(regexp_replace(set_comment,'[^0-9,A-Z,a-z]','_','g')) FROM $qw_clname.sl_set WHERE set_origin = ? ORDER BY set_id;";
+
+        $sth = $dbh->prepare($query);
+        $sth->bind_param(1, $nodenumber);
+
+        $sth->execute();
+
+        while (my @set = $sth->fetchrow) { 
+            push(@g_sets,  \@set);
+        }
+
+        $sth->finish;
+        $dbh->disconnect();
+    };
+    if ($@) {
+        if ($g_debug) {
+            printlogln($prefix,$logfile,$log_prefix,lookupMsg('dbg_generic', $@));
+        }
+        die lookupMsg('err_pgsql_connect');
+    }
+
+    return scalar(@g_sets);
+}
+
+sub loadLag {
+    my $dbconninfo = shift;
+    my $clname = shift;
+    my $dbuser = shift;
+    my $dbpass = shift;
+    my $prefix = shift;
+    my $logfile = shift;
+    my $log_prefix = shift;
+
+    my $dsn;
+    my $dbh;
+    my $sth;
+    my $query;
+    my $qw_clname;
+
+    @g_lags = ();
+
+    $dsn = "DBI:Pg:$dbconninfo;";
+    eval {
+        $dbh = DBI->connect($dsn, $dbuser, $dbpass, {RaiseError => 1});
+        $qw_clname = $dbh->quote_identifier("_" . $clname);
+        $query = "SELECT a.st_origin || ' (' || coalesce(trim(replace(substring(b.no_comment from '\\\\((.+)\\\\)'), ' ','')), 'node' || b.no_id) || ')<->'
+                || a.st_received || ' (' || coalesce(trim(replace(substring(c.no_comment from '\\\\((.+)\\\\)'), ' ','')), 'node' || c.no_id) || ') Events: '
+                || a.st_lag_num_events || ' Time: ' || a.st_lag_time 
+            FROM $qw_clname.sl_status a
+            INNER JOIN $qw_clname.sl_node b on a.st_origin = b.no_id
+            INNER JOIN $qw_clname.sl_node c on a.st_received = c.no_id";
+
+        $sth = $dbh->prepare($query);
+        $sth->execute();
+
+        while (my $lag = $sth->fetchrow) { 
+            push(@g_lags,  $lag);
+        }
+
+        $sth->finish;
+        $dbh->disconnect();
+    };
+    if ($@) {
+        if ($g_debug) {
+            printlogln($prefix,$logfile,$log_prefix,lookupMsg('dbg_generic', $@));
+        }
+        die lookupMsg('err_pgsql_connect');
+    }
+
+    return scalar(@g_lags);
+}
+
+sub chooseNode {
+    my $type = shift;
+    my $prefix = shift;
+    my $logfile = shift;
+    my $log_prefix = shift;
+    my $last_choice = shift;
+    my $line;
+    my $choice;
+    my %options;
+    my $ok;
+    my @sets_from;
+    my @sets_to;
+    my $found = false;
+
+    $line = sprintf "%-4s %-14s %-10s %-24s %-s\n", lookupMsg('interactive_head_id'), lookupMsg('interactive_head_name'), lookupMsg('interactive_head_status'), lookupMsg('interactive_head_providers'), lookupMsg('interactive_head_config');
+    printlog($prefix,$logfile,$log_prefix,"$line");
+    $line = sprintf "%-4s %-14s %-10s %-24s %-s\n", "="x(length(lookupMsg('interactive_head_id'))), "="x(length(lookupMsg('interactive_head_name'))), "="x(length(lookupMsg('interactive_head_status'))), "="x(length(lookupMsg('interactive_head_providers'))), "="x(length(lookupMsg('interactive_head_config')));
+    printlog($prefix,$logfile,$log_prefix,"$line");
+
+    foreach (@g_cluster) {
+        $line = sprintf "%-4s %-14s %-10s %-24s %-s\n", $_->[0], $_->[4], $_->[6], ($_->[1] // "<NONE>"), (lookupMsg('interactive_detail_1') . ($_->[3] // "<NONE>"));
+        printlog($prefix,$logfile,$log_prefix,"$line");
+        $line = sprintf "%-55s %-s\n", " ", (lookupMsg('interactive_detail_2') . ($_->[7] // "<NONE>"));
+        printlog($prefix,$logfile,$log_prefix,"$line");
+        $line = sprintf "%-55s %-s\n", " ", (lookupMsg('interactive_detail_3') . ($_->[5] // "<NONE>"));
+        printlogln($prefix,$logfile,$log_prefix,"$line");
+        $options{$_->[0]} = {name => $_->[4], sets => ($_->[3] // ""), status => $_->[6], provider => $_->[7]};
+    }
+    if ($type !~ m/info/i) {
+        printlog($prefix,$logfile,$log_prefix,lookupMsg('interactive_choose_node', $type));
+        $choice = <>;
+        chomp($choice);
+    
+        if(exists($options{$choice})) {
+            if ($options{$choice}->{status} ne "ACTIVE") {
+                printlogln($prefix,$logfile,$log_prefix,lookupMsg('err_inactive', $choice, lc($options{$choice}->{status})));
+                $choice = -1;
+            }
+            elsif (($type =~ m/from/i) && (length(trim($options{$choice}->{sets})) <= 0)) {
+                printlogln($prefix,$logfile,$log_prefix,lookupMsg('err_not_origin', $choice));
+                $choice = -1;
+            }    
+            elsif ($type =~ m/to/i) {
+                if (length(trim($options{$choice}->{provider})) <= 0) {
+                    printlogln($prefix,$logfile,$log_prefix,lookupMsg('err_not_provider', $choice));
+                    $choice = -1;
+                }
+                else {
+                    foreach my $old_origin (@g_cluster) {
+                        if ($old_origin->[0] == $last_choice) {
+                            @sets_from = split(',', $old_origin->[3]);
+                            @sets_to =  split(',', $options{$choice}->{provider});
+                            if (checkProvidesAllSets(\@sets_from, \@sets_to)) {
+                                $found = true;
+                            }
+                            else {
+                                printlogln($prefix,$logfile,$log_prefix,lookupMsg('err_not_provider_sets',$choice,$old_origin->[3],$options{$choice}->{providers}));
+                                $choice = -1;
+                            }
+                            last;
+                        }
+                    }
+                    unless ($found) {
+                        printlogln($prefix,$logfile,$log_prefix,lookupMsg('err_no_configuration', $last_choice));
+                        $choice = -1;
+                    } 
+                }
+            }    
+            else {
+                printlog($prefix,$logfile,$log_prefix,lookupMsg('interactive_confirm',$type,$choice,$options{$choice}->{name}));
+                $ok = <>;
+                chomp($ok);    
+                if ($ok !~ /^[Y|O]$/i) {
+                    printlogln($prefix,$logfile,$log_prefix,lookupMsg('exit_noaction'));
+                    $choice = 0;
+                }
+            }
+        }
+        elsif (!length($choice)) {
+            printlogln($prefix,$logfile,$log_prefix,lookupMsg('err_must_enter_node_id'));
+            $choice = -1;
+        }
+        else {
+            printlogln($prefix,$logfile,$log_prefix,lookupMsg('err_not_a_node_id', $choice));
+            $choice = -1;
+        }
+    }
+
+    return $choice;
+}
+
+sub writePreamble {
+    my $filename = shift;
+    my $dbconninfo = shift;
+    my $clname = shift;
+    my $dbuser = shift;
+    my $dbpass = shift;
+    my $sets = shift;
+    my $aliases = shift;
+    my $prefix = shift;
+    my $logfile = shift;
+    my $log_prefix = shift;
+    my $comment_all_failed = shift;
+    my $set_count;
+    my $line_prefix;
+    my $success = false;
+
+    my ($year, $month, $day, $hour, $min, $sec) = (localtime(time))[5,4,3,2,1,0];
+    my $date = sprintf ("%02d:%02d:%02d on %02d/%02d/%04d", $hour, $min, $sec, $day, $month+1, $year+1900);
+
+    if (open(SLONFILE, ">", $filename)) {    
+        print SLONFILE ("# Script autogenerated on $date\n\n");
+        print SLONFILE ("######\n# Preamble (cluster structure)\n######\n\n# Cluster name\n");
+        if ($aliases) {
+            print SLONFILE ("DEFINE slony_cluster_name $clname;\n");
+            print SLONFILE ("CLUSTER NAME = \@slony_cluster_name;\n\n");
+        }
+        else {
+            print SLONFILE ("CLUSTER NAME = $clname;\n\n");
+        }
+        foreach (@g_cluster) {
+            $line_prefix = '';
+            if (($comment_all_failed) && (exists $g_backups{$_->[0]})) {
+                $line_prefix = "# (Node $_->[0] unavailable) ";
+            }
+            elsif (!$g_fail_subonly) {
+                foreach my $unresponsive (@g_unresponsive) {
+                    if (($_->[0] == $unresponsive->[0]) && !defined($_->[9])) {
+                        $line_prefix = "# (Node $_->[0] unavailable subscriber only) ";
+                    }
+                }
+            }
+            print SLONFILE ("# Preamble for node $_->[0] named $_->[4]\n");
+            if ($aliases) {
+                print SLONFILE ($line_prefix . "DEFINE $_->[4] $_->[0];\n");
+                print SLONFILE ($line_prefix . "DEFINE $_->[4]_conninfo '$_->[2]';\n");
+                print SLONFILE ($line_prefix . "NODE \@$_->[4] ADMIN CONNINFO = \@$_->[4]_conninfo;\n\n");
+            }
+            else {
+                print SLONFILE ($line_prefix . "NODE $_->[0] ADMIN CONNINFO = '$_->[2]';\n\n");
+            }
+            if (($aliases) && ($sets)) {
+                $set_count = loadSets($dbconninfo, $clname, $_->[0], $dbuser, $dbpass, $prefix, $logfile, $log_prefix);
+                if ($set_count > 0) {
+                    print SLONFILE ("# Sets provided (currently) by node $_->[0]\n");
+                    foreach my $set (@g_sets) {
+                        print SLONFILE ($line_prefix . "DEFINE $set->[1] $set->[0];\n");
+                    }
+                    print SLONFILE ("\n");
+                }
+            }
+        }    
+        $success = true;
+    }
+    else {
+        printlogln($prefix,$logfile,$log_prefix,lookupMsg('err_write_fail', "script", $filename));
+        $success = false; 
+    }
+    return $success;
+}
+
+sub writeMoveSet {
+    my $prefix = shift;
+    my $dbconninfo = shift;
+    my $clname = shift;
+    my $dbuser = shift;
+    my $dbpass = shift;
+    my $from = shift;
+    my $to = shift;
+    my $subs = shift;
+    my $aliases = shift;
+    my $logfile = shift;
+    my $log_prefix = shift;
+    my $from_name;
+    my $to_name;
+    my $set_count;
+    my $line_prefix;
+    my $try_prefix = "";
+    my ($year, $month, $day, $hour, $min, $sec) = (localtime(time))[5,4,3,2,1,0];
+    my $filetime = sprintf ("%02d_%02d_%04d_%02d:%02d:%02d", $day, $month+1, $year+1900, $hour, $min, $sec);
+    my $filename = $prefix . "/" . $clname . "-move_sets_from_" . $from . "_to_" . $to . "_on_" . $filetime . ".scr";
+
+    if ($g_use_try_blocks) {
+        $try_prefix = "\t";
+    }
+    my @subprov_name;
+    my $subprov_idx;
+    my $subprov;
+    my ($node, $setlist);
+    my ($node_name, $setlist_name);
+    my @subsets;
+
+    unless (writePreamble($filename, $dbconninfo, $clname, $dbuser, $dbpass, true, $aliases, $prefix, $logfile, $log_prefix, true)) {
+        printlogln($prefix,$logfile,$log_prefix,lookupMsg('err_incomplete_preamble'));
+    }
+
+    foreach (@g_cluster) {
+        if ($_->[0] == $from) {
+            $from_name = $_->[4];
+        }
+        elsif ($_->[0] == $to) {
+            $to_name = $_->[4];
+        }
+    }
+
+    if (open(SLONFILE, ">>", $filename)) {
+
+        print SLONFILE ("######\n# Actions (changes to cluster structure)\n######\n");
+        
+        $set_count = loadSets($dbconninfo, $clname, $from, $dbuser, $dbpass, $prefix, $logfile, $log_prefix);
+        if ($set_count > 0) {
+
+            if ($g_lockset_method ne "single") {
+                if ($g_use_try_blocks) {
+                    print SLONFILE ("TRY {\n");                
+                }
+                foreach (@g_sets) {
+                    if ($aliases) {    
+                        print SLONFILE ($try_prefix . "ECHO 'Locking set $_->[1] ($_->[0])';\n");
+                        print SLONFILE ($try_prefix . "LOCK SET ( ID = \@$_->[1], ORIGIN = \@$from_name);\n");
+                    }
+                    else {
+                        print SLONFILE ($try_prefix . "ECHO 'Locking set $_->[0]';\n");
+                        print SLONFILE ($try_prefix . "LOCK SET ( ID = $_->[0], ORIGIN = $from);\n");
+                    }
+                
+                }
+                print SLONFILE ("\n");
+                foreach (@g_sets) {
+                    if ($aliases) {    
+                        print SLONFILE ($try_prefix . "ECHO 'Moving set $_->[1] ($_->[0])';\n");
+                        print SLONFILE ($try_prefix . "MOVE SET ( ID = \@$_->[1], OLD ORIGIN = \@$from_name, NEW ORIGIN = \@$to_name);\n");
+                    }
+                    else {
+                        print SLONFILE ($try_prefix . "ECHO 'Moving set $_->[0]';\n");
+                        print SLONFILE ($try_prefix . "MOVE SET ( ID = $_->[0], OLD ORIGIN = $from, NEW ORIGIN = $to);\n");
+                    }
+                
+                }                
+                if ($g_use_try_blocks) {
+                    print SLONFILE ("}\nON ERROR {\n");
+                    foreach (@g_sets) {
+                        if ($aliases) {    
+                            print SLONFILE ($try_prefix . "ECHO 'Unlocking set $_->[1] ($_->[0])';\n");
+                            print SLONFILE ($try_prefix . "UNLOCK SET ( ID = \@$_->[1], ORIGIN = \@$from_name);\n");
+                        }
+                        else {
+                            print SLONFILE ($try_prefix . "ECHO 'Unlocking set $_->[0]';\n");
+                            print SLONFILE ($try_prefix . "UNLOCK SET ( ID = $_->[0], ORIGIN = $from);\n");
+                        }
+                    }
+                    print SLONFILE ("\tEXIT 1;\n}\nON SUCCESS {\n");
+                }
+                if ($aliases) {    
+                    print SLONFILE ($try_prefix . "WAIT FOR EVENT (ORIGIN = \@$from_name, CONFIRMED = ALL, WAIT ON = \@$from_name, TIMEOUT = 0);\n");
+                }
+                else {
+                    print SLONFILE ($try_prefix . "WAIT FOR EVENT (ORIGIN = $from, CONFIRMED = ALL, WAIT ON = $from, TIMEOUT = 0);\n");
+                }
+                if ($g_use_try_blocks) {
+                    print SLONFILE ("}\n");
+                }
+            }
+            foreach (@g_sets) {
+                if ($g_lockset_method eq "single") {
+                    if ($aliases) {    
+                        print SLONFILE ("\nECHO 'Moving set $_->[1] ($_->[0])';\n");
+                        if ($g_use_try_blocks) {
+                            print SLONFILE ("TRY {\n");
+                        }
+                        print SLONFILE ($try_prefix . "LOCK SET ( ID = \@$_->[1], ORIGIN = \@$from_name);\n");
+                        print SLONFILE ($try_prefix . "MOVE SET ( ID = \@$_->[1], OLD ORIGIN = \@$from_name, NEW ORIGIN = \@$to_name);\n");
+                        if ($g_use_try_blocks) {
+                            print SLONFILE ("}\nON ERROR {\n" . $try_prefix . "UNLOCK SET ( ID = \@$_->[1], ORIGIN = \@$from_name);\n" . $try_prefix . "EXIT 1;\n}\n");
+                        }
+                        print SLONFILE ("WAIT FOR EVENT (ORIGIN = \@$from_name, CONFIRMED = ALL, WAIT ON = \@$from_name, TIMEOUT = 0);\n");
+                    }
+                    else {
+                        print SLONFILE ("\nECHO 'Moving set $_->[0]';\n");
+                        if ($g_use_try_blocks) {
+                            print SLONFILE ("TRY {\n");
+                        }
+                        print SLONFILE ($try_prefix . "LOCK SET ( ID = $_->[0], ORIGIN = $from);\n");
+                        print SLONFILE ($try_prefix . "MOVE SET ( ID = $_->[0], OLD ORIGIN = $from, NEW ORIGIN = $to);\n");
+                        if ($g_use_try_blocks) {
+                            print SLONFILE ("}\nON ERROR {\n" . $try_prefix . "UNLOCK SET ( ID = $_->[0], ORIGIN = $from);\n" . $try_prefix . "EXIT 1;\n}\n");
+                        }
+                        print SLONFILE ("WAIT FOR EVENT (ORIGIN = $from, CONFIRMED = ALL, WAIT ON = $from, TIMEOUT = 0);\n");
+                    }
+                }    
+                if (($subs) && ($g_resubscribe_method eq 'subscribe')) { 
+    
+                    foreach my $other_subs (@g_cluster) {
+                        if (($other_subs->[6] ne "INACTIVE") && ($other_subs->[0] != $from) && ($other_subs->[0] != $to)) {
+
+                            if (exists $g_backups{$other_subs->[0]}) {
+                                $line_prefix = "# (Node $other_subs->[0] unavailable) ";
+                            }
+                            else {
+                                $line_prefix = '';
+                            }
+
+                            # mess here needs cleaning up
+                            @subprov_name = (split(';', $other_subs->[8]));
+                            $subprov_idx = 0;
+                            foreach $subprov (split(';', $other_subs->[5])) {
+                                ($node, $setlist) = (split('->', $subprov)) ;
+                                ($node_name, $setlist_name) = (split('->', $subprov_name[$subprov_idx])) ;
+                                $subprov_idx++;
+                                $node =~ s/n//g;
+                                $setlist =~ s/(\)|\(|s)//g;
+                                @subsets = (split(',', $setlist)) ;
+
+                                if ($g_debug) {
+                                    printlogln($prefix,$logfile,$log_prefix,lookupMsg('dbg_resubscribe', $_->[1], $_->[0]), $other_subs->[0], $other_subs->[4], $setlist, $setlist_name, $node, $node_name);
+                                }    
+
+                                if ($_->[0] ~~ @subsets) {
+                                    if ($node == $from) {
+                                        if ($aliases) {
+                                            print SLONFILE ($line_prefix . 
+                                                "ECHO 'Issuing subscribe for set $_->[1] ($_->[0]) provider $to_name ($to) -> " .
+                                                "receiver $other_subs->[4] ($other_subs->[0])';\n");
+                                                   print SLONFILE ($line_prefix . 
+                                                "SUBSCRIBE SET ( ID = \@$_->[1], PROVIDER = \@$to_name, " .
+                                                "RECEIVER = \@$other_subs->[4], FORWARD = YES);\n");
+                                        }
+                                        else {
+                                            print SLONFILE ($line_prefix . 
+                                                "ECHO 'Issuing subscribe for set $_->[1] ($_->[0]) provider $to -> " .
+                                                "receiver $other_subs->[0]';\n");
+                                            print SLONFILE ($line_prefix . "SUBSCRIBE SET ( ID = $_->[0], PROVIDER = $to, " .
+                                                "RECEIVER = $other_subs->[0], FORWARD = YES);\n");
+                                        }
+                                    }
+                                    else {
+                                        if ($aliases) {
+                                            print SLONFILE ($line_prefix . 
+                                                "ECHO 'Issuing subscribe for set $_->[1] ($_->[0]) provider $node_name ($node) -> " . 
+                                                "receiver $other_subs->[4] ($other_subs->[0])';\n");
+                                            print SLONFILE ($line_prefix . "SUBSCRIBE SET ( ID = \@$_->[1], PROVIDER = \@$node_name, " .
+                                                "RECEIVER = \@$other_subs->[4], FORWARD = YES);\n");
+                                            }
+                                            else {
+                                                print SLONFILE ($line_prefix . 
+                                                "ECHO 'Issuing subscribe for set $_->[1] ($_->[0]) provider $node -> " .
+                                                "receiver $other_subs->[0]';\n");
+                                                print SLONFILE ($line_prefix . "SUBSCRIBE SET ( ID = $_->[0], PROVIDER = $node, " .
+                                                "RECEIVER = $other_subs->[0], FORWARD = YES);\n");
+                                        }
+                                    }
+                                }
+                            }
+                        }
+                    }
+                }    
+            }
+
+            if (($subs) && ($g_resubscribe_method eq 'resubscribe')) { 
+
+                foreach my $other_subs (@g_cluster) {
+                    if (($other_subs->[6] ne "INACTIVE") && ($other_subs->[0] != $from) && ($other_subs->[0] != $to)) {
+                        if (exists $g_backups{$other_subs->[0]}) {
+                            $line_prefix = "# (Node $other_subs->[0] unavailable) ";
+                        }
+                        else {
+                            $line_prefix = '';
+                        }
+
+                        @subprov_name = (split(';', $other_subs->[8]));
+                        $subprov_idx = 0;
+                        foreach $subprov (split(';', $other_subs->[5])) {
+                            ($node, $setlist) = (split('->', $subprov)) ;
+                            ($node_name, $setlist_name) = (split('->', $subprov_name[$subprov_idx])) ;
+                            $subprov_idx++;
+                            $node =~ s/n//g;
+    
+                            print SLONFILE ("\n");
+                            if ($node == $from) {
+                                if ($aliases) {
+                                    print SLONFILE ($line_prefix .
+                                        "ECHO 'Issuing resubscribe for provider $to_name ($to) -> receiver $other_subs->[4] ($other_subs->[0])';\n");
+                                    print SLONFILE ($line_prefix .
+                                        "RESUBSCRIBE NODE ( ORIGIN = \@$to_name, PROVIDER = \@$to_name, RECEIVER = \@$other_subs->[4]);\n");
+                                 }
+                                 else {
+                                     print SLONFILE ($line_prefix .
+                                        "ECHO 'Issuing resubscribe for provider $to -> receiver $other_subs->[0]';\n");
+                                    print SLONFILE ($line_prefix . 
+                                        "SUBSCRIBE NODE ( ORIGIN = $to, PROVIDER = $to, RECEIVER = $other_subs->[0] );\n");
+                                 }            
+                            }
+                            else {
+                                if ($aliases) {
+                                    print SLONFILE ($line_prefix .
+                                        "ECHO 'Issuing resubscribe for provider $node_name ($node) -> receiver $other_subs->[4] ($other_subs->[0])';\n");
+                                    print SLONFILE ($line_prefix . 
+                                        "RESUBSCRIBE NODE ( ORIGIN = \@$to_name, PROVIDER = \@$node_name, RECEIVER = \@$other_subs->[4]);\n");
+                                }
+                                else {
+                                    print SLONFILE ($line_prefix .
+                                        "ECHO 'Issuing resubscribe for provider $node -> receiver $other_subs->[0]';\n");
+                                    print SLONFILE ($line_prefix .
+                                        "RESUBSCRIBE NODE ( ORIGIN = $to, PROVIDER = $node, RECEIVER = $other_subs->[0]);\n");
+                                }
+                            }
+                        }
+                    }
+                }
+            }
+
+        }
+
+        if ($aliases) {    
+            print SLONFILE ("\nECHO 'All sets originating from $from_name (id $from) have been moved to $to_name (id $to), ensure you modify any existing slonik scripts to reflect the new origin';\n");
+        }
+        else {
+            print SLONFILE ("\nECHO 'All sets originating from node $from have been moved to node $to, ensure you modify the any existing slonik scripts to reflect the new origin';\n");
+        }
+        close (SLONFILE);
+    }
+    else {
+        printlogln($prefix,$logfile,$log_prefix,lookupMsg('err_write_fail', "script", $filename));
+    }
+    return $filename;
+}
+
+sub writeFailover {
+    my $prefix = shift;
+    my $dbconninfo = shift;
+    my $clname = shift;
+    my $dbuser = shift;
+    my $dbpass = shift;
+    my $from = shift;
+    my $to = shift;
+    my $subs = shift;
+    my $aliases = shift;
+    my $logfile = shift;
+    my $log_prefix = shift;
+    my $filename;
+    my $written;
+    my $event_node;
+    my ($year, $month, $day, $hour, $min, $sec) = (localtime(time))[5,4,3,2,1,0];
+    my $filetime = sprintf ("%02d_%02d_%04d_%02d:%02d:%02d", $day, $month+1, $year+1900, $hour, $min, $sec);
+
+    if (defined($from) && defined($to)) {
+        $filename = $prefix . "/" . $clname . "-failover_from_" . $from . "_to_" . $to . "_on_" . $filetime . ".scr";
+    }
+    else {
+        $filename = $prefix . "/" . $clname . "-autofailover_on_" . $filetime . ".scr";
+    }
+
+    unless (writePreamble($filename, $dbconninfo, $clname, $dbuser, $dbpass, false, $aliases, $prefix, $logfile, $log_prefix, false)) {
+        printlogln($prefix,$logfile,$log_prefix,lookupMsg('err_incomplete_preamble'));
+    }
+
+    if (open(SLONFILE, ">>", $filename)) {
+
+        print SLONFILE ("######\n# Actions (changes to cluster structure)\n######\n\n");
+        if ($g_debug) {
+            printlogln($prefix,$logfile,$log_prefix,lookupMsg('dbg_failover_method',$g_failover_method));
+        }
+
+        foreach (@g_failed) {
+            foreach my $backup (@g_cluster) {
+                if ($backup->[0] == $g_backups{$_->[0]}) {
+                    ## Here we have both details of the backup node and the failed node
+                    if ($aliases) {
+                        print SLONFILE ("ECHO 'Failing over slony cluster from $_->[4] (id $_->[0]) to $backup->[4] (id $backup->[0])';\n");
+                    }
+                    else {
+                        print SLONFILE ("ECHO 'Failing over slony cluster from node $_->[0] to node $backup->[0]';\n");
+                    }   
+                    last;
+                }
+            }
+        }
+
+        print SLONFILE ("FAILOVER (\n\t");
+        $written = 0;
+        foreach (@g_failed) {
+            foreach my $backup (@g_cluster) {
+
+                if ($backup->[0] == $g_backups{$_->[0]}) {
+                    ## Here we have both details of the backup node and the failed node
+                    if ($g_failover_method eq 'new') {
+                        if( $written != 0 ) {
+                            print SLONFILE (",\n\t");
+                        }
+                        print SLONFILE ("NODE = (");
+                    }
+                    else {
+                        if( $written != 0 ) {
+                            print SLONFILE ("\n);\nFAILOVER (\n\t");
+                        }
+                    }
+                    if ($aliases) {
+                        print SLONFILE ("ID = \@$_->[4], BACKUP NODE = \@$backup->[4]");
+                    }
+                    else {
+                        print SLONFILE ("ID = $_->[0], BACKUP NODE = $backup->[0]");
+                    }
+                    if ($g_failover_method eq 'new') {
+                        print SLONFILE (")");
+                    }
+                    last;
+                }
+            }
+            $written++;
+        }
+        print SLONFILE ("\n);\n");
+
+        if ($g_drop_failed) {
+
+
+            if (($g_failover_method eq 'new')  && (scalar(@g_failed) > 1)) {
+                foreach (@g_failed) {
+                    if ($aliases) {
+                        print SLONFILE ("ECHO 'Dropping node $_->[4] ($_->[0])';\n");
+                    }
+                    else {
+                        print SLONFILE ("ECHO 'Dropping node $_->[0]';\n");
+                    }   
+                }
+
+                print SLONFILE ("DROP NODE (ID = '");
+                undef $event_node;
+            }
+            $written = 0;
+            foreach (@g_failed) {
+                foreach my $backup (@g_cluster) {
+                    if ($backup->[0] == $g_backups{$_->[0]}) {
+                        if (!defined($event_node)) {
+                            if ($aliases) {
+                                $event_node = $backup->[4];
+                            }
+                            else {
+                                $event_node = $backup->[0];
+                            }
+                        } 
+                        if (($g_failover_method eq 'new')  && (scalar(@g_failed) > 1)) {
+                            if( $written != 0 ) {
+                                print SLONFILE (",");
+                            }
+                            ## Don;t bother being pissy and trying to define array values 
+                            #if ($aliases) {
+                            #    print SLONFILE "\@$_->[4]";
+                            #}
+                            #else {
+                                print SLONFILE $_->[0];
+                            #}
+                            $written++;
+                        }
+                        else {
+                            if ($aliases) {
+                                print SLONFILE ("ECHO 'Dropping node $_->[4] ($_->[0])';\n");
+                                print SLONFILE ("DROP NODE (ID = \@$_->[4], EVENT NODE = \@$backup->[4]);\n");
+                            }
+                            else {
+                                print SLONFILE ("ECHO 'Dropping node $_->[0]';\n");
+                                print SLONFILE ("DROP NODE (ID = $_->[0], EVENT NODE = $backup->[0]);\n");
+                            }
+                        }
+                        last;
+                    }
+                }   
+            }
+            if (($g_failover_method eq 'new')  && (scalar(@g_failed) > 1)) {
+                if ($aliases) {
+                     print SLONFILE ("', EVENT NODE = \@$event_node);\n");
+                }
+                else {
+                     print SLONFILE ("', EVENT NODE = $event_node);\n");
+                }
+            }
+        }
+
+    }
+    else {
+        printlog($prefix,$logfile,$log_prefix,lookupMsg('err_write_fail', "script", $filename));
+    }
+    return $filename;
+
+}
+
+sub lookupMsg {
+    my $name = shift || '?';
+    my $line_call;
+    my $text;
+
+    if (exists $message{$g_lang}{$name}) {
+        $text = $message{$g_lang}{$name};
+    }
+    elsif (exists $message{'en'}{$name}) {
+        $text = $message{'en'}{$name};
+    }
+    else {
+        $line_call = (caller)[2];
+        $text = qq{Failed to lookup text "$name" at line $line_call};
+    }
+
+    my $x=1;
+    {
+        my $val = $_[$x-1];
+        $val = '?' if ! defined $val;
+        last unless $text =~ s/\$$x/$val/g;
+        $x++;
+        redo;
+    }
+    return $text;
+}
+
+sub trim($) {
+    my $string = shift;
+    $string =~ s/^\s+//;
+    $string =~ s/\s+$//;
+    return $string;
+}
+
+sub println {
+    print ((@_ ? join($/, @_) : $_), $/);
+}
+
+sub printlog {
+    my $prefix = shift;
+    my $logfile_name = shift;
+    my $log_prefix = shift;
+    my $message = shift;
+    my $logfile;
+    my $date;
+
+    print $message;
+
+    if (defined($logfile_name)) {
+
+        # Do we have to do this all the time? Perhaps could check parameters first
+        if ($logfile_name =~ /^\//i) {
+            $logfile = strftime($logfile_name, localtime);
+        }
+        else {
+            $logfile = "$prefix/" . strftime($logfile_name, localtime);
+        }
+    
+        if ($log_prefix =~ m/(\%[mt])/) {
+            my ($year, $month, $day, $hour, $min, $sec) = (localtime(time))[5,4,3,2,1,0];
+            my ($h_sec, $h_msec) = gettimeofday;
+            $date = sprintf ("%02d-%02d-%04d %02d:%02d:%02d.%03d", $day, $month+1, $year+1900, $hour, $min, $sec, $h_msec/1000);
+            $log_prefix =~ s/\%m/$date/g;
+    
+            $date = sprintf ("%02d-%02d-%04d %02d:%02d:%02d", $day, $month+1, $year+1900, $hour, $min, $sec);
+            $log_prefix =~ s/\%t/$date/g;
+        }
+        if ($log_prefix =~ m/(\%p)/) {
+            $log_prefix =~ s/\%p/$g_pid/g;
+        }
+
+        if (open(LOGFILE, ">>", $logfile)) {
+            print LOGFILE $log_prefix . " " . $message;
+            close (LOGFILE);
+        }
+        else {
+            println(lookupMsg('err_write_fail', "logfile", $logfile));
+        }
+    }
+}
+
+sub printlogln {
+    printlog ($_[0], $_[1], $_[2], $_[3] . $/);
+}
+
+sub logDB {
+    my $dbconninfo = shift;
+    my $dbuser = shift;
+    my $dbpass = shift;
+    my $exit_code = shift;
+    my $reason = shift;
+    my $prefix = shift;
+    my $logfile = shift;
+    my $log_prefix = shift;
+    my $clname = shift;
+    my $script = shift;
+
+    my $dsn;
+    my $dbh;
+    my $sth;
+    my $query;
+
+    my $results;
+    my $script_data;
+
+    unless($results = (read_file($logfile))) { 
+        printlogln($prefix,$logfile,$log_prefix,lookupMsg('err_read_fail', "logfile", $logfile));
+    }
+
+    if (defined($script) && (-e $script)) {
+        unless ($script_data = (read_file($script))) { 
+            printlogln($prefix,$logfile,$log_prefix,lookupMsg('err_read_fail', "script file", $script));
+        }
+    }
+    else {
+        $script_data = "No script data was generated.";
+        $script = "No script generated.";
+    }
+
+    $dsn = "DBI:Pg:$dbconninfo;";
+    eval {
+        $dbh = DBI->connect($dsn, $dbuser, $dbpass, {RaiseError => 1});
+        $query = "INSERT INTO public.failovers (reason, exit_code, results, script, cluster_name)
+              VALUES (?, ?, ?, ?, ?)";
+
+        $sth = $dbh->prepare($query);
+
+        $sth->bind_param(1, $reason);
+        $sth->bind_param(2, $exit_code);
+        $sth->bind_param(3, $results);
+        $sth->bind_param(4, $script . ":\n" . $script_data);
+        $sth->bind_param(5, $clname);
+
+        $sth->execute();
+
+        $sth->finish;
+        $dbh->disconnect();
+    };
+    if ($@) {
+        if ($g_debug) {
+            printlogln($prefix,$logfile,$log_prefix,lookupMsg('dbg_generic', $@));
+        }
+        die lookupMsg('err_pgsql_connect');
+    }
+
+    return true;
+}
+
+sub getUUID {
+    my $date_string = shift;
+    my $g_ug  = new Data::UUID;
+    my $g_uuid = $g_ug->create_from_name("failover_script", $date_string);
+    my $g_uuid_str  = $g_ug->to_string($g_uuid);
+    return $g_uuid_str;
+}
+
+sub writePID {
+    my $prefix = shift;
+    my $logfile = shift;
+    my $log_prefix = shift;
+    my $pidfile_name = shift;
+    my $pidfile;
+    my $success = true;
+    if ($pidfile_name =~ /^\//i) {
+        $pidfile = $pidfile_name;
+    }
+    else {
+        $pidfile = "$prefix/" . $pidfile_name;
+    }
+    eval {
+        open (PIDFILE, ">", $pidfile);
+        print PIDFILE $$;
+        close (PIDFILE);
+    };
+    if ($@) {
+        if ($g_debug) {
+            printlogln($prefix,$logfile,$log_prefix, lookupMsg('dbg_generic', $!));       
+        }
+        printlogln($prefix,$logfile,$log_prefix, lookupMsg('err_write_fail', "pid file", $pidfile));
+        $success = false;
+    }
+    return $success;
+}
+
+sub removePID {
+    my $prefix = shift;
+    my $logfile = shift;
+    my $log_prefix = shift;
+    my $pidfile_name = shift;
+    my $pidfile;
+    my $success = true;
+
+    if ($pidfile_name =~ /^\//i) {
+        $pidfile = $pidfile_name;
+    }
+    else {
+        $pidfile = "$prefix/" . $pidfile_name;
+    }
+    eval {
+        if (-f $pidfile) {
+            unlink $pidfile;
+        }
+        else {
+            printlogln($prefix,$logfile,$log_prefix, lookupMsg('dbg_generic', 'PID file never existed to be removed'));
+        } 
+    };
+    if ($@) {
+        if ($g_debug) {
+            printlogln($prefix,$logfile,$log_prefix, lookupMsg('dbg_generic', $!));
+        }
+        printlogln($prefix,$logfile,$log_prefix, lookupMsg('err_unlink_fail', "pid file", $pidfile));
+        $success = false;
+    }
+    return $success;
+}
+
+sub checkProvidesAllSets { 
+    my ($originSets, $providerSets) = @_;
+    my %test_hash;
+
+    undef @test_hash{@$originSets};       # add a hash key for each element of @$originSets
+    delete @test_hash{@$providerSets};    # remove all keys for elements of @$providerSets
+
+    return !%test_hash;              # return false if any keys are left in the hash
+}
+
+sub checkSubscribesAnySets {
+    my ($originSets, $subscriberSets) = @_;
+    my $before;
+    my $after;
+    my %test_hash;
+
+    undef @test_hash{@$originSets};       # add a hash key for each element of @$originSets
+    $before = scalar(keys %test_hash);
+    delete @test_hash{@$subscriberSets};    # remove all keys for elements of @$subscriberSets
+    $after = scalar(keys %test_hash);
+    return ($before != $after);        # return false if no keys were removed from the hash
+}
+
+sub getConfig {
+    my $cfgfile = shift;
+    my @fields;
+    my $success = false;
+
+    if (open(CFGFILE, "<", $cfgfile)) {
+        foreach (<CFGFILE>) {
+            chomp $_;
+            for ($_) {
+                s/\r//;
+                s/\#.*//;
+            }
+            if (length(trim($_))) {
+                @fields = split('=', $_, 2);
+                given(lc($fields[0])) {
+                    when(/\blang\b/i) {
+                        $g_lang = trim($fields[1]);
+                    }
+                    when(/\bslony_database_host\b/i) {
+                        $g_dbhost = trim($fields[1]);
+                    }
+                    when(/\bslony_database_port\b/i) {
+                        $g_dbport = $fields[1];
+                    }
+                    when(/\bslony_database_name\b/i) {
+                        $g_dbname = trim($fields[1]);
+                    }
+                    when(/\bslony_database_user\b/i) {
+                        $g_dbuser = trim($fields[1]);
+                    }
+                    when(/\bslony_database_password\b/i) {
+                        $g_dbpass = trim($fields[1]);
+                    }
+                    when(/\bslony_cluster_name\b/i) {
+                        $g_clname = trim($fields[1]);
+                    }
+                    when(/\benable_debugging\b/i) {
+                        $g_debug = checkBoolean(trim($fields[1]));
+                    }
+                    when(/\bprefix_directory\b/i) {
+                        $g_prefix = trim($fields[1]);
+                    }
+                    when(/\bseparate_working_directory\b/i) {
+                        $g_separate_working = checkBoolean(trim($fields[1]));
+                    }
+                    when(/\bpid_filename\b/i) {
+                        $g_pidfile = trim($fields[1]);
+                    }
+                    when(/\bfailover_offline_subscriber_only\b/i) {
+                        $g_fail_subonly = checkBoolean(trim($fields[1]));
+                    }
+                    when(/\bdrop_failed_nodes\b/i) {
+                        $g_drop_failed = checkBoolean(trim($fields[1]));
+                    }
+                    when(/\blog_line_prefix\b/i) {
+                        $g_log_prefix = trim($fields[1]);
+                    }
+                    when(/\blog_filename\b/i) {
+                        $g_logfile = trim($fields[1]);
+                    }
+                    when(/\blog_to_postgresql\b/i) {
+                        $g_log_to_db = checkBoolean(trim($fields[1]));
+                    }
+                    when(/\blog_database_host\b/i) {
+                        $g_logdb_host = trim($fields[1]);
+                    }
+                    when(/\blog_database_port\b/i) {
+                        $g_logdb_port = $fields[1];
+                    }
+                    when(/\blog_database_name\b/i) {
+                        $g_logdb_name = trim($fields[1]);
+                    }
+                    when(/\blog_database_user\b/i) {
+                        $g_logdb_user = trim($fields[1]);
+                    }
+                    when(/\blog_database_password\b/i) {
+                        $g_logdb_pass = trim($fields[1]);
+                    }
+                    when(/\benable_try_blocks\b/i) {
+                        $g_use_try_blocks = checkBoolean(trim($fields[1]));
+                    }
+                    when(/\bpull_aliases_from_comments\b/i) {
+                        $g_use_comment_aliases = checkBoolean(trim($fields[1]));
+                    }
+                    when(/\bslonik_path\b/i) {
+                        $g_slonikpath = trim($fields[1]);
+                    }
+                    when(/\blockset_method\b/i) {
+                        $g_lockset_method = trim($fields[1]);
+                    }
+                    when(/\benable_autofailover\b/i) {
+                        $g_autofailover = checkBoolean(trim($fields[1]));
+                    }
+                    when(/\bautofailover_poll_interval\b/i) {
+                        $g_autofailover_poll_interval = checkInteger(trim($fields[1]));
+                    }
+                    when(/\bautofailover_node_retry\b/i) {
+                        $g_autofailover_retry = checkInteger(trim($fields[1]));
+                    }
+                    when(/\bautofailover_sleep_time\b/i) {
+                        $g_autofailover_retry_sleep = checkInteger(trim($fields[1]));
+                    }
+                    when(/\bautofailover_forwarding_providers\b/i) {
+                        $g_autofailover_provs = checkBoolean(trim($fields[1]));
+                    }
+                    when(/\bautofailover_config_any_node\b/i) {
+                        $g_autofailover_config_any = checkBoolean(trim($fields[1]));
+                    }
+                }
+            }
+        }
+        close (CFGFILE);
+
+        $success = true;
+    }
+    else {
+        println(lookupMsg('err_fail_config'));
+    }
+
+    return $success;
+}
+
+sub checkBoolean {
+    my $text = shift;
+    my $value = undef;
+    if ( grep /^$text$/i, ("y","yes","t","true","on") ) {
+        $value = true;
+    }
+    elsif ( grep /^$text$/i, ("n","no","f","false","off") ) {
+        $value = false;
+    }
+    return $value;
+}
+
+sub checkInteger {
+    my $integer = shift;
+    my $value = undef;
+
+    if (($integer * 1) eq $integer) {
+        $value = int($integer);
+    }
+    return $value;
+}
+
+
+sub runSlonik {
+    my $script = shift;
+    my $prefix = shift;
+    my $logfile = shift;
+    my $log_prefix = shift;
+    my $success;
+
+    if ($g_debug) {
+         printlogln($prefix,$logfile,$log_prefix, lookupMsg('dbg_slonik_script', $script));
+    }
+    if (open(SLONIKSTATUS, "-|", "slonik $script 2>&1")) {
+        while (<SLONIKSTATUS>) {
+            printlogln($prefix,$logfile,$log_prefix,lookupMsg('slonik_output', $_));
+        }
+        close(SLONIKSTATUS);
+        $success = true;
+    }
+    else {
+        printlogln($prefix,$logfile,$log_prefix, lookupMsg('err_running_slonik', $!));
+        $success = false;
+    }
+    return $success;
+}
+
+sub autoFailover {
+    my $dbconninfo = shift;
+    my $clname = shift;
+    my $dbuser = shift;
+    my $dbpass = shift;
+    my $prefix = shift;
+    my $logfile = shift;
+    my $log_prefix = shift;
+
+    my $cluster_time;
+    my $failed;
+    my $actions;
+    my $current_retry;
+    my $cluster_loaded;
+    my @cluster;
+    my $node_count;
+    my $version;
+
+    printlogln($prefix,$logfile,$log_prefix,lookupMsg('autofailover_init'));
+    printlogln($prefix,$logfile,$log_prefix,lookupMsg('autofailover_init_cnf', ($g_autofailover_config_any ? 'any' : 'specified target')));
+    printlogln($prefix,$logfile,$log_prefix,lookupMsg('autofailover_init_pol', $g_autofailover_poll_interval));
+    printlogln($prefix,$logfile,$log_prefix,lookupMsg('autofailover_init_ret', $g_autofailover_retry, $g_autofailover_retry_sleep));
+    printlogln($prefix,$logfile,$log_prefix,lookupMsg('autofailover_init_set', ($g_autofailover_provs ? 'will' : 'will not')));
+
+    while (true) {  
+        # Probe current cluster configuration every minute 
+        if (!defined($cluster_time) || (time()-$cluster_time > 60)) {
+
+            $cluster_loaded = false;
+            if (!defined($cluster_time) || !$g_autofailover_config_any) {
+                eval {
+                    ($node_count, $version) = loadCluster($dbconninfo, $clname, $dbuser, $dbpass, $prefix, $logfile, $log_prefix);
+                    die lookupMsg('err_cluster_empty') if ($node_count == 0);
+                    @cluster = @g_cluster;
+                    die lookupMsg('err_cluster_lone') if ($node_count == 1);
+                    $cluster_loaded = true;
+                };
+                if ($@) {
+                    printlogln($prefix,$logfile,$log_prefix, lookupMsg('load_cluster_fail', 'from supplied configuration'));
+                    if ($g_debug) {
+                        printlogln($prefix,$logfile,$log_prefix,lookupMsg('dbg_generic', $@));
+                    }
+                }
+            }
+            else {
+                foreach (@cluster) {
+                    if ($_->[6]  eq "ACTIVE") {
+                        unless ($cluster_loaded) {
+                            eval {
+                                ($node_count, $version) = loadCluster($_->[2], $clname, $dbuser, $dbpass, $prefix, $logfile, $log_prefix);
+                                die lookupMsg('err_cluster_empty') if ($node_count == 0);
+                                @cluster = @g_cluster;
+                                die lookupMsg('err_cluster_lone') if ($node_count == 1);
+                                $cluster_loaded = true;
+                            };
+                            if ($@) {
+                                printlogln($prefix,$logfile,$log_prefix, lookupMsg('load_cluster_fail', 'from node ' . $_->[0] . ': trying next node'));
+                                if ($g_debug) {
+                                    printlogln($prefix,$logfile,$log_prefix,lookupMsg('dbg_generic', $@));
+                                }
+                            }
+                        }
+                    }
+                }
+            }
+
+            if ($cluster_loaded) {
+                printlogln($prefix,$logfile,$log_prefix,lookupMsg('autofailover_load_cluster', (!defined($cluster_time) ? "Loaded" : "Reloaded"), $version, $clname, $node_count));
+                $cluster_time = time();
+            }
+            else {
+                printlogln($prefix,$logfile,$log_prefix, lookupMsg('load_cluster_fail', 'from any node'));
+            }
+        }
+
+        if ($cluster_loaded) {
+            $current_retry = 0; 
+            undef $failed;
+            while(($current_retry <= $g_autofailover_retry) && ((!defined($failed)) || ($failed > 0))) {
+                # Check status of cluster
+                $failed = checkFailed($clname, $dbuser, $dbpass, $prefix, $logfile, $log_prefix);
+                if ($failed == 0) {
+                    if ($g_debug) {
+                        printlogln($prefix,$logfile,$log_prefix,lookupMsg('dbg_cluster_good'));
+                    }
+                    if ($current_retry > 0) {
+                        printlogln($prefix,$logfile,$log_prefix,lookupMsg('cluster_fixed'));
+                    }
+                }
+                $current_retry++;
+                if (($failed > 0) && ($current_retry <= $g_autofailover_retry)) {
+                    printlogln($prefix,$logfile,$log_prefix,lookupMsg('cluster_failed', $failed,$g_autofailover_retry_sleep,$current_retry,$g_autofailover_retry));
+                    usleep($g_autofailover_retry_sleep * 1000);
+                }
+            }
+            if ($failed > 0) {
+                $actions = findBackup($clname, $dbuser, $dbpass, $prefix, $logfile, $log_prefix);
+                if ($actions > 0) {
+                    printlogln($prefix,$logfile,$log_prefix,lookupMsg('autofailover_proceed'));
+                    foreach my $failed ( keys %g_backups ) {
+                        printlogln($prefix,$logfile,$log_prefix,lookupMsg('autofailover_detail', $failed, $g_backups{$failed}));
+                    }
+                    $g_script = writeFailover($prefix, $dbconninfo, $clname, $dbuser, $dbpass, undef, undef, $g_subs_follow_origin, $g_use_comment_aliases, $logfile, $log_prefix);   
+                    unless (runSlonik($g_script, $prefix, $logfile, $log_prefix)) {
+                        printlogln($prefix,$logfile,$log_prefix,lookupMsg('err_execute_fail', 'slonik script', $g_script));
+                    }
+                    $cluster_loaded = false;
+                    #print "SCRIPT: $g_script\n";
+                    #exit(0);
+                }
+                else {
+                    printlogln($prefix,$logfile,$log_prefix,lookupMsg('autofailover_halt', $failed));
+                }
+            }
+            usleep($g_autofailover_poll_interval * 1000);
+        }
+        else {
+            sleep(10);
+        }
+
+    }
+}
+
+sub checkFailed {
+    my $clname = shift;
+    my $dbuser = shift;
+    my $dbpass = shift;
+    my $prefix = shift;
+    my $logfile = shift;
+    my $log_prefix = shift;
+
+    my $dsn;
+    my $dbh;
+    my $sth;
+    my $query;
+    my $result_count = 0;
+    my $prov_failed = 0;
+    my $subonly_failed = 0;
+
+    undef @g_unresponsive;
+
+    foreach (@g_cluster) {
+        if ($_->[6] eq "ACTIVE") {
+            if ($g_debug) {
+                printlogln($prefix,$logfile,$log_prefix,lookupMsg('dbg_autofailover_check',$_->[0], ($_->[4] // "unnamed"),(defined($_->[9]) ? "provider of sets $_->[9]" : "sole subscriber"),$_->[2]));
+            }
+
+            if ($g_debug) {
+                if ((defined($_->[3])) || ($g_autofailover_provs && defined($_->[9]))) {
+                    printlogln($prefix,$logfile,$log_prefix,lookupMsg('dbg_autofailover_active_check', 'provider', $_->[0]));
+                }
+                else {
+                    printlogln($prefix,$logfile,$log_prefix,lookupMsg('dbg_autofailover_active_check', 'subscriber only', $_->[0]));
+                }
+            }
+
+            $dsn = "DBI:Pg:$_->[2];";
+            eval {
+                $dbh = DBI->connect($dsn, $dbuser, $dbpass, {RaiseError => 1});
+                $query = "SELECT count(*) FROM pg_namespace WHERE nspname = ?";
+                $sth = $dbh->prepare($query);
+                $sth->bind_param(1, "_" . $clname);
+                $sth->execute();
+
+                $result_count = $result_count+$sth->rows;
+    
+                $sth->finish;
+                $dbh->disconnect();
+            };
+            if ($@) {
+                if ($g_debug) {
+                    printlogln($prefix,$logfile,$log_prefix,lookupMsg('dbg_generic', $@));
+                }
+                push(@g_unresponsive, \@$_); 
+                if ((defined($_->[3])) || ($g_autofailover_provs && defined($_->[9]))) {
+                    printlogln($prefix,$logfile,$log_prefix,lookupMsg('autofailover_unresponsive', $_->[0]));
+                    $prov_failed++;
+                }
+                else {
+                    printlogln($prefix,$logfile,$log_prefix,lookupMsg('autofailover_unresponsive_subonly', $_->[0]));
+                    if ($g_fail_subonly) {
+                        $subonly_failed++;
+                    }
+                }
+            }
+        }
+        else {
+            if ($g_debug) {
+                printlogln($prefix,$logfile,$log_prefix,lookupMsg('dbg_autofailover_check',$_->[0], ($_->[4] // "unnamed"), lc($_->[6] // "unknown") .  ' node', $_->[2]));
+            }
+        }
+    }
+    if ($prov_failed > 0) {
+        return ($prov_failed+$subonly_failed);
+    }
+    else {
+        return $prov_failed;
+    }
+}
+
+sub findBackup {
+    my $clname = shift;
+    my $dbuser = shift;
+    my $dbpass = shift;
+    my $prefix = shift;
+    my $logfile = shift;
+    my $log_prefix = shift;
+
+    my $dsn;
+    my $dbh;
+    my $sth;
+    my $query;
+    my $qw_clname;
+    my $result_count = 0;
+    my $lowest_lag;
+    my $best_node_id;    
+    my $best_node_is_direct;    
+    my @sets_from;
+    my @sets_to;
+    my %backup_for_set_chosen;
+
+    undef %g_backups;
+    undef @g_failed;
+
+    foreach (@g_unresponsive) {
+        if ($g_fail_subonly || (defined($_->[3])) || ($g_autofailover_provs && defined($_->[9]))) {
+            printlogln($prefix,$logfile,$log_prefix,lookupMsg('autofailover_promote_find', ($_->[9] // "none"), $_->[0]));
+
+            undef $best_node_id;
+            $lowest_lag = (1<<$Config{ivsize}*8-1)-1;
+
+            if (defined($_->[9]) && (exists $backup_for_set_chosen{$_->[9]})) {
+                $best_node_id = $backup_for_set_chosen{$_->[9]};
+                printlogln($prefix,$logfile,$log_prefix,lookupMsg('autofailover_promote_found', $_->[9], $_->[0]));
+            }
+            else {
+                foreach my $subscriber (@g_cluster) {
+                    if ($subscriber->[0] != $_->[0]) {
+                        if ($g_debug) {
+                            printlogln($prefix,$logfile,$log_prefix,lookupMsg('autofailover_check_sub',$subscriber->[0]));
+                        }
+    
+                        $dsn = "DBI:Pg:$subscriber->[2]";
+        
+                        eval {
+                            $dbh = DBI->connect($dsn, $dbuser, $dbpass, {RaiseError => 1});
+                            $qw_clname = $dbh->quote_identifier("_" . $clname);
+
+                            $query = "SELECT extract(epoch from a.st_lag_time), (a.st_received = ?) AS direct 
+                                FROM $qw_clname.sl_status a
+                                INNER JOIN $qw_clname.sl_subscribe b ON b.sub_provider = a.st_received AND b.sub_receiver = a.st_origin
+                                WHERE b.sub_active 
+                                GROUP BY a.st_lag_time, a.st_received;";
+
+                            $sth = $dbh->prepare($query);
+                            $sth->bind_param(1, $_->[0]);
+                            $sth->execute();
+
+                            while (my @subinfo = $sth->fetchrow) {
+
+                                undef @sets_from;
+                                if (defined($_->[9])) {
+                                    printlogln($prefix,$logfile,$log_prefix,lookupMsg('autofailover_node_detail', $subscriber->[0], ($subinfo[1] ? "a direct subscriber" : "an indirect subscriber"), $subscriber->[7], $subinfo[0]));
+                                    @sets_from = split(',',$_->[9]);
+                                    @sets_to = split(',',$subscriber->[7]);
+                                }
+                                elsif ($g_fail_subonly) {
+                                    # Subscriber only node will have no active sets forwarding sets to check
+                                    printlogln($prefix,$logfile,$log_prefix,lookupMsg('autofailover_node_detail', $subscriber->[0], "suitable backup for this subscriber only node"  , $subscriber->[7], $subinfo[0]));
+                                    @sets_from = (0);
+                                    @sets_to = (0);
+                                }
+
+                                if ((checkProvidesAllSets(\@sets_from, \@sets_to)) && (($subinfo[0] < $lowest_lag) || (!$best_node_is_direct && $subinfo[1]))) {
+                                    printlogln($prefix,$logfile,$log_prefix,lookupMsg('autofailover_promote_best', $subscriber->[0], $subinfo[0]));
+                                    $best_node_id = $subscriber->[0];
+                                    $lowest_lag = $subinfo[0];
+                                    $best_node_is_direct = $subinfo[1];
+                                }
+                            }
+                        };
+                        if ($@) {
+                            printlogln($prefix,$logfile,$log_prefix,lookupMsg('autofailover_check_sub_fail', $subscriber->[0]));
+                            if ($g_debug) {
+                                printlogln($prefix,$logfile,$log_prefix,lookupMsg('dbg_generic', $@));
+                            }
+                        }
+                    }
+                }
+            }
+            if (defined($best_node_id)) { 
+                push(@g_failed, \@$_);
+                $g_backups{$_->[0]} = $best_node_id;
+                if (defined($_->[9]) && !(exists $g_backups{$_->[9]})) {
+                    $backup_for_set_chosen{$_->[9]} = $best_node_id;
+                }
+            }
+            else {
+                printlog($prefix,$logfile,$log_prefix,lookupMsg('autofailover_promote_fail')); 
+            }
+        }
+        else {
+            printlogln($prefix,$logfile,$log_prefix,lookupMsg('autofailover_promote_skip', $_->[0]));
+        }
+    }
+    return keys(%g_backups);
+}
\ No newline at end of file