#!/usr/bin/env python
##############################################################################
# Copyright (c) Members of the EGEE Collaboration. 2011.
# See http://www.eu-egee.org/partners/ for details on the copyright
# holders.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS
# OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##############################################################################
#
# NAME :        check_dpm_infosys
#
# DESCRIPTION : Checks the published information
#
# AUTHORS :     Alejandro.Alvarez.Ayllon@cern.ch
#
##############################################################################

import ldap
import os
import socket
import time
from lcgdmcommon import *

class check_dpm_infosys:
  "Checks correctness of information published in the BDII"
  __version__     = "0.0.1"
  __nagios_id__   = "DM-INFOSYS"

  # Defaults
  DEFAULT_PORT = 2170

  # Specific parameters, where key = short, value = long (i.e. {"h":"help", "C:":"command="})
  # getopt format. The long version will be the one passed even when the short is specified
  __additional_opts__ = {"H:": "host=",
                         "p:": "port="}

  # Specific usage information
  __usage__ = """
\t-H, --host\tThe host to query. If not specified, DPM_HOST will be used. 'localhost' in last instance.
\t-p, --port\tThe ldap port. Default: %d

This probe expects a running local BDII and checks the correctness of information published in it. The rfio, gridftp and srm are checked as well the srm manager service (httpg://$DPM_HOST:8446/srm/managerv2)

Description of work executed by the probe:

\t1. Initialize a ldap connection to the headnode
\t2. Check if informations about gridftp and rfio protocols are correctly published
\t3. Check if informations about srmv2 protocol is correctly published  
\t4. check if informations about "httpg://hostname:8446/srm/managerv2" service is correctly published
\t5. Return values to nagios
\t\tNo Warning alert can be set
\t\tCritical alert is triggerd if the ldap server is unreachable or one of the previous item don't publish informations correctly

""" % (DEFAULT_PORT)

  # Methods

  def __init__(self, opt = {}, args = []):
    """
    Constructor

    @param opt  Contains a dictionary with the long option name as the key, and the argument as value
    @param args Contains the arguments not associated with any option
    """
    # Host
    if "host" in opt:
      self.host = opt["host"]
    elif "DPM_HOST" in os.environ:
      self.host = os.environ["DPM_HOST"]
    else:
      self.host = socket.getfqdn()

    # Port
    if "port" in opt:
      self.port = int(opt["port"])
    else:
      self.port = self.DEFAULT_PORT

    # Others
    self.base_dn = "mds-vo-name=resource,o=grid"

  def time_start(self):
    self._start = time.time()

  def time_elapsed(self):
    return time.time() - self._start

  def main(self):
    """
    Test code itself. May raise exceptions.

    @return A tuple (exit code, message, performance)
    """
    debug("Connect to %s:%d" % (self.host, self.port))

    try:
      conn = ldap.initialize("ldap://%s:%d" % (self.host, self.port))
      conn.simple_bind_s()   
    except:
      return (EX_CRITICAL, "ldap server ldap://%s:%d unreachable" %(self.host, self.port), None)

    self.time_start()
    # Check published protocols
    for protocol in ["rfio", "gsiftp"]:
      result = conn.search_s(self.base_dn, ldap.SCOPE_SUBTREE, "GlueSEAccessProtocolLocalID=%s" % protocol)
      if not len(result):
        return (EX_CRITICAL, "Access protocol %s not published" % protocol, None)
      elif verbosity(V_EXTENDED):
        print "Access protocol %s published" % protocol

    for protocol in ["srmv2"]:
      result = conn.search_s(self.base_dn, ldap.SCOPE_SUBTREE, "GlueSEControlProtocolLocalID=%s" % protocol)
      if not len(result):
        return (EX_CRITICAL, "Control protocol %s not published" % protocol, None)
      elif verbosity(V_EXTENDED): 
        print "Control protocol %s published" % protocol

    # Services
    for s in ["httpg://%s:8446/srm/managerv2"]:
      service = s % self.host
      result = conn.search_s(self.base_dn, ldap.SCOPE_SUBTREE, "GlueServiceUniqueID=%s" % service)
      if not len(result):
        return (EX_CRITICAL, "Service %s not published" % service, None)
      elif verbosity(V_EXTENDED):
        print "Service %s published" % service

    # Success!
    elapsed = self.time_elapsed()
    return (EX_OK, "Published information OK", "elapsed=%.4fs" % elapsed)
 
# When called directly
if __name__ == "__main__":
  run(check_dpm_infosys)

