import os import sys import json import functools from collector.sender import send from collector.collector import * from collector.helper import * CONFIG_FILE = os.getenv("COLLECTOR_CONF", "collector.json") REQUIRED = ["graphite","os","data"] VALID_DATAPOINTS = ["storage"] VALID_OS = ["linux", "osx"] REQUIRED_STORAGE = ["path", "name"] CONFIG = {} HOSTNAME = get_hostname() with open(CONFIG_FILE) as f: CONFIG = json.loads(f.read()) # Check top level missing = get_missing(REQUIRED, CONFIG.keys()) if len(missing) > 0: print(f"Missing: {', '.join(missing)}") sys.exit(1) # Now that we know we have a graphite server sender = functools.partial( send, server = CONFIG["graphite"]["server"], port = CONFIG["graphite"]["port"] ) # Check data exists data = CONFIG["data"] if type(data) != dict: print("Config at 'data' missing values.") sys.exit(1) data_points = data.keys() if not len(data_points) > 0: print("No data points found in config.") sys.exit(1) unknown = get_missing(data_points, VALID_DATAPOINTS) if len(unknown) > 0: print(f"Unknown data: {', '.join(unknown)}") sys.exit(1) # Check storage if "storage" in data_points: storage_data = data["storage"] # Check if storage is a list if type(storage_data) != list: print("storage data must be a list") sys.exit(1) # Check if storage is not empty if not len(storage_data) > 0: print("Storage data cannot be empty") sys.exit(1) for item in storage_data: # Check if the storage item is a dict if type(item) != dict: print(f"Storage data at {item} is not an object.") # Make sure each storage item has the required fields missing = get_missing(REQUIRED_STORAGE, item.keys()) if len(missing) > 0: print(f"Storage data at {item} missing: {', '.join(missing)}") # Collect data else: name = item["name"] path = item["path"] storage_sender = functools.partial(sender, f"{HOSTNAME}.storage.{name}") thread(collect_storage, storage_sender, path)