]> git.proxmox.com Git - pve-cluster.git/blob - src/PVE/Notify.pm
notify: adapt to matcher based notification system
[pve-cluster.git] / src / PVE / Notify.pm
1 package PVE::Notify;
2
3 use strict;
4 use warnings;
5
6 use PVE::Cluster qw(cfs_register_file cfs_read_file cfs_lock_file cfs_write_file);
7 use Proxmox::RS::Notify;
8
9 cfs_register_file(
10 'notifications.cfg',
11 \&parse_notification_config,
12 \&write_notification_config,
13 );
14
15 cfs_register_file(
16 'priv/notifications.cfg',
17 \&parse_notification_config,
18 \&write_notification_config,
19 );
20
21 sub parse_notification_config {
22 my ($filename, $raw) = @_;
23
24 $raw = '' if !defined($raw);
25 return $raw;
26 }
27
28 sub write_notification_config {
29 my ($filename, $config) = @_;
30 return $config;
31 }
32
33 sub lock_config {
34 my ($code, $timeout) = @_;
35
36 cfs_lock_file('notifications.cfg', $timeout, sub {
37 cfs_lock_file('priv/notifications.cfg', $timeout, $code);
38 die $@ if $@;
39 });
40 die $@ if $@;
41 }
42
43 sub read_config {
44 my $config = cfs_read_file('notifications.cfg');
45 my $priv_config = cfs_read_file('priv/notifications.cfg');
46
47 my $notification_config = Proxmox::RS::Notify->parse_config($config, $priv_config);
48
49 return $notification_config;
50 }
51
52 sub write_config {
53 my ($notification_config) = @_;
54
55 my ($config, $priv_config) = $notification_config->write_config();
56 cfs_write_file('notifications.cfg', $config, 1);
57 cfs_write_file('priv/notifications.cfg', $priv_config, 1);
58 }
59
60 my $send_notification = sub {
61 my ($severity, $title, $message, $template_data, $fields, $config) = @_;
62 $config = read_config() if !defined($config);
63 $config->send($severity, $title, $message, $template_data, $fields);
64 };
65
66 sub notify {
67 my ($severity, $title, $message, $template_data, $fields, $config) = @_;
68 $send_notification->(
69 $severity,
70 $title,
71 $message,
72 $template_data,
73 $fields,
74 $config
75 );
76 }
77
78 sub info {
79 my ($title, $message, $template_data, $fields, $config) = @_;
80 $send_notification->(
81 'info',
82 $title,
83 $message,
84 $template_data,
85 $fields,
86 $config
87 );
88 }
89
90 sub notice {
91 my ($title, $message, $template_data, $fields, $config) = @_;
92 $send_notification->(
93 'notice',
94 $title,
95 $message,
96 $template_data,
97 $fields,
98 $config
99 );
100 }
101
102 sub warning {
103 my ($title, $message, $template_data, $fields, $config) = @_;
104 $send_notification->(
105 'warning',
106 $title,
107 $message,
108 $template_data,
109 $fields,
110 $config
111 );
112 }
113
114 sub error {
115 my ($title, $message, $template_data, $fields, $config) = @_;
116 $send_notification->(
117 'error',
118 $title,
119 $message,
120 $template_data,
121 $fields,
122 $config
123 );
124 }
125
126 sub check_may_use_target {
127 my ($target, $rpcenv) = @_;
128 my $user = $rpcenv->get_user();
129
130 my $config = read_config();
131 my $entities = $config->get_referenced_entities($target);
132
133 for my $entity (@$entities) {
134 $rpcenv->check($user, "/mapping/notification/$entity", [ 'Mapping.Use' ]);
135 }
136 }
137
138 1;