Skip to content

Commit decad6b

Browse files
committed
Initial upload
1 parent 56976eb commit decad6b

File tree

3 files changed

+146
-2
lines changed

3 files changed

+146
-2
lines changed

README.md

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,68 @@
1-
# registry-ntfy
2-
ntfy.sh Error Notifier for Namingo Registry
1+
# ntfy.sh Error Notifier for Namingo Registry
2+
3+
This script checks for new high-severity errors from Namingo Registry and sends real-time push notifications via [ntfy.sh](https://ntfy.sh).
4+
5+
## ⚙️ Setup Steps
6+
7+
### 1. 📥 Download
8+
9+
Clone or copy the files to your server:
10+
11+
```bash
12+
sudo mkdir -p /opt/registry/ntfy
13+
cd /opt/registry/ntfy
14+
# Add ntfy_error_notifier.php and ntfy_config.php here
15+
```
16+
17+
### 2. 🛠 Configure
18+
19+
Edit `ntfy_config.php` with your database credentials and ntfy topic name:
20+
21+
```php
22+
return [
23+
'db' => [
24+
'host' => 'localhost',
25+
'name' => 'registry',
26+
'user' => 'youruser',
27+
'pass' => 'yourpass',
28+
'charset' => 'utf8mb4'
29+
],
30+
'ntfy_topic' => 'https://ntfy.sh/my-channel', // Replace with your topic
31+
'min_level' => 400,
32+
'last_id_file' => __DIR__ . '/.last_id'
33+
];
34+
```
35+
36+
### 3. 🔔 Create an ntfy.sh Topic
37+
38+
- Visit [https://ntfy.sh](https://ntfy.sh)
39+
- Choose a topic name (e.g., `my-channel`)
40+
- Subscribe to it using the ntfy app (Android/iOS) or web
41+
42+
No registration required, but for private topics, consider [authentication options](https://docs.ntfy.sh/publish/#authentication).
43+
44+
### 4. 🧪 Test Run
45+
46+
Run manually to ensure everything works:
47+
48+
```bash
49+
php /opt/registry/ntfy/ntfy_error_notifier.php
50+
```
51+
52+
### 5. 🕒 Add to Cron
53+
54+
Open your crontab:
55+
56+
```bash
57+
crontab -e
58+
```
59+
60+
Add the following line to check for errors every minute:
61+
62+
```bash
63+
* * * * * /usr/bin/php /opt/registry/ntfy/ntfy_error_notifier.php > /dev/null 2>&1
64+
```
65+
66+
## 📄 License
67+
68+
MIT License

ntfy_config.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
return [
3+
'db' => [
4+
'host' => 'localhost',
5+
'name' => 'registry',
6+
'user' => 'youruser',
7+
'pass' => 'yourpass',
8+
'charset' => 'utf8mb4'
9+
],
10+
'ntfy_topic' => 'https://ntfy.sh/my-channel', // Your ntfy.sh topic URL
11+
'min_level' => 400, // Minimum level to report (400=ERROR, 500=CRITICAL)
12+
'last_id_file' => __DIR__ . '/.last_id'
13+
];

ntfy_error_notifier.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
$config = require __DIR__ . '/ntfy_config.php';
4+
5+
try {
6+
// Setup DB
7+
$dsn = "mysql:host={$config['db']['host']};dbname={$config['db']['name']};charset={$config['db']['charset']}";
8+
$pdo = new PDO($dsn, $config['db']['user'], $config['db']['pass'], [
9+
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
10+
]);
11+
12+
// Track last ID
13+
$lastIdFile = $config['last_id_file'];
14+
$lastId = file_exists($lastIdFile) ? (int)file_get_contents($lastIdFile) : 0;
15+
16+
// Fetch new severe logs
17+
$stmt = $pdo->prepare("
18+
SELECT id, level, level_name, message, created_at
19+
FROM error_log
20+
WHERE id > :last_id AND level >= :min_level
21+
ORDER BY id ASC
22+
");
23+
$stmt->execute([
24+
':last_id' => $lastId,
25+
':min_level' => $config['min_level']
26+
]);
27+
28+
$logs = $stmt->fetchAll(PDO::FETCH_ASSOC);
29+
if (!$logs) {
30+
exit(0); // Nothing new
31+
}
32+
33+
foreach ($logs as $log) {
34+
$text = "[{$log['created_at']}] [{$log['level_name']}] {$log['message']}";
35+
sendNtfy($config['ntfy_topic'], $text, $log['level']);
36+
$lastId = $log['id'];
37+
}
38+
39+
// Update last ID
40+
file_put_contents($lastIdFile, $lastId);
41+
} catch (Throwable $e) {
42+
// Log to system log or email in real production use
43+
error_log("ntfy_error_notifier failed: " . $e->getMessage());
44+
exit(1);
45+
}
46+
47+
// ntfy.sh notifier
48+
function sendNtfy(string $topic, string $message, int $level): void {
49+
$priority = $level >= 500 ? 'urgent' : ($level >= 400 ? 'high' : 'default');
50+
$title = $level >= 500 ? '🚨 Critical Error' : '⚠️ Error';
51+
52+
$ch = curl_init($topic);
53+
curl_setopt_array($ch, [
54+
CURLOPT_POST => true,
55+
CURLOPT_POSTFIELDS => $message,
56+
CURLOPT_HTTPHEADER => [
57+
"Title: $title",
58+
"Priority: $priority"
59+
],
60+
CURLOPT_RETURNTRANSFER => true,
61+
CURLOPT_TIMEOUT => 5
62+
]);
63+
curl_exec($ch);
64+
curl_close($ch);
65+
}

0 commit comments

Comments
 (0)