#!/usr/local/groundwork/perl/bin/perl # # Copyright 2010 GroundWork Open Source Solutions, Inc. ("GroundWork") # All rights reserved. Use is subject to GroundWork commercial license terms. # # Name: delete_hosts.pl # Purpose: Allows an administrator to list the hosts defined in the Monarch configuration # and then pick which ones to delete. The administrator is then offered the option # of doing a Commit operation. This script is useful in a number of situations # including where more devices might have been added to the GroundWork system than # are licensed. BEGIN { unshift @INC, "/usr/local/groundwork/core/monarch/lib"; unshift @INC, "/usr/local/groundwork/monarch/lib"; }; use strict; use ExtUtils::MakeMaker qw(prompt); use dassmonarch; use Getopt::Long; use vars qw($opt_f $opt_h $opt_c); my $PROGNAME = "delete_hosts.pl"; my $VERSION = "1.0"; Getopt::Long::Configure('bundling'); GetOptions ("h" => \$opt_h, "help" => \$opt_h, "c" => \$opt_h, "commit" => \$opt_c, "f=s" => \$opt_f, "file=s" => \$opt_f); if ($opt_h) { print "Usage: $PROGNAME [ -f | --file ] [ -c | --commit ] [ -h | --help ] [ -f | --file ] - optional list of hosts to delete, one hostname per line [ -c | --commit ] - optional flag to have the script do a commit without prompt [ -h | --help ] - display this usage information Version: $VERSION\n"; exit 0; } # Construct an instance of class dassmonarch my $monarchapi = dassmonarch->new(); # set this to error, in order to get minimal debug messages, verbose creates a lot of output #$monarchapi->set_debuglevel('verbose'); $monarchapi->set_debuglevel('error'); my $ans = "n"; my @hoststodelete; if ($opt_f) { # Read file push into array open ( FILE, "<$opt_f" ) or die ("Unable to open file $opt_f for reading. $@."); while ( my $host = ) { chomp $host; push @hoststodelete, $host; } close ( FILE ); $ans = "y"; } else { # Present list of hosts to user and request specification of which ones to delete print "Getting list of hosts from Monarch... "; my @hostlist = $monarchapi->get_hostlist(); print scalar(@hostlist) . ".\n"; if (!scalar(@hostlist)) { print "No hosts to delete. Exiting.\n"; exit 1; } @hoststodelete = picklist([sort @hostlist],"Select hosts to delete", '', '', ''); # Present list of hosts user has requested to delete and ask for confirmation print "You have selected the following hosts to delete:\n"; foreach my $host (@hoststodelete) { print "$host,"; } print "\n\n"; my $prompt = qq{Please answer 'y' if you wish to delete the specified hosts, otherwise answer 'n' to exit.}; $ans = prompt($prompt,"y"); } if ($ans =~ /y/) { # Perform host deletion foreach my $host (@hoststodelete) { print "Deleting host: $host\n"; $monarchapi->delete_host($host); } } else { # Abort without doing anything more print "Exiting without deleting any hosts.\n"; exit 0; } print "\nAll selected/valid hosts have been deleted.\n"; if (!$opt_c) { my $prompt = qq{Please answer 'y' if you wish to commit the Monarch configuration to Nagios, otherwise answer 'n' to exit. Make sure no one is working in the Configuration user interface before answering 'y'.}; $ans = prompt($prompt,"y"); } if ($ans =~ /y/ || $opt_c) { if ($monarchapi->pre_flight_check()) { $monarchapi->generateAndCommit(); } else { print "\nError with pre-flight check. Review your configuration using the\nweb interface.\n\n"; exit 1; } print "\nMonarch configuration has been committed to Nagios.\n\n"; } else { print "\nExiting without commit. Review your configuration using the web interface.\n\n"; } exit 0; sub picklist { my($items,$prompt,$default,$require_nonempty,$empty_warning)=@_; $default ||= ''; my $pos = 0; my @nums; while (1) { # display, at most, 15 items at a time my $limit = $#{ $items } - $pos; $limit = 15 if $limit > 15; # show the next $limit items, get the new position $pos = display_some($items, $limit, $pos); $pos = 0 if $pos >= @$items; my $num = prompt($prompt,$default); @nums = split (' ', $num); my $i = scalar @$items; (warn "invalid items entered, try again\n"), next if grep (/\D/ || $_ < 1 || $_ > $i, @nums); if ($require_nonempty) { (warn "$empty_warning\n"); } print "\n"; # a blank line continues... next unless @nums; last; } for (@nums) { $_-- } @{$items}[@nums]; } sub display_some { my ($items, $limit, $pos) = @_; $pos ||= 0; my @displayable = @$items[$pos .. ($pos + $limit)]; for my $item (@displayable) { printf "(%d) %s\n", ++$pos, $item; } printf("%d more items, hit SPACE RETURN to show them\n", (@$items - $pos) ) if $pos < @$items; return $pos; }