#!/bin/bash
# Description: zabbix ${DB_BIN}/mongodb monitor
# Note: Zabbix 4.0.7
# Date: 2018-10-30 16:49:26
# Verion: 1.0

DB_HOST="127.0.0.1"
DB_PORT="27017"
DB_BIN="/usr/local/mongodb/bin"

if [[ $# == 1 ]];then
case $1 in
	insert)
	echo "db.serverStatus().opcounters" |${DB_BIN}/mongo ${DB_HOST}:${DB_PORT}/admin |grep "insert"|awk '{print $3}'|sed 's/,//'
	;;
	query)
	echo "db.serverStatus().opcounters" |${DB_BIN}/mongo ${DB_HOST}:${DB_PORT}/admin |grep "query"|awk '{print $3}'|sed 's/,//'
	;;
	delete)
	echo "db.serverStatus().opcounters" |${DB_BIN}/mongo ${DB_HOST}:${DB_PORT}/admin |grep "delete"|awk '{print $3}'|sed 's/,//'
	;;
	getmore)
	echo "db.serverStatus().opcounters" |${DB_BIN}/mongo ${DB_HOST}:${DB_PORT}/admin |grep "getmore"|awk '{print $3}'|sed 's/,//'
	;;
	command)
	echo "db.serverStatus().opcounters" |${DB_BIN}/mongo ${DB_HOST}:${DB_PORT}/admin |grep "command"|awk '{print $3}'|sed 's/,//'
	;;
	update)
	echo "db.serverStatus().opcounters" |${DB_BIN}/mongo ${DB_HOST}:${DB_PORT}/admin |grep "update"|awk '{print $3}'|sed 's/,//'
	;;
	total)
	echo "db.serverStatus().globalLock.currentQueue"|${DB_BIN}/mongo ${DB_HOST}:${DB_PORT}/admin |grep "total"|awk '{print $4}'|sed 's/,//'
	;;
	readers)
	echo "db.serverStatus().globalLock.currentQueue"|${DB_BIN}/mongo ${DB_HOST}:${DB_PORT}/admin |grep "readers"|awk '{print $7}'|sed 's/,//'
	;;
	writers)
	echo "db.serverStatus().globalLock.currentQueue"|${DB_BIN}/mongo ${DB_HOST}:${DB_PORT}/admin |grep "writers"|awk '{print $10}'|sed 's/,//'
	;;
	*)
	echo -e  "\033[33mUsage: $0 {insert|query|delete|getmore|command|update|total|readers|writers}\033[0m"
	exit 1
esac
else
	echo -e  "\033[33mUsage: $0 {insert|query|delete|getmore|command|update|total|readers|writers}\033[0m"
	exit 1
fi
