'''
Created on May 25, 2011

@author: bzcseife

This is a python ganglia plugin which monitors the status of an OSD service of the XtreemFS 
filesystem. It is intend to run on the same host as the OSD and gathers information of the OSD per
SNMP. Therefore you have to configure your OSD to provide a SNMP Agent on this host.

'''
#TODO: If ganglia supports 64bit values uses 64bit integers instead of converting all 64 bit integers
#reported from the SNMP Agent to 32bit integers.


import random
from pysnmp.entity.rfc3413.oneliner import cmdgen
from pysnmp.entity.rfc3413.oneliner.cmdgen import UdpTransportTarget



    
descriptors = list()
Random_Max = 50
Constant_Value = 50



#Get the used memory of the JVM
def JvmUsedMem(name):
    errorIndication, errorStatus, errorIndex, varBinds = cmdgen.CommandGenerator().getCmd(authData,
                                                                                        transportTarget,
                                                                                        (1, 3, 6, 1, 4, 1, 38350, 1, 1, 0))
            
    if (errorStatus == False and errorIndication == None):       
        return int(varBinds[0][1] / 1024 / 1024)
    else:
        return 0       



#Get the free memory of the JVM
def JvmFreeMem(name):
    errorIndication, errorStatus, errorIndex, varBinds = cmdgen.CommandGenerator().getCmd(authData,
                                                                                        transportTarget,
                                                                                        (1, 3, 6, 1, 4, 1, 38350, 1, 2, 0))
            
    if (errorStatus == False and errorIndication == None):       
        return int(varBinds[0][1] / 1024 / 1024)
    else:
        return 0       


#Get the number of client connections
def ClientConnections(name):
    errorIndication, errorStatus, errorIndex, varBinds = cmdgen.CommandGenerator().getCmd(authData,
                                                                                        transportTarget,
                                                                                        (1, 3, 6, 1, 4, 1, 38350, 1, 7, 0))
            
    if (errorStatus == False and errorIndication == None):       
        return int(varBinds[0][1])
    else:
        return 0       


#Get the number of pending requests
def PendingRequests(name):
    errorIndication, errorStatus, errorIndex, varBinds = cmdgen.CommandGenerator().getCmd(authData,
                                                                                        transportTarget,
                                                                                        (1, 3, 6, 1, 4, 1, 38350, 1, 8, 0))
            
    if (errorStatus == False and errorIndication == None):       
        return int(varBinds[0][1])
    else:
        return 0       

#Get the number of objects received
def ObjectsReceived(name):
    errorIndication, errorStatus, errorIndex, varBinds = cmdgen.CommandGenerator().getCmd(authData,
                                                                                        transportTarget,
                                                                                        (1, 3, 6, 1, 4, 1, 38350, 4, 1, 0))
            
    if (errorStatus == False and errorIndication == None):       
        return int(varBinds[0][1])
    else:
        return 0       

#Get the number of replicated objects received
def ReplObjectsReceived(name):
    errorIndication, errorStatus, errorIndex, varBinds = cmdgen.CommandGenerator().getCmd(authData,
                                                                                        transportTarget,
                                                                                        (1, 3, 6, 1, 4, 1, 38350, 4, 2, 0))
            
    if (errorStatus == False and errorIndication == None):       
        return int(varBinds[0][1])
    else:
        return 0       

#Get the number of replicated objects transmitted
def ObjectsTransmitted(name):
    errorIndication, errorStatus, errorIndex, varBinds = cmdgen.CommandGenerator().getCmd(authData,
                                                                                        transportTarget,
                                                                                        (1, 3, 6, 1, 4, 1, 38350, 4, 3, 0))
            
    if (errorStatus == False and errorIndication == None):       
        return int(varBinds[0][1])
    else:
        return 0       

#Get the number of replicated bytes received
def ReplBytesReceived(name):
    errorIndication, errorStatus, errorIndex, varBinds = cmdgen.CommandGenerator().getCmd(authData,
                                                                                        transportTarget,
                                                                                        (1, 3, 6, 1, 4, 1, 38350, 4, 4, 0))
            
    if (errorStatus == False and errorIndication == None):       
        return int(varBinds[0][1] / 1024 / 1024)
    else:
        return 0       

