cnfparse.py 1.58 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
        params["frequency"]       =  config.get("Client" , "Frequency")
10
        params["debugMode"]       =  config.get("Client" , "Debug")
11 12 13 14 15 16
        params["server_address"]  =  config.get("Server" , "Address")
        params["server_port"]     =  config.get("Server" , "Port")
        params["amqp_queue"]      =  config.get("AMQP"   , "Queue")
        params["amqp_user"]       =  config.get("AMQP"   , "User")
        params["amqp_pass"]       =  config.get("AMQP"   , "Pass")
        params["amqp_virtual_host"] =  config.get("AMQP"   , "Vhost")
17 18 19 20 21
        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")
root committed
22
        metrics["network"]    =  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