#!/usr/bin/perl
#%# family=auto
#%# capabilities=autoconf suggest

use strict;

use LWP;
use JSON;

if ( $ARGV[0] and $ARGV[0] eq "autoconf")
{
    print "yes\n";
    exit 0;
}

my $url = $ENV{oxJolokiaUrl};
my $username = $ENV{oxJolokiaUser};
my $password = $ENV{oxJolokiaPassword};

if ( $ARGV[0] and $ARGV[0] eq "suggest")
{
    callUrlForNode("/search/com.openexchange.nosql.cassandra:00=Cassandra%20Node%20Monitoring%20Bean,*,name=*");
    exit 0;
}

my $node=$0;
$node=~s/.*_(\d{1,3}_\d{1,3}_\d{1,3}_\d{1,3})/$1/;
$node=~s/_/\./g;

if ( $ARGV[0] and $ARGV[0] eq "config")
{
    print "graph_title Cassandra Node $node\n";
    print "graph_args --base 1000 -l 0\n";
    print "graph_category Open Xchange\n";
    print "graph_vlabel Number\n";
    print "connections.draw LINE1\n";
    print "connections.label Connections\n";
    print "inFlightQueries.draw LINE1\n";
    print "inFlightQueries.label In Flight Queries\n";
    print "trashedConnections.draw LINE1\n";
    print "trashedConnections.label Trashed Connections\n";
    exit 0;
}

print("Calling URL for node $node");
callUrl("connections","/read/com.openexchange.nosql.cassandra:00=Cassandra Node Monitoring Bean,01=datacenter1,02=rack1,name=$node/Connections");
callUrl("inFlightQueries","/read/com.openexchange.nosql.cassandra:00=Cassandra Node Monitoring Bean,01=datacenter1,02=rack1,name=$node/InFlightQueries");
callUrl("trashedConnections","/read/com.openexchange.nosql.cassandra:00=Cassandra Node Monitoring Bean,01=datacenter1,02=rack1,name=$node/TrashedConnections");

sub callUrl {
    my $ua = LWP::UserAgent->new();
    my $req = HTTP::Request->new(GET => "$url$_[1]");
    $req->authorization_basic($username,$password);

    $req->header('Accept', => 'text/html');

    my $response = $ua->request($req);
    print "$url$_[1]";


    if ($response->is_success) {
        my $json = decode_json($response->decoded_content);
        if (defined $json->{value}){
            print "$_[0].value ";
            print $json->{value},"\n";
        }
    }
    else {
        my $status = $response->status_line;
        if ($status == 404) {
            die "Link to servlet might not be set correctly, this can be done by altering /etc/munin/plugin-conf.d/ox and setting the correct path to your jolokia servlet";
        }
        elsif ($status == 401) {
            die "Credentials to login might be not set correctly. The credentials are set inside /opt/open-xchange/etc/jolokia.properties on the OX, for munin, those need to be altered  at /etc/munin/plugin-conf.d/ox";
        }
        else {
            die "Something went wrong:\n",$status;
        }
    }
}

sub callUrlForNode {
    my $ua = LWP::UserAgent->new();
    my $req = HTTP::Request->new(GET => "$url$_[0]");
    $req->authorization_basic($username,$password);

    $req->header('Accept', => 'text/html');

    my $response = $ua->request($req);
    if ($response->is_success) {
        my $json = decode_json($response->decoded_content);
        my @array = @{$json->{value}};

        foreach my $entry (@array) {
            if($entry =~ /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/) {
                my $ip = $1;
                $ip=~s/\./_/g;
                print "$ip\n";
            }
        }
    }
    else {
        my $status = $response->status_line;
        if ($status == 404) {
            die "Link to servlet might not be set correctly, this can be done by altering /etc/munin/plugin-conf.d/ox and setting the correct path to your jolokia servlet";
        }
        elsif ($status == 401) {
            die "Credentials to login might be not set correctly. The credentials are set inside /opt/open-xchange/etc/jolokia.properties on the OX, for munin, those need to be altered  at /etc/munin/plugin-conf.d/ox";
        }
        else {
            die "Something went wrong:\n",$status;
        }
    }
}