Analyzing System Uptime and Idle Time with Python and Matplotlib
import pandas as pd
import matplotlib.pyplot as plt
from datetime import datetime
import math

# read data from the CSV file
data = pd.read_csv('log_file.txt', delimiter=',')

# get all start time values
start_times = list(data['Start_time'])
# get all end time values
end_times = list(data['End_time'])

time_format = "%Y-%m-%d %H:%M:%S"

active_s = []
idle_s = []

times_num = len(start_times)
for s, e in zip(start_times, end_times):
    # convert time stamp strings to datetime objects
    start = datetime.strptime(s, time_format)
    end = datetime.strptime(e, time_format)
    duration = end - start
    duration_s = duration.seconds 
    active_s.append(1) # mark as active
    idle_s.append(0) # mark as idle
    index = start_times.index(s)
    if index != times_num - 1:
        next_index = index + 1
        next_start = datetime.strptime(start_times[next_index], time_format)
        gap = next_start - end
        gap_s = gap.seconds  
        if gap_s <= 80:
            active_s.append(1) # mark as active
            idle_s.append(0) # mark as idle
        else:
            active_s.append(0) # mark as idle
            idle_s.append(1) # mark as active

# invert arrays to get idle time intervals
idle_s = list(map(lambda x: int(not x), idle_s))

width = 0.8  # bar width in charts

fig, ax = plt.subplots()
ax.bar(range(len(active_s)), active_s, width, color='green')
ax.bar(range(len(idle_s)), idle_s, width, color='blue')

# hide axes
plt.axis('off')
plt.show()

Last modified on 2025-03-09