-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtristanbailey_examine_system.py
152 lines (115 loc) · 3.95 KB
/
tristanbailey_examine_system.py
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#Author: Tristan Bailey
#Date Created: 2/27/2022
#Last Modified: 2/27/2022
#Class: CS446
#Assignment: PA 1
#File: Python3 program for Part 1
#importing of allowed libraries
#check if this library is ok
from datetime import datetime
import os
import sys
import subprocess
def cpu_model():
try:
cpu_file = open("/proc/cpuinfo", 'r')
cpu_type = ""
cpu_model = ""
lines = cpu_file.read()
lines = lines.split("\n")
cpu_type = lines[1].split(":")
cpu_model = lines[4].split(":")
cpu_file.close()
return([cpu_type[1], cpu_model[1]])
except:
print("Failed to open cpuinfo file")
return("")
def kernal_version():
try:
version_file = open("/proc/version", 'r')
temp = version_file.read()
version_file.close()
version_info = temp.split(" ")
return(version_info[0] + " " + version_info[1] + " " + version_info[2])
except:
print("Failed to open version file")
return("")
#returns an inte representing the seconds since the system was last booted
def sys_time_since_boot():
try:
uptime_file = open("/proc/uptime", 'r')
temp = uptime_file.read()
uptime_file.close()
#gets the first number in uptime file which is the time since last boot
#which is in seconds
times = temp.split(" ")
return(float(times[0]))
except:
print("Failed to open uptime file")
return(-1)
def last_boot_time():
try:
stat_file = open("/proc/stat", 'r')
lines = stat_file.read()
stat_file.close()
lines = lines.replace("\n", " ")
lines = lines.split(" ")
b_time = 0
for i in range(len(lines)):
if(lines[i] == "btime"):
b_time = int(lines[i + 1])
#convert from unix epoch to regular date format
boot_date = datetime.fromtimestamp(b_time)
return(boot_date)
except:
print("Failed to open stat file")
return(-1)
def num_proc_run():
try:
stat_file = open("/proc/stat", 'r')
lines = stat_file.read()
stat_file.close()
lines = lines.replace("\n", " ")
lines = lines.split(" ")
processes = 0
for i in range(len(lines)):
if(lines[i] == "processes"):
processes = int(lines[i + 1])
#convert from unix epoch to regular date format
return(processes)
except:
print("Failed to open stat file")
return(-1)
def num_disk_reqs():
try:
#open, read, and close diskstats file
disk_file = open("/proc/diskstats", 'r')
lines = disk_file.readlines()
disk_file.close()
total = 0
#go through each line from the diskstats file
for line in lines:
tokenize = line.split(" ")
#removes all empty strings from the tokenized list
strings = []
for string in tokenize:
if (string != ""):
strings.append(string)
total = total + int(strings[3]) + int(strings[7])
#return the totla number of disk read and writes
return total
except:
print("Failed to open diskstat file")
try:
my_file = open("tristanbailey_systemDetails.txt", "w")
cpu_info = cpu_model()
my_file.write("CPU type: " + cpu_info[0] + "\t")
my_file.write("CPU Model: " + cpu_info[1] + "\n")
my_file.write("Kernal Version: " + kernal_version() + "\n")
my_file.write("Time Since Last Boot: " + str(sys_time_since_boot()) + " seconds" + "\n")
my_file.write("Booted: " + str(last_boot_time()) + "\n")
my_file.write("Disk Requests Made(successful writes + reads): " + str(num_disk_reqs()) + "\n")
my_file.write("Processes Created Since Last Boot: " + str(num_proc_run()) + "\n")
my_file.close()
except:
print("Could not create/write to tristanbailey_systemDetails.txt")