|
| 1 | +--- |
| 2 | +sidebar_position: 2 |
| 3 | +--- |
| 4 | + |
| 5 | +# Push Monitor |
| 6 | + |
| 7 | +Push Monitor is a monitoring method where your application actively sends heartbeat signals to **Tianji** instead of Tianji checking your service. This is particularly useful for monitoring background tasks, cron jobs, or services behind firewalls that cannot be accessed directly. |
| 8 | + |
| 9 | +## How It Works |
| 10 | + |
| 11 | +1. **Tianji** provides you with a unique push endpoint URL |
| 12 | +2. Your application sends HTTP POST requests to this endpoint at regular intervals |
| 13 | +3. If no heartbeat is received within the configured timeout period, Tianji triggers an alert |
| 14 | + |
| 15 | +## Configuration |
| 16 | + |
| 17 | +When creating a Push Monitor, you need to configure: |
| 18 | + |
| 19 | +- **Monitor Name**: A descriptive name for your monitor |
| 20 | +- **Timeout**: The maximum time (in seconds) to wait between heartbeats before considering the service down |
| 21 | +- **Recommended Interval**: How often your application should send heartbeats (typically the same as timeout) |
| 22 | + |
| 23 | +## Push Endpoint Format |
| 24 | + |
| 25 | +``` |
| 26 | +POST https://tianji.example.com/api/push/{pushToken} |
| 27 | +``` |
| 28 | + |
| 29 | +### Status Parameters |
| 30 | + |
| 31 | +- **Normal Status**: Send without parameters or with `?status=up` |
| 32 | +- **Down Status**: Send with `?status=down` to manually trigger an alert |
| 33 | +- **Custom Message**: Add `&msg=your-message` to include additional information |
| 34 | +- **Custom Value**: Add `&value=123` to track numeric values |
| 35 | + |
| 36 | +## Examples |
| 37 | + |
| 38 | +### Basic Heartbeat (cURL) |
| 39 | + |
| 40 | +```bash |
| 41 | +# Send heartbeat every 60 seconds |
| 42 | +while true; do |
| 43 | + curl -X POST "https://tianji.example.com/api/push/<your-push-token>" |
| 44 | + sleep 60 |
| 45 | +done |
| 46 | +``` |
| 47 | + |
| 48 | +### JavaScript/Node.js |
| 49 | + |
| 50 | +```javascript |
| 51 | +// Send heartbeat every 60 seconds |
| 52 | +setInterval(async () => { |
| 53 | + try { |
| 54 | + await fetch('https://tianji.example.com/api/push/<your-push-token>', { |
| 55 | + method: 'POST' |
| 56 | + }); |
| 57 | + console.log('Heartbeat sent successfully'); |
| 58 | + } catch (error) { |
| 59 | + console.error('Failed to send heartbeat:', error); |
| 60 | + } |
| 61 | +}, 60000); |
| 62 | +``` |
| 63 | + |
| 64 | +### Python |
| 65 | + |
| 66 | +```python |
| 67 | +import requests |
| 68 | +import time |
| 69 | + |
| 70 | +def send_heartbeat(): |
| 71 | + try: |
| 72 | + response = requests.post('https://tianji.example.com/api/push/<your-push-token>') |
| 73 | + print('Heartbeat sent successfully') |
| 74 | + except Exception as e: |
| 75 | + print(f'Failed to send heartbeat: {e}') |
| 76 | + |
| 77 | +# Send heartbeat every 60 seconds |
| 78 | +while True: |
| 79 | + send_heartbeat() |
| 80 | + time.sleep(60) |
| 81 | +``` |
| 82 | + |
| 83 | +## Use Cases |
| 84 | + |
| 85 | +### 1. Cron Jobs |
| 86 | + |
| 87 | +Monitor the execution of scheduled tasks: |
| 88 | + |
| 89 | +```bash |
| 90 | +#!/bin/bash |
| 91 | +# your-cron-job.sh |
| 92 | + |
| 93 | +# Your actual job logic here |
| 94 | +./run-backup.sh |
| 95 | + |
| 96 | +# Send success signal |
| 97 | +if [ $? -eq 0 ]; then |
| 98 | + curl -X POST "https://tianji.example.com/api/push/<your-push-token>?status=up&msg=backup-completed" |
| 99 | +else |
| 100 | + curl -X POST "https://tianji.example.com/api/push/<your-push-token>?status=down&msg=backup-failed" |
| 101 | +fi |
| 102 | +``` |
| 103 | + |
| 104 | +### 2. Background Services |
| 105 | + |
| 106 | +Monitor long-running background processes: |
| 107 | + |
| 108 | +```python |
| 109 | +import requests |
| 110 | +import time |
| 111 | +import threading |
| 112 | + |
| 113 | +class ServiceMonitor: |
| 114 | + def __init__(self, push_url): |
| 115 | + self.push_url = push_url |
| 116 | + self.running = True |
| 117 | + |
| 118 | + def start_heartbeat(self): |
| 119 | + def heartbeat_loop(): |
| 120 | + while self.running: |
| 121 | + try: |
| 122 | + requests.post(self.push_url) |
| 123 | + time.sleep(30) # Send every 30 seconds |
| 124 | + except Exception as e: |
| 125 | + print(f"Heartbeat failed: {e}") |
| 126 | + |
| 127 | + thread = threading.Thread(target=heartbeat_loop) |
| 128 | + thread.daemon = True |
| 129 | + thread.start() |
| 130 | + |
| 131 | +# Usage |
| 132 | +monitor = ServiceMonitor('https://tianji.example.com/api/push/<your-push-token>') |
| 133 | +monitor.start_heartbeat() |
| 134 | + |
| 135 | +# Your main service logic here |
| 136 | +while True: |
| 137 | + # Do your work |
| 138 | + time.sleep(1) |
| 139 | +``` |
| 140 | + |
| 141 | +### 3. Database Sync Jobs |
| 142 | + |
| 143 | +Monitor data synchronization tasks: |
| 144 | + |
| 145 | +```python |
| 146 | +import requests |
| 147 | +import schedule |
| 148 | +import time |
| 149 | + |
| 150 | +def sync_data(): |
| 151 | + try: |
| 152 | + # Your data sync logic here |
| 153 | + result = perform_data_sync() |
| 154 | + |
| 155 | + if result.success: |
| 156 | + requests.post( |
| 157 | + 'https://tianji.example.com/api/push/<your-push-token>', |
| 158 | + params={'status': 'up', 'msg': f'synced-{result.records}-records'} |
| 159 | + ) |
| 160 | + else: |
| 161 | + requests.post( |
| 162 | + 'https://tianji.example.com/api/push/<your-push-token>', |
| 163 | + params={'status': 'down', 'msg': 'sync-failed'} |
| 164 | + ) |
| 165 | + except Exception as e: |
| 166 | + requests.post( |
| 167 | + 'https://tianji.example.com/api/push/<your-push-token>', |
| 168 | + params={'status': 'down', 'msg': f'error-{str(e)}'} |
| 169 | + ) |
| 170 | + |
| 171 | +# Schedule to run every hour |
| 172 | +schedule.every().hour.do(sync_data) |
| 173 | + |
| 174 | +while True: |
| 175 | + schedule.run_pending() |
| 176 | + time.sleep(1) |
| 177 | +``` |
| 178 | + |
| 179 | +## Best Practices |
| 180 | + |
| 181 | +1. **Set Appropriate Timeouts**: Configure timeout values based on your application's needs. For frequent tasks, use shorter timeouts. For periodic jobs, use longer timeouts. |
| 182 | + |
| 183 | +2. **Handle Network Failures**: Implement retry logic in your heartbeat code to handle temporary network issues. |
| 184 | + |
| 185 | +3. **Use Meaningful Messages**: Include descriptive messages with your heartbeats to provide context when reviewing logs. |
| 186 | + |
| 187 | +4. **Monitor Critical Paths**: Place heartbeat calls at critical points in your application flow, not just at the beginning. |
| 188 | + |
| 189 | +5. **Exception Handling**: Send a "down" status when an exception occurs in your application. |
| 190 | + |
| 191 | +## Troubleshooting |
| 192 | + |
| 193 | +### Common Issues |
| 194 | + |
| 195 | +**No heartbeats received**: |
| 196 | +- Verify the push token is correct |
| 197 | +- Check network connectivity from your application to Tianji |
| 198 | +- Ensure your application is actually running the heartbeat code |
| 199 | + |
| 200 | +**Frequent false alarms**: |
| 201 | +- Increase the timeout value |
| 202 | +- Check if your application is experiencing performance issues |
| 203 | +- Review network stability between your app and Tianji |
| 204 | + |
| 205 | +**Missing heartbeats**: |
| 206 | +- Add error handling and logging to your heartbeat code |
| 207 | +- Consider implementing retry logic for failed requests |
| 208 | +- Monitor your application's resource usage |
| 209 | + |
| 210 | +## Security Considerations |
| 211 | + |
| 212 | +- Keep your push tokens secure and don't expose them in public repositories |
| 213 | +- Use HTTPS endpoints to encrypt data in transit |
| 214 | +- Consider rotating push tokens periodically |
| 215 | +- Limit the frequency of heartbeats to avoid overwhelming your Tianji instance |
| 216 | + |
| 217 | +Push monitoring provides a reliable way to monitor services that traditional ping-based monitoring cannot reach, making it an essential tool for comprehensive infrastructure monitoring. |
0 commit comments