detectkit 0.2.4__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- detectkit/__init__.py +17 -0
- detectkit/alerting/__init__.py +13 -0
- detectkit/alerting/channels/__init__.py +21 -0
- detectkit/alerting/channels/base.py +193 -0
- detectkit/alerting/channels/email.py +146 -0
- detectkit/alerting/channels/factory.py +193 -0
- detectkit/alerting/channels/mattermost.py +53 -0
- detectkit/alerting/channels/slack.py +55 -0
- detectkit/alerting/channels/telegram.py +110 -0
- detectkit/alerting/channels/webhook.py +139 -0
- detectkit/alerting/orchestrator.py +369 -0
- detectkit/cli/__init__.py +1 -0
- detectkit/cli/commands/__init__.py +1 -0
- detectkit/cli/commands/init.py +282 -0
- detectkit/cli/commands/run.py +486 -0
- detectkit/cli/commands/test_alert.py +184 -0
- detectkit/cli/main.py +186 -0
- detectkit/config/__init__.py +30 -0
- detectkit/config/metric_config.py +499 -0
- detectkit/config/profile.py +285 -0
- detectkit/config/project_config.py +164 -0
- detectkit/config/validator.py +124 -0
- detectkit/core/__init__.py +6 -0
- detectkit/core/interval.py +132 -0
- detectkit/core/models.py +106 -0
- detectkit/database/__init__.py +27 -0
- detectkit/database/clickhouse_manager.py +393 -0
- detectkit/database/internal_tables.py +724 -0
- detectkit/database/manager.py +324 -0
- detectkit/database/tables.py +138 -0
- detectkit/detectors/__init__.py +6 -0
- detectkit/detectors/base.py +441 -0
- detectkit/detectors/factory.py +138 -0
- detectkit/detectors/statistical/__init__.py +8 -0
- detectkit/detectors/statistical/iqr.py +508 -0
- detectkit/detectors/statistical/mad.py +478 -0
- detectkit/detectors/statistical/manual_bounds.py +206 -0
- detectkit/detectors/statistical/zscore.py +491 -0
- detectkit/loaders/__init__.py +6 -0
- detectkit/loaders/metric_loader.py +470 -0
- detectkit/loaders/query_template.py +164 -0
- detectkit/orchestration/__init__.py +9 -0
- detectkit/orchestration/task_manager.py +746 -0
- detectkit/utils/__init__.py +17 -0
- detectkit/utils/stats.py +196 -0
- detectkit-0.2.4.dist-info/METADATA +237 -0
- detectkit-0.2.4.dist-info/RECORD +51 -0
- detectkit-0.2.4.dist-info/WHEEL +5 -0
- detectkit-0.2.4.dist-info/entry_points.txt +2 -0
- detectkit-0.2.4.dist-info/licenses/LICENSE +21 -0
- detectkit-0.2.4.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Implementation of 'dtk init' command.
|
|
3
|
+
|
|
4
|
+
Creates a new detectkit project with proper structure.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import os
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
|
|
10
|
+
import click
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def run_init(project_name: str, target_dir: str):
|
|
14
|
+
"""
|
|
15
|
+
Initialize a new detectkit project.
|
|
16
|
+
|
|
17
|
+
Args:
|
|
18
|
+
project_name: Name of the project
|
|
19
|
+
target_dir: Directory to create project in
|
|
20
|
+
|
|
21
|
+
Creates:
|
|
22
|
+
project_name/
|
|
23
|
+
├── detectkit_project.yml
|
|
24
|
+
├── profiles.yml
|
|
25
|
+
├── metrics/
|
|
26
|
+
│ └── .gitkeep
|
|
27
|
+
└── sql/
|
|
28
|
+
└── .gitkeep
|
|
29
|
+
"""
|
|
30
|
+
target_path = Path(target_dir) / project_name
|
|
31
|
+
|
|
32
|
+
# Check if project already exists
|
|
33
|
+
if target_path.exists():
|
|
34
|
+
click.echo(
|
|
35
|
+
click.style(
|
|
36
|
+
f"Error: Directory '{target_path}' already exists!",
|
|
37
|
+
fg="red",
|
|
38
|
+
bold=True,
|
|
39
|
+
)
|
|
40
|
+
)
|
|
41
|
+
return
|
|
42
|
+
|
|
43
|
+
# Create project directory
|
|
44
|
+
click.echo(f"Creating detectkit project '{project_name}' in {target_dir}...")
|
|
45
|
+
|
|
46
|
+
target_path.mkdir(parents=True, exist_ok=True)
|
|
47
|
+
|
|
48
|
+
# Create subdirectories
|
|
49
|
+
(target_path / "metrics").mkdir(exist_ok=True)
|
|
50
|
+
(target_path / "sql").mkdir(exist_ok=True)
|
|
51
|
+
|
|
52
|
+
# Create .gitkeep files
|
|
53
|
+
(target_path / "metrics" / ".gitkeep").touch()
|
|
54
|
+
(target_path / "sql" / ".gitkeep").touch()
|
|
55
|
+
|
|
56
|
+
# Create detectkit_project.yml
|
|
57
|
+
project_config = f"""# detectkit project configuration
|
|
58
|
+
name: {project_name}
|
|
59
|
+
version: '1.0'
|
|
60
|
+
|
|
61
|
+
# Paths
|
|
62
|
+
metrics_path: metrics
|
|
63
|
+
sql_path: sql
|
|
64
|
+
|
|
65
|
+
# Default profile to use
|
|
66
|
+
default_profile: dev
|
|
67
|
+
|
|
68
|
+
# Default table names (can be overridden in metrics)
|
|
69
|
+
tables:
|
|
70
|
+
datapoints: _dtk_datapoints
|
|
71
|
+
detections: _dtk_detections
|
|
72
|
+
tasks: _dtk_tasks
|
|
73
|
+
|
|
74
|
+
# Default timeouts (seconds)
|
|
75
|
+
timeouts:
|
|
76
|
+
load: 1800 # 30 minutes
|
|
77
|
+
detect: 3600 # 1 hour
|
|
78
|
+
alert: 300 # 5 minutes
|
|
79
|
+
"""
|
|
80
|
+
|
|
81
|
+
(target_path / "detectkit_project.yml").write_text(project_config)
|
|
82
|
+
|
|
83
|
+
# Create profiles.yml
|
|
84
|
+
profiles_config = """# Database connection profiles
|
|
85
|
+
# Copy this file to ~/.detectkit/profiles.yml for user-level config
|
|
86
|
+
|
|
87
|
+
dev:
|
|
88
|
+
type: clickhouse
|
|
89
|
+
host: localhost
|
|
90
|
+
port: 9000
|
|
91
|
+
database: default
|
|
92
|
+
user: default
|
|
93
|
+
password: ""
|
|
94
|
+
|
|
95
|
+
# ClickHouse specific settings
|
|
96
|
+
settings:
|
|
97
|
+
max_execution_time: 300
|
|
98
|
+
|
|
99
|
+
prod:
|
|
100
|
+
type: clickhouse
|
|
101
|
+
host: "{{ env_var('CLICKHOUSE_HOST') }}"
|
|
102
|
+
port: 9000
|
|
103
|
+
database: monitoring
|
|
104
|
+
user: "{{ env_var('CLICKHOUSE_USER') }}"
|
|
105
|
+
password: "{{ env_var('CLICKHOUSE_PASSWORD') }}"
|
|
106
|
+
|
|
107
|
+
settings:
|
|
108
|
+
max_execution_time: 600
|
|
109
|
+
|
|
110
|
+
# Example PostgreSQL profile
|
|
111
|
+
# postgres_dev:
|
|
112
|
+
# type: postgres
|
|
113
|
+
# host: localhost
|
|
114
|
+
# port: 5432
|
|
115
|
+
# database: monitoring
|
|
116
|
+
# user: postgres
|
|
117
|
+
# password: postgres
|
|
118
|
+
# schema: public
|
|
119
|
+
|
|
120
|
+
# Example MySQL profile
|
|
121
|
+
# mysql_dev:
|
|
122
|
+
# type: mysql
|
|
123
|
+
# host: localhost
|
|
124
|
+
# port: 3306
|
|
125
|
+
# database: monitoring
|
|
126
|
+
# user: root
|
|
127
|
+
# password: root
|
|
128
|
+
|
|
129
|
+
# Alert channels configuration
|
|
130
|
+
alert_channels:
|
|
131
|
+
# Mattermost channel
|
|
132
|
+
mattermost_alerts:
|
|
133
|
+
type: mattermost
|
|
134
|
+
webhook_url: "{{ env_var('MATTERMOST_WEBHOOK_URL') }}"
|
|
135
|
+
username: detectkit
|
|
136
|
+
icon_url: https://example.com/detectkit-icon.png
|
|
137
|
+
|
|
138
|
+
# Slack channel example
|
|
139
|
+
# slack_alerts:
|
|
140
|
+
# type: slack
|
|
141
|
+
# webhook_url: "{{ env_var('SLACK_WEBHOOK_URL') }}"
|
|
142
|
+
# channel: "#alerts"
|
|
143
|
+
# username: detectkit
|
|
144
|
+
|
|
145
|
+
# Generic webhook example
|
|
146
|
+
# webhook_alerts:
|
|
147
|
+
# type: webhook
|
|
148
|
+
# url: "{{ env_var('WEBHOOK_URL') }}"
|
|
149
|
+
# method: POST
|
|
150
|
+
# headers:
|
|
151
|
+
# Authorization: "Bearer {{ env_var('WEBHOOK_TOKEN') }}"
|
|
152
|
+
"""
|
|
153
|
+
|
|
154
|
+
(target_path / "profiles.yml").write_text(profiles_config)
|
|
155
|
+
|
|
156
|
+
# Create example metric
|
|
157
|
+
example_metric = """# Example metric configuration
|
|
158
|
+
name: example_cpu_usage
|
|
159
|
+
description: CPU usage monitoring example
|
|
160
|
+
|
|
161
|
+
# Data source
|
|
162
|
+
query: |
|
|
163
|
+
SELECT
|
|
164
|
+
timestamp,
|
|
165
|
+
cpu_usage as value
|
|
166
|
+
FROM system_metrics
|
|
167
|
+
WHERE metric_name = 'cpu_usage'
|
|
168
|
+
AND timestamp >= {{ from_date }}
|
|
169
|
+
AND timestamp < {{ to_date }}
|
|
170
|
+
ORDER BY timestamp
|
|
171
|
+
|
|
172
|
+
# Or use external SQL file:
|
|
173
|
+
# query_file: sql/cpu_usage.sql
|
|
174
|
+
|
|
175
|
+
# Time interval between datapoints
|
|
176
|
+
interval: 1min
|
|
177
|
+
|
|
178
|
+
# Loading configuration
|
|
179
|
+
loading:
|
|
180
|
+
fill_gaps: true
|
|
181
|
+
max_gap_fill: 10 # Fill up to 10 missing points
|
|
182
|
+
|
|
183
|
+
# Seasonality extraction
|
|
184
|
+
extract_seasonality:
|
|
185
|
+
- minute_of_hour
|
|
186
|
+
- hour_of_day
|
|
187
|
+
- day_of_week
|
|
188
|
+
|
|
189
|
+
# Anomaly detectors
|
|
190
|
+
detectors:
|
|
191
|
+
- type: zscore
|
|
192
|
+
params:
|
|
193
|
+
threshold: 3.0
|
|
194
|
+
window_size: 100
|
|
195
|
+
|
|
196
|
+
- type: mad
|
|
197
|
+
params:
|
|
198
|
+
threshold: 3.0
|
|
199
|
+
window_size: 100
|
|
200
|
+
|
|
201
|
+
# Alerting (optional)
|
|
202
|
+
alerting:
|
|
203
|
+
enabled: true
|
|
204
|
+
|
|
205
|
+
# Alert channel names (defined in profiles.yml)
|
|
206
|
+
channels:
|
|
207
|
+
- mattermost_alerts
|
|
208
|
+
|
|
209
|
+
# Alert conditions
|
|
210
|
+
consecutive_anomalies: 3
|
|
211
|
+
alert_on_missing_data: false
|
|
212
|
+
|
|
213
|
+
# Tags for selection
|
|
214
|
+
tags:
|
|
215
|
+
- critical
|
|
216
|
+
- system
|
|
217
|
+
"""
|
|
218
|
+
|
|
219
|
+
(target_path / "metrics" / "example_cpu_usage.yml").write_text(example_metric)
|
|
220
|
+
|
|
221
|
+
# Create README
|
|
222
|
+
readme = f"""# {project_name}
|
|
223
|
+
|
|
224
|
+
detectkit monitoring project.
|
|
225
|
+
|
|
226
|
+
## Getting Started
|
|
227
|
+
|
|
228
|
+
1. Configure your database connection in `profiles.yml`
|
|
229
|
+
|
|
230
|
+
2. Create metric definitions in `metrics/` directory
|
|
231
|
+
|
|
232
|
+
3. Run metrics:
|
|
233
|
+
```bash
|
|
234
|
+
cd {project_name}
|
|
235
|
+
dtk run --select example_cpu_usage
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
## Project Structure
|
|
239
|
+
|
|
240
|
+
- `detectkit_project.yml` - Project configuration
|
|
241
|
+
- `profiles.yml` - Database connection profiles
|
|
242
|
+
- `metrics/` - Metric definitions (YAML files)
|
|
243
|
+
- `sql/` - SQL query files (optional)
|
|
244
|
+
|
|
245
|
+
## Commands
|
|
246
|
+
|
|
247
|
+
```bash
|
|
248
|
+
# Run single metric
|
|
249
|
+
dtk run --select cpu_usage
|
|
250
|
+
|
|
251
|
+
# Run with specific steps
|
|
252
|
+
dtk run --select cpu_usage --steps load,detect
|
|
253
|
+
|
|
254
|
+
# Run metrics by tag
|
|
255
|
+
dtk run --select tag:critical
|
|
256
|
+
|
|
257
|
+
# Reload data from specific date
|
|
258
|
+
dtk run --select cpu_usage --from 2024-01-01
|
|
259
|
+
|
|
260
|
+
# Full refresh
|
|
261
|
+
dtk run --select cpu_usage --full-refresh
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
## Documentation
|
|
265
|
+
|
|
266
|
+
See https://github.com/alexeiveselov92/detectkit for full documentation.
|
|
267
|
+
"""
|
|
268
|
+
|
|
269
|
+
(target_path / "README.md").write_text(readme)
|
|
270
|
+
|
|
271
|
+
# Success message
|
|
272
|
+
click.echo()
|
|
273
|
+
click.echo(click.style("✓ Project created successfully!", fg="green", bold=True))
|
|
274
|
+
click.echo()
|
|
275
|
+
click.echo("Your new detectkit project is ready!")
|
|
276
|
+
click.echo()
|
|
277
|
+
click.echo("Next steps:")
|
|
278
|
+
click.echo(f" 1. cd {project_name}")
|
|
279
|
+
click.echo(" 2. Configure database connection in profiles.yml")
|
|
280
|
+
click.echo(" 3. Create or edit metric definitions in metrics/")
|
|
281
|
+
click.echo(" 4. Run: dtk run --select example_cpu_usage")
|
|
282
|
+
click.echo()
|