]> git.proxmox.com Git - pve-guest-common.git/blob - PVE/VZDump/Plugin.pm
vzdump: allow all defined log levels
[pve-guest-common.git] / PVE / VZDump / Plugin.pm
1 package PVE::VZDump::Plugin;
2
3 use strict;
4 use warnings;
5
6 use POSIX qw(strftime);
7
8 use PVE::Tools;
9 use PVE::SafeSyslog;
10
11 my $log_level = {
12 err => 'ERROR:',
13 info => 'INFO:',
14 warn => 'WARN:',
15 };
16
17 sub debugmsg {
18 my ($mtype, $msg, $logfd, $syslog) = @_;
19
20 chomp $msg;
21
22 return if !$msg;
23
24 my $level = $log_level->{$mtype} ? $mtype : 'err';
25 my $pre = $log_level->{$level};
26
27 my $timestr = strftime ("%F %H:%M:%S", CORE::localtime);
28
29 syslog ($level, "$pre $msg") if $syslog;
30
31 foreach my $line (split (/\n/, $msg)) {
32 print STDERR "$pre $line\n";
33 print $logfd "$timestr $pre $line\n" if $logfd;
34 }
35 }
36
37 sub set_logfd {
38 my ($self, $logfd) = @_;
39
40 $self->{logfd} = $logfd;
41 }
42
43 sub cmd {
44 my ($self, $cmdstr, %param) = @_;
45
46 my $logfunc = sub {
47 my $line = shift;
48 debugmsg ('info', $line, $self->{logfd});
49 };
50
51 PVE::Tools::run_command($cmdstr, %param, logfunc => $logfunc);
52 }
53
54 sub cmd_noerr {
55 my ($self, $cmdstr, %param) = @_;
56
57 my $res;
58 eval { $res = $self->cmd($cmdstr, %param); };
59 $self->logerr ($@) if $@;
60 return $res;
61 }
62
63 sub loginfo {
64 my ($self, $msg) = @_;
65
66 debugmsg('info', $msg, $self->{logfd}, 0);
67 }
68
69 sub logerr {
70 my ($self, $msg) = @_;
71
72 debugmsg('err', $msg, $self->{logfd}, 0);
73 }
74
75 sub type {
76 return 'unknown';
77 };
78
79 sub vmlist {
80 my ($self) = @_;
81
82 return [ keys %{$self->{vmlist}} ] if $self->{vmlist};
83
84 return [];
85 }
86
87 sub vm_status {
88 my ($self, $vmid) = @_;
89
90 die "internal error"; # implement in subclass
91 }
92
93 sub prepare {
94 my ($self, $task, $vmid, $mode) = @_;
95
96 die "internal error"; # implement in subclass
97 }
98
99 sub lock_vm {
100 my ($self, $vmid) = @_;
101
102 die "internal error"; # implement in subclass
103 }
104
105 sub unlock_vm {
106 my ($self, $vmid) = @_;
107
108 die "internal error"; # implement in subclass
109 }
110
111 sub stop_vm {
112 my ($self, $task, $vmid) = @_;
113
114 die "internal error"; # implement in subclass
115 }
116
117 sub start_vm {
118 my ($self, $task, $vmid) = @_;
119
120 die "internal error"; # implement in subclass
121 }
122
123 sub suspend_vm {
124 my ($self, $task, $vmid) = @_;
125
126 die "internal error"; # implement in subclass
127 }
128
129 sub resume_vm {
130 my ($self, $task, $vmid) = @_;
131
132 die "internal error"; # implement in subclass
133 }
134
135 sub snapshot {
136 my ($self, $task, $vmid) = @_;
137
138 die "internal error"; # implement in subclass
139 }
140
141 sub copy_data_phase2 {
142 my ($self, $task, $vmid) = @_;
143
144 die "internal error"; # implement in subclass
145 }
146
147 sub assemble {
148 my ($self, $task, $vmid) = @_;
149
150 die "internal error"; # implement in subclass
151 }
152
153 sub archive {
154 my ($self, $task, $vmid, $filename, $comp) = @_;
155
156 die "internal error"; # implement in subclass
157 }
158
159 sub cleanup {
160 my ($self, $task, $vmid) = @_;
161
162 die "internal error"; # implement in subclass
163 }
164
165 1;