002-Remote-Logging-Funnel/examples.md
2025-06-06 11:10:04 +02:00

191 lines
No EOL
5.5 KiB
Markdown

# Simple Data Collector API - Usage Examples
## Starting the Server
### Using Virtual Environment (Recommended)
```bash
# Create virtual environment (if not already created)
python -m venv venv
# Activate virtual environment
# On Linux/Mac:
source venv/bin/activate
# On Windows:
# venv\Scripts\activate
# Install dependencies
pip install -r requirements.txt
# Run the server
python main.py
```
The server will start on `http://localhost:8000`
## Simple Data Collection
### Basic Usage - curl (with Bearer Authentication)
```bash
# Send simple comma-separated data to run1 (requires input token)
curl -X POST http://localhost:8000/api/run1/ \
-H "Authorization: Bearer input_token_123" \
-d "host_a,is ok"
# Send data to different runs
curl -X POST http://localhost:8000/api/run1/ \
-H "Authorization: Bearer input_token_123" \
-d "host_b,is ok"
curl -X POST http://localhost:8000/api/run1/ \
-H "Authorization: Bearer input_token_123" \
-d "host_c,failed"
curl -X POST http://localhost:8000/api/run2/ \
-H "Authorization: Bearer input_token_123" \
-d "server_x,healthy"
```
### PowerShell Examples (with Bearer Authentication)
```powershell
# Send OK status from PowerShell (requires input token)
$hostname = $env:COMPUTERNAME
$status = "is ok"
$headers = @{ "Authorization" = "Bearer input_token_123" }
Invoke-RestMethod -Uri "http://your-server:8000/api/run1/" -Method Post -Body "$hostname,$status" -Headers $headers
# Send error status
$hostname = $env:COMPUTERNAME
$status = "failed - disk full"
$headers = @{ "Authorization" = "Bearer input_token_123" }
Invoke-RestMethod -Uri "http://your-server:8000/api/run1/" -Method Post -Body "$hostname,$status" -Headers $headers
```
### wget Examples (with Bearer Authentication)
```bash
# Send data using wget (requires input token)
wget --post-data="host_d,is ok" \
--header="Authorization: Bearer input_token_123" \
http://localhost:8000/api/run1/
wget --post-data="host_e,network error" \
--header="Authorization: Bearer input_token_123" \
http://localhost:8000/api/run1/
```
### Multiple Endpoints (with Bearer Authentication)
```bash
# Different runs create separate directories and files (requires input token)
curl -X POST http://localhost:8000/api/daily-check/ \
-H "Authorization: Bearer input_token_123" \
-d "server1,ok"
curl -X POST http://localhost:8000/api/backup-status/ \
-H "Authorization: Bearer input_token_123" \
-d "server1,completed"
curl -X POST http://localhost:8000/api/security-scan/ \
-H "Authorization: Bearer input_token_123" \
-d "server1,clean"
```
## Retrieving Results
### View all results for a run (with Bearer Authentication)
```bash
# Get all results for run1 (requires read token)
curl -H "Authorization: Bearer read_token_456" \
http://localhost:8000/results/run1/
# Get results for other runs
curl -H "Authorization: Bearer read_token_456" \
http://localhost:8000/results/daily-check/
curl -H "Authorization: Bearer read_token_456" \
http://localhost:8000/results/backup-status/
```
### PowerShell - Get results (with Bearer Authentication)
```powershell
# Get results from PowerShell (requires read token)
$headers = @{ "Authorization" = "Bearer read_token_456" }
Invoke-RestMethod -Uri "http://your-server:8000/results/run1/" -Method Get -Headers $headers
```
## File Output
Data is saved to text files with timestamps:
### Directory Structure
```
input/
├── run1/
│ └── results.txt
├── daily-check/
│ └── results.txt
└── backup-status/
└── results.txt
```
### Example File Content (input/run1/results.txt)
```
2025-01-06 10:30:15 - host_a,is ok
2025-01-06 10:30:22 - host_b,is ok
2025-01-06 10:30:45 - host_c,failed
2025-01-06 10:31:02 - host_d,is ok
```
## Real-World Intune PowerShell Example (with Bearer Authentication)
```powershell
# Place this in your Intune PowerShell script
try {
# Your script logic here
Write-Host "Script executed successfully"
# Send success status (requires input token)
$hostname = $env:COMPUTERNAME
$headers = @{ "Authorization" = "Bearer input_token_123" }
Invoke-RestMethod -Uri "http://your-api-server:8000/api/intune-deployment/" -Method Post -Body "$hostname,success" -Headers $headers
} catch {
# Send failure status with error
$hostname = $env:COMPUTERNAME
$error = $_.Exception.Message
$headers = @{ "Authorization" = "Bearer input_token_123" }
Invoke-RestMethod -Uri "http://your-api-server:8000/api/intune-deployment/" -Method Post -Body "$hostname,failed - $error" -Headers $headers
}
```
## Health Check
```bash
curl http://localhost:8000/health
```
## API Documentation
Visit `http://localhost:8000/docs` for interactive API documentation.
## Authentication Tokens
### Required Tokens
- **Input Token** (for POST operations): `input_token_123`
- **Read Token** (for GET operations): `read_token_456`
### Security Notes
- POST endpoints require the `input_token_123` bearer token
- GET endpoints require the `read_token_456` bearer token
- Change these tokens in `main.py` lines 22-23 for production use
## Key Features
- **Simple Input**: Just send plain text data (e.g., "host_a,is ok")
- **Bearer Authentication**: Separate tokens for input and read operations
- **Automatic Directories**: Each run name creates its own directory
- **Timestamped Entries**: Every entry gets a timestamp
- **Multiple Runs**: Support multiple concurrent data collection runs
- **Easy Retrieval**: Get all results via REST API
- **Plain Text Output**: Results saved as simple text files