Skip to content

Commit

Permalink
Modified logging. Fixed Settings file.
Browse files Browse the repository at this point in the history
  • Loading branch information
n0nuser committed Mar 27, 2022
1 parent c88fd8f commit e446251
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 27 deletions.
10 changes: 5 additions & 5 deletions monitor_agent/core/helper.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging


def getLogger(CONFIG):
def getLogger(level: str, filename: str):
log_level = ""
tranlation = {
"debug": logging.DEBUG,
Expand All @@ -11,8 +11,8 @@ def getLogger(CONFIG):
"critical": logging.CRITICAL,
}
try:
if CONFIG.logging.level in tranlation.keys():
log_level = tranlation[CONFIG.logging.level]
if level in tranlation.keys():
log_level = tranlation[level]
else:
logging.warning("Level not established in Settings.json")
log_level = logging.info
Expand All @@ -25,15 +25,15 @@ def getLogger(CONFIG):

log_filename = ""
try:
log_filename = CONFIG.logging.filename
log_filename = filename
except AttributeError as e:
logging.warning(
'Log filename not established in Settings.json.\nDefault "monitor.log" file will be used.',
exc_info=True,
)
log_filename = "monitor.log"

return logging.basicConfig(
logging.basicConfig(
level=log_level,
filename=log_filename,
format="%(asctime)s - %(levelname)s - %(message)s",
Expand Down
21 changes: 14 additions & 7 deletions monitor_agent/core/metricFunctions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
import requests
import logging

from monitor_agent.main import LOGGER
from core.models import Status, MetricDynamic, MetricStatic
from monitor_agent.core.models import Status, MetricDynamic, MetricStatic


def execution_time_decorator(function) -> typing.Tuple[float, dict]:
Expand Down Expand Up @@ -34,18 +33,26 @@ def send_metrics_adapter(function_list: list) -> typing.Tuple[dict, dict]:
elapsed_time.update(f_time)
data.update(f_data)
except TypeError as msg:
LOGGER.warning(f"TypeError: {msg}", exc_info=True)
logging.warning(f"TypeError: {msg}", exc_info=True)
continue
return elapsed_time, data


def send_metrics(
url: str, elapsed_time: dict, data: dict, file_enabled: bool, file_path: str
url: str, elapsed_time: dict, data: dict, file_enabled: bool, file_path: str, auth: dict, port:int
):
auth.update({"port": port})
auth.pop("__module__", None)
auth.pop("__dict__", None)
auth.pop("__weakref__", None)
auth.pop("__doc__", None)
status = Status(elapsed=elapsed_time).__dict__
json_request = {"data": data, "status": status}
r = requests.post(url, json=json_request)
# DEBUG
json_request = {"auth": auth, "data": data, "status": status}
try:
r = requests.post(url, json=json_request, headers={'Authorization': auth["api_token"]})
except requests.exceptions.InvalidSchema as e:
logging.critical(f"Agent could not send metrics to server {url}", exc_info=True)
logging.debug(f"Response: {r.text}")
logging.debug(f"Status Code: {r.status_code}")
if file_enabled:
with open(file_path, "w") as f:
Expand Down
4 changes: 2 additions & 2 deletions monitor_agent/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import time
import datetime

from monitor_agent.main import LOGGER
from monitor_agent.main import logging


class Status:
Expand Down Expand Up @@ -115,7 +115,7 @@ def _process(ram: int, pc_cpu_percent):
# Requires elevated permissions
process[p.pid]["path"] = p.exe()
except (PermissionError, psutil.AccessDenied):
LOGGER.warning(
logging.warning(
f"Could not get Username or Path for process {p.name()}"
)
return process
Expand Down
19 changes: 14 additions & 5 deletions monitor_agent/main.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import json
from monitor_agent.core.helper import getLogger
import uvicorn
import logging
import requests
from .settings import Settings
from .core.command import Command
from fastapi import FastAPI, UploadFile
from fastapi_utils.tasks import repeat_every
from monitor_agent.core.helper import getLogger


try:
Expand All @@ -15,7 +15,7 @@
logging.critical(f'Error in "settings.json". {msg}')
exit()

LOGGER = getLogger(CONFIG)
getLogger(CONFIG.logging.level, CONFIG.logging.filename)

from .core.metricFunctions import send_metrics, send_metrics_adapter, static, dynamic

Expand Down Expand Up @@ -63,21 +63,26 @@ async def mod_settings(settings: UploadFile):
CONFIG = Settings()
return msg

logging.debug(CONFIG.metrics.post_interval)

@api.on_event("startup")
@repeat_every(seconds=CONFIG.metrics.post_interval, logger=LOGGER, wait_first=True)
@repeat_every(seconds=CONFIG.metrics.post_interval, logger=logging, wait_first=True)
def periodic():
# https://github.com/tiangolo/fastapi/issues/520
# https://fastapi-utils.davidmontague.xyz/user-guide/repeated-tasks/#the-repeat_every-decorator
# Changed Timeloop for this
elapsed_time, data = send_metrics_adapter([static, dynamic])
logging.debug("Sending metrics")
send_metrics(
url=CONFIG.metrics.post_url,
elapsed_time=elapsed_time,
data=data,
file_enabled=CONFIG.metrics.enable_logfile,
file_path=CONFIG.metrics.log_filename,
auth=dict(CONFIG.auth.__dict__),
port=CONFIG.uvicorn.port
)
logging.debug("Metrics sent!")

alert = {}
if data["cpu_percent"] >= CONFIG.thresholds.cpu_percent:
Expand All @@ -90,7 +95,11 @@ def periodic():
except KeyError as msg:
pass
if alert:
r = requests.post(CONFIG.alerts.url, json={"alert": alert})
try:
r = requests.post(CONFIG.alerts.url, json={"alert": alert})
except requests.exceptions.InvalidSchema as e:
logging.error(f"Agent could not send an alert to {CONFIG.alerts.url}", exc_info=True)



def start():
Expand All @@ -104,4 +113,4 @@ def start():
try:
uvicorn.run(**uviconfig)
except:
LOGGER.critical("Unable to run server.", exc_info=True)
logging.critical("Unable to run server.", exc_info=True)
14 changes: 7 additions & 7 deletions monitor_agent/settings.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
{
"alerts": {
"url": "127.0.0.1:8000/alerts"
"url": "http://127.0.0.1:8000/api/alerts/"
},
"auth": {
"id_token": "",
"api_token": "",
"name": ""
"api_token": "7320730b62807c221d0058eff8d13fa26b714db6",
"id_token": "79cff2298c9a54d0ccef292fe8938e4055faa708",
"name": "NUC Server"
},
"logging": {
"filename": "monitor.log",
"level": "info"
"level": "debug"
},
"metrics": {
"enable_logfile": false,
"get_endpoint": true,
"log_filename": "metrics.json",
"post_interval": 60,
"post_url": "http://httpbin.org/post"
"post_url": "http://127.0.0.1:8000/api/metrics/"
},
"thresholds": {
"cpu_percent": 50,
Expand All @@ -27,7 +27,7 @@
"debug": true,
"host": "0.0.0.0",
"log_level": "trace",
"port": 8000,
"port": 8080,
"reload": true,
"timeout_keep_alive": 5,
"workers": 4
Expand Down
1 change: 0 additions & 1 deletion monitor_agent/settings.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import os
import sys
import json
import logging

Expand Down

0 comments on commit e446251

Please sign in to comment.