#!/bin/bash

scripts_dir=/etc/zabbix/scripts/cassandra

cassandra_script=$scripts_dir/cassandra.pl
cassandra_nodes_script=$scripts_dir/check_cassandra_nodes.pl
cassandra_tpstats_script=$scripts_dir/check_cassandra_tpstats.pl

usage() {
  echo "Usage:"
  echo "${0##*/} --round"
  echo "${0##*/} --status_main"
  echo "${0##*/} --status_main_text"
  echo "${0##*/} --status_heap_mem"
  echo "${0##*/} --info_id"
  echo "${0##*/} --info_gossip"
  echo "${0##*/} --info_thrift"
  echo "${0##*/} --info_exceptions"
  echo "${0##*/} --info_native_transport"
  echo "${0##*/} --info_uptime"
  echo "${0##*/} --status_nodes_up"
  echo "${0##*/} --status_nodes_down"
  echo "${0##*/} --status_nodes_text"
  echo "${0##*/} --status_nodes_globalstatus"
  echo "${0##*/} --tpstats_globalstatus"
  echo "${0##*/} --tpstats_readstage_active"
  echo "${0##*/} --tpstats_readstage_pending"
  echo "${0##*/} --tpstats_readstage_completed"
  echo "${0##*/} --tpstats_readstage_blocked"
  echo "${0##*/} --tpstats_readstage_all_time_blocked"

  exit
}

#UserParameter=cassandra.status.main
function status_main {
out=`$cassandra_script 2>&1`

if [[ $out =~ ^CASSANDRA[[:space:]]OK.*||WARNING[[:space:]]-[[:space:]]WARNING:[[:space:]]\d+%[[:space:]]heap[[:space:]]used.* ]]; then
    echo "1"
  else
    echo "0"
fi
}

#UserParameter=cassandra.status.main.text
function status_main_text {
  $cassandra_script 2>&1
}

#UserParameter=cassandra.status.heap_mem
function status_heap_mem {
out=`$cassandra_script 2>&1 |awk -F 'heap_mem=' '{print $2}'`
check=`bc <<< "$out > 0"`

if [ $check -eq 1 ]; then
	echo $out
  else
    echo "0"
fi
}

#UserParameter=cassandra.info.id
function info_id {
  nodetool info |egrep "^ID" |awk -F ' : ' '{print $2}'
}


#UserParameter=cassandra.info.gossip
function info_gossip {
out=`nodetool info |egrep "^Gossip"|awk -F ' : ' '{print $2}'`

if [[ $out =~ ^true$ ]]; then
    echo "0"
  else
    echo "1"
fi
}

#UserParameter=cassandra.info.thrift
function info_thrift {
out=`nodetool info |egrep "^Thrift"|awk -F ' : ' '{print $2}'`

if [[ $out =~ ^true$ ]]; then
    echo "0"
  else
    echo "1"
fi
}

#UserParameter=cassandra.info.exceptions
function info_exceptions {
out=`nodetool info |egrep "^Exceptions"|awk -F ' : ' '{print $2}'`

if [ "$out" -ge "0" ]; then
    echo $out
  else
    echo "ZBX_NOTSUPPORTED"
fi
}

#UserParameter=cassandra.info.native_transport
function info_native_transport {
out=`nodetool info |egrep "^Native Transport"|awk -F 'active: ' '{print $2}'`

if [[ $out =~ ^true$ ]]; then
    echo "0"
  else
    echo "1"
fi
}

#UserParameter=cassandra.info.uptime
function info_uptime {
out=`nodetool info |egrep "^Uptime"|awk -F ' : ' '{print $2}'`

if [ "$out" -ge "0" ]; then
    echo $out
  else
    echo "ZBX_NOTSUPPORTED"
fi
}

#UserParameter=cassandra.status.nodes_up
function status_nodes_up {
out=`$cassandra_nodes_script 2>&1 |sed 's/.*nodes_up=\([0-9]\+\).*/\1/g'| tr -d '\n'`

if [[ $out =~ ^[0-9]+$ ]]; then
    echo $out
  else
    echo "0"
fi
}

