#!/usr/local/bin/python3.4
# Copyright (c) 2015,
#
# This module is free software; you can redistribute it and/or modify it
# under the terms of GNU General Public License (GPL) version 2.

import logging
import configparser

_CONFIG_FILE = "/usr/local/nagios/etc/svc/host_info.properties"

def retrieveAccessInfoForDevice(host):
    """
    get credential from configuration file, return (user,password)
    """
    _METHOD_ = "retrieveAccessInfoForDevice"
    config = configparser.ConfigParser()
    fileList = config.read(_CONFIG_FILE)
    if len(fileList) == 0:
        logging.critical(
            "CRITICAL %s::config file [%s] not found", _METHOD_, _CONFIG_FILE)
        raise BaseException("Configuration file not found")
    
    if config.has_section(host) == False:
        logging.critical(
            "CRITICAL %s::section [%s] not found in config file [%s]", _METHOD_, host, _CONFIG_FILE)
        raise BaseException("Section not found in configuration file")
    
    option_user = "userid"
    option_pw = "password"
    if config.has_option(host, option_user) == False:
        logging.critical(
            "CRITICAL %s::option [%s] not found for section [%s] in config file [%s]", _METHOD_, option_user, host, _CONFIG_FILE)
        raise BaseException("option not found in configuration file")
    
    if config.has_option(host, option_pw) == False:
        logging.critical(
            "CRITICAL %s::option [%s] not found for section [%s] in config file [%s]", _METHOD_, option_pw, host, _CONFIG_FILE)
        raise BaseException("option not found in configuration file")
    
    return (config.get(host, option_user), config.get(host, option_pw))