mirror of
https://codeberg.org/privacy1st/de-p1st-monitor
synced 2024-11-23 19:49:32 +01:00
journald compatible log levels
This commit is contained in:
parent
8d12061ce2
commit
d2d7c52cbf
@ -4,6 +4,8 @@ import csv
|
||||
from collections import deque
|
||||
from pathlib import Path
|
||||
|
||||
from de.p1st.monitor.print_util import print_debug
|
||||
|
||||
|
||||
def read(file: Path) -> list[list[str]]:
|
||||
"""
|
||||
@ -84,7 +86,7 @@ def append(file: Path, rows: list[list[str]]) -> None:
|
||||
def test():
|
||||
file = Path('/var/log/de-p1st-monitor/cpu_avg.csv')
|
||||
data = read_last(file, 4, 10)
|
||||
print(data)
|
||||
print_debug(data)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
@ -2,23 +2,25 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from datetime import datetime, timezone
|
||||
|
||||
from de.p1st.monitor.print_util import print_debug
|
||||
|
||||
|
||||
def test():
|
||||
dt = datetime.now()
|
||||
|
||||
print('non UTC:')
|
||||
print(dt)
|
||||
print_debug('non UTC:')
|
||||
print_debug(dt)
|
||||
|
||||
print('\nUTC:')
|
||||
print(now())
|
||||
print(to_str(now()))
|
||||
print(now_str())
|
||||
print(from_str(to_str(now())))
|
||||
print_debug('\nUTC:')
|
||||
print_debug(now())
|
||||
print_debug(to_str(now()))
|
||||
print_debug(now_str())
|
||||
print_debug(from_str(to_str(now())))
|
||||
|
||||
print('\nlocalized:')
|
||||
print(dt.tzinfo)
|
||||
print_debug('\nlocalized:')
|
||||
print_debug(dt.tzinfo)
|
||||
dt = dt.replace(tzinfo=timezone.utc)
|
||||
print(dt)
|
||||
print_debug(dt)
|
||||
|
||||
|
||||
def now() -> datetime:
|
||||
|
@ -9,6 +9,7 @@ import psutil
|
||||
from de.p1st.monitor import datetime_util, csv_util
|
||||
from de.p1st.monitor.logger import Logger
|
||||
from de.p1st.monitor.logger_ex import LoggerReadEx
|
||||
from de.p1st.monitor.print_util import print_debug
|
||||
from de.p1st.monitor.warn import WarnLevel, WarnMessage
|
||||
|
||||
|
||||
@ -43,8 +44,7 @@ class NetworkLogger(Logger):
|
||||
delta_received = curr_row[2] - prev_row[2]
|
||||
|
||||
if delta_sent < 0 or delta_received < 0:
|
||||
print(f'bytes received/sent counter did overflow after {prev_row[0]}',
|
||||
file=sys.stderr)
|
||||
print_debug(f'bytes received/sent counter did overflow after {prev_row[0]}')
|
||||
continue
|
||||
|
||||
elapsed_seconds = elapsed_time.total_seconds()
|
||||
|
@ -1,12 +1,12 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
import argparse
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
from de.p1st.monitor.cfg.singleton import init_cfg
|
||||
from de.p1st.monitor.cfg.loggers import get_loggers
|
||||
from de.p1st.monitor.logger_ex import LoggerReadEx
|
||||
from de.p1st.monitor.print_util import print_err, print_debug
|
||||
|
||||
|
||||
def main():
|
||||
@ -34,13 +34,13 @@ def main():
|
||||
def export():
|
||||
loggers, logger_arg_exs = get_loggers()
|
||||
if len(logger_arg_exs) > 0:
|
||||
print('\nCONFIGURATION ERROR: Could not instantiate some of the loggers!', file=sys.stderr)
|
||||
print_err('\nCONFIGURATION ERROR: Could not instantiate some of the loggers!')
|
||||
print_exs(logger_arg_exs, [f'{n}.' for n in range(1, 1 + len(logger_arg_exs))])
|
||||
exit(1)
|
||||
|
||||
for logger in loggers:
|
||||
export_path: Path = logger.export_data()
|
||||
print(export_path)
|
||||
print_debug(export_path)
|
||||
|
||||
|
||||
def log():
|
||||
@ -48,7 +48,7 @@ def log():
|
||||
logger_read_exs = []
|
||||
logger_warnings = 0
|
||||
for logger_ct, logger in enumerate(loggers, start=1):
|
||||
# print(f'Running logger {logger_ct}/{len(loggers)} ...')
|
||||
# print_debug(f'Running logger {logger_ct}/{len(loggers)} ...')
|
||||
try:
|
||||
logger.update()
|
||||
except LoggerReadEx as e:
|
||||
@ -59,10 +59,10 @@ def log():
|
||||
logger_warnings += 1
|
||||
|
||||
if len(logger_arg_exs) > 0:
|
||||
print('\nCONFIGURATION ERROR: Could not instantiate some of the loggers!', file=sys.stderr)
|
||||
print_err('\nCONFIGURATION ERROR: Could not instantiate some of the loggers!')
|
||||
print_exs(logger_arg_exs, [f'{n}.' for n in range(1, 1 + len(logger_arg_exs))])
|
||||
if len(logger_read_exs) > 0:
|
||||
print('\nRUNTIME ERROR: Some loggers could not fetch sensor data!', file=sys.stderr)
|
||||
print_err('\nRUNTIME ERROR: Some loggers could not fetch sensor data!')
|
||||
print_exs(logger_read_exs, [f'{n}.' for n in range(1, 1 + len(logger_read_exs))])
|
||||
|
||||
if len(logger_arg_exs) + len(logger_read_exs) > 0 or logger_warnings > 0:
|
||||
@ -74,7 +74,7 @@ def print_exs(exs: list[Exception], headers: list):
|
||||
# Indent str(e) with \t
|
||||
body = '\t' + '\n\t'.join(str(e).splitlines())
|
||||
|
||||
print(f'{header}\n{body}', file=sys.stderr)
|
||||
print_err(f'{header}\n{body}')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
46
src/de/p1st/monitor/print_util.py
Normal file
46
src/de/p1st/monitor/print_util.py
Normal file
@ -0,0 +1,46 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
import sys
|
||||
|
||||
|
||||
# https://www.freedesktop.org/software/systemd/man/latest/sd-daemon.html
|
||||
#
|
||||
# define SD_EMERG "<0>" /* system is unusable */
|
||||
# define SD_ALERT "<1>" /* action must be taken immediately */
|
||||
# define SD_CRIT "<2>" /* critical conditions */
|
||||
# define SD_ERR "<3>" /* error conditions */
|
||||
# define SD_WARNING "<4>" /* warning conditions */
|
||||
# define SD_NOTICE "<5>" /* normal but significant condition */
|
||||
# define SD_INFO "<6>" /* informational */
|
||||
# define SD_DEBUG "<7>" /* debug-level messages */
|
||||
|
||||
def print_emerg(message: object):
|
||||
print(f'<0> {message}', file=sys.stderr)
|
||||
|
||||
|
||||
def print_alert(message: object):
|
||||
print(f'<1> {message}', file=sys.stderr)
|
||||
|
||||
|
||||
def print_crit(message: object):
|
||||
print(f'<2> {message}', file=sys.stderr)
|
||||
|
||||
|
||||
def print_err(message: object):
|
||||
print(f'<3> {message}', file=sys.stderr)
|
||||
|
||||
|
||||
def print_warning(message: object):
|
||||
print(f'<4> {message}', file=sys.stderr)
|
||||
|
||||
|
||||
def print_notice(message: object):
|
||||
print(f'<5> {message}', file=sys.stdout)
|
||||
|
||||
|
||||
def print_info(message: object):
|
||||
print(f'<6> {message}', file=sys.stdout)
|
||||
|
||||
|
||||
def print_debug(message: object):
|
||||
print(f'<7> {message}', file=sys.stdout)
|
@ -1,11 +1,11 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
from __future__ import annotations
|
||||
import sys
|
||||
from datetime import datetime
|
||||
from enum import Enum
|
||||
from functools import total_ordering
|
||||
from typing import TextIO
|
||||
|
||||
from de.p1st.monitor.print_util import print_warning, print_crit
|
||||
|
||||
|
||||
# https://docs.python.org/3/library/functools.html#functools.total_ordering
|
||||
@ -58,8 +58,12 @@ class WarnMessage:
|
||||
"""
|
||||
if self.level == WarnLevel.NONE:
|
||||
pass
|
||||
elif self.level == WarnLevel.NORMAL:
|
||||
print_warning(self.formatted_message())
|
||||
elif self.level == WarnLevel.HIGH:
|
||||
print_crit(self.formatted_message())
|
||||
else:
|
||||
print(self.formatted_message(), file=self.file())
|
||||
raise NotImplementedError()
|
||||
|
||||
return self
|
||||
|
||||
@ -73,7 +77,3 @@ class WarnMessage:
|
||||
return f'[CRITICAL] {self.date}: '
|
||||
return f'{self.date}: '
|
||||
|
||||
def file(self) -> TextIO:
|
||||
if self.level == WarnLevel.NONE:
|
||||
raise ValueError()
|
||||
return sys.stderr
|
||||
|
Loading…
Reference in New Issue
Block a user