-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsystem_info.py
More file actions
1181 lines (1004 loc) · 52.3 KB
/
system_info.py
File metadata and controls
1181 lines (1004 loc) · 52.3 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import platform
import psutil
import json
import time
from datetime import datetime
from typing import Dict, Any, Optional
class SystemInfoCollector:
"""Collects system information and generates reports."""
def __init__(self):
self.timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
def get_system_info(self) -> Dict[str, Any]:
"""Collect comprehensive system information."""
return {
"timestamp": self.timestamp,
"system": self._get_system_info(),
"cpu": self._get_cpu_info(),
"memory": self._get_memory_info(),
"disk": self._get_disk_info(),
"network": self._get_network_info(),
"boot_time": self._get_boot_time()
}
def _get_system_info(self) -> Dict[str, str]:
"""Get basic system information."""
return {
"system": platform.system(),
"node_name": platform.node(),
"release": platform.release(),
"version": platform.version(),
"machine": platform.machine(),
"processor": platform.processor(),
"python_version": platform.python_version()
}
def _get_cpu_info(self) -> Dict[str, Any]:
"""Get CPU information."""
return {
"physical_cores": psutil.cpu_count(logical=False),
"total_cores": psutil.cpu_count(logical=True),
"cpu_percent": psutil.cpu_percent(interval=1, percpu=True),
"cpu_freq": {
"current": psutil.cpu_freq().current if hasattr(psutil.cpu_freq(), 'current') else "N/A",
"max": psutil.cpu_freq().max if hasattr(psutil.cpu_freq(), 'max') else "N/A"
}
}
def _get_memory_info(self) -> Dict[str, Any]:
"""Get memory information."""
virtual_mem = psutil.virtual_memory()
swap_mem = psutil.swap_memory()
return {
"total_ram": self._format_bytes(virtual_mem.total),
"available_ram": self._format_bytes(virtual_mem.available),
"used_ram": self._format_bytes(virtual_mem.used),
"ram_percent": virtual_mem.percent,
"total_swap": self._format_bytes(swap_mem.total),
"used_swap": self._format_bytes(swap_mem.used),
"swap_percent": swap_mem.percent
}
def _get_disk_info(self) -> Dict[str, Any]:
"""Get disk/partition information."""
partitions = []
for partition in psutil.disk_partitions():
try:
usage = psutil.disk_usage(partition.mountpoint)
partitions.append({
"device": partition.device,
"mountpoint": partition.mountpoint,
"fstype": partition.fstype,
"total": self._format_bytes(usage.total),
"used": self._format_bytes(usage.used),
"free": self._format_bytes(usage.free),
"percent": usage.percent
})
except Exception:
continue
return {"partitions": partitions}
def _get_network_info(self) -> Dict[str, Any]:
"""Get network interface information."""
interfaces = {}
for interface, addrs in psutil.net_if_addrs().items():
interfaces[interface] = []
for addr in addrs:
interfaces[interface].append({
"family": str(addr.family),
"address": addr.address,
"netmask": addr.netmask if addr.netmask else "N/A",
"broadcast": addr.broadcast if addr.broadcast else "N/A"
})
return {
"interfaces": interfaces,
"io_counters": {
"bytes_sent": self._format_bytes(psutil.net_io_counters().bytes_sent),
"bytes_recv": self._format_bytes(psutil.net_io_counters().bytes_recv),
"packets_sent": psutil.net_io_counters().packets_sent,
"packets_recv": psutil.net_io_counters().packets_recv
}
}
def _get_boot_time(self) -> str:
"""Get system boot time."""
return datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S")
@staticmethod
def _format_bytes(bytes_num: int) -> str:
"""Format bytes to human-readable format."""
for unit in ['B', 'KB', 'MB', 'GB', 'TB']:
if bytes_num < 1024.0:
return f"{bytes_num:.2f} {unit}"
bytes_num /= 1024.0
return f"{bytes_num:.2f} PB"
class ReportGenerator:
"""Generates different types of reports from system information."""
@staticmethod
def generate_text_report(sys_info: Dict[str, Any]) -> str:
"""Generate a human-readable text report."""
report = []
report.append("=" * 50)
report.append(f"SYSTEM INFORMATION REPORT - {sys_info['timestamp']}")
report.append("=" * 50)
# System Information
report.append("\nSYSTEM INFORMATION")
report.append("-" * 30)
for key, value in sys_info['system'].items():
report.append(f"{key.replace('_', ' ').title()}: {value}")
# CPU Information
report.append("\nCPU INFORMATION")
report.append("-" * 30)
cpu = sys_info['cpu']
report.append(f"Physical Cores: {cpu['physical_cores']}")
report.append(f"Total Cores: {cpu['total_cores']}")
report.append(f"CPU Usage: {sum(cpu['cpu_percent'])/len(cpu['cpu_percent']):.1f}%")
report.append(f"CPU Frequency: {cpu['cpu_freq']['current']} MHz")
# Memory Information
report.append("\nMEMORY INFORMATION")
report.append("-" * 30)
mem = sys_info['memory']
report.append(f"Total RAM: {mem['total_ram']}")
report.append(f"Available RAM: {mem['available_ram']}")
report.append(f"Used RAM: {mem['used_ram']} ({mem['ram_percent']}%)")
report.append(f"Total Swap: {mem['total_swap']}")
report.append(f"Used Swap: {mem['used_swap']} ({mem['swap_percent']}%)")
# Disk Information
report.append("\nDISK INFORMATION")
report.append("-" * 30)
for partition in sys_info['disk']['partitions']:
report.append(f"\nDevice: {partition['device']} ({partition['fstype']}) mounted on {partition['mountpoint']}")
report.append(f" Total: {partition['total']}")
report.append(f" Used: {partition['used']} ({partition['percent']}%)")
report.append(f" Free: {partition['free']}")
# Network Information
report.append("\nNETWORK INFORMATION")
report.append("-" * 30)
report.append("Network Interfaces:")
for interface, addrs in sys_info['network']['interfaces'].items():
report.append(f" {interface}:")
for addr in addrs:
if ':' not in addr['address']: # Skip IPv6 for brevity
report.append(f" {addr['family']}: {addr['address']}")
net_io = sys_info['network']['io_counters']
report.append(f"\nNetwork I/O:")
report.append(f" Bytes Sent: {net_io['bytes_sent']}")
report.append(f" Bytes Received: {net_io['bytes_recv']}")
# Boot Time
report.append("\nSYSTEM UPTIME")
report.append("-" * 30)
report.append(f"System Boot Time: {sys_info['boot_time']}")
return "\n".join(report)
@staticmethod
def generate_html_report(sys_info: Dict[str, Any]) -> str:
"""Generate an HTML report."""
# Format disk rows
disk_rows = []
for partition in sys_info['disk']['partitions']:
disk_rows.append(
f"<tr>"
f"<td>{partition['device']}</td>"
f"<td>{partition['mountpoint']}</td>"
f"<td>{partition['fstype']}</td>"
f"<td>{partition['total']}</td>"
f"<td>{partition['used']}</td>"
f"<td>{partition['free']}</td>"
f"<td>{partition['percent']}%</td>"
f"</tr>"
)
# Format network interfaces
network_interfaces = []
for interface, addrs in sys_info['network']['interfaces'].items():
network_interfaces.append(f"<h4>{interface}</h4><ul>")
for addr in addrs:
if ':' not in addr['address']: # Skip IPv6 for brevity
network_interfaces.append(f"<li><strong>{addr['family']}:</strong> {addr['address']}")
if addr['netmask'] != 'N/A':
network_interfaces.append(f" (Netmask: {addr['netmask']})")
if addr['broadcast'] != 'N/A':
network_interfaces.append(f" (Broadcast: {addr['broadcast']})")
network_interfaces.append("</li>")
network_interfaces.append("</ul>")
# Calculate average CPU usage
cpu_percent_avg = sum(sys_info['cpu']['cpu_percent']) / len(sys_info['cpu']['cpu_percent'])
# Format the HTML content
return f"""<!DOCTYPE html>
<html>
<head>
<title>System Information Report</title>
<style>
body {{ font-family: Arial, sans-serif; line-height: 1.6; margin: 0; padding: 20px; }}
.container {{ max-width: 1200px; margin: 0 auto; }}
.header {{ background: #333; color: white; padding: 20px; border-radius: 5px; margin-bottom: 20px; }}
.section {{ margin-bottom: 30px; }}
.section h2 {{ border-bottom: 2px solid #333; padding-bottom: 5px; }}
.grid {{ display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 20px; }}
.card {{ background: #f4f4f4; padding: 15px; border-radius: 5px; }}
.card h3 {{ margin-top: 0; }}
table {{ width: 100%; border-collapse: collapse; margin: 10px 0; }}
th, td {{ padding: 8px; text-align: left; border-bottom: 1px solid #ddd; }}
th {{ background-color: #f2f2f2; }}
.footer {{ margin-top: 30px; text-align: center; font-size: 0.9em; color: #666; }}
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>System Information Report</h1>
<p>Generated on: {sys_info['timestamp']}</p>
</div>
<div class="section">
<h2>System Information</h2>
<div class="grid">
<div class="card">
<h3>System</h3>
<p><strong>OS:</strong> {sys_info['system']['system']} {sys_info['system']['release']}</p>
<p><strong>Version:</strong> {sys_info['system']['version']}</p>
<p><strong>Node Name:</strong> {sys_info['system']['node_name']}</p>
<p><strong>Machine:</strong> {sys_info['system']['machine']}</p>
<p><strong>Processor:</strong> {sys_info['system']['processor']}</p>
<p><strong>Python Version:</strong> {sys_info['system']['python_version']}</p>
</div>
<div class="card">
<h3>CPU</h3>
<p><strong>Physical Cores:</strong> {sys_info['cpu']['physical_cores']}</p>
<p><strong>Total Cores:</strong> {sys_info['cpu']['total_cores']}</p>
<p><strong>CPU Usage:</strong> {cpu_percent_avg:.1f}%</p>
<p><strong>Current Frequency:</strong> {sys_info['cpu']['cpu_freq']['current']} MHz</p>
<p><strong>Max Frequency:</strong> {sys_info['cpu']['cpu_freq']['max']} MHz</p>
</div>
<div class="card">
<h3>Memory</h3>
<p><strong>Total RAM:</strong> {sys_info['memory']['total_ram']}</p>
<p><strong>Available RAM:</strong> {sys_info['memory']['available_ram']} ({sys_info['memory']['ram_percent']}% used)</p>
<p><strong>Total Swap:</strong> {sys_info['memory']['total_swap']}</p>
<p><strong>Used Swap:</strong> {sys_info['memory']['used_swap']} ({sys_info['memory']['swap_percent']}% used)</p>
</div>
</div>
</div>
<div class="section">
<h2>Disk Information</h2>
<table>
<tr>
<th>Device</th>
<th>Mount Point</th>
<th>File System</th>
<th>Total</th>
<th>Used</th>
<th>Free</th>
<th>Use %</th>
</tr>
{''.join(disk_rows)}
</table>
</div>
<div class="section">
<h2>Network Information</h2>
<h3>Network Interfaces</h3>
{''.join(network_interfaces)}
<h3>Network I/O</h3>
<p><strong>Bytes Sent:</strong> {sys_info['network']['io_counters']['bytes_sent']}</p>
<p><strong>Bytes Received:</strong> {sys_info['network']['io_counters']['bytes_recv']}</p>
<p><strong>Packets Sent:</strong> {sys_info['network']['io_counters']['packets_sent']:,}</p>
<p><strong>Packets Received:</strong> {sys_info['network']['io_counters']['packets_recv']:,}</p>
</div>
<div class="section">
<h2>System Uptime</h2>
<p><strong>System Boot Time:</strong> {sys_info['boot_time']}</p>
<p><strong>Report Generated:</strong> {sys_info['timestamp']}</p>
</div>
<div class="footer">
<p>Generated by SystemInfoGenerator | {sys_info['timestamp']}</p>
</div>
</div>
</body>
</html>"""
@staticmethod
def generate_json_report(sys_info: Dict[str, Any]) -> str:
"""Generate a JSON report."""
return json.dumps(sys_info, indent=4)
@staticmethod
def generate_csv_report(sys_info: Dict[str, Any]) -> str:
"""Generate a CSV report."""
import csv
import io
output = io.StringIO()
writer = csv.writer(output)
# Write header
writer.writerow(['Category', 'Metric', 'Value'])
# System Information
writer.writerow(['System', 'OS', f"{sys_info['system']['system']} {sys_info['system']['release']}"])
writer.writerow(['System', 'Node Name', sys_info['system']['node_name']])
writer.writerow(['System', 'Version', sys_info['system']['version']])
writer.writerow(['System', 'Machine', sys_info['system']['machine']])
writer.writerow(['System', 'Processor', sys_info['system']['processor']])
writer.writerow(['System', 'Python Version', sys_info['system']['python_version']])
# CPU Information
writer.writerow(['CPU', 'Physical Cores', sys_info['cpu']['physical_cores']])
writer.writerow(['CPU', 'Total Cores', sys_info['cpu']['total_cores']])
writer.writerow(['CPU', 'Average Usage', f"{sum(sys_info['cpu']['cpu_percent']) / len(sys_info['cpu']['cpu_percent']):.1f}%"])
writer.writerow(['CPU', 'Current Frequency', f"{sys_info['cpu']['cpu_freq']['current']} MHz"])
writer.writerow(['CPU', 'Max Frequency', f"{sys_info['cpu']['cpu_freq']['max']} MHz"])
# Memory Information
writer.writerow(['Memory', 'Total RAM', sys_info['memory']['total_ram']])
writer.writerow(['Memory', 'Available RAM', sys_info['memory']['available_ram']])
writer.writerow(['Memory', 'Used RAM', f"{sys_info['memory']['used_ram']} ({sys_info['memory']['ram_percent']}%)"])
writer.writerow(['Memory', 'Total Swap', sys_info['memory']['total_swap']])
writer.writerow(['Memory', 'Used Swap', f"{sys_info['memory']['used_swap']} ({sys_info['memory']['swap_percent']}%)"])
# Disk Information
for i, partition in enumerate(sys_info['disk']['partitions']):
writer.writerow([
'Disk' if i == 0 else '',
partition['mountpoint'],
f"{partition['used']} / {partition['total']} ({partition['percent']}% used)"
])
# Network Information
for interface, addrs in sys_info['network']['interfaces'].items():
for addr in addrs:
if ':' not in addr['address']: # Skip IPv6 for brevity
writer.writerow(['Network', f"{interface} ({addr['family']})", addr['address']])
# Network I/O
io = sys_info['network']['io_counters']
writer.writerow(['Network', 'Bytes Sent', io['bytes_sent']])
writer.writerow(['Network', 'Bytes Received', io['bytes_recv']])
writer.writerow(['Network', 'Packets Sent', io['packets_sent']])
writer.writerow(['Network', 'Packets Received', io['packets_recv']])
# System Uptime
writer.writerow(['System', 'Boot Time', sys_info['boot_time']])
writer.writerow(['System', 'Report Generated', sys_info['timestamp']])
return output.getvalue()
@staticmethod
def generate_pdf_report(sys_info: Dict[str, Any]) -> bytes:
"""Generate a PDF report."""
from reportlab.lib import colors
from reportlab.lib.pagesizes import letter
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle, Paragraph, Spacer, Image
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib.units import inch
import tempfile
import os
# Create a temporary file for the PDF
with tempfile.NamedTemporaryFile(delete=False, suffix='.pdf') as tmp:
pdf_path = tmp.name
# Create PDF document
doc = SimpleDocTemplate(pdf_path, pagesize=letter)
elements = []
styles = getSampleStyleSheet()
# Title
title_style = ParagraphStyle(
'Title',
parent=styles['Heading1'],
fontSize=18,
spaceAfter=20,
alignment=1 # Center aligned
)
elements.append(Paragraph("System Information Report", title_style))
# Subtitle
subtitle_style = ParagraphStyle(
'Subtitle',
parent=styles['Normal'],
fontSize=10,
spaceAfter=20,
alignment=1 # Center aligned
)
elements.append(Paragraph(f"Generated on: {sys_info['timestamp']}", subtitle_style))
# System Information
elements.append(Paragraph("System Information", styles['Heading2']))
sys_data = [
['OS', f"{sys_info['system']['system']} {sys_info['system']['release']}"],
['Node Name', sys_info['system']['node_name']],
['Version', sys_info['system']['version']],
['Machine', sys_info['system']['machine']],
['Processor', sys_info['system']['processor']],
['Python Version', sys_info['system']['python_version']]
]
sys_table = Table(sys_data, colWidths=[doc.width/3.0]*2)
sys_table.setStyle(TableStyle([
('BACKGROUND', (0, 0), (-1, 0), colors.grey),
('TEXTCOLOR', (0, 0), (-1, 0), colors.whitesmoke),
('ALIGN', (0, 0), (-1, -1), 'LEFT'),
('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'),
('FONTSIZE', (0, 0), (-1, 0), 10),
('BOTTOMPADDING', (0, 0), (-1, 0), 8),
('BACKGROUND', (0, 1), (-1, -1), colors.white),
('GRID', (0, 0), (-1, -1), 1, colors.lightgrey),
('VALIGN', (0, 0), (-1, -1), 'MIDDLE'),
]))
elements.append(sys_table)
elements.append(Spacer(1, 20))
# CPU Information
elements.append(Paragraph("CPU Information", styles['Heading2']))
cpu_percent_avg = sum(sys_info['cpu']['cpu_percent']) / len(sys_info['cpu']['cpu_percent'])
cpu_data = [
['Physical Cores', sys_info['cpu']['physical_cores']],
['Total Cores', sys_info['cpu']['total_cores']],
['Average Usage', f"{cpu_percent_avg:.1f}%"],
['Current Frequency', f"{sys_info['cpu']['cpu_freq']['current']} MHz"],
['Max Frequency', f"{sys_info['cpu']['cpu_freq']['max']} MHz"]
]
cpu_table = Table(cpu_data, colWidths=[doc.width/3.0]*2)
cpu_table.setStyle(TableStyle([
('BACKGROUND', (0, 0), (-1, 0), colors.grey),
('TEXTCOLOR', (0, 0), (-1, 0), colors.whitesmoke),
('ALIGN', (0, 0), (-1, -1), 'LEFT'),
('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'),
('FONTSIZE', (0, 0), (-1, 0), 10),
('BOTTOMPADDING', (0, 0), (-1, 0), 8),
('BACKGROUND', (0, 1), (-1, -1), colors.white),
('GRID', (0, 0), (-1, -1), 1, colors.lightgrey),
('VALIGN', (0, 0), (-1, -1), 'MIDDLE'),
]))
elements.append(cpu_table)
elements.append(Spacer(1, 20))
# Memory Information
elements.append(Paragraph("Memory Information", styles['Heading2']))
mem_data = [
['Total RAM', sys_info['memory']['total_ram']],
['Available RAM', sys_info['memory']['available_ram']],
['Used RAM', f"{sys_info['memory']['used_ram']} ({sys_info['memory']['ram_percent']}%)"],
['Total Swap', sys_info['memory']['total_swap']],
['Used Swap', f"{sys_info['memory']['used_swap']} ({sys_info['memory']['swap_percent']}%)"]
]
mem_table = Table(mem_data, colWidths=[doc.width/3.0]*2)
mem_table.setStyle(TableStyle([
('BACKGROUND', (0, 0), (-1, 0), colors.grey),
('TEXTCOLOR', (0, 0), (-1, 0), colors.whitesmoke),
('ALIGN', (0, 0), (-1, -1), 'LEFT'),
('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'),
('FONTSIZE', (0, 0), (-1, 0), 10),
('BOTTOMPADDING', (0, 0), (-1, 0), 8),
('BACKGROUND', (0, 1), (-1, -1), colors.white),
('GRID', (0, 0), (-1, -1), 1, colors.lightgrey),
('VALIGN', (0, 0), (-1, -1), 'MIDDLE'),
]))
elements.append(mem_table)
elements.append(Spacer(1, 20))
# Disk Information
if sys_info['disk']['partitions']:
elements.append(Paragraph("Disk Information", styles['Heading2']))
disk_headers = ['Mount Point', 'File System', 'Total', 'Used', 'Free', 'Use %']
disk_data = [disk_headers]
for partition in sys_info['disk']['partitions']:
disk_data.append([
partition['mountpoint'],
partition['fstype'],
partition['total'],
partition['used'],
partition['free'],
f"{partition['percent']}%"
])
# Calculate column widths (adjust as needed)
col_widths = [
doc.width * 0.2, # Mount Point
doc.width * 0.15, # File System
doc.width * 0.15, # Total
doc.width * 0.15, # Used
doc.width * 0.15, # Free
doc.width * 0.1, # Use %
]
disk_table = Table(disk_data, colWidths=col_widths)
disk_table.setStyle(TableStyle([
('BACKGROUND', (0, 0), (-1, 0), colors.grey),
('TEXTCOLOR', (0, 0), (-1, 0), colors.whitesmoke),
('ALIGN', (0, 0), (-1, -1), 'LEFT'),
('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'),
('FONTSIZE', (0, 0), (-1, -1), 8),
('BOTTOMPADDING', (0, 0), (-1, 0), 8),
('BACKGROUND', (0, 1), (-1, -1), colors.white),
('GRID', (0, 0), (-1, -1), 1, colors.lightgrey),
('VALIGN', (0, 0), (-1, -1), 'MIDDLE'),
]))
elements.append(disk_table)
elements.append(Spacer(1, 20))
# Network Information
elements.append(Paragraph("Network Information", styles['Heading2']))
# Network Interfaces
elements.append(Paragraph("Network Interfaces", styles['Heading3']))
net_data = [['Interface', 'Family', 'Address', 'Netmask', 'Broadcast']]
for interface, addrs in sys_info['network']['interfaces'].items():
for addr in addrs:
net_data.append([
interface,
addr['family'],
addr['address'],
addr.get('netmask', 'N/A'),
addr.get('broadcast', 'N/A')
])
# Calculate column widths (adjust as needed)
net_col_widths = [
doc.width * 0.15, # Interface
doc.width * 0.1, # Family
doc.width * 0.25, # Address
doc.width * 0.25, # Netmask
doc.width * 0.15, # Broadcast
]
net_table = Table(net_data, colWidths=net_col_widths)
net_table.setStyle(TableStyle([
('BACKGROUND', (0, 0), (-1, 0), colors.grey),
('TEXTCOLOR', (0, 0), (-1, 0), colors.whitesmoke),
('ALIGN', (0, 0), (-1, -1), 'LEFT'),
('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'),
('FONTSIZE', (0, 0), (-1, -1), 8),
('BOTTOMPADDING', (0, 0), (-1, 0), 8),
('BACKGROUND', (0, 1), (-1, -1), colors.white),
('GRID', (0, 0), (-1, -1), 1, colors.lightgrey),
('VALIGN', (0, 0), (-1, -1), 'MIDDLE'),
]))
elements.append(net_table)
elements.append(Spacer(1, 20))
# Network I/O
elements.append(Paragraph("Network I/O Counters", styles['Heading3']))
io_data = [
['Bytes Sent', sys_info['network']['io_counters']['bytes_sent']],
['Bytes Received', sys_info['network']['io_counters']['bytes_recv']],
['Packets Sent', f"{sys_info['network']['io_counters']['packets_sent']:,}"],
['Packets Received', f"{sys_info['network']['io_counters']['packets_recv']:,}"]
]
io_table = Table(io_data, colWidths=[doc.width/3.0]*2)
io_table.setStyle(TableStyle([
('BACKGROUND', (0, 0), (-1, 0), colors.grey),
('TEXTCOLOR', (0, 0), (-1, 0), colors.whitesmoke),
('ALIGN', (0, 0), (-1, -1), 'LEFT'),
('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'),
('FONTSIZE', (0, 0), (-1, 0), 10),
('BOTTOMPADDING', (0, 0), (-1, 0), 8),
('BACKGROUND', (0, 1), (-1, -1), colors.white),
('GRID', (0, 0), (-1, -1), 1, colors.lightgrey),
('VALIGN', (0, 0), (-1, -1), 'MIDDLE'),
]))
elements.append(io_table)
elements.append(Spacer(1, 20))
# System Uptime
elements.append(Paragraph("System Uptime", styles['Heading2']))
uptime_data = [
['Boot Time', sys_info['boot_time']],
['Report Generated', sys_info['timestamp']]
]
uptime_table = Table(uptime_data, colWidths=[doc.width/3.0]*2)
uptime_table.setStyle(TableStyle([
('BACKGROUND', (0, 0), (-1, 0), colors.grey),
('TEXTCOLOR', (0, 0), (-1, 0), colors.whitesmoke),
('ALIGN', (0, 0), (-1, -1), 'LEFT'),
('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'),
('FONTSIZE', (0, 0), (-1, 0), 10),
('BOTTOMPADDING', (0, 0), (-1, 0), 8),
('BACKGROUND', (0, 1), (-1, -1), colors.white),
('GRID', (0, 0), (-1, -1), 1, colors.lightgrey),
('VALIGN', (0, 0), (-1, -1), 'MIDDLE'),
]))
elements.append(uptime_table)
# Footer
elements.append(Spacer(1, 20))
footer_style = ParagraphStyle(
'Footer',
parent=styles['Normal'],
fontSize=8,
spaceBefore=20,
alignment=1 # Center aligned
)
elements.append(Paragraph("Generated by SystemInfoGenerator", footer_style))
# Build the PDF
doc.build(elements)
# Read the generated PDF
with open(pdf_path, 'rb') as f:
pdf_data = f.read()
# Clean up the temporary file
try:
os.unlink(pdf_path)
except:
pass
return pdf_data
@staticmethod
def generate_gui_report(sys_info: Dict[str, Any]):
"""Generate a GUI report using tkinter."""
try:
import tkinter as tk
from tkinter import ttk
from tkinter.scrolledtext import ScrolledText
import webbrowser
class SystemInfoGUI:
def __init__(self, sys_info):
self.sys_info = sys_info
self.root = tk.Tk()
self.root.title("System Information Report")
self.root.geometry("900x700")
self.root.minsize(800, 600)
# Configure style
style = ttk.Style()
style.configure("TNotebook.Tab", padding=[10, 5], font=('Arial', 10, 'bold'))
style.configure("Title.TLabel", font=('Arial', 16, 'bold'))
style.configure("Subtitle.TLabel", font=('Arial', 12, 'bold'))
# Create main container
main_frame = ttk.Frame(self.root, padding="10")
main_frame.pack(fill=tk.BOTH, expand=True)
# Add title
title = ttk.Label(
main_frame,
text="System Information Report",
style="Title.TLabel"
)
title.pack(pady=(0, 10))
# Add timestamp
timestamp = ttk.Label(
main_frame,
text=f"Generated on: {sys_info['timestamp']}"
)
timestamp.pack(pady=(0, 15))
# Create notebook for tabs
notebook = ttk.Notebook(main_frame)
notebook.pack(fill=tk.BOTH, expand=True)
# System Info Tab
self._create_system_tab(notebook)
# CPU Info Tab
self._create_cpu_tab(notebook)
# Memory Info Tab
self._create_memory_tab(notebook)
# Disk Info Tab
self._create_disk_tab(notebook)
# Network Info Tab
self._create_network_tab(notebook)
# About Us Tab
self._create_about_tab(notebook)
# Add export buttons
btn_frame = ttk.Frame(main_frame)
btn_frame.pack(fill=tk.X, pady=(10, 0))
ttk.Button(
btn_frame,
text="Export as Text",
command=lambda: self._export_report('text')
).pack(side=tk.LEFT, padx=2)
ttk.Button(
btn_frame,
text="Export as HTML",
command=lambda: self._export_report('html')
).pack(side=tk.LEFT, padx=2)
ttk.Button(
btn_frame,
text="Export as JSON",
command=lambda: self._export_report('json')
).pack(side=tk.LEFT, padx=2)
ttk.Button(
btn_frame,
text="Export as CSV",
command=lambda: self._export_report('csv')
).pack(side=tk.LEFT, padx=2)
ttk.Button(
btn_frame,
text="Export as PDF",
command=lambda: self._export_report('pdf')
).pack(side=tk.LEFT, padx=2)
# Center the window
self.root.eval('tk::PlaceWindow . center')
def _create_system_tab(self, notebook):
frame = ttk.Frame(notebook, padding=10)
notebook.add(frame, text="System")
# System Info
sys_frame = ttk.LabelFrame(frame, text="System Information", padding=10)
sys_frame.pack(fill=tk.X, pady=5)
sys_info = self.sys_info['system']
self._add_info_row(sys_frame, "OS:", f"{sys_info['system']} {sys_info['release']}")
self._add_info_row(sys_frame, "Version:", sys_info['version'])
self._add_info_row(sys_frame, "Node Name:", sys_info['node_name'])
self._add_info_row(sys_frame, "Machine:", sys_info['machine'])
self._add_info_row(sys_frame, "Processor:", sys_info['processor'])
self._add_info_row(sys_frame, "Python Version:", sys_info['python_version'])
# Boot Time
boot_frame = ttk.LabelFrame(frame, text="System Uptime", padding=10)
boot_frame.pack(fill=tk.X, pady=5)
self._add_info_row(boot_frame, "Boot Time:", self.sys_info['boot_time'])
self._add_info_row(boot_frame, "Report Generated:", self.sys_info['timestamp'])
def _create_cpu_tab(self, notebook):
frame = ttk.Frame(notebook, padding=10)
notebook.add(frame, text="CPU")
cpu = self.sys_info['cpu']
# CPU Info
info_frame = ttk.LabelFrame(frame, text="CPU Information", padding=10)
info_frame.pack(fill=tk.X, pady=5)
self._add_info_row(info_frame, "Physical Cores:", cpu['physical_cores'])
self._add_info_row(info_frame, "Total Cores:", cpu['total_cores'])
self._add_info_row(info_frame, "CPU Usage:", f"{sum(cpu['cpu_percent'])/len(cpu['cpu_percent']):.1f}%")
self._add_info_row(info_frame, "Current Frequency:", f"{cpu['cpu_freq']['current']} MHz")
self._add_info_row(info_frame, "Max Frequency:", f"{cpu['cpu_freq']['max']} MHz")
# CPU Usage per Core
usage_frame = ttk.LabelFrame(frame, text="CPU Usage per Core", padding=10)
usage_frame.pack(fill=tk.BOTH, expand=True, pady=5)
for i, percent in enumerate(cpu['cpu_percent']):
self._add_progress_row(usage_frame, f"Core {i + 1}:", percent)
def _create_memory_tab(self, notebook):
frame = ttk.Frame(notebook, padding=10)
notebook.add(frame, text="Memory")
mem = self.sys_info['memory']
# RAM Info
ram_frame = ttk.LabelFrame(frame, text="RAM", padding=10)
ram_frame.pack(fill=tk.X, pady=5)
self._add_progress_row(ram_frame, "RAM Usage:", mem['ram_percent'],
f"{mem['used_ram']} / {mem['total_ram']}")
# Swap Info
swap_frame = ttk.LabelFrame(frame, text="Swap Memory", padding=10)
swap_frame.pack(fill=tk.X, pady=5)
self._add_progress_row(swap_frame, "Swap Usage:", mem['swap_percent'],
f"{mem['used_swap']} / {mem['total_swap']}")
def _create_disk_tab(self, notebook):
frame = ttk.Frame(notebook, padding=10)
notebook.add(frame, text="Disks")
# Create Treeview for disk info
columns = ('device', 'mountpoint', 'fstype', 'total', 'used', 'free', 'percent')
tree = ttk.Treeview(frame, columns=columns, show='headings', selectmode='browse')
# Define headings
tree.heading('device', text='Device')
tree.heading('mountpoint', text='Mount Point')
tree.heading('fstype', text='File System')
tree.heading('total', text='Total')
tree.heading('used', text='Used')
tree.heading('free', text='Free')
tree.heading('percent', text='Use %')
# Set column widths
tree.column('device', width=100, anchor='w')
tree.column('mountpoint', width=150, anchor='w')
tree.column('fstype', width=80, anchor='w')
tree.column('total', width=100, anchor='e')
tree.column('used', width=100, anchor='e')
tree.column('free', width=100, anchor='e')
tree.column('percent', width=70, anchor='e')
# Add scrollbar
scrollbar = ttk.Scrollbar(frame, orient=tk.VERTICAL, command=tree.yview)
tree.configure(yscroll=scrollbar.set)
# Pack the tree and scrollbar
tree.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
# Add data to the tree
for partition in self.sys_info['disk']['partitions']:
tree.insert('', tk.END, values=(
partition['device'],
partition['mountpoint'],
partition['fstype'],
partition['total'],
partition['used'],
partition['free'],
f"{partition['percent']}%"
))
def _create_network_tab(self, notebook):
frame = ttk.Frame(notebook, padding=10)
notebook.add(frame, text="Network")
# Network Interfaces
if_frame = ttk.LabelFrame(frame, text="Network Interfaces", padding=10)
if_frame.pack(fill=tk.X, pady=5)
for interface, addrs in self.sys_info['network']['interfaces'].items():
if_frame_lbl = ttk.Label(if_frame, text=f"{interface}:", font=('Arial', 10, 'bold'))
if_frame_lbl.pack(anchor='w', pady=(5, 2))
for addr in addrs:
if ':' not in addr['address']: # Skip IPv6 for brevity
addr_str = f"{addr['family']}: {addr['address']}"
if addr['netmask'] != 'N/A':
addr_str += f" (Netmask: {addr['netmask']})"
if addr['broadcast'] != 'N/A':
addr_str += f" (Broadcast: {addr['broadcast']})"
ttk.Label(if_frame, text=addr_str).pack(anchor='w', padx=20)
# Network I/O
io_frame = ttk.LabelFrame(frame, text="Network I/O Counters", padding=10)
io_frame.pack(fill=tk.X, pady=5)
io = self.sys_info['network']['io_counters']
self._add_info_row(io_frame, "Bytes Sent:", io['bytes_sent'])
self._add_info_row(io_frame, "Bytes Received:", io['bytes_recv'])
self._add_info_row(io_frame, "Packets Sent:", f"{io['packets_sent']:,}")
self._add_info_row(io_frame, "Packets Received:", f"{io['packets_recv']:,}")
def _add_info_row(self, parent, label, value):
"""Helper method to add a label and value to a frame."""
frame = ttk.Frame(parent)
frame.pack(fill=tk.X, pady=2)
lbl = ttk.Label(frame, text=label, width=20, anchor='w')
lbl.pack(side=tk.LEFT)
val = ttk.Label(frame, text=value, anchor='w')
val.pack(side=tk.LEFT, fill=tk.X, expand=True)
def _add_progress_row(self, parent, label, percent, text=None):
"""Helper method to add a progress bar row."""
frame = ttk.Frame(parent)
frame.pack(fill=tk.X, pady=2)
lbl = ttk.Label(frame, text=label, width=15, anchor='w')
lbl.pack(side=tk.LEFT)
style = ttk.Style()
style_name = f"{label.replace(' ', '').lower()}.Horizontal.TProgressbar"
style.configure(style_name, thickness=20)
progress = ttk.Progressbar(
frame,
orient=tk.HORIZONTAL,
length=200,
mode='determinate',
style=style_name
)
progress['value'] = percent
progress.pack(side=tk.LEFT, padx=5, fill=tk.X, expand=True)
if text:
ttk.Label(frame, text=text, width=30).pack(side=tk.LEFT, padx=5)
else:
ttk.Label(frame, text=f"{percent:.1f}%").pack(side=tk.LEFT, padx=5)
def _create_about_tab(self, notebook):
"""Create the About Us tab with project information."""
frame = ttk.Frame(notebook, padding=20)
notebook.add(frame, text="About Us")
# Project Title
title_font = ('Arial', 16, 'bold')
header_font = ('Arial', 12, 'bold')
text_font = ('Arial', 10)
ttk.Label(
frame,
text="System Information Tool",
font=title_font,
justify='center'
).pack(pady=(0, 20))
# Project Description
desc_frame = ttk.LabelFrame(frame, text="Project Description", padding=10)
desc_frame.pack(fill='x', pady=5)
description = (
"The System Information Tool is a comprehensive solution developed as part of the System Administration and Maintenance course project for the Finals."
"This powerful application offers real-time system monitoring and detailed hardware information, including CPU and memory usage statistics."
"It also provides in-depth network configuration details and performance metrics, along with thorough disk usage analysis."
"The tool enhances productivity by allowing users to export comprehensive reports in multiple formats, including Text, HTML, JSON, CSV, and PDF, making it an essential utility for system administrators and IT professionals."
)
ttk.Label(
desc_frame,
text=description,
font=text_font,
wraplength=600,
justify='left'
).pack(anchor='w')
# Team Members
team_frame = ttk.LabelFrame(frame, text="Development Team", padding=10)
team_frame.pack(fill='x', pady=5)
team_members = [
"Belza, John Jaylyn I.",
"Constantino, Alvin Jr. B.",
"Sabangan, Ybo T.",
"Santiago, James Aries G.",
"Silvestre, Jesse Lei C."
]
for member in team_members:
ttk.Label(
team_frame,