#!/usr/bin/python

"""
Plugin to monitor ArangoDB  servers. It works with the new server statistics 
interface of ArangoDB 1.3. Not every value seems senseful, but there are
nice graphs generated...

Author: Ralf Geschke <ralf@kuerbis.org>
Version: 2013062601

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

Usage:
        - Link or copy to /etc/munin/plugins
        - To enable extra graphs, also link to one or more of the
        possible links (given below)
        - Then restart munin-node

Links possible:
        arangodb_conn                   HTTP client connections
        arangodb_time_total		Total request/queue/connection time
        arangodb_bytes_total		Total sent/received bytes 


Configuration:
        - No configuration required. Just enable the admin interface of ArangoDB.

Thanks to the authors of other Python munin plugins. I've used some of 
them as inspiring example. 

Possible todos:
        - support of munin-like configuration parameters
        - add more statistics
        
"""

from os.path import basename
import urllib2
import sys

try:
    import json
except ImportError:
    import simplejson as json


def getServerStatus(group):
    raw = urllib2.urlopen( "http://127.0.0.1:8529/_admin/statistics" ).read()
    
    return json.loads( raw )[group]

def doData(plugin_name):
    if plugin_name == 'arangodb_conn':
        print "connections.value " + str( getServerStatus('client')["httpConnections"] )

    elif plugin_name== 'arangodb_time_total':
        data = getServerStatus('client')
        timeTotal = data['totalTime']['sum'] 
        timeConnection = data['connectionTime']['sum'] 
        timeRequest = data['requestTime']['sum'] 
        timeQueue = data['queueTime']['sum'] 
         
        print "total.value " + str(int(round(timeTotal)))
        print "connection.value " + str(int(round(timeConnection)))
        print "request.value " + str(int(round(timeRequest)))
        print "queue.value " + str(int(round(timeQueue)))
        
    elif plugin_name== 'arangodb_bytes_total':
        data = getServerStatus('client')
        bytesReceived = data['bytesReceived']['sum']
        bytesSent = data['bytesSent']['sum']
        print "received.value " + str(int(round(bytesReceived)))
        print "sent.value " + str(int(round(bytesSent)))
        
    else:
        pass
    
def doConfig(plugin_name):
    if plugin_name == 'arangodb_conn':
        print "graph_title ArangoDB current connections"
        print "graph_args --base 1000 -l 0"
        print "graph_vlabel connections"
        print "graph_category ArangoDB"
        print "connections.label connections"

    elif plugin_name == 'arangodb_time_total':
        print "graph_title ArangoDB total time"
        print "graph_args --base 1000 -l 0"
        print "graph_vlabel seconds"
        print "graph_category ArangoDB"
        print "total.label total"
        print "connection.label connection"
        print "request.label request"
        print "queue.label queue"
        
    elif plugin_name == 'arangodb_bytes_total':
        print "graph_title ArangoDB total bytes"
        print "graph_args --base 1024"
        print "graph_vlabel total bytes received (-) / sent (+)"
        print "graph_category ArangoDB"
        print "graph_order received sent"
        print "received.graph no"
        print "received.draw LINE2"
        print "received.type DERIVE"
        print "received.min 0"
        print "received.label Bytes received"
        print "received.cdef received,8,*"
        print "sent.draw LINE2"
        print "sent.type DERIVE"
        print "sent.min 0"
        print "sent.label bytes"
        print "sent.cdef sent,8,*"
        print "sent.negative received"

    else:
        pass
     


plugin_name = basename(sys.argv[0])

if __name__ == "__main__":
    if len(sys.argv) > 1 and sys.argv[1] == "config":
        doConfig(plugin_name)
    else:
        doData(plugin_name)


