#!/usr/bin/python3

import os
from lib.PipeUtil import load_json_file, cfe


json_conf = load_json_file("../conf/as6.json")
ams_id = json_conf['site']['ams_id'].upper()
if cfe("/mnt/archive.allsky.tv/" + ams_id + "/monitor/", 1) == 0:
   os.makedirs("/mnt/archive.allsky.tv/" + ams_id + "/monitor/")

print("Monitor")
os.system("echo " )
os.system("echo '### station ###' > /mnt/archive.allsky.tv/" + ams_id + "/monitor/status2.txt")
os.system("echo " + ams_id + " >> /mnt/archive.allsky.tv/" + ams_id + "/monitor/status2.txt")
os.system("echo '*** station ***' >> /mnt/archive.allsky.tv/" + ams_id + "/monitor/status2.txt")
os.system("echo '### date ###' >> /mnt/archive.allsky.tv/" + ams_id + "/monitor/status2.txt")
os.system("date -Iseconds >> /mnt/archive.allsky.tv/" + ams_id + "/monitor/status2.txt")
print("Monitor2")
os.system("echo '*** date ***' >> /mnt/archive.allsky.tv/" + ams_id + "/monitor/status2.txt")
os.system("echo '### who ###' >> /mnt/archive.allsky.tv/" + ams_id + "/monitor/status2.txt")
os.system("who -a >>  /mnt/archive.allsky.tv/" + ams_id + "/monitor/status2.txt")
os.system("echo '*** who ***' >> /mnt/archive.allsky.tv/" + ams_id + "/monitor/status2.txt")
os.system("echo '### uptime ###' >> /mnt/archive.allsky.tv/" + ams_id + "/monitor/status2.txt")
os.system("uptime >>  /mnt/archive.allsky.tv/" + ams_id + "/monitor/status2.txt")
print("Monitor3")
os.system("echo '*** uptime ***' >> /mnt/archive.allsky.tv/" + ams_id + "/monitor/status2.txt")
os.system("echo '### cpu ###' >> /mnt/archive.allsky.tv/" + ams_id + "/monitor/status2.txt")
os.system("echo 'USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND' >>  /mnt/archive.allsky.tv/" + ams_id + "/monitor/status2.txt")
os.system("ps -aux | sort -nr -k 3 | head -10 >>  /mnt/archive.allsky.tv/" + ams_id + "/monitor/status2.txt")
print("Monitor4")
os.system("echo '*** cpu ***' >> /mnt/archive.allsky.tv/" + ams_id + "/monitor/status2.txt")
os.system("echo '### mem ###' >>  /mnt/archive.allsky.tv/" + ams_id + "/monitor/status2.txt")
os.system("echo 'USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND' >>  /mnt/archive.allsky.tv/" + ams_id + "/monitor/status2.txt")
os.system("ps -aux | sort -nr -k 4 | head -10 >>  /mnt/archive.allsky.tv/" + ams_id + "/monitor/status2.txt")
os.system("echo '*** mem ***' >> /mnt/archive.allsky.tv/" + ams_id + "/monitor/status2.txt")
os.system("echo '### iostat ###' >>  /mnt/archive.allsky.tv/" + ams_id + "/monitor/status2.txt")
print("Monitor5")
os.system("iostat >>  /mnt/archive.allsky.tv/" + ams_id + "/monitor/status2.txt")
os.system("echo '*** iostat ***' >> /mnt/archive.allsky.tv/" + ams_id + "/monitor/status2.txt")
os.system("echo '### free ###' >>  /mnt/archive.allsky.tv/" + ams_id + "/monitor/status2.txt")
os.system("free -m >>  /mnt/archive.allsky.tv/" + ams_id + "/monitor/status2.txt")
os.system("echo '*** free ***' >> /mnt/archive.allsky.tv/" + ams_id + "/monitor/status2.txt")
print("Monitor find HD")
os.system("echo '### find ###' >>  /mnt/archive.allsky.tv/" + ams_id + "/monitor/status2.txt")
os.system("find /mnt/ams2/HD -mmin 1 -printf '%f\t%s\n' >>  /mnt/archive.allsky.tv/" + ams_id + "/monitor/status2.txt")
os.system("echo '*** find ***' >> /mnt/archive.allsky.tv/" + ams_id + "/monitor/status2.txt")
os.system("echo '### df ###' >>  /mnt/archive.allsky.tv/" + ams_id + "/monitor/status2.txt")
os.system("df --type=ext4 >> /mnt/archive.allsky.tv/" + ams_id + "/monitor/status2.txt")
os.system("echo '*** df ***' >> /mnt/archive.allsky.tv/" + ams_id + "/monitor/status2.txt")
os.system("echo '### end ###' >>  /mnt/archive.allsky.tv/" + ams_id + "/monitor/status2.txt")
