Nagios没有统计MongoDB数据库记录的相关脚本,就自已抄刀干起来了,还挺实用:

#!/usr/bin/perl
#author:xiqing_lee
#Count hc online users
use MongoDB;
use MongoDB::Database;
use MongoDB::Connection;
use strict;
use warnings;
#ARGS judge
sub usage(){         # help  
  print  "Usage:check_hcuser -h IP -w # -c #\nFor example:check_hcuser -h 192.168.66.11 -w 88 -c 222\n";
  exit 2;
}
my $x = "";
my $warnning="0";
my $criting="0";
my $host="";
if ($#ARGV < 5){  # args total judge
  &usage;
  exit;
}
while($#ARGV >= 0){
 $x = shift @ARGV;
 if ("$x" eq "-w") {
   $warnning = shift @ARGV;
 }
 if ("$x" eq "-c") {
  $criting = shift @ARGV;
 }
 if ("$x" eq "-h") {
  $host = shift @ARGV;
 }
}
if ("$warnning" > "$criting"){
  &usage;
}
# MongoDB records NUM  
my $connection = MongoDB::Connection->new(host => $host, port => 27017);
my $database   = $connection->ossDev;
my $collection = $database->get_collection("T_Session");
my $countssp =  $collection->count({ state => "ONLINE" });
#my $curcors = $collection->find();
my $msg = "initx";
my $status = "3";
if ("$countssp" >= "$criting"){
  $msg = "CRITICAL";
  $status = 2;
}
elsif("$countssp" >= "$warnning" ){
  $msg = "WARNNING";
  $status = 1;
}else{
  $msg = "OK";
  $status = 0;
}
print "$msg - Current  users are $countssp |Users=$countssp;1500;2000\n";
exit $status;