aboutsummaryrefslogtreecommitdiff
path: root/script/logging.py
blob: dca54139eb763358350efeb93f394125725adf5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/python3.5
# -*- coding: utf-8 -*-
#
# License: MIT
# Author: Andreas Lindhé

import sys
import time
import psutil
from datetime import datetime as dt


log = []

def main(filepath="/tmp/sysinfo.log", log_interval=10, write_interval=600):
  global log
  while True:
    for i in range(write_interval//log_interval):
      log.append( get_log_data() )
      time.sleep( log_interval )
    write_to_file(filepath)
    log = []


def get_log_data():
  now = "{:%Y-%m-%d %H:%M:%S};".format( dt.now() )
  cpu = psutil.cpu_percent(percpu=True, interval=1)
  cpufmt = len(cpu) * ' {:>5}'
  cores = cpufmt.format(*cpu) + ';'
  ram = str( psutil.virtual_memory().percent )+';'
  boot_time = "{:%Y-%m-%d %H:%M:%S};".format( dt.fromtimestamp(psutil.boot_time()) )
  return " ".join([now, cores, ram, boot_time])


def write_to_file(filepath="/tmp/asdf.log"):
  try:
    with open(filepath, 'a') as f:
      for line in log:
        f.write(line + '\n')
  except OSError as e:
    print("Error reading file {}: {}".format(filepath, e), file=sys.stderr)

if __name__ == '__main__':
  program = sys.argv[0]
  if len(sys.argv) == 2:
    filepath = sys.argv[1]
    log_interval = 10
    write_interval = 60
  elif len(sys.argv) == 4:
    filepath = sys.argv[1]
    log_interval = int(sys.argv[2])
    write_interval = int(sys.argv[3])
  else:
    print("Usage: {} /path/to/file [log_interval] [write_interval]")
    print("log_interval is the time in seconds between each log line")
    print("write_interval is the time in seconds between each write")
    sys.exit(1)

  try:
    main(filepath, log_interval, write_interval)
  except KeyboardInterrupt:
    sys.stderr.write("Interrupted\n")
    sys.exit(0)