import
authorMatthias Förste foerste@schlittermann.de
Tue, 17 Apr 2012 12:28:34 +0200
changeset 0 04e8bd508e9c
child 1 12340919f4e9
import
get-all
get-config
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/get-all	Tue Apr 17 12:28:34 2012 +0200
@@ -0,0 +1,27 @@
+#! /usr/bin/perl -w
+# Grit Schlorke
+# zieht alle configs nacheinander
+#
+
+use strict;
+use Net::Ping;
+
+my $dirname = "/root/Configs/Hosts";
+my $ping = Net::Ping->new("icmp");
+
+opendir (DIR, $dirname) or die "Can't open directory: \"$dirname\": $!\n";
+
+my $file;
+
+while (defined ($file = readdir(DIR))) {
+    if ($file =~ /^\./){ next};
+    if ($file =~ /mailserver.innoserver.com/){next};
+    if ($file =~ /heinz.dd.dtele.de/){next};
+    if ($file =~ /sonne.dd.dtele.de/){next};
+    if ($file =~ /gtd15.g-t-d.de/){next};
+    if ($ping->ping($file, 3)) {
+	system ("./get-config", $file) == 0 or warn "Can't get-config (returned: $? // $!)\n";
+    }
+}
+
+# vim:sts=4 sw=4 ai aw sm:
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/get-config	Tue Apr 17 12:28:34 2012 +0200
@@ -0,0 +1,178 @@
+#! /usr/bin/perl -w
+# (c) Heiko Schlittermann <hs@schlittermann.de>
+my $USAGE = <<'XXX';
+Usage: $ME [options] host ...
+       -b --[no]backup make backup [$opt_backup]
+       -m --[no]mkdir  make needed archive directory [$opt_mkdir]
+       -v --[no]verbose be verbose [$opt_verbose]
+       -k --keepgoing  don't stop on errors [$opt_keepgoing]
+       -d --[no]debug show rsync command line
+XXX
+
+use strict;
+use File::Basename;
+use Getopt::Long;
+use Socket;
+use IO::File;
+
+my $ME = basename $0;
+my $ARCHIVE = "./Hosts";
+my $CONFIGS = "./configs";
+
+my $opt_backup = 0;
+my $opt_mkdir = 0;
+my $opt_verbose = 0;
+my $opt_keepgoing = 0;
+my $opt_debug = 0;
+
+sub readConfig($);
+
+MAIN: {
+
+    GetOptions(
+	"mkdir!" => \$opt_mkdir,
+	"verbose!" => \$opt_verbose,
+	"keepgoing" => \$opt_keepgoing,
+	"backup!" => \$opt_backup,
+	"debug!" => \$opt_debug,
+    ) or die eval "\"$USAGE\"";
+
+    @ARGV or die eval "\"$USAGE\"";
+    foreach (@ARGV) {
+	local $_ = $_;
+	(my $host = $_) =~ s/.*\/([a-z0-9.-]+).*/$1/;
+	my $ip = scalar gethostbyname($host);
+	$ip or do {
+	    warn "can't resolve $host\n";
+	    next;
+	};
+	my $addr = inet_ntoa($ip);
+	my $dst = "$ARCHIVE/$host";
+	my $done = "$ARCHIVE/.done.$host";
+	my $conf = "$CONFIGS/$host";
+	    -f $conf or $conf = "$CONFIGS/default";
+
+	-d $dst or $opt_mkdir and ( mkdir $dst, 0700 
+	    or die "$ME: Can't mkdir $dst: $!\n" );
+
+	-d $dst or die "$ME: $dst: $!\n";
+
+
+	print "OK, Host: $host [$addr] - $conf\n";
+
+	my @config = readConfig($conf);
+
+	my @cmd = (qw(rsync --rsh), "ssh -x", 
+		qw(--compress --numeric-ids
+		   --delete --delete-excluded 
+		   --archive --relative));
+	push @cmd, "--backup" if $opt_backup;
+	push @cmd, "--verbose" if $opt_verbose;
+
+	if (my @excludes = map { /^!\s*(.*)/ ? ("--exclude" => $1) : () } @config) {
+	    push @cmd, @excludes;
+	}
+
+	foreach (@config) {
+
+	    /^[\/\?]/ or next;	# Nun muß es mit / oder ? losgehen
+
+	    my $gflag = s/^(.)\s*(?=\/)// ? $1 : "";
+
+	    my @files = /\|\|/ ? split /\s*\|\|\s*/, $_ : $_;
+	    my @status;
+
+
+	    FILES: foreach (@files) {
+
+		my $flag = @files > 1 ? "?" : $gflag;
+
+		my $status = "";
+
+		my $src = "root\@$host:$_";
+		print "* [" . ($flag ? $flag : " ") . "] $src -> $dst ";
+
+		open(TMP, "+>/tmp/xx.$$") or die "$ME: Can't open /tmp/xx.$$";
+		unlink "/tmp/xx.$$";
+		my $pid = fork();
+
+		die "Can't fork" if not defined $pid;
+		warn "== DEBUG: @cmd $src $dst\n" if $opt_debug;
+		if ($pid == 0) {	# child
+		    open(STDERR, ">&TMP") or die "$!";
+		    open(STDOUT, ">/dev/null") unless $opt_verbose;
+		    exec @cmd, $src, $dst;
+		} else {
+		    waitpid $pid, 0;
+		}
+		if ($?) { 
+		    $status = "ERR " . ($? >> 8);
+		    $status .= " SIGNAL " . ($? & 127);
+		    seek(TMP, 0, 0);
+		    $_ = <TMP>;
+
+		    if ($flag eq "?" and /(?<=failed:)(.*\(2\).*)/) {
+			$status = "OK (but: $1)";
+		    } else {
+			$status .= "\n\t$_";
+		    }
+		} else { $status = "OK"; };
+
+		push @status, "$src: $status";
+
+		open(DATE, ">>$done") or die "$ME: Can't open >>$done: $!\n";
+		print(DATE scalar(localtime), " [$status]\n");
+		close (DATE);
+
+		if ($status !~ /^OK/) {
+		    die "\t$ME: system command ended with $status" unless $opt_keepgoing;
+		} else {
+		    warn "[$status]\n";
+		}
+
+		last FILES if $status eq "OK";
+
+	    }
+
+	    if ($gflag ne "?" and @files > 1 and ! grep /: OK$/, @status) {
+		die "** Alternative failed:\n",
+		    join("\n", map {"\t$_" } @status), "\n";
+	    }
+	}
+
+	system "ssh", "root\@$host", "touch /var/tmp/get-config.stamp";
+	#system "ssh", "root\@$host", "nagios-client-check";
+
+    }
+
+    print "\n";
+}
+
+sub readConfig($) {
+    my @conf;
+
+    my $file = shift;
+    my $cf = new IO::File $file or die "Can't open $file: $!\n";
+
+    while (<$cf>) {
+	s/^\s*//;
+	next if /^(#|\s*$)/;
+	chomp;
+
+
+	while (s/\s*\\$// and !eof) {
+	    chomp($_ .= " " . <$cf>);
+	}
+
+	if (/^<(.*)/) {
+	    push @conf, readConfig(dirname($file) . "/$1");
+	    next;
+	}
+
+	push @conf, $_;
+    }
+
+    return @conf;
+}
+
+# vim:sts=4 sw=4 aw ai sm: