Skip to content

Commit d2249f0

Browse files
committed
Python files
1 parent 315ce24 commit d2249f0

File tree

4 files changed

+241
-0
lines changed

4 files changed

+241
-0
lines changed

Windows_host.py

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import abc
2+
import socket
3+
import psutil
4+
import sys
5+
import json
6+
7+
8+
class HostInfo:
9+
__metaclass__ = abc.ABCMeta
10+
11+
def __init__(self):
12+
self.hostname = None
13+
self.memory = None
14+
self.cpu = None
15+
self.ip = None
16+
self.disk_size = None
17+
18+
@abc.abstractmethod
19+
def get_hardware_info(self):
20+
pass
21+
22+
def update_param(self, hostname, memory, cpu, ip, disk_size):
23+
self.hostname = hostname
24+
self.memory = memory
25+
self.cpu = cpu
26+
self.ip = ip
27+
self.disk_size = disk_size
28+
29+
def display_hardware_info(self):
30+
dict_data = {
31+
"hostname": self.hostname,
32+
"memory": self.memory,
33+
"cpu": self.cpu,
34+
"ip": self.ip,
35+
"disk_size": self.disk_size
36+
}
37+
38+
json_data = json.dumps(dict_data)
39+
print(json_data)
40+
41+
42+
class LinuxHost(HostInfo):
43+
def get_hardware_info(self):
44+
hostname = socket.gethostname()
45+
memory = str(psutil.virtual_memory()[0]) + " GB"
46+
cpu = psutil.cpu_count()
47+
ip = socket.gethostbyname(hostname)
48+
disk_size = str(psutil.disk_usage("/")[0] // (2**30)) + " GB"
49+
self.update_param(hostname, memory, cpu, ip, disk_size)
50+
51+
52+
class WindowsHost(HostInfo):
53+
def get_hardware_info(self):
54+
hostname = socket.gethostname()
55+
memory = str(psutil.virtual_memory()[0]) + " GB"
56+
cpu = psutil.cpu_count()
57+
ip = socket.gethostbyname(hostname)
58+
disk_size = str(psutil.disk_usage("/")[0] // (2 ** 30)) + " GB"
59+
self.update_param(hostname, memory, cpu, ip, disk_size)
60+
61+
62+
is_windows = sys.platform.startswith('win')
63+
if is_windows:
64+
obj = WindowsHost()
65+
obj.get_hardware_info()
66+
obj.display_hardware_info()
67+
68+
69+
is_linux = sys.platform.startswith('lin')
70+
if is_linux:
71+
obj = LinuxHost()
72+
obj.get_hardware_info()
73+
obj.display_hardware_info()
74+

json_text.py

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import json
2+
3+
4+
def get_input():
5+
inp = input("Enter Memory/CPU/Linux/windows: ").upper()
6+
7+
pattern = ['MEMORY', 'CPU', 'LINUX', 'WINDOWS']
8+
9+
if inp not in pattern:
10+
print("Exception: Invalid Choice")
11+
12+
return inp
13+
14+
15+
def get_sys_memory_cpu(inp):
16+
inp = inp.lower()
17+
with open('inventory.json', 'r', encoding='UTF-8') as file:
18+
data = file.read()
19+
data_list=[]
20+
data_dict = json.loads(data)
21+
for i in data_dict["inventory"]:
22+
(data_list.append(i))
23+
24+
25+
26+
27+
28+
29+
30+
31+
32+
def get_win_linux(inp):
33+
inp = inp.lower().capitalize()
34+
with open('inventory.json', 'r', encoding='UTF-8') as file:
35+
data = file.read()
36+
data_dict = json.loads(data)
37+
all_sys = []
38+
for key, val in data_dict.items():
39+
for ind, dict_ele in enumerate(val):
40+
if (ind + 1) % 2 == 0:
41+
win_linux = dict_ele.get('os')
42+
if inp == win_linux:
43+
all_sys.append(dict_ele)
44+
return all_sys
45+
46+
47+
inp = get_input()
48+
49+
if inp in ['MEMORY']:
50+
data_dict = get_sys_memory_cpu(inp)
51+
print(data_dict)
52+
53+
if inp in ['LINUX', 'WINDOWS']:
54+
all_sys = get_win_linux(inp)
55+
print(all_sys)

log_parser_new.py

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import sys
2+
import os
3+
4+
5+
class Parser:
6+
def __init__(self):
7+
self.file_name = None
8+
self.lines = None
9+
self.filter_input = None
10+
self.n_lines_data = None
11+
self.filter_input_modified = []
12+
13+
def read_file_data(self):
14+
with open(self.file_name, 'r', encoding="UTF-8") as file:
15+
data = [next(file) for x in range(self.lines)]
16+
self.n_lines_data = data
17+
18+
def get_input(self):
19+
file_name = input("Enter log file name: ")
20+
if not os.path.exists(file_name):
21+
print("Enter Correct file-name")
22+
sys.exit()
23+
try:
24+
num_lines = int(input("Enter number of lines you want to read: "))
25+
except:
26+
num_lines = 10
27+
filter_input = (input("Types of log messages 'ERROR','DEBUG','INFO','WARNING': ") or "ERROR").split(",")
28+
29+
for i in filter_input:
30+
i = i.upper().replace(" ", "")
31+
if i not in ['ERROR', 'DEBUG', 'INFO', 'WARNING']:
32+
print("invalid input: ", i)
33+
sys.exit()
34+
else:
35+
self.filter_input_modified.append('[' + i + ']')
36+
37+
self.file_name = file_name
38+
self.lines = num_lines
39+
self.filter_input = filter_input
40+
# print(self.filter_input_modified)
41+
42+
def filter_data_display(self):
43+
output_list = []
44+
45+
for i in self.filter_input_modified:
46+
for j in self.n_lines_data:
47+
if i in j.split(" "):
48+
output_list.append(j)
49+
50+
return output_list
51+
52+
53+
obj = Parser()
54+
obj.get_input()
55+
obj.read_file_data()
56+
output_list = obj.filter_data_display()
57+
print(output_list)

os_commands.py

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import subprocess
2+
3+
4+
def get_cmd():
5+
cmd = input("Enter CMD or type 'b' to break: ")
6+
return cmd
7+
8+
9+
def cmd_exec(cmd):
10+
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
11+
(output, err) = p.communicate()
12+
status = 'Success'
13+
error = 'None'
14+
if len(output) == 0:
15+
output = ""
16+
status = "Failed"
17+
error = f"{cmd} is not recognized as an internal or external command,operable program or batch file."
18+
else:
19+
output = str(output)
20+
return output, error, status
21+
22+
23+
res_li = []
24+
25+
while True:
26+
cmd = get_cmd()
27+
if cmd.lower() == 'b':
28+
break
29+
try:
30+
output, error, status = cmd_exec(cmd)
31+
except:
32+
output = ""
33+
status = "Failed"
34+
error = "None"
35+
res_dict = {
36+
cmd: {
37+
"output": output,
38+
"status": status,
39+
"error": error
40+
41+
}
42+
}
43+
44+
res_li.append(res_dict)
45+
key_list = []
46+
value_list = []
47+
for i in res_li:
48+
for k, v in i.items():
49+
if k not in key_list:
50+
key_list.append(k)
51+
value_list.append(v)
52+
dic = {}
53+
for i in range(len(key_list)):
54+
dic[key_list[i]]=value_list[i]
55+
print(dic)

0 commit comments

Comments
 (0)