''' Author: Nikita Yadav
	This script will calculate the cpu and memory utilization by the system during runtime the output of which is a graph on html page also the second tab of html gives the system logs
'''
import argparse
import os
import matplotlib.pyplot as plt
import datetime
import base64
import logging
import threading
import time
fh = ''
fh_1 = ''
def cpu_percent(cmd):
    global fh
    fh = os.popen(cmd)
def memory_percent(cmd):
    global fh_1
    fh_1 = os.popen(cmd)
def htmlimage(data_4, data):
    html = open("log.html", 'w')
    img = data_4 + " " + data
    html.write(img)
def main():
    global duration
    parser = argparse.ArgumentParser()
    parser.add_argument("-t", "--duration", type=int, help="Enter the Time for which you want to run test (in minutes)")
    try:
        args = parser.parse_args()
        if (args.duration is not None):
            duration = args.duration
    except Exception as e:
        logging.exception(e)
        exit(2)
    endTime = datetime.datetime.now() + datetime.timedelta(seconds=duration)
    now = datetime.datetime.now()
    starttime = datetime.datetime(now.year, now.month, now.day, now.hour, now.minute, now.second)
    delta = datetime.timedelta(seconds=10)
    cpu_stats_data = {"system": [], "kernel": []}
    memory_stats_data = {"Total": [], "Used": []}
    iterations = duration * 60
    cmd_1 = "top -bn1 -d 1 -n " + str(iterations) + " | grep '%Cpu(s)' "
    cmd_2 = "top -bn1 -d 1  -n " + str(iterations) + " | grep 'MiB Mem'"
    t1 = threading.Thread(target=cpu_percent, args=(cmd_1,))
    t2 = threading.Thread(target=memory_percent, args=(cmd_2,))
    t1.start()
    t2.start()
    t1.join()
    t2.join()
    time.sleep(10)
    output = fh.read() + fh.readline()
    output_1 = fh_1.read() + fh_1.readline()
    data = output.split('\n')
    data_1 = output_1.split('\n')
    for i in data:
        # print(i.split(','))
        if len(i) > 3:
            cpu_stats_data["system"].append(float(i.split(',')[0].split()[1]))
            cpu_stats_data["kernel"].append(float(i.split(',')[1].split()[0]))
        # print(cpu_stats_data)
    for i in data_1:
        if len(i) > 3:
            memory_stats_data["Total"].append(float(i.split(',')[0].split()[3]))
            memory_stats_data["Used"].append(float(i.split(',')[2].split()[0]))
     # print(memory_stats_data)
    sample_times = [starttime + i * delta for i in range(len(cpu_stats_data["system"]))]
    label_locations = [d for d in sample_times if d.minute % 1 == 0]
    labels = [d.strftime('%Y-%m-%d %H:%M:%S') for d in label_locations]
    # print(labels)
    #print(sample_times)
    # thread creation
    # graphs
    #plot1
    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1)
    ax.plot(sample_times, cpu_stats_data['system'], '-', lw=1, color='r', label="system cpu%")
    ax.plot(sample_times, cpu_stats_data['kernel'], '-', lw=1, color='b', label="kernel cpu%")
    ax.set_ylabel('CPU (%)', color='r')
    ax.set_xlabel('time (s)')
    plt.tight_layout()
    ax.set_ylim(0., max(cpu_stats_data['system']) + 20)
    plt.xticks(rotation='vertical')
    fig.legend(["System CPU Utilization", "Kernel CPU Utilization"], loc='upper center')
    ax.grid()
    fig.savefig("cpu.png")
    #plot2
    fig_1 = plt.figure()
    ax_1 = fig_1.add_subplot(1, 1, 1)
    ax_1.plot(sample_times, memory_stats_data["Total"], '-', lw=1, color='r', label="Total MEMORY AVAILABLE")
    ax_1.plot(sample_times, memory_stats_data["Used"], '-', lw=1, color='b', label="Total MEMORY USED")
    ax_1.set_ylabel('Total available', color='r')
    ax_1.set_xlabel('time (s)')
    plt.tight_layout()
    ax_1.set_ylim(0., max(memory_stats_data["Total"]) + 2000)
    plt.xticks(rotation='vertical')
    fig_1.legend(["TOTAL MEMORY AVAILABLE", "TOTAL MEMORY USED"], loc='upper center')
    ax_1.grid()
    fig_1.savefig("MEMORY.png")
    cmd_1 = "timeout 2s journalctl -p err --since '24 hour ago' > syslog.txt"
    fh_2 = os.system(cmd_1)
    fi_open = open("syslog.txt", "r+")
    out = fi_open.read()
    data_3 = out.split(" ")
    data_uri = base64.b64encode(open('cpu.png', 'rb').read()).decode('utf-8')
    data_uri_1 = base64.b64encode(open('MEMORY.png', 'rb').read()).decode('utf-8')
    data_a = ''.format(
    data_uri) + " " + '
'.format(data_uri_1)
    data_4 = "
" + data_a + "