#!/usr/bin/perl # author: papanda@papa.to #%# family=auto #%# capabilities=autoconf my $name = 'status'; my $u = ''; my $p = ''; my $eth = 'eth0'; my $ip = '127.0.0.1'; if (open(OUT, "/sbin/ifconfig $eth|")) { while () { if ($_ =~ m!inet([^:]+):([\d\.]+)!) { $ip = $2; last; } } close(OUT); } my $port = 27017; if ($0 =~ m!_([\d]+)$!) { $port = $1; } my $cmd = "/usr/local/bin/mongostat"; if (!-x $cmd) { $cmd = "/usr/bin/mongostat"; if (!-x $cmd) { $cmd = "/usr/sbin/mongostat"; if (!-x $cmd) { $cmd = 'mongostat'; } } } my $auth = ''; if ($u && $p) { $auth = " -u $u -p $p "; } if (exists $ARGV[0] and $ARGV[0] eq "autoconf") { if (-x $cmd) { my $ret = system("$cmd -h $ip --port $port -n 1 $auth 1 >/dev/null 2>/dev/null"); if ($ret == 0 && $port > 4900) { print "yes\n"; exit 0; } } print "no\n"; exit 1; } if (exists $ARGV[0] and $ARGV[0] eq "config") { print "graph_title MongoDB $ip:$port $name\n"; print "graph_args --base 1000 --lower-limit 0\n"; print "graph_category MongoDB\n"; print "graph_vlabel Percentage(%)\n"; print "faults.label faults\n"; print "faults.info page faults\n"; print "faults.draw LINE2\n"; print "locked.label locked\n"; print "locked.info global write locked\n"; print "locked.draw LINE2\n"; print "idxmiss.label idxmiss\n"; print "idxmiss.info index error rate\n"; print "idxmiss.draw LINE2\n"; print "repl.label repl\n"; print "repl.info primary:100,secondary:80,recovering:30,unknown:0\n"; print "repl.draw LINE2\n"; exit 0; } my $faults = 0; my $locked = 0; my $idxmiss = 0; my $repl = 0; open(OUT, "$cmd -h $ip --port $port -n 1 $auth 1 |"); while () { my @list = split(/[\s]+/, $_); if (scalar @list >= 21) { my $replstr = $list[20]; $repl = 0; if ($replstr eq 'M' || $replstr eq 'PRI') { $repl = 100; } elsif ($replstr eq 'SEC') { $repl = 80; } elsif ($replstr eq 'REC') { $repl = 30; } if ($repl == 0) { next; } $faults = $list[11]; $locked = $list[12]; $idxmiss = $list[13]; last; } } close(OUT); printf "faults.value %d\n", $faults; printf "locked.value %d\n", $locked; printf "idxmiss.value %d\n", $idxmiss; printf "repl.value %d\n", $repl; exit 0;