From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 A69AC9B19D for ; Wed, 24 May 2023 15:58:05 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 333AD1F7DF for ; Wed, 24 May 2023 15:57:40 +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 ; Wed, 24 May 2023 15:57:37 +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 77D3846E31 for ; Wed, 24 May 2023 15:57:33 +0200 (CEST) From: Lukas Wagner To: pve-devel@lists.proxmox.com Date: Wed, 24 May 2023 15:56:35 +0200 Message-Id: <20230524135649.934881-29-l.wagner@proxmox.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230524135649.934881-1-l.wagner@proxmox.com> References: <20230524135649.934881-1-l.wagner@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL -0.159 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 T_SCC_BODY_TEXT_LINE -0.01 - Subject: [pve-devel] [PATCH v2 pve-manager 28/42] add PVE::Notify module X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 24 May 2023 13:58:05 -0000 The PVE::Notify module is a very thin wrapper around the Proxmox::RS::Notify module, feeding the configuration from the new 'notifications.cfg' and 'priv/notifications.cfg' files into it. Also, PVE::Notify contains some useful helpers. Signed-off-by: Lukas Wagner --- PVE/Makefile | 1 + PVE/Notify.pm | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 PVE/Notify.pm diff --git a/PVE/Makefile b/PVE/Makefile index 48b85d33..d6317dde 100644 --- a/PVE/Makefile +++ b/PVE/Makefile @@ -13,6 +13,7 @@ PERLSOURCE = \ HTTPServer.pm \ Jobs.pm \ NodeConfig.pm \ + Notify.pm \ Report.pm \ VZDump.pm diff --git a/PVE/Notify.pm b/PVE/Notify.pm new file mode 100644 index 00000000..01609b83 --- /dev/null +++ b/PVE/Notify.pm @@ -0,0 +1,84 @@ +package PVE::Notify; + +use strict; +use warnings; + +use PVE::Cluster; +use PVE::RS::Notify; + +PVE::Cluster::cfs_register_file( + 'notifications.cfg', + \&parse_notification_config, + \&write_notification_config, +); + +PVE::Cluster::cfs_register_file( + 'priv/notifications.cfg', + \&parse_notification_config, + \&write_notification_config, +); + +sub parse_notification_config { + my ($filename, $raw) = @_; + + $raw = '' if !defined($raw); + return $raw; +} + +sub write_notification_config { + my ($filename, $config) = @_; + return $config; +} + +sub lock_config { + my ($code, $timeout) = @_; + + PVE::Cluster::cfs_lock_file('notifications.cfg', $timeout, sub { + PVE::Cluster::cfs_lock_file('priv/notifications.cfg', $timeout, $code); + die $@ if $@; + }); + die $@ if $@; +} + +sub read_config { + my $config = PVE::Cluster::cfs_read_file('notifications.cfg'); + my $priv_config = PVE::Cluster::cfs_read_file('priv/notifications.cfg'); + + return PVE::RS::Notify->parse_config($config, $priv_config); +} + +sub write_config { + my ($rust_config) = @_; + + my ($config, $priv_config) = $rust_config->write_config(); + PVE::Cluster::cfs_write_file('notifications.cfg', $config); + PVE::Cluster::cfs_write_file('priv/notifications.cfg', $priv_config); +} + +sub send_notification { + my ($channel, $severity, $title, $message, $properties, $config) = @_; + $config = read_config() if !defined($config); + $config->send($channel, $severity, $title, $message, $properties); +} + +sub info { + my ($channel, $title, $message, $properties, $config) = @_; + send_notification($channel, 'info', $title, $message, $properties, $config); +} + +sub notice { + my ($channel, $title, $message, $properties, $config) = @_; + send_notification($channel, 'notice', $title, $message, $properties, $config); +} + +sub warning { + my ($channel, $title, $message, $properties, $config) = @_; + send_notification($channel, 'warning', $title, $message, $properties, $config); +} + +sub error { + my ($channel, $title, $message, $properties, $config) = @_; + send_notification($channel, 'error', $title, $message, $properties, $config); +} + +1; -- 2.30.2