#UserParameter=cassandra.status.nodes_down
function status_nodes_down {
out=`$cassandra_nodes_script 2>&1 |sed 's/.*nodes_down=\([0-9]\+\).*/\1/g'| tr -d '\n'`

if [[ $out =~ ^[0-9]+$ ]]; then
    echo $out
  else
    echo "0"
fi
}

#UserParameter=cassandra.status.nodes.text
function status_nodes_text {
  $cassandra_nodes_script 2>&1
}

#UserParameter=cassandra.status.nodes.globalstatus
function status_nodes_globalstatus {
  $cassandra_nodes_script 2>&1 |egrep "^OK: [0-9]+ nodes up" -c
}

#UserParameter=cassandra.tpstats.globalstatus
function tpstats_globalstatus {
  $cassandra_tpstats_script 2>&1 |egrep "^OK:" -c
}

#UserParameter=cassandra.tpstats.readstage.active
function tpstats_readstage_active {
out=`$cassandra_tpstats_script 2>&1 |sed 's/.*ReadStage_Active=\([0-9]\+\).*/\1/g'| tr -d '\n'`

if [[ $out =~ ^[0-9]+$ ]]; then
    echo $out
  else
    echo "ZBX_NOTSUPPORTED"
fi
}

#UserParameter=cassandra.tpstats.readstage.pending
function tpstats_readstage_pending {
out=`$cassandra_tpstats_script 2>&1 |sed 's/.*ReadStage_Pending=\([0-9]\+\).*/\1/g'| tr -d '\n'`

if [[ $out =~ ^[0-9]+$ ]]; then
    echo $out
  else
    echo "ZBX_NOTSUPPORTED"
fi
}

#UserParameter=cassandra.tpstats.readstage.completed
function tpstats_readstage_completed {
out=`$cassandra_tpstats_script 2>&1 |sed 's/.*ReadStage_Completed=\([0-9]\+\).*/\1/g'| tr -d '\n'`

if [[ $out =~ ^[0-9]+$ ]]; then
    echo $out
  else
    echo "ZBX_NOTSUPPORTED"
fi
}

#UserParameter=cassandra.tpstats.readstage.blocked
function tpstats_readstage_blocked {
out=`$cassandra_tpstats_script 2>&1 |sed 's/.*ReadStage_Blocked=\([0-9]\+\).*/\1/g'| tr -d '\n'`

if [[ $out =~ ^[0-9]+$ ]]; then
    echo $out
  else
    echo "ZBX_NOTSUPPORTED"
fi
}

#UserParameter=cassandra.tpstats.readstage.all_time_blocked
function tpstats_readstage_all_time_blocked {
out=`$cassandra_tpstats_script 2>&1 |sed 's/.*ReadStage_All_time_blocked=\([0-9]\+\).*/\1/g'| tr -d '\n'`

if [[ $out =~ ^[0-9]+$ ]]; then
    echo $out
  else
    echo "ZBX_NOTSUPPORTED"
fi
}


test x"$1" == x"" && usage
test x"$1" == x"--status_main" && status_main
test x"$1" == x"--status_main_text" && status_main_text
test x"$1" == x"--status_heap_mem" && status_heap_mem
test x"$1" == x"--info_id" && info_id
test x"$1" == x"--info_gossip" && info_gossip
test x"$1" == x"--info_thrift" && info_thrift
test x"$1" == x"--info_exceptions" && info_exceptions
test x"$1" == x"--info_native_transport" && info_native_transport
test x"$1" == x"--info_uptime" && info_uptime
test x"$1" == x"--status_nodes_up" && status_nodes_up
test x"$1" == x"--status_nodes_down" && status_nodes_down
test x"$1" == x"--status_nodes_text" && status_nodes_text
test x"$1" == x"--status_nodes_globalstatus" && status_nodes_globalstatus
test x"$1" == x"--tpstats_globalstatus" && tpstats_globalstatus
test x"$1" == x"--tpstats_readstage_active" && tpstats_readstage_active
test x"$1" == x"--tpstats_readstage_pending" && tpstats_readstage_pending
test x"$1" == x"--tpstats_readstage_completed" && tpstats_readstage_completed
test x"$1" == x"--tpstats_readstage_blocked" && tpstats_readstage_blocked
test x"$1" == x"--tpstats_readstage_all_time_blocked" && tpstats_readstage_all_time_blocked

