forked from librenms/librenms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notifications.php
162 lines (150 loc) · 4.58 KB
/
notifications.php
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
<?php
/* Copyright (C) 2015 Daniel Preussker, QuxLabs UG <[email protected]>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>. */
/**
* Notification Poller
*
* @copyright 2015 Daniel Preussker, QuxLabs UG
* @copyright 2017 Tony Murray
* @author Daniel Preussker
* @author Tony Murray <[email protected]>
* @license GPL
*
* @link https://www.librenms.org
*/
/**
* Pull notifications from remotes
*
* @return array Notifications
*/
function get_notifications()
{
$obj = [];
foreach (\LibreNMS\Config::get('notifications') as $name => $url) {
echo '[ ' . date('r') . ' ] ' . $url . ' ';
$feed = json_decode(json_encode(simplexml_load_string(file_get_contents($url))), true);
if (isset($feed['channel'])) {
$feed = parse_rss($feed);
} else {
$feed = parse_atom($feed);
}
array_walk($feed, function (&$items, $key, $url) {
$items['source'] = $url;
}, $url);
$obj = array_merge($obj, $feed);
echo '(' . sizeof($obj) . ')' . PHP_EOL;
}
$obj = array_sort_by_column($obj, 'datetime');
return $obj;
}
/**
* Post notifications to users
*
* @return null
*/
function post_notifications()
{
$notifs = get_notifications();
echo '[ ' . date('r') . ' ] Updating DB ';
foreach ($notifs as $notif) {
if (dbFetchCell('select 1 from notifications where checksum = ?', [$notif['checksum']]) != 1 && dbInsert($notif, 'notifications') > 0) {
echo '.';
}
}
echo ' Done';
echo PHP_EOL;
}
/**
* Parse RSS
*
* @param array $feed RSS Object
* @return array Parsed Object
*/
function parse_rss($feed)
{
$obj = [];
if (! array_key_exists('0', $feed['channel']['item'])) {
$feed['channel']['item'] = [$feed['channel']['item']];
}
foreach ($feed['channel']['item'] as $item) {
$obj[] = [
'title'=>$item['title'],
'body'=>$item['description'],
'checksum'=>hash('sha512', $item['title'] . $item['description']),
'datetime'=>strftime('%F', strtotime($item['pubDate']) ?: time()),
];
}
return $obj;
}
/**
* Parse Atom
*
* @param array $feed Atom Object
* @return array Parsed Object
*/
function parse_atom($feed)
{
$obj = [];
if (! array_key_exists('0', $feed['entry'])) {
$feed['entry'] = [$feed['entry']];
}
foreach ($feed['entry'] as $item) {
$obj[] = [
'title'=>$item['title'],
'body'=>$item['content'],
'checksum'=>hash('sha512', $item['title'] . $item['content']),
'datetime'=>strftime('%F', strtotime($item['updated']) ?: time()),
];
}
return $obj;
}
/**
* Create a new custom notification. Duplicate title+message notifications will not be created.
*
* @param string $title
* @param string $message
* @param int $severity 0=ok, 1=warning, 2=critical
* @param string $source A string describing what created this notification
* @param string $date
* @return bool
*/
function new_notification($title, $message, $severity = 0, $source = 'adhoc', $date = null)
{
$notif = [
'title' => $title,
'body' => $message,
'severity' => $severity,
'source' => $source,
'checksum' => hash('sha512', $title . $message),
'datetime' => strftime('%F', is_null($date) ? time() : strtotime($date)),
];
if (dbFetchCell('SELECT 1 FROM `notifications` WHERE `checksum` = ?', [$notif['checksum']]) != 1) {
return dbInsert($notif, 'notifications') > 0;
}
return false;
}
/**
* Removes all notifications with the given title.
* This should be used with care.
*
* @param string $title
*/
function remove_notification($title)
{
$ids = dbFetchColumn('SELECT `notifications_id` FROM `notifications` WHERE `title`=?', [$title]);
foreach ($ids as $id) {
dbDelete('notifications', '`notifications_id`=?', [$id]);
dbDelete('notifications_attribs', '`notifications_id`=?', [$id]);
}
}