-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
628 lines (523 loc) · 20.1 KB
/
Copy pathapp.py
File metadata and controls
628 lines (523 loc) · 20.1 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
from pathlib import Path
import pandas as pd
import streamlit as st
from utils.inventory_tools import (
IMPORTANT_COLUMNS,
clean_inventory_dataframe,
filter_inventory,
get_assets_by_category,
get_inventory_summary,
validate_columns,
)
from utils.report_generator import dataframe_to_csv, generate_text_report
from utils.validation_tools import (
calculate_documentation_health,
find_duplicate_ips,
find_invalid_ips,
find_missing_documentation,
find_missing_ips,
find_outdated_systems,
find_stale_records,
get_issue_list,
)
BASE_DIR = Path(__file__).parent
SAMPLE_DATA_PATH = BASE_DIR / "sample_data" / "infrastructure_sample.csv"
LOGO_PATH = BASE_DIR / "assets" / "logo.png"
st.set_page_config(
page_title="InfraMapper by mrachcore",
page_icon=str(LOGO_PATH),
layout="wide",
initial_sidebar_state="expanded",
)
def apply_custom_css():
"""Apply a dark professional dashboard style."""
st.markdown(
"""
<style>
:root {
--bg: #07111f;
--panel: #0d1b2d;
--panel-soft: #102238;
--line: rgba(111, 231, 255, 0.18);
--cyan: #27e4ff;
--blue: #3a8dff;
--green: #4ade80;
--amber: #fbbf24;
--red: #fb7185;
--text: #e5f0ff;
--muted: #8aa1bd;
}
.stApp {
background:
radial-gradient(circle at top left, rgba(39, 228, 255, 0.08), transparent 28rem),
linear-gradient(135deg, #050b13 0%, #07111f 52%, #0b1726 100%);
color: var(--text);
}
section[data-testid="stSidebar"] {
background: linear-gradient(180deg, #07111f 0%, #0b1726 100%);
border-right: 1px solid var(--line);
}
section[data-testid="stSidebar"] h1,
section[data-testid="stSidebar"] h2,
section[data-testid="stSidebar"] h3 {
color: var(--text);
}
div[data-testid="stMetric"] {
background: linear-gradient(180deg, rgba(16, 34, 56, 0.96), rgba(9, 21, 36, 0.96));
border: 1px solid var(--line);
border-radius: 12px;
padding: 18px 18px 14px;
box-shadow: 0 12px 35px rgba(0, 0, 0, 0.25);
}
div[data-testid="stMetricValue"] {
color: var(--cyan);
}
.main-title {
font-size: 3rem;
font-weight: 800;
margin-bottom: 0;
letter-spacing: 0;
}
.subtitle {
color: var(--muted);
font-size: 1.05rem;
margin-top: 0;
}
.tagline {
color: var(--cyan);
font-size: 1.05rem;
margin-bottom: 1.5rem;
}
.card {
background: linear-gradient(180deg, rgba(16, 34, 56, 0.96), rgba(10, 23, 39, 0.96));
border: 1px solid var(--line);
border-radius: 12px;
padding: 1.1rem;
height: 100%;
box-shadow: 0 16px 35px rgba(0, 0, 0, 0.22);
}
.info-card-title {
color: var(--text);
font-weight: 700;
font-size: 1.02rem;
margin-bottom: 0.35rem;
}
.info-card-body {
color: var(--muted);
font-size: 0.94rem;
line-height: 1.55;
}
.badge {
display: inline-block;
padding: 0.25rem 0.55rem;
border-radius: 999px;
font-size: 0.78rem;
font-weight: 700;
margin: 0.12rem;
border: 1px solid rgba(255, 255, 255, 0.12);
}
.badge-green { background: rgba(74, 222, 128, 0.14); color: var(--green); }
.badge-amber { background: rgba(251, 191, 36, 0.14); color: var(--amber); }
.badge-red { background: rgba(251, 113, 133, 0.14); color: var(--red); }
.badge-cyan { background: rgba(39, 228, 255, 0.12); color: var(--cyan); }
.terminal-box {
background: #040b13;
border: 1px solid rgba(39, 228, 255, 0.22);
border-radius: 12px;
color: #b7f7ff;
padding: 1rem;
font-family: Consolas, Monaco, monospace;
white-space: pre-wrap;
max-height: 420px;
overflow: auto;
}
.topology-card {
border-left: 4px solid var(--cyan);
background: linear-gradient(90deg, rgba(39, 228, 255, 0.08), rgba(16, 34, 56, 0.65));
border-radius: 10px;
padding: 1rem;
}
.stDataFrame {
border: 1px solid var(--line);
border-radius: 10px;
}
h1, h2, h3 {
color: var(--text);
letter-spacing: 0;
}
</style>
""",
unsafe_allow_html=True,
)
def render_header(title="InfraMapper", subtitle="by mrachcore"):
"""Render consistent page branding."""
logo_col, text_col = st.columns([1, 5])
with logo_col:
if LOGO_PATH.exists():
st.image(str(LOGO_PATH), use_container_width=True)
with text_col:
st.markdown(f"<div class='main-title'>{title}</div>", unsafe_allow_html=True)
st.markdown(f"<div class='subtitle'>{subtitle}</div>", unsafe_allow_html=True)
st.markdown(
"<div class='tagline'>Local infrastructure inventory & documentation dashboard</div>",
unsafe_allow_html=True,
)
def render_info_card(title, body):
"""Render a small dashboard information card."""
st.markdown(
f"""
<div class="card">
<div class="info-card-title">{title}</div>
<div class="info-card-body">{body}</div>
</div>
""",
unsafe_allow_html=True,
)
def render_status_badge(label, color="cyan"):
"""Render a colored status badge."""
st.markdown(
f"<span class='badge badge-{color}'>{label}</span>",
unsafe_allow_html=True,
)
def render_terminal_box(text):
"""Render text output in a terminal-style box."""
st.markdown(f"<div class='terminal-box'>{text}</div>", unsafe_allow_html=True)
def get_inventory():
"""Return the active inventory dataframe from session state."""
return st.session_state.get("inventory_df")
def show_no_inventory_message():
"""Tell users how to start when no CSV has been loaded yet."""
st.info("No inventory loaded yet. Go to Upload Inventory and upload a CSV or load the included sample data.")
def load_sample_inventory():
"""Load the bundled sample CSV into session state."""
sample_df = pd.read_csv(SAMPLE_DATA_PATH)
st.session_state.inventory_df = clean_inventory_dataframe(sample_df)
st.session_state.inventory_source = "Included sample CSV"
def dashboard_page():
render_header()
st.write("A local dashboard for infrastructure inventory, documentation checks and asset review.")
df = get_inventory()
if df is not None and not df.empty:
summary = get_inventory_summary(df)
col1, col2, col3, col4 = st.columns(4)
col1.metric("Total Assets", summary["total_assets"])
col2.metric("Online", summary["online_assets"])
col3.metric("Offline", summary["offline_assets"])
col4.metric("Unknown Status", summary["unknown_status_assets"])
else:
st.warning("Start by uploading an inventory CSV or load the sample data.")
st.subheader("What InfraMapper Helps With")
cards = [
("Inventory Upload", "Load CSV-based asset inventories without databases, cloud services, or live scans."),
("Asset Overview", "Review devices by type, status, environment, and criticality in clean tables."),
("IP Conflict Detection", "Find duplicate, missing, and invalid IP addresses before they cause confusion."),
("Documentation Health", "Spot missing ownership, locations, operating systems, and lifecycle fields."),
("Lifecycle Review", "Identify outdated systems and stale inventory records for follow-up."),
("Report Export", "Generate simple TXT and CSV exports for documentation or portfolio review."),
]
for row_start in range(0, len(cards), 3):
cols = st.columns(3)
for col, (title, body) in zip(cols, cards[row_start:row_start + 3]):
with col:
render_info_card(title, body)
st.subheader("How To Use")
st.markdown(
"""
1. Upload CSV inventory
2. Review assets
3. Check IP conflicts
4. Validate documentation quality
5. Export report
"""
)
def upload_inventory_page():
st.title("Upload Inventory")
st.write("Upload a CSV inventory file or load the included sample data.")
col1, col2 = st.columns([2, 1])
with col1:
uploaded_file = st.file_uploader("Choose CSV file", type=["csv"])
with col2:
st.write("")
st.write("")
if st.button("Load Included Sample CSV", use_container_width=True):
load_sample_inventory()
st.success("Sample inventory loaded.")
if uploaded_file is not None:
try:
uploaded_df = pd.read_csv(uploaded_file)
if uploaded_df.empty:
st.warning("The uploaded CSV is empty. Please add inventory rows and try again.")
else:
st.session_state.inventory_df = clean_inventory_dataframe(uploaded_df)
st.session_state.inventory_source = uploaded_file.name
st.success("Inventory uploaded and cleaned successfully.")
except Exception as error:
st.error(f"Could not read this CSV file. Please check the format and try again. Details: {error}")
df = get_inventory()
if df is None:
show_no_inventory_message()
return
validation = validate_columns(df)
st.subheader("Detected Inventory")
st.metric("Total Rows", len(df))
st.write(f"Source: {st.session_state.get('inventory_source', 'Unknown')}")
st.write("Detected columns:")
st.write(", ".join(validation["existing_columns"]))
if validation["missing_columns"]:
st.warning("Missing recommended columns: " + ", ".join(validation["missing_columns"]))
else:
st.success("All recommended columns are available.")
st.dataframe(df.head(25), use_container_width=True)
def asset_inventory_page():
st.title("Asset Inventory")
df = get_inventory()
if df is None or df.empty:
show_no_inventory_message()
return
summary = get_inventory_summary(df)
col1, col2, col3, col4 = st.columns(4)
col1.metric("Total Assets", summary["total_assets"])
col2.metric("Online Assets", summary["online_assets"])
col3.metric("Offline Assets", summary["offline_assets"])
col4.metric("Unknown Status", summary["unknown_status_assets"])
st.subheader("Filters")
filter_cols = st.columns(4)
filters = {}
for column, col in zip(["device_type", "status", "environment", "criticality"], filter_cols):
options = sorted([value for value in df[column].unique() if value != ""])
filters[column] = col.multiselect(column.replace("_", " ").title(), options)
filtered_df = filter_inventory(df, filters)
st.subheader("Inventory Table")
st.dataframe(filtered_df, use_container_width=True)
st.subheader("Inventory Breakdown")
col1, col2, col3 = st.columns(3)
with col1:
st.write("Assets by Device Type")
st.dataframe(get_assets_by_category(filtered_df, "device_type"), use_container_width=True)
with col2:
st.write("Assets by Environment")
st.dataframe(get_assets_by_category(filtered_df, "environment"), use_container_width=True)
with col3:
st.write("Assets by Criticality")
st.dataframe(get_assets_by_category(filtered_df, "criticality"), use_container_width=True)
def ip_conflict_page():
st.title("IP Conflict Check")
st.write("IP conflicts can cause connectivity issues and should be reviewed.")
df = get_inventory()
if df is None or df.empty:
show_no_inventory_message()
return
duplicate_ips = find_duplicate_ips(df)
invalid_ips = find_invalid_ips(df)
missing_ips = find_missing_ips(df)
col1, col2, col3 = st.columns(3)
col1.metric("Duplicate IP Groups", len(duplicate_ips))
col2.metric("Invalid IP Records", len(invalid_ips))
col3.metric("Missing IP Records", len(missing_ips))
st.subheader("Duplicate IP Addresses")
if duplicate_ips.empty:
st.success("No duplicate IP addresses found.")
else:
st.dataframe(duplicate_ips, use_container_width=True)
st.subheader("Invalid IP Values")
if invalid_ips.empty:
st.success("No invalid IP values found.")
else:
st.dataframe(invalid_ips, use_container_width=True)
st.subheader("Missing IP Addresses")
if missing_ips.empty:
st.success("No missing IP addresses found.")
else:
st.dataframe(missing_ips, use_container_width=True)
def documentation_health_page():
st.title("Documentation Health")
st.write("Find assets with missing or incomplete documentation.")
df = get_inventory()
if df is None or df.empty:
show_no_inventory_message()
return
health = calculate_documentation_health(df)
missing_docs = find_missing_documentation(df)
col1, col2, col3 = st.columns(3)
col1.metric("Completeness", f"{health['completeness_percentage']}%")
col2.metric("Missing Fields", health["total_missing_fields"])
col3.metric("Score", health["score_label"])
score_color = "green"
if health["score_label"] == "Good":
score_color = "cyan"
elif health["score_label"] == "Needs Review":
score_color = "amber"
elif health["score_label"] == "Poor":
score_color = "red"
render_status_badge(health["score_label"], score_color)
col1, col2, col3, col4 = st.columns(4)
col1.metric("Missing Owner", int(df["owner"].apply(lambda value: str(value).strip() == "").sum()))
col2.metric("Missing Location", int(df["location"].apply(lambda value: str(value).strip() == "").sum()))
col3.metric("Missing OS", int(df["os"].apply(lambda value: str(value).strip() == "").sum()))
col4.metric("Missing Criticality", int(df["criticality"].apply(lambda value: str(value).strip() == "").sum()))
st.subheader("Assets With Missing Documentation")
if missing_docs.empty:
st.success("No missing documentation found.")
else:
st.dataframe(missing_docs, use_container_width=True)
def lifecycle_review_page():
st.title("Lifecycle Review")
st.write("Lifecycle review helps identify systems that may require updates, replacement, or documentation review.")
df = get_inventory()
if df is None or df.empty:
show_no_inventory_message()
return
threshold_days = st.slider("Stale inventory threshold in days", 30, 730, 180, 30)
outdated_systems = find_outdated_systems(df)
stale_records = find_stale_records(df, threshold_days)
high_critical_outdated = outdated_systems[
outdated_systems["criticality"].str.lower().isin(["high", "critical"])
]
col1, col2, col3 = st.columns(3)
col1.metric("Outdated Systems", len(outdated_systems))
col2.metric("Stale Records", len(stale_records))
col3.metric("High/Critical Outdated", len(high_critical_outdated))
st.subheader("Outdated Systems")
if outdated_systems.empty:
st.success("No outdated systems found from the built-in example list.")
else:
st.dataframe(outdated_systems, use_container_width=True)
st.subheader("Stale Inventory Records")
if stale_records.empty:
st.success("No stale records found for the selected threshold.")
else:
st.dataframe(stale_records, use_container_width=True)
def reports_page():
st.title("Reports")
df = get_inventory()
if df is None or df.empty:
show_no_inventory_message()
return
threshold_days = st.slider("Report stale record threshold in days", 30, 730, 180, 30)
summary = get_inventory_summary(df)
health = calculate_documentation_health(df)
duplicate_ips = find_duplicate_ips(df)
invalid_ips = find_invalid_ips(df)
missing_ips = find_missing_ips(df)
outdated_systems = find_outdated_systems(df)
stale_records = find_stale_records(df, threshold_days)
issue_list = get_issue_list(df)
critical_assets = df[df["criticality"].str.lower() == "critical"]["hostname"].head(10).tolist()
issues = {
"duplicate_ip_groups": len(duplicate_ips),
"invalid_ip_count": len(invalid_ips),
"missing_ip_count": len(missing_ips),
"documentation_completeness": health["completeness_percentage"],
"documentation_score": health["score_label"],
"outdated_system_count": len(outdated_systems),
"stale_record_count": len(stale_records),
"top_critical_assets": critical_assets,
}
report_text = generate_text_report(summary, issues, threshold_days)
st.subheader("Report Preview")
render_terminal_box(report_text)
col1, col2, col3 = st.columns(3)
col1.download_button(
"Download TXT Report",
data=report_text,
file_name="inframapper_report.txt",
mime="text/plain",
use_container_width=True,
)
col2.download_button(
"Download Cleaned Inventory CSV",
data=dataframe_to_csv(df),
file_name="cleaned_inventory.csv",
mime="text/csv",
use_container_width=True,
)
col3.download_button(
"Download Issue List CSV",
data=dataframe_to_csv(issue_list),
file_name="inframapper_issues.csv",
mime="text/csv",
use_container_width=True,
)
def about_page():
render_header()
st.write(
"InfraMapper is a local infrastructure inventory and documentation dashboard created as part "
"of my learning path during my Ausbildung as Fachinformatiker für Systemintegration."
)
st.subheader("Purpose")
st.markdown(
"""
- Document infrastructure assets
- Review inventory quality
- Detect IP conflicts
- Identify missing documentation
- Review outdated systems
- Generate infrastructure reports
"""
)
st.subheader("Skills")
badges = [
"Python",
"Streamlit",
"pandas",
"CSV Analysis",
"Infrastructure Inventory",
"Asset Management",
"Documentation",
"Reporting",
"Sysadmin Tools",
]
badge_html = "".join([f"<span class='badge badge-cyan'>{badge}</span>" for badge in badges])
st.markdown(badge_html, unsafe_allow_html=True)
st.subheader("Project Link")
st.write("https://github.com/mrachcore/infra-mapper")
st.subheader("Disclaimer")
st.warning(
"This tool does not scan networks or connect to production systems. "
"It only analyzes uploaded CSV inventory files locally."
)
def render_sidebar():
"""Render the sidebar navigation and small project status."""
if LOGO_PATH.exists():
st.sidebar.image(str(LOGO_PATH), use_container_width=True)
st.sidebar.title("InfraMapper")
st.sidebar.caption("by mrachcore")
pages = [
"Dashboard",
"Upload Inventory",
"Asset Inventory",
"IP Conflict Check",
"Documentation Health",
"Lifecycle Review",
"Reports",
"About",
]
selected_page = st.sidebar.radio("Navigation", pages)
st.sidebar.markdown("---")
df = get_inventory()
if df is not None and not df.empty:
st.sidebar.success(f"{len(df)} assets loaded")
else:
st.sidebar.info("No inventory loaded")
return selected_page
def main():
apply_custom_css()
selected_page = render_sidebar()
if selected_page == "Dashboard":
dashboard_page()
elif selected_page == "Upload Inventory":
upload_inventory_page()
elif selected_page == "Asset Inventory":
asset_inventory_page()
elif selected_page == "IP Conflict Check":
ip_conflict_page()
elif selected_page == "Documentation Health":
documentation_health_page()
elif selected_page == "Lifecycle Review":
lifecycle_review_page()
elif selected_page == "Reports":
reports_page()
elif selected_page == "About":
about_page()
if __name__ == "__main__":
main()