Commit 04565dfa authored by Jonathan Schaeffer's avatar Jonathan Schaeffer
Browse files

Parsing de base OK

parent 4c843c8a
#!/bin/env python2
# -*- coding: utf-8 -*-
import logging.config
import os
import subprocess
from time import gmtime,strftime
import yaml
from influxdb import InfluxDBClient
from pprint import pprint, pformat
def test_config_file(f = "config.yml"):
result = False
print(f"Searching for {f}")
if os.path.isfile(os.path.realpath(f)):
print(f"Config file is {f}")
result = True
return result
def get_config_file(directories = [os.path.dirname(os.path.realpath(__file__)), "~/config/resif_data_reporter", "/etc/resif_data_reporter"]):
"""
Return the configuration file from ordered list of directories.
If config.yml is present, then return full path.
Return empty string if nothing found
Todo : return error.
"""
for d in directories:
if test_config_file(f"{d}/config.yml"):
return f"{d}/config.yml"
return ""
def validate_config(cfg):
"""
Validate configuration
"""
# Logger file exists and is readable ?
return True
def main():
config_path = get_config_file()
with open(config_path, 'r') as ymlfile:
cfg = yaml.load(ymlfile)
validate_config(cfg)
# At this point we ensure that configuration is sane.
logging.config.fileConfig(cfg["logger_file"])
logger = logging.getLogger("resif_data_reporter")
logger.info("Starting")
data = {}
influxdb_json_data = []
for volume in cfg['volumes']:
lines = subprocess.check_output(["du", "-d2", volume]).decode("utf-8").splitlines()
for l in lines:
(size, path) = l.split('\t')
path = path.split('/')[2:]
if len(path) == 2:
logger.debug(f"path : {path}")
logger.debug(f"size : {size}")
# data : {2011: {G: 23, FR: 100, ...} 2012: {G: 12, FR: 120, ...}, ...}
if not (path[0] in data) :
data[path[0]] = {}
if not (path[1] in data[path[0]]) :
data[path[0]][path[1]] = 0
data[path[0]][path[1]] += int(size)
logger.info(pformat(data))
# Now, send this data to influxdb
# 1. connect to influxdb
# 2. write metric
if __name__ == "__main__":
main()
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment