Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5375515
  • 博文数量: 1144
  • 博客积分: 11974
  • 博客等级: 上将
  • 技术积分: 12312
  • 用 户 组: 普通用户
  • 注册时间: 2005-04-13 20:06
文章存档

2017年(2)

2016年(14)

2015年(10)

2014年(28)

2013年(23)

2012年(29)

2011年(53)

2010年(86)

2009年(83)

2008年(43)

2007年(153)

2006年(575)

2005年(45)

分类: Mysql/postgreSQL

2011-10-09 07:54:58

> db.foo.insert({"x":new Date(2010,0,24,9,23,34)});              
> db.foo.insert({"x":new Date(2010,0,26,9,23,34)});
> db.foo.insert({"x":new Date(2010,0,27,9,23,34)});
> db.foo.insert({"x":new Date(2010,0,30,9,23,34)});
> db.foo.insert({"x":new Date(2010,1,02,9,23,34)});
>
> db.foo.find();                                       
{ "_id" : ObjectId("4d65bbec3ebb3cfc9ca664e0"), "x" : "Sun Jan 24 2010 09:23:34 GMT+0800 (CST)" }
{ "_id" : ObjectId("4d65bbf23ebb3cfc9ca664e1"), "x" : "Tue Jan 26 2010 09:23:34 GMT+0800 (CST)" }
{ "_id" : ObjectId("4d65bbf53ebb3cfc9ca664e2"), "x" : "Wed Jan 27 2010 09:23:34 GMT+0800 (CST)" }
{ "_id" : ObjectId("4d65bbf93ebb3cfc9ca664e3"), "x" : "Sat Jan 30 2010 09:23:34 GMT+0800 (CST)" }
{ "_id" : ObjectId("4d65bc023ebb3cfc9ca664e4"), "x" : "Tue Feb 02 2010 09:23:34 GMT+0800 (CST)" }
>
> db.foo.find({"x":{$lt:new Date(2010,0,27,9,23,34)}});
{ "_id" : ObjectId("4d65bbec3ebb3cfc9ca664e0"), "x" : "Sun Jan 24 2010 09:23:34 GMT+0800 (CST)" }
{ "_id" : ObjectId("4d65bbf23ebb3cfc9ca664e1"), "x" : "Tue Jan 26 2010 09:23:34 GMT+0800 (CST)" }
>
> db.foo.find({"x":{$gt:new Date(2010,0,27,9,23,34)}});
{ "_id" : ObjectId("4d65bbf93ebb3cfc9ca664e3"), "x" : "Sat Jan 30 2010 09:23:34 GMT+0800 (CST)" }
{ "_id" : ObjectId("4d65bc023ebb3cfc9ca664e4"), "x" : "Tue Feb 02 2010 09:23:34 GMT+0800 (CST)" }
>
 
 
#!/usr/bin/perl
002   
003 use strict;
004 use warnings;
005 use Getopt::Std;                                 # Parm passing routine
006 use MongoDB;
007 use Time::Local;
008   
009 format HEADER =
010 ------------------------------------------------------------------------------
011 Syntax : $0 -h vipname -p portnum -u usage
012   
013 Options:
014   
015    -h vipname
016    -p portnum  - default is 27017
017    -u usage
018   
019 ------------------------------------------------------------------------------
020 .
021   
022 my $hostname;
023 my $portnum;
024 my $dbconn;
025 my $dbadmin;
026 my $dbcollection;
027   
028 # ---------------------
029 # Main
030 # ---------------------
031   
032 &do_opts();
033 &db_connect();
034 &get_stats();
035 &db_disconnect();
036   
037 sub do_opts {
038   #--------------------------------------------------------------------------
039   # Split the options and load variables
040   #--------------------------------------------------------------------------
041   
042   # Exempt these varaibles from needing to be declared
043   use vars  '$opt_h','$opt_p','$opt_u';
044   
045   my $returncode = getopts('h:pu') ;
046   
047   &do_help if (defined $opt_u or $returncode != 1 );
048   &do_help if (! $opt_h);
049   
050   $hostname          = $opt_h;
051   $portnum           = 27017 || $opt_p;
052 }
053   
054 sub db_connect {
055   $dbconn    = MongoDB::Connection->new(host => "$hostname:$portnum");
056   $dbadmin   = $dbconn->admin;
057 }
058   
059 sub db_disconnect {
060 }
061   
062 sub do_help {
063   #--------------------------------------------------------------------------
064   # Give help screen and exit
065   #--------------------------------------------------------------------------
066   $~ = "HEADER";
067   write;
068   &quit(0);
069 }
070   
071 sub quit {
072   exit;
073 }
074   
075 sub get_stats {
076   
077   my $result = $dbadmin->run_command({serverStatus => 1});
078   #my $result = $dbadmin->run_command({'serverStatus' => 1, 'repl' => 2});      #didn't work with 2 parameters
079   #my $result = $dbadmin->get_collection('$cmd')->find_one(Tie::IxHash->new('serverStatus' => 1, 'repl' => 2));   #works, but complex syntax
080   
081   print "current : $result->{connections}->{current}\n";
082   
083   print "resident : $result->{mem}->{resident}\n";
084   print "mapped : $result->{mem}->{mapped}\n";
085   print "virtual : $result->{mem}->{virtual}\n";
086   
087   print "accesses : $result->{indexCounters}->{btree}->{accesses}\n";
088   print "hits : $result->{indexCounters}->{btree}->{hits}\n";
089   print "misses : $result->{indexCounters}->{btree}->{misses}\n";
090   print "resets : $result->{indexCounters}->{btree}->{resets}\n";
091   
092   print "flushes : $result->{backgroundFlushing}->{flushes}\n";
093   print "total_ms : $result->{backgroundFlushing}->{total_ms}\n";
094   print "average_ms : $result->{backgroundFlushing}->{average_ms}\n";
095   print "last_ms : $result->{backgroundFlushing}->{last_ms}\n";
096   
097   print "insert : $result->{opcounters}->{insert}\n";
098   print "query : $result->{opcounters}->{query}\n";
099   print "update : $result->{opcounters}->{update}\n";
100   print "delete : $result->{opcounters}->{delete}\n";
101   print "getmore : $result->{opcounters}->{getmore}\n";
102   print "command : $result->{opcounters}->{command}\n";
103   
104   my $lagsec = -1;
105   my $primary_optimeDate;
106   my $primary_time;
107   my $my_optimeDate;
108   my $my_time;
109   $result = $dbadmin->run_command({replSetGetStatus => 1});
110   foreach my $repl ( @{$result->{members}} ) {
111      if ($repl->{stateStr} eq "PRIMARY") {
112        $primary_optimeDate = $repl->{optimeDate};
113      }
114      if (exists($repl->{self}) && $repl->{self} == 1) {
115        $my_optimeDate = $repl->{optimeDate};
116      }
117   }
118   
119   #data format:  2011-09-20T21:08:04
120   #print "$primary_optimeDate : $my_optimeDate\n";
121   if($primary_optimeDate =~ /(\d+)-(\d+)-(\d+)T(\d+):(\d+):(\d+)/) {
122     my ($p_sec, $p_min, $p_hour, $p_day, $p_mon,$p_year) = ($6, $5, $4, $3, $2, $1);
123     $p_mon = $p_mon - 1;
124     $p_year = $p_year - 1900;
125     $primary_time = timegm($p_sec, $p_min, $p_hour, $p_day, $p_mon,$p_year);
126   }
127   if($my_optimeDate =~ /(\d+)-(\d+)-(\d+)T(\d+):(\d+):(\d+)/) {
128     my ($m_sec, $m_min, $m_hour, $m_day, $m_mon,$m_year) = ($6, $5, $4, $3, $2, $1);
129     #print "$m_sec, $m_min, $m_hour, $m_day, $m_mon, $m_year\n";
130     $m_mon = $m_mon - 1;
131     $m_year = $m_year - 1900;
132     $my_time = timegm($m_sec, $m_min, $m_hour, $m_day, $m_mon,$m_year);
133   }
134   $lagsec = $primary_time - $my_time if($my_time && $primary_time);
135   print "$primary_time: $my_time\n";
136   print "lagSeconds : $lagsec\n";
137 }
 
 
 
 
 
