]> git.proxmox.com Git - pve-cluster.git/blob - src/PVE/Notify.pm
046436202c19050babd8d82977ead076922983a6
[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 my $mail_to_root_target = 'mail-to-root';
22
23 sub parse_notification_config {
24 my ($filename, $raw) = @_;
25
26 $raw = '' if !defined($raw);
27 return $raw;
28 }
29
30 sub write_notification_config {
31 my ($filename, $config) = @_;
32 return $config;
33 }
34
35 sub lock_config {
36 my ($code, $timeout) = @_;
37
38 cfs_lock_file('notifications.cfg', $timeout, sub {
39 cfs_lock_file('priv/notifications.cfg', $timeout, $code);
40 die $@ if $@;
41 });
42 die $@ if $@;
43 }
44
45 sub read_config {
46 my $config = cfs_read_file('notifications.cfg');
47 my $priv_config = cfs_read_file('priv/notifications.cfg');
48
49 my $notification_config = Proxmox::RS::Notify->parse_config($config, $priv_config);
50
51 eval {
52 # This target should always be available...
53 $notification_config->add_sendmail_endpoint(
54 $mail_to_root_target,
55 undef,
56 ['root@pam'],
57 undef,
58 undef,
59 'Send mail to root@pam\'s email address'
60 );
61 };
62
63 return $notification_config;
64 }
65
66 sub write_config {
67 my ($notification_config) = @_;
68
69 eval {
70 # ... but don't persist it to the config.
71 # Rationale: If it is in the config, the user might think
72 # that it can be changed by editing the configuration there.
73 # However, since we always add it in `read_config`, any changes
74 # will be implicitly overridden by the default.
75
76 # If users want's to change the configuration, they are supposed to
77 # create a new sendmail endpoint.
78 $notification_config->delete_sendmail_endpoint($mail_to_root_target);
79 };
80
81 my ($config, $priv_config) = $notification_config->write_config();
82 cfs_write_file('notifications.cfg', $config);
83 cfs_write_file('priv/notifications.cfg', $priv_config);
84 }
85
86 sub default_target {
87 return $mail_to_root_target;
88 }
89
90 my $send_notification = sub {
91 my ($target, $severity, $title, $message, $properties, $config) = @_;
92 $config = read_config() if !defined($config);
93 my ($module, $file, $line) = caller(1);
94
95 # Augment properties with the source code location of the notify call
96 my $props_with_source = {
97 %$properties,
98 source => {
99 module => $module,
100 file => $file,
101 line => $line,
102 }
103 };
104
105 $config->send($target, $severity, $title, $message, $props_with_source);
106 };
107
108 sub notify {
109 my ($target, $severity, $title, $message, $properties, $config) = @_;
110 $send_notification->($target, $severity, $title, $message, $properties, $config);
111 }
112
113 sub info {
114 my ($target, $title, $message, $properties, $config) = @_;
115 $send_notification->($target, 'info', $title, $message, $properties, $config);
116 }
117
118 sub notice {
119 my ($target, $title, $message, $properties, $config) = @_;
120 $send_notification->($target, 'notice', $title, $message, $properties, $config);
121 }
122
123 sub warning {
124 my ($target, $title, $message, $properties, $config) = @_;
125 $send_notification->($target, 'warning', $title, $message, $properties, $config);
126 }
127
128 sub error {
129 my ($target, $title, $message, $properties, $config) = @_;
130 $send_notification->($target, 'error', $title, $message, $properties, $config);
131 }
132
133 sub check_may_use_target {
134 my ($target, $rpcenv) = @_;
135 my $user = $rpcenv->get_user();
136
137 my $config = read_config();
138 my $entities = $config->get_referenced_entities($target);
139
140 for my $entity (@$entities) {
141 $rpcenv->check($user, "/mapping/notification/$entity", [ 'Mapping.Use' ]);
142 }
143 }
144
145 1;