-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
277 lines (228 loc) · 8.91 KB
/
Copy pathapp.py
File metadata and controls
277 lines (228 loc) · 8.91 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
import streamlit as st
import ollama
from datetime import datetime
# Page config
st.set_page_config(
page_title="DevOps Log Analyzer",
page_icon="🔍",
layout="wide"
)
# Title
st.title("🔍 DevOps Log Analyzer")
st.markdown("*Analyze error logs and stack traces privately using local AI*")
st.markdown("---")
# Sidebar - Privacy info
with st.sidebar:
st.header("🔒 Privacy First")
st.info("""
**Your logs stay 100% local:**
- ✅ Ollama runs on your machine
- ✅ No data sent to external APIs
- ✅ No internet connection needed
- ✅ Safe for production logs
""")
st.markdown("---")
st.header("📊 How it works")
st.markdown("""
1. Paste your error log
2. AI analyzes locally with Ollama
3. Get error type, root cause, fixes
""")
st.markdown("---")
# Check Ollama connection
try:
ollama.list()
st.success("✅ Ollama connected")
except:
st.error("❌ Ollama not running")
st.markdown("Run: `ollama serve`")
# Auto-fill example if selected (must be before tabs to populate text_area)
if 'example_log' in st.session_state:
st.session_state['log_input'] = st.session_state['example_log']
del st.session_state['example_log']
# Main content
tab1, tab2, tab3 = st.tabs(["🔍 Analyze Logs", "📚 Examples", "ℹ️ About"])
with tab1:
st.header("Paste Your Error Log")
# Log input
log_input = st.text_area(
"Paste error logs, stack traces, or error messages:",
height=300,
key="log_input",
placeholder="""Example:
2024-01-15 10:30:45 ERROR [main] database.py:45 - Connection failed
Traceback (most recent call last):
File "database.py", line 45, in connect
conn = psycopg2.connect(dsn)
psycopg2.OperationalError: could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 5432?
"""
)
# Analysis options
col1, col2 = st.columns([3, 1])
with col1:
analysis_type = st.selectbox(
"Analysis Type:",
["Quick Analysis", "Detailed Root Cause", "Solution Suggestions", "All"]
)
with col2:
st.markdown("<br>", unsafe_allow_html=True)
analyze_button = st.button("🔍 Analyze", type="primary", use_container_width=True)
# Analysis
if analyze_button:
if not log_input.strip():
st.warning("⚠️ Please paste a log to analyze")
else:
with st.spinner("🤖 Analyzing log locally with Ollama..."):
try:
# Build prompt based on analysis type
if analysis_type == "Quick Analysis":
prompt = f"""Analyze this error log and provide:
1. Error Type (in 1-2 words)
2. Quick Summary (1 sentence)
3. Severity (Critical/High/Medium/Low)
Log:
{log_input}"""
elif analysis_type == "Detailed Root Cause":
prompt = f"""Analyze this error log and provide detailed root cause analysis:
1. What went wrong?
2. Why did it happen?
3. Which component failed?
4. Timeline of events
Log:
{log_input}"""
elif analysis_type == "Solution Suggestions":
prompt = f"""Analyze this error and provide solutions:
1. Immediate fixes to try
2. Configuration changes needed
3. Code changes (if applicable)
4. Prevention strategies
Log:
{log_input}"""
else: # All
prompt = f"""Analyze this error log comprehensively:
1. **Error Type & Severity**
2. **Root Cause Analysis**
3. **Immediate Fix Suggestions**
4. **Long-term Prevention**
5. **Related Documentation**
Log:
{log_input}"""
# Call Ollama
response = ollama.chat(
model='llama3.2',
messages=[{
'role': 'system',
'content': 'You are an expert DevOps engineer analyzing error logs. Provide clear, actionable insights.'
}, {
'role': 'user',
'content': prompt
}]
)
# Display results
st.markdown("---")
st.markdown("### 📋 Analysis Results")
st.markdown(response['message']['content'])
# Add timestamp
st.caption(f"Analysis completed at {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
# Download option
analysis_text = f"""# Log Analysis Report
Generated: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}
## Original Log:
{log_input}
## Analysis:
{response['message']['content']}
"""
st.download_button(
label="📥 Download Analysis",
data=analysis_text,
file_name=f"log_analysis_{datetime.now().strftime('%Y%m%d_%H%M%S')}.txt",
mime="text/plain"
)
except Exception as e:
st.error(f"❌ Error: {str(e)}")
st.info("Make sure Ollama is running: `ollama serve`")
with tab2:
st.header("📚 Example Logs to Try")
examples = {
"Database Connection Error": """2024-01-15 10:30:45 ERROR [main] database.py:45 - Connection failed
Traceback (most recent call last):
File "database.py", line 45, in connect
conn = psycopg2.connect(dsn)
psycopg2.OperationalError: could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?""",
"Kubernetes Pod CrashLoop": """2024-01-15 14:22:33 ERROR Pod web-app-7d8c9f-xyz in namespace production is in CrashLoopBackOff
Events:
Back-off restarting failed container
Error: failed to start container "web-app": Error response from daemon:
OCI runtime create failed: container_linux.go:380: starting container process caused:
exec: "npm": executable file not found in $PATH""",
"Python Memory Error": """2024-01-15 16:45:12 CRITICAL [worker-1] app.py:234 - Memory allocation failed
Traceback (most recent call last):
File "app.py", line 234, in process_data
result = pd.read_csv(large_file, chunksize=None)
File "pandas/io/parsers/readers.py", line 912, in read_csv
return _read(filepath_or_buffer, kwds)
MemoryError: Unable to allocate 8.5 GiB for an array with shape (1000000000, 12) and data type float64""",
"Docker Build Failure": """Step 5/12 : RUN npm install
---> Running in abc123def456
npm ERR! code ENOTFOUND
npm ERR! errno ENOTFOUND
npm ERR! network request to https://registry.npmjs.org/express failed, reason: getaddrinfo ENOTFOUND registry.npmjs.org
npm ERR! network This is a problem related to network connectivity.
The command '/bin/sh -c npm install' returned a non-zero code: 1"""
}
for title, example in examples.items():
with st.expander(f"📝 {title}"):
st.code(example, language="log")
if st.button(f"Try this example", key=title):
st.session_state['example_log'] = example
st.rerun()
with tab3:
st.header("ℹ️ About DevOps Log Analyzer")
col1, col2 = st.columns(2)
with col1:
st.markdown("""
### 🎯 Purpose
This tool helps DevOps engineers analyze error logs and stack traces
**privately** using a local AI model (Ollama).
### 🔒 Privacy Benefits
- **No external API calls** - Everything runs locally
- **Safe for production logs** - Your sensitive logs never leave your machine
- **Compliance friendly** - No data sent to ChatGPT/Claude
- **Works offline** - No internet required after setup
### 🛠️ Use Cases
- Analyze production error logs
- Debug stack traces
- Understand Kubernetes events
- Investigate container failures
- Parse complex error messages
""")
with col2:
st.markdown("""
### 🚀 Tech Stack
- **Streamlit** - Web UI framework
- **Ollama** - Local LLM runtime
- **llama3.2** - AI model (3B parameters)
### 📦 Installation
```bash
# Install Ollama
curl -fsSL https://ollama.com/install.sh | sh
# Pull llama3.2 model
ollama pull llama3.2
# Start Ollama server
ollama serve
# Install Python dependencies
pip install streamlit ollama
# Run this app
streamlit run app.py
```
### 🤝 Contributing
This is a demo project for learning LLMOps!
""")
st.markdown("---")
st.info("💡 **Pro Tip**: You can analyze multiple logs in sequence. Each analysis is independent and private.")
# Footer
st.markdown("---")
st.markdown("*🔒 100% Local AI - Your logs never leave your machine | Built with Streamlit + Ollama*")