The script run 2 commands to get the MongoDB status. We can get the same by run db._adminCommand({serverStatus : 1}) and db._adminCommand({replSetGetStatus : 1}) in MongoDB Shell .   Let’s take a look at the output of the later one:
---------------------------------------------------------------
shared:PRIMARY> db._adminCommand({replSetGetStatus : 1});
02 {
03     "set" : "shared",
04     "date" : ISODate("2011-09-22T14:36:40Z"),
05     "myState" : 1,
06     "members" : [
07         {
08             "_id" : 0,
09             "name" : "shared.mongodb.org:27017",
10             "health" : 1,
11             "state" : 2,
12             "stateStr" : "SECONDARY",
13             "uptime" : 28033,
14             "optime" : {
15                 "t" : 1316677688000,
16                 "i" : 2
17             },
18             "optimeDate" : ISODate("2011-09-22T07:48:08Z"),
19             "lastHeartbeat" : ISODate("2011-09-22T14:36:39Z")
20         },
21         {
22             "_id" : 1,
23             "name" : "shared.mongodb.org:27017",
24             "health" : 1,
25             "state" : 1,
26             "stateStr" : "PRIMARY",
27             "optime" : {
28                 "t" : 1316677688000,
29                 "i" : 2
30             },
31             "optimeDate" : ISODate("2011-09-22T07:48:08Z"),
32             "self" : true
33         }
34     ],
35     "ok" : 1
36 }
---------------------------------------------------------------------------------
By comparing the output of MongoDB Shell command and the result in Perl. We can find that the result in Perl is mapped to a hash value, and the value of the hash may be a value, a hash or a array. It’s very easy to get the value we want. Here are the script output example:
-------------------------------------------------------------------------
./getMongoDBStats.pl -h sharedmongo1.mongodb.com
02 current : 3
03 resident : 33
04 mapped : 1392
05 virtual : 3915
06 accesses : 1
07 hits : 1
08 misses : 0
09 resets : 0
10 flushes : 10508
11 total_ms : 27876
12 average_ms : 2.65283593452608
13 last_ms : 0
14 insert : 0
15 query : 9
16 update : 164909
17 delete : 32
18 getmore : 164897
19 command : 253693
20 1316677688: 1316677688
21 lagSeconds : 0

Of course, it’s a quite simple one for MongoDB administrator. But it’s a good and great start :)

阅读(1109) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~