Friday, January 04, 2013

MongoDB Shard Stats One-Liner

I've been testing, and I want to get the shard stats in one line.  That is I want this:

    krice@zaphod:~/$ mongo --host boxname303.example.com --port 40000 --eval "sh.status()"
    MongoDB shell version: 2.0.6
    connecting to: boxname303.example.com:40000/test
    --- Sharding Status ---
      sharding version: { "_id" : 1, "version" : 3 }
      shards:
            {  "_id" : "shard-00",  "host" : "shard-00/boxname301.example.com:30001,boxname303.example.com:30000" }
            {  "_id" : "shard-01",  "host" : "shard-01/boxname302.example.com:30101,boxname304.example.com:30100" }
            {  "_id" : "shard-02",  "host" : "shard-02/boxname301.example.com:30201,boxname303.example.com:30200" }
            {  "_id" : "shard-03",  "host" : "shard-03/boxname302.example.com:30301,boxname304.example.com:30300" }
            {  "_id" : "shard-04",  "host" : "shard-04/boxname301.example.com:30401,boxname303.example.com:30400" }
            {  "_id" : "shard-05",  "host" : "shard-05/boxname302.example.com:30501,boxname304.example.com:30500" }
            {  "_id" : "shard-06",  "host" : "shard-06/boxname301.example.com:30601,boxname303.example.com:30600" }
            {  "_id" : "shard-07",  "host" : "shard-07/boxname302.example.com:30701,boxname304.example.com:30700" }
            {  "_id" : "shard-08",  "host" : "shard-08/boxname301.example.com:30801,boxname303.example.com:30800" }
            {  "_id" : "shard-09",  "host" : "shard-09/boxname302.example.com:30901,boxname304.example.com:30900" }
            {  "_id" : "shard-10",  "host" : "shard-10/boxname301.example.com:31011,boxname303.example.com:31010" }
            {  "_id" : "shard-11",  "host" : "shard-11/boxname302.example.com:31111,boxname304.example.com:31110" }
            {  "_id" : "shard-12",  "host" : "shard-12/boxname301.example.com:31211,boxname303.example.com:31210" }
            {  "_id" : "shard-13",  "host" : "shard-13/boxname302.example.com:31311,boxname304.example.com:31310" }
            {  "_id" : "shard-14",  "host" : "shard-14/boxname301.example.com:31411,boxname303.example.com:31410" }
            {  "_id" : "shard-15",  "host" : "shard-15/boxname302.example.com:31511,boxname304.example.com:31510" }
      databases:
            {  "_id" : "admin",  "partitioned" : false,  "primary" : "config" }
            {  "_id" : "megamaid",  "partitioned" : true,  "primary" : "shard-00" }
                    megamaid.metricValue chunks:
                                    shard-05        2
                                    shard-13        2
                                    shard-10        2
                                    shard-15        2
                                    shard-01        2
                                    shard-08        2
                                    shard-04        2
                                    shard-07        2
                                    shard-12        2
                                    shard-03        2
                                    shard-09        2
                                    shard-06        2
                                    shard-11        2
                                    shard-14        2
                                    shard-00        2
                                    shard-02        5
                            too many chunks to print, use verbose if you want to force print
            {  "_id" : "test",  "partitioned" : false,  "primary" : "shard-14" }
    krice4@zaphod:~/$



But, I want just the numbers so I can see if I'm balanced.  Here's the one-liner:

alias shardbal="mongo --host boxname.example.com --port 40000 --eval \"sh.status()\" | egrep 'shard\-[0-9]{2}[^\"/]' | awk '{print \$2}' | tr '\n' ',' | xargs echo \"Shard Balance is: \" "
krice4@zaphod:~/$ shardbal
Shard Balance is:  4,4,4,3,4,4,4,4,4,4,4,4,4,4,4,4,