#Get the number of bytes received
def BytesReceived(name):
    errorIndication, errorStatus, errorIndex, varBinds = cmdgen.CommandGenerator().getCmd(authData,
                                                                                        transportTarget,
                                                                                        (1, 3, 6, 1, 4, 1, 38350, 4, 5, 0))
            
    if (errorStatus == False and errorIndication == None):       
        return int(varBinds[0][1] / 1024 / 1024)
    else:
        return 0       

#Get the number of bytes transmitted
def BytesTransmitted(name):
    errorIndication, errorStatus, errorIndex, varBinds = cmdgen.CommandGenerator().getCmd(authData,
                                                                                        transportTarget,
                                                                                        (1, 3, 6, 1, 4, 1, 38350, 4, 6, 0))
            
    if (errorStatus == False and errorIndication == None):       
        return int(varBinds[0][1] / 1024 / 1024)
    else:
        return 0       

#Get the length of the preprocessing stage queue 
def PreprocQueueLength(name):
    errorIndication, errorStatus, errorIndex, varBinds = cmdgen.CommandGenerator().getCmd(authData,
                                                                                        transportTarget,
                                                                                        (1, 3, 6, 1, 4, 1, 38350, 4, 7, 0))
            
    if (errorStatus == False and errorIndication == None):       
        return int(varBinds[0][1] / 1024 / 1024)
    else:
        return 0       

#Get the length of the storage stage queue 
def StorageQueueLength(name):
    errorIndication, errorStatus, errorIndex, varBinds = cmdgen.CommandGenerator().getCmd(authData,
                                                                                        transportTarget,
                                                                                        (1, 3, 6, 1, 4, 1, 38350, 4, 8, 0))
            
    if (errorStatus == False and errorIndication == None):       
        return int(varBinds[0][1])
    else:
        return 0       

#Get the length of the deletion stage queue 
def DeletionQueueLength(name):
    errorIndication, errorStatus, errorIndex, varBinds = cmdgen.CommandGenerator().getCmd(authData,
                                                                                        transportTarget,
                                                                                        (1, 3, 6, 1, 4, 1, 38350, 4, 9, 0))
            
    if (errorStatus == False and errorIndication == None):       
        return int(varBinds[0][1])
    else:
        return 0       


#Get the number of open files from the OSD per snmp
def OsdOpenFiles(name):
    errorIndication, errorStatus, errorIndex, varBinds = cmdgen.CommandGenerator().getCmd(authData,
                                                                                        transportTarget,
                                                                                        (1, 3, 6, 1, 4, 1, 38350, 4, 10, 0))
       
    if (errorStatus == False and errorIndication == None):       
        return int(varBinds[0][1])
    else:
        return 0       



#Get the number of deleted files from the OSD per snmp
def OsdDeletedFiles(name):
    errorIndication, errorStatus, errorIndex, varBinds = cmdgen.CommandGenerator().getCmd(authData,
                                                                                        transportTarget,
                                                                                        (1, 3, 6, 1, 4, 1, 38350, 4, 11, 0))
       
    if (errorStatus == False and errorIndication == None):       
        return int(varBinds[0][1])
    else:
        return 0       


#Get the free space from the OSD per snmp
def OsdFreeSpace(name):
    errorIndication, errorStatus, errorIndex, varBinds = cmdgen.CommandGenerator().getCmd(authData,
                                                                                        transportTarget,
                                                                                        (1, 3, 6, 1, 4, 1, 38350, 4, 12, 0))
    if (errorStatus == False and errorIndication == None):       
        return int(varBinds[0][1] / 1024 / 1024)
    else:
        return 0       

#get the status of the OSD
def Status(name):
    errorIndication, errorStatus, errorIndex, varBinds = cmdgen.CommandGenerator().getCmd(authData,
                                                                                        transportTarget,
                                                                                        (1, 3, 6, 1, 4, 1, 38350, 1, 11, 0))
            
    if (errorStatus == False and errorIndication == None):       
        return str(varBinds[0][1])
    else:
        return "OFFLINE"

#get the UUID of the OSD
#OID: 1.3.6.1.4.1.38350.1.13.0
def Uuid(name):
    errorIndication, errorStatus, errorIndex, varBinds = cmdgen.CommandGenerator().getCmd(authData,
                                                                                        transportTarget,
                                                                                        (1, 3, 6, 1, 4, 1, 38350, 1, 13, 0))
            
    if (errorStatus == False and errorIndication == None):       
        return str(varBinds[0][1])
    else: