cnfparse.py 1.59 KB
Newer Older
1
import ConfigParser as configparser
2 3 4 5 6 7

def importConf(path_to_file):
    config = configparser.RawConfigParser(allow_no_value = False)
    try:
        config.read(path_to_file)
        params = {}
8
        metrics = {}
9 10 11 12
        params["frequency"]       =  config.get("Client" , "Frequency")
        params["debugMode"]       =  config.get("Client" , "Debug")
        params["server_address"]  =  config.get("Server" , "Address")
        params["server_port"]     =  config.get("Server" , "Port")
13 14 15
        params["amqp_queue"]      =  config.get("AMQP"   , "Queue")
        params["amqp_user"]       =  config.get("AMQP"   , "User")
        params["amqp_pass"]       =  config.get("AMQP"   , "Pass")
16 17 18 19 20 21 22
        metrics["cpu.usage"]       =  config.get("Metrics", "cpuUsage")
        metrics["memory.usage"]    =  config.get("Metrics", "memoryUsage")
        metrics["user.count"]      =  config.get("Metrics", "userCount")
        metrics["swap.usage"]      =  config.get("Metrics", "swapUsage")
        metrics["system.boot_time"]=  config.get("Metrics", "systemBootTime")
        metrics["package.traffic"] =  config.get("Metrics", "packageTraffic")
        metrics["data.traffic"]    =  config.get("Metrics", "dataTraffic")
23 24 25 26 27 28 29 30 31 32
    except configparser.NoSectionError:
        print("Config file contains error! Reason: Missing section.")
        raise
    except configparser.ParsingError:
        print("Config file contains error! Reason: Cannot parse.")
        raise
    except configparser.MissingSectionHeader:
        print("Config file contains error! Reason: Missing section-header.")
        raise

33
    return params, metrics
34 35