Code: Select all
#!/usr/bin/perl
#
# Script to change zone states to and from active/inactive. This script
# could be added to cron to allow timed zone changes as oposed to changing
# run times for entire monitors.
# Author: Lonny Selinger Tue Nov 28 12:30:54 CST 2017
# lonny<dot>selinger<at>gmail<dot>com
# 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/>.
use strict;
use warnings;
use DBI;
use Getopt::Long;
# The assumption is that zm is the standard DB name and that is hasn't
# been changed. If it has, change to your DB name in the following line
my $dsn = "DBI:mysql:zm";
# Set your own user name and password to your DB.
# Also keep this in a locked down place wih limited access and chmod 700
# I was going to have user/pass as options however I think it's safer to hide
# the script rather than providing plain text passwords in a crontab.
my $username = 'CHANGEME;
my $password = 'CHANGEME';
my $type = undef;
GetOptions ("report|r" => \my $report,
"help|h" => \my $help,
"state|s=s" => \my $state,
"zone|z=s" => \my $zone
);
# Busy logic the forward to subs. This certainly isn't lean ;-)
if ($help) { help(); }
if ($report) { report(); }
if (!$zone || !$state) { help(); }
if ($state eq 'inactive' or $state eq 'active') {
if ($state eq 'inactive') {
$type = 'Inactive';
} else {
if ($state eq 'active') {
$type = 'Active';
}
}
my $dbh = DBI->connect($dsn,$username,$password);
print "Changing $zone to $state\n";
my $sth = $dbh->prepare("UPDATE Zones SET Type='$type' WHERE Name='$zone'");
$sth->execute();
exit(0);
} else {
help();
exit(0);
}
# Basic dump/report section. I do no validation currently on Zone names
# in the main section that alters the DB. IOW you can misspell or supply
# Zone names that do not exist and I do not verify they are valid.
# I decided to throw in a quick dump of the Zone Name, ID, and Monitor ID
# to help whoever uses this to select the zone they want to alter.
sub report {
my $dbh = DBI->connect($dsn,$username,$password);
my $sth = $dbh->prepare("SELECT Name,Id,MonitorId from Zones");
$sth->execute();
$sth->dump_results();
$sth->finish();
exit(0);
}
# Help section
sub help {
print "\nUsage: $0 [--report/-r] [--help/-h] --state/-s --zone/-z\n\n";
print "\tAt a minimum you'll need to specify a zone and the state\n";
print "\tyou want it in\n\n";
print "\t -z Zone you would like to change the state of\n";
print "\t -s State you would like it in (active/inactive)\n";
print "\t -r Provide a simple dump of Zone names, Zone ID,\n";
print "\t and Monitor Id# to help identify the zone you want to affect.\n";
print "\t -h for this menu\n\n";
exit(0);
} #EOF
Code: Select all
59 17 * * * /usr/local/src/test/zone_ctrl.pl -z Driveway -s inactive 2>&1
01 21 * * * /usr/local/src/test/zone_ctrl.pl -z Driveway -s active 2>&1
59 17 * * * /usr/local/src/test/zone_ctrl.pl -z Yard -s inactive 2>&1
01 21 * * * /usr/local/src/test/zone_ctrl.pl -z Yard -s active 2>&1