From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <l.wagner@proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by lists.proxmox.com (Postfix) with ESMTPS id 509A191DA0
 for <pve-devel@lists.proxmox.com>; Mon, 27 Mar 2023 17:20:22 +0200 (CEST)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id 00C27D4F3
 for <pve-devel@lists.proxmox.com>; Mon, 27 Mar 2023 17:19:25 +0200 (CEST)
Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com
 [94.136.29.106])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by firstgate.proxmox.com (Proxmox) with ESMTPS
 for <pve-devel@lists.proxmox.com>; Mon, 27 Mar 2023 17:19:23 +0200 (CEST)
Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1])
 by proxmox-new.maurer-it.com (Proxmox) with ESMTP id BB69846FFE
 for <pve-devel@lists.proxmox.com>; Mon, 27 Mar 2023 17:19:22 +0200 (CEST)
From: Lukas Wagner <l.wagner@proxmox.com>
To: pve-devel@lists.proxmox.com
Date: Mon, 27 Mar 2023 17:18:50 +0200
Message-Id: <20230327151857.495565-12-l.wagner@proxmox.com>
X-Mailer: git-send-email 2.30.2
In-Reply-To: <20230327151857.495565-1-l.wagner@proxmox.com>
References: <20230327151857.495565-1-l.wagner@proxmox.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-SPAM-LEVEL: Spam detection results:  0
 AWL -0.173 Adjusted score from AWL reputation of From: address
 BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
 DMARC_MISSING             0.1 Missing DMARC policy
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
Subject: [pve-devel] [PATCH pve-manager 11/18] add PVE::Notification module
X-BeenThere: pve-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox VE development discussion <pve-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pve-devel/>
List-Post: <mailto:pve-devel@lists.proxmox.com>
List-Help: <mailto:pve-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=subscribe>
X-List-Received-Date: Mon, 27 Mar 2023 15:20:22 -0000

The PVE::Notification is a very thin wrapper around the
Proxmox::RS::Notification module, feeding the configuration from the
new 'notifications.cfg' file into it as a raw string.

Signed-off-by: Lukas Wagner <l.wagner@proxmox.com>
---
 PVE/Makefile        |  1 +
 PVE/Notification.pm | 60 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 61 insertions(+)
 create mode 100644 PVE/Notification.pm

diff --git a/PVE/Makefile b/PVE/Makefile
index 48b85d33..0501b204 100644
--- a/PVE/Makefile
+++ b/PVE/Makefile
@@ -13,6 +13,7 @@ PERLSOURCE = 			\
 	HTTPServer.pm		\
 	Jobs.pm			\
 	NodeConfig.pm		\
+	Notification.pm		\
 	Report.pm		\
 	VZDump.pm
 
diff --git a/PVE/Notification.pm b/PVE/Notification.pm
new file mode 100644
index 00000000..9933c4de
--- /dev/null
+++ b/PVE/Notification.pm
@@ -0,0 +1,60 @@
+package PVE::Notification;
+
+use strict;
+use warnings;
+
+use PVE::Cluster;
+use Proxmox::RS::Notification;
+
+PVE::Cluster::cfs_register_file(
+    'notifications.cfg',
+    \&parse_notification_config,
+    \&write_notification_config,
+);
+
+sub parse_notification_config {
+    my ($filename, $raw) = @_;
+
+    $raw = '' if !defined($raw);
+    my $config = Proxmox::RS::Notification->new($raw);
+
+    return $config;
+}
+
+sub write_notification_config {
+    my ($filename, $config) = @_;
+
+    return $config->write();
+}
+
+sub config {
+    return PVE::Cluster::cfs_read_file('notifications.cfg');
+}
+
+sub send_notification {
+    my ($severity, $title, $message, $properties, $config) = @_;
+    $config = config() if !defined($config);
+    $config->send($severity, $title, $message, $properties);
+}
+
+sub info {
+    my ($title, $message, $config) = @_;
+    send_notification("info", $title, $message, undef, $config);
+}
+
+sub notice {
+    my ($title, $message, $config) = @_;
+    send_notification("notice", $title, $message, undef, $config);
+}
+
+sub warning {
+    my ($title, $message, $config) = @_;
+    send_notification("warning", $title, $message, undef, $config);
+}
+
+sub error {
+    my ($title, $message, $config) = @_;
+    send_notification("error", $title, $message, undef, $config);
+}
+
+1;
-- 
2.30.2