aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Lindhé <andreas@lindhe.io>2018-04-06 11:13:29 +0200
committerAndreas Lindhé <andreas@lindhe.io>2018-04-06 11:13:29 +0200
commit2842bb1f114fbecdd953e33e536fc112be2f4dc0 (patch)
tree0f26946a35e398f8426adf0fb0bc883e453ab504
parentf406a5d8409062ad296eb696332659a70e87a4f7 (diff)
downloadmidbro-2842bb1f114fbecdd953e33e536fc112be2f4dc0.tar.gz
midbro-2842bb1f114fbecdd953e33e536fc112be2f4dc0.tar.bz2
Add the logging and USB mount/umount scripts
-rwxr-xr-xscript/logging.py63
-rwxr-xr-xscript/usb.sh46
2 files changed, 109 insertions, 0 deletions
diff --git a/script/logging.py b/script/logging.py
new file mode 100755
index 0000000..dca5413
--- /dev/null
+++ b/script/logging.py
@@ -0,0 +1,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)
diff --git a/script/usb.sh b/script/usb.sh
new file mode 100755
index 0000000..73c946c
--- /dev/null
+++ b/script/usb.sh
@@ -0,0 +1,46 @@
+#!/bin/bash
+
+#Function to check if the time is during working hours
+function workingHours()
+{
+ now=$(date +%H:%M)
+ if [[ "$now" < "15:00" ]] && [[ "$now" > "13:00" ]]; then
+ return 0;
+ else
+ return 1;
+ fi
+}
+
+# Mount locations
+primary="/mnt/primary"
+backup="/mnt/backup"
+
+# Create directories
+if ! [ -d "$primary" ]; then
+ mkdir -p "$primary"
+fi
+
+if ! [ -d "$backup" ]; then
+ mkdir -p "$backup"
+fi
+
+# If primary is not mounted, mount it
+if ! mount | grep $primary > /dev/null 2>&1; then
+ mount LABEL=PRIMARY "$primary" > /dev/null 2>&1
+fi
+
+# If primary is mounted, sync
+if mount | grep $primary > /dev/null 2>&1; then
+ rsync -rltDqzPcO --no-perms --backup-dir=old /home/pi/data "$primary"
+fi
+
+# If is not working hours and backup is not mounted, mount it
+if ! workingHours && ! mount | grep $backup > /dev/null 2>&1; then
+ mount LABEL=BACKUP "$backup" > /dev/null 2>&1
+fi
+
+# If the backup is mounted, sync
+if mount | grep $backup > /dev/null 2>&1; then
+ rsync -rltDqzPcO --no-perms --backup-dir=old /home/pi/data "$backup"
+ umount LABEL=BACKUP "$backup" > /dev/null 2>&1
+fi