#!/usr/bin/env perl 
#===============================================================================
#
#         FILE: mon_node_output_parse_diff.pl
#
#        USAGE: ./mon_node_output_parse_diff.pl <input-file-name>
#
#  DESCRIPTION: Purpose of this script is to parse mon node output
#               to find differences of 
#               between nodes. 
#               
#
#      OPTIONS: ---
# REQUIREMENTS: Textfile containing output from mon node command
#         BUGS: ---
#        NOTES: ---
#       AUTHOR: Jonatan Sundeen 
# ORGANIZATION: 
#      VERSION: 1.0
#      CREATED: 2017-08-16 09:04:03
#     REVISION: ---
#===============================================================================

use strict;
use warnings;
use utf8;

my $usage ='
Usage:
my-program <input-file-name>

Use case to check services active_checks_enabled
Get data
mon node ctrl --self --all "mon query ls services -c host_name,description,active_checks_enabled" > mon_node_services.txt
Parse data with scrip
perl mon_node_output_parse_diff.pl mon_node_services.txt 

Use case to check hosts active_checks_enabled
Get data
mon node ctrl --self --all "mon query ls hosts -c name,active_checks_enabled" > mon_node_hosts.txt
Parse data with scrip
perl mon_node_output_parse_diff.pl mon_node_hosts.txt 

';


if ( not defined $ARGV[0] ) {
    print $usage;
    exit 1;
}

my ($file1) = @ARGV;
my %file_1_hash;
my %line_hash;
my %host_name_hash;
my %service_description_hash;
my %current_state_hash;
my %nodes_hash;
my $line;
my $line_counter = 0;
my $show_match = 0;

#read the file into a hash 
open (FILE1, "<$file1") or die "can't open file $file1\n";
while ($line=<FILE1>){
    chomp ($line);
    $line_counter++;
    if (!($line =~ m/^\s*$/)){
        $file_1_hash{$line_counter}=$line;
    }
}
close (FILE1);

#parse variables
my $do_parse = 1;
my $counter = 0;
my $current_host = "localhost";
foreach my $line (sort { $a <=> $b } keys %file_1_hash) { 
    if ( $file_1_hash{$line} =~ /\#--- REMOTE OUTPUT START.*/ ) {
        $do_parse = 1;
    } elsif ( $file_1_hash{$line} =~ /^Connecting to.*/ ) {
        $do_parse = 0;
        $current_host = (split(/'/,$file_1_hash{$line}))[1];
        $counter++;
    } elsif ( $do_parse == 0) {
        next; 
    } elsif ( $file_1_hash{$line} =~ /.*;.*/) {
        if ( defined $line_hash{$file_1_hash{$line}} && $line_hash{$file_1_hash{$line}} ne "" ) {
            $line_hash{$file_1_hash{$line}} .= ",";
        }
		$line_hash{$file_1_hash{$line}} .= $current_host;
		$nodes_hash{$current_host}++;
    } else {
    } 
}


print "*****\n";
print "Nodes found and count: \n";
foreach my $node (keys %nodes_hash) {
	print "$node $nodes_hash{$node};\n";
}

print "*****\n";
print "Settings differ here:\n";
# Check and print where settings differ
foreach my $line (keys %line_hash) {
    my ($setting) = (split(/;/,$line))[-1];
    my $test;
    my @data = split(/;/,$line);
    
    for( my $a = 0; $a < (scalar(@data)-1); $a = $a + 1 ){
        $test .= $data[$a].";";
    }
    
    
    if ( $setting == 1 ) {
        $test .= "0";
    } else {
        $test .= "1";
    }
    
    #print "$test\n"; ###Debug
    
    if ( defined $line_hash{$test} && $line_hash{$test} ne "" ) {
        print "$line";
        print "\n";
        print "$line_hash{$line}";
        print "\n";
        print "$test";
        print "\n";
        print "$line_hash{$test}";
        print "\n";
        print "*****\n";
    }
}
