]> git.proxmox.com Git - pve-ha-manager.git/blame - src/PVE/HA/Sim/Hardware.pm
sim hardware: avoid hard error on usage stats parsing
[pve-ha-manager.git] / src / PVE / HA / Sim / Hardware.pm
CommitLineData
8b3f9144
DM
1package PVE::HA::Sim::Hardware;
2
3# Simulate Hardware resources
4
5# power supply for nodes: on/off
6# network connection to nodes: on/off
7# watchdog devices for nodes
0cfd8f5b
DM
8
9use strict;
10use warnings;
a0a7d11e 11
0cfd8f5b 12use Fcntl qw(:DEFAULT :flock);
787b66eb
DM
13use File::Copy;
14use File::Path qw(make_path remove_tree);
a0a7d11e
TL
15use IO::File;
16use JSON;
17use POSIX qw(strftime EINTR);
18
c982dfee 19use PVE::HA::FenceConfig;
7d33cb12 20use PVE::HA::Groups;
f5a14b93 21
17b5cf98 22my $watchdog_timeout = 60;
0bba8f60 23
787b66eb
DM
24# Status directory layout
25#
26# configuration
27#
8456bde2
DM
28# $testdir/cmdlist Command list for simulation
29# $testdir/hardware_status Hardware description (number of nodes, ...)
30# $testdir/manager_status CRM status (start with {})
31# $testdir/service_config Service configuration
eea0c609 32# $testdir/static_service_stats Static service usage information (cpu, memory)
abc920b4 33# $testdir/groups HA groups configuration
8456bde2 34# $testdir/service_status_<node> Service status
ed408b44 35# $testdir/datacenter.cfg Datacenter wide HA configuration
3c36cbca 36
9329c1e2
DM
37#
38# runtime status for simulation system
39#
40# $testdir/status/cluster_locks Cluster locks
41# $testdir/status/hardware_status Hardware status (power/network on/off)
eea0c609 42# $testdir/status/static_service_stats Static service usage information (cpu, memory)
9329c1e2 43# $testdir/status/watchdog_status Watchdog status
787b66eb
DM
44#
45# runtime status
9329c1e2 46#
8456bde2
DM
47# $testdir/status/lrm_status_<node> LRM status
48# $testdir/status/manager_status CRM status
abc920b4 49# $testdir/status/crm_commands CRM command queue
8456bde2
DM
50# $testdir/status/service_config Service configuration
51# $testdir/status/service_status_<node> Service status
abc920b4 52# $testdir/status/groups HA groups configuration
c4a221bc
DM
53
54sub read_lrm_status {
55 my ($self, $node) = @_;
56
57 my $filename = "$self->{statusdir}/lrm_status_$node";
58
289e4784 59 return PVE::HA::Tools::read_json_from_file($filename, {});
c4a221bc
DM
60}
61
62sub write_lrm_status {
63 my ($self, $node, $status_obj) = @_;
64
65 my $filename = "$self->{statusdir}/lrm_status_$node";
66
289e4784 67 PVE::HA::Tools::write_json_to_file($filename, $status_obj);
c4a221bc 68}
787b66eb 69
8b3f9144 70sub read_hardware_status_nolock {
0cfd8f5b
DM
71 my ($self) = @_;
72
8b3f9144 73 my $filename = "$self->{statusdir}/hardware_status";
0cfd8f5b
DM
74
75 my $raw = PVE::Tools::file_get_contents($filename);
76 my $cstatus = decode_json($raw);
77
78 return $cstatus;
79}
80
8b3f9144 81sub write_hardware_status_nolock {
0cfd8f5b
DM
82 my ($self, $cstatus) = @_;
83
8b3f9144 84 my $filename = "$self->{statusdir}/hardware_status";
0cfd8f5b
DM
85
86 PVE::Tools::file_set_contents($filename, encode_json($cstatus));
87};
88
95360669
DM
89sub read_service_config {
90 my ($self) = @_;
91
92 my $filename = "$self->{statusdir}/service_config";
289e4784 93 my $conf = PVE::HA::Tools::read_json_from_file($filename);
95360669
DM
94
95 foreach my $sid (keys %$conf) {
96 my $d = $conf->{$sid};
8456bde2
DM
97
98 die "service '$sid' without assigned node!" if !$d->{node};
99
ec368d74 100 if ($sid =~ m/^(vm|ct|fa):(\d+)$/) {
b026c8c9
DM
101 $d->{type} = $1;
102 $d->{name} = $2;
95360669
DM
103 } else {
104 die "implement me";
105 }
106 $d->{state} = 'disabled' if !$d->{state};
bb07bd2c 107 $d->{state} = 'started' if $d->{state} eq 'enabled'; # backward compatibility
ec368d74
TL
108 $d->{max_restart} = 1 if !defined($d->{max_restart});
109 $d->{max_relocate} = 1 if !defined($d->{max_relocate});
95360669
DM
110 }
111
112 return $conf;
113}
114
76b83c72
FE
115sub update_service_config {
116 my ($self, $sid, $param) = @_;
117
118 my $conf = $self->read_service_config();
119
120 my $sconf = $conf->{$sid} || die "no such resource '$sid'\n";
121
122 foreach my $k (%$param) {
123 $sconf->{$k} = $param->{$k};
124 }
125
126 $self->write_service_config($conf);
127}
128
79e0e005
DM
129sub write_service_config {
130 my ($self, $conf) = @_;
131
95360669
DM
132 $self->{service_config} = $conf;
133
79e0e005
DM
134 my $filename = "$self->{statusdir}/service_config";
135 return PVE::HA::Tools::write_json_to_file($filename, $conf);
c982dfee
TL
136}
137
138sub read_fence_config {
139 my ($self) = @_;
140
141 my $raw = undef;
142
143 my $filename = "$self->{statusdir}/fence.cfg";
144 if (-e $filename) {
145 $raw = PVE::Tools::file_get_contents($filename);
146 }
147
148 return PVE::HA::FenceConfig::parse_config($filename, $raw);
149}
150
151sub exec_fence_agent {
152 my ($self, $agent, $node, @param) = @_;
153
154 # let all agent succeed and behave the same for now
155 $self->sim_hardware_cmd("power $node off", $agent);
156
157 return 0; # EXIT_SUCCESS
158}
79e0e005 159
e5f43426
TL
160sub set_service_state {
161 my ($self, $sid, $state) = @_;
162
163 my $conf = $self->read_service_config();
164 die "no such service '$sid'" if !$conf->{$sid};
165
166 $conf->{$sid}->{state} = $state;
167
168 $self->write_service_config($conf);
169
170 return $conf;
171}
172
27ccc95c
TL
173sub add_service {
174 my ($self, $sid, $opts) = @_;
175
176 my $conf = $self->read_service_config();
177 die "resource ID '$sid' already defined\n" if $conf->{$sid};
178
179 $conf->{$sid} = $opts;
1323ef6e 180 $conf->{$sid}->@{qw(type name)} = split(/:/, $sid);
27ccc95c
TL
181
182 $self->write_service_config($conf);
183
184 return $conf;
185}
186
187sub delete_service {
188 my ($self, $sid) = @_;
189
190 my $conf = $self->read_service_config();
191
192 die "no such service '$sid'" if !$conf->{$sid};
193
194 delete $conf->{$sid};
195
196 $self->write_service_config($conf);
197
198 return $conf;
199}
200
8456bde2 201sub change_service_location {
6da27e23 202 my ($self, $sid, $current_node, $new_node) = @_;
8456bde2
DM
203
204 my $conf = $self->read_service_config();
205
206 die "no such service '$sid'\n" if !$conf->{$sid};
207
289e4784 208 die "current_node for '$sid' does not match ($current_node != $conf->{$sid}->{node})\n"
6da27e23 209 if $current_node ne $conf->{$sid}->{node};
289e4784 210
6da27e23 211 $conf->{$sid}->{node} = $new_node;
8456bde2
DM
212
213 $self->write_service_config($conf);
214}
215
cde11324
TL
216sub service_has_lock {
217 my ($self, $sid) = @_;
218
219 my $conf = $self->read_service_config();
220
221 die "no such service '$sid'\n" if !$conf->{$sid};
222
223 return $conf->{$sid}->{lock};
224}
225
226sub lock_service {
227 my ($self, $sid, $lock) = @_;
228
229 my $conf = $self->read_service_config();
230
231 die "no such service '$sid'\n" if !$conf->{$sid};
232
233 $conf->{$sid}->{lock} = $lock || 'backup';
234
235 $self->write_service_config($conf);
236
237 return $conf;
238}
239
240sub unlock_service {
241 my ($self, $sid, $lock) = @_;
242
243 my $conf = $self->read_service_config();
244
245 die "no such service '$sid'\n" if !$conf->{$sid};
246
247 if (!defined($conf->{$sid}->{lock})) {
cde11324
TL
248 return undef;
249 }
250
251 if (defined($lock) && $conf->{$sid}->{lock} ne $lock) {
252 warn "found lock '$conf->{$sid}->{lock}' trying to remove '$lock' lock\n";
253 return undef;
254 }
255
256 my $removed_lock = delete $conf->{$sid}->{lock};
257
258 $self->write_service_config($conf);
259
260 return $removed_lock;
261}
262
b70aa69e 263sub queue_crm_commands_nolock {
3b996922
DM
264 my ($self, $cmd) = @_;
265
266 chomp $cmd;
267
b70aa69e
DM
268 my $data = '';
269 my $filename = "$self->{statusdir}/crm_commands";
270 if (-f $filename) {
271 $data = PVE::Tools::file_get_contents($filename);
272 }
273 $data .= "$cmd\n";
274 PVE::Tools::file_set_contents($filename, $data);
275
276 return undef;
277}
278
279sub queue_crm_commands {
280 my ($self, $cmd) = @_;
281
282 my $code = sub { $self->queue_crm_commands_nolock($cmd); };
289e4784 283
3b996922
DM
284 $self->global_lock($code);
285
286 return undef;
287}
288
289sub read_crm_commands {
290 my ($self) = @_;
291
292 my $code = sub {
293 my $data = '';
294
295 my $filename = "$self->{statusdir}/crm_commands";
296 if (-f $filename) {
297 $data = PVE::Tools::file_get_contents($filename);
298 }
299 PVE::Tools::file_set_contents($filename, '');
300
301 return $data;
302 };
289e4784 303
3b996922
DM
304 return $self->global_lock($code);
305}
306
abc920b4
DM
307sub read_group_config {
308 my ($self) = @_;
309
310 my $filename = "$self->{statusdir}/groups";
311 my $raw = '';
312 $raw = PVE::Tools::file_get_contents($filename) if -f $filename;
313
7d33cb12 314 return PVE::HA::Groups->parse_config($filename, $raw);
abc920b4
DM
315}
316
c4a221bc 317sub read_service_status {
8456bde2 318 my ($self, $node) = @_;
c4a221bc 319
8456bde2 320 my $filename = "$self->{statusdir}/service_status_$node";
289e4784 321 return PVE::HA::Tools::read_json_from_file($filename);
c4a221bc
DM
322}
323
324sub write_service_status {
8456bde2
DM
325 my ($self, $node, $data) = @_;
326
327 my $filename = "$self->{statusdir}/service_status_$node";
328 my $res = PVE::HA::Tools::write_json_to_file($filename, $data);
329
330 # fixme: add test if a service runs on two nodes!!!
c4a221bc 331
8456bde2 332 return $res;
289e4784 333}
c4a221bc 334
eea0c609
FE
335sub read_static_service_stats {
336 my ($self) = @_;
337
338 my $filename = "$self->{statusdir}/static_service_stats";
49b0ccc7
TL
339 my $stats = eval { PVE::HA::Tools::read_json_from_file($filename) };
340 $self->log('error', "loading static service stats failed - $@") if $@;
eea0c609
FE
341
342 return $stats;
343}
344
abc920b4
DM
345my $default_group_config = <<__EOD;
346group: prefer_node1
347 nodes node1
e941bdc5 348 nofailback 1
abc920b4
DM
349
350group: prefer_node2
351 nodes node2
e941bdc5 352 nofailback 1
abc920b4
DM
353
354group: prefer_node3
7a294ad4 355 nodes node3
e941bdc5 356 nofailback 1
abc920b4
DM
357__EOD
358
0cfd8f5b
DM
359sub new {
360 my ($this, $testdir) = @_;
361
362 die "missing testdir" if !$testdir;
363
ba9e808e
TL
364 die "testdir '$testdir' does not exist or is not a directory!\n"
365 if !-d $testdir;
366
0cfd8f5b
DM
367 my $class = ref($this) || $this;
368
369 my $self = bless {}, $class;
370
787b66eb
DM
371 my $statusdir = $self->{statusdir} = "$testdir/status";
372
373 remove_tree($statusdir);
374 mkdir $statusdir;
0cfd8f5b 375
787b66eb
DM
376 # copy initial configuartion
377 copy("$testdir/manager_status", "$statusdir/manager_status"); # optional
79e0e005 378
abc920b4
DM
379 if (-f "$testdir/groups") {
380 copy("$testdir/groups", "$statusdir/groups");
381 } else {
382 PVE::Tools::file_set_contents("$statusdir/groups", $default_group_config);
383 }
384
79e0e005
DM
385 if (-f "$testdir/service_config") {
386 copy("$testdir/service_config", "$statusdir/service_config");
387 } else {
388 my $conf = {
eda9314d
DM
389 'vm:101' => { node => 'node1', group => 'prefer_node1' },
390 'vm:102' => { node => 'node2', group => 'prefer_node2' },
391 'vm:103' => { node => 'node3', group => 'prefer_node3' },
392 'vm:104' => { node => 'node1', group => 'prefer_node1' },
393 'vm:105' => { node => 'node2', group => 'prefer_node2' },
394 'vm:106' => { node => 'node3', group => 'prefer_node3' },
79e0e005
DM
395 };
396 $self->write_service_config($conf);
397 }
787b66eb 398
853f5867
DM
399 if (-f "$testdir/hardware_status") {
400 copy("$testdir/hardware_status", "$statusdir/hardware_status") ||
401 die "Copy failed: $!\n";
402 } else {
403 my $cstatus = {
404 node1 => { power => 'off', network => 'off' },
405 node2 => { power => 'off', network => 'off' },
406 node3 => { power => 'off', network => 'off' },
407 };
408 $self->write_hardware_status_nolock($cstatus);
409 }
787b66eb 410
c982dfee
TL
411 if (-f "$testdir/fence.cfg") {
412 copy("$testdir/fence.cfg", "$statusdir/fence.cfg");
413 }
0cfd8f5b 414
ed408b44
TL
415 if (-f "$testdir/datacenter.cfg") {
416 copy("$testdir/datacenter.cfg", "$statusdir/datacenter.cfg");
417 }
418
eea0c609
FE
419 if (-f "$testdir/static_service_stats") {
420 copy("$testdir/static_service_stats", "$statusdir/static_service_stats");
421 }
422
8b3f9144 423 my $cstatus = $self->read_hardware_status_nolock();
0cfd8f5b
DM
424
425 foreach my $node (sort keys %$cstatus) {
0bba8f60 426 $self->{nodes}->{$node} = {};
8456bde2
DM
427
428 if (-f "$testdir/service_status_$node") {
429 copy("$testdir/service_status_$node", "$statusdir/service_status_$node");
289e4784 430 } else {
8456bde2
DM
431 $self->write_service_status($node, {});
432 }
0cfd8f5b
DM
433 }
434
95360669
DM
435 $self->{service_config} = $self->read_service_config();
436
0cfd8f5b
DM
437 return $self;
438}
439
440sub get_time {
441 my ($self) = @_;
442
bf93e2a2 443 die "implement in subclass";
0cfd8f5b
DM
444}
445
446sub log {
fde8362a 447 my ($self, $level, $msg, $id) = @_;
0cfd8f5b
DM
448
449 chomp $msg;
450
451 my $time = $self->get_time();
452
fde8362a
DM
453 $id = 'hardware' if !$id;
454
0bba8f60 455 printf("%-5s %5d %12s: $msg\n", $level, $time, $id);
0cfd8f5b
DM
456}
457
458sub statusdir {
459 my ($self, $node) = @_;
460
461 return $self->{statusdir};
462}
463
ed408b44
TL
464sub read_datacenter_conf {
465 my ($self, $node) = @_;
466
467 my $filename = "$self->{statusdir}/datacenter.cfg";
468 return PVE::HA::Tools::read_json_from_file($filename, {});
469}
470
8b3f9144 471sub global_lock {
0cfd8f5b
DM
472 my ($self, $code, @param) = @_;
473
8b3f9144 474 my $lockfile = "$self->{statusdir}/hardware.lck";
0cfd8f5b
DM
475 my $fh = IO::File->new(">>$lockfile") ||
476 die "unable to open '$lockfile'\n";
477
478 my $success;
479 for (;;) {
480 $success = flock($fh, LOCK_EX);
481 if ($success || ($! != EINTR)) {
482 last;
483 }
484 if (!$success) {
9de9a6ce 485 close($fh);
63f6a08c 486 die "can't acquire lock '$lockfile' - $!\n";
0cfd8f5b
DM
487 }
488 }
9de9a6ce 489
0cfd8f5b
DM
490 my $res;
491
9de9a6ce 492 eval { $res = &$code($fh, @param) };
0cfd8f5b 493 my $err = $@;
289e4784 494
0cfd8f5b
DM
495 close($fh);
496
497 die $err if $err;
289e4784 498
0cfd8f5b
DM
499 return $res;
500}
501
8b3f9144
DM
502my $compute_node_info = sub {
503 my ($self, $cstatus) = @_;
504
505 my $node_info = {};
506
507 my $node_count = 0;
508 my $online_count = 0;
509
510 foreach my $node (keys %$cstatus) {
511 my $d = $cstatus->{$node};
512
513 my $online = ($d->{power} eq 'on' && $d->{network} eq 'on') ? 1 : 0;
514 $node_info->{$node}->{online} = $online;
515
516 $node_count++;
517 $online_count++ if $online;
518 }
519
520 my $quorate = ($online_count > int($node_count/2)) ? 1 : 0;
289e4784 521
8b3f9144
DM
522 if (!$quorate) {
523 foreach my $node (keys %$cstatus) {
524 my $d = $cstatus->{$node};
525 $node_info->{$node}->{online} = 0;
526 }
527 }
528
529 return ($node_info, $quorate);
530};
531
532sub get_node_info {
533 my ($self) = @_;
534
5516f102
TL
535 my $cstatus = $self->read_hardware_status_nolock();
536 my ($node_info, $quorate) = &$compute_node_info($self, $cstatus);
8b3f9144
DM
537
538 return ($node_info, $quorate);
539}
540
ba2a45cd
TL
541# helper for Sim/ only
542sub get_cfs_state {
543 my ($self, $node, $state) = @_;
544
545 # TODO: ensure nolock is OK when adding this to RTSim
546 my $cstatus = $self->read_hardware_status_nolock();
547 my $res = $cstatus->{$node}->{cfs}->{$state};
548
549 # we assume default true if not defined
550 return !defined($res) || $res;
551}
552
a5d48ae1
TL
553# simulate hardware commands, the following commands are available:
554# power <node> <on|off>
555# network <node> <on|off>
556# delay <seconds>
1b21e7e6 557# skip-round <crm|lrm> [<rounds=1>]
a5d48ae1
TL
558# cfs <node> <rw|update> <work|fail>
559# reboot <node>
560# shutdown <node>
561# restart-lrm <node>
562# service <sid> <started|disabled|stopped|ignored>
563# service <sid> <migrate|relocate> <target>
564# service <sid> stop <timeout>
565# service <sid> lock/unlock [lockname]
566# service <sid> <add|delete>
8b3f9144 567sub sim_hardware_cmd {
fde8362a 568 my ($self, $cmdstr, $logid) = @_;
0cfd8f5b 569
e08a0717
TL
570 my $code = sub {
571 my ($lock_fh) = @_;
572
573 my $cstatus = $self->read_hardware_status_nolock();
574
0e13a6c1
TL
575 my ($cmd, $objid, $action, @params) = split(/\s+/, $cmdstr);
576 my $param = $params[0]; # for convenience/legacy
e08a0717
TL
577
578 die "sim_hardware_cmd: no node or service for command specified"
579 if !$objid;
580
581 my ($node, $sid, $d);
582
583 if ($cmd eq 'service') {
584 $sid = PVE::HA::Tools::pve_verify_ha_resource_id($objid);
585 } else {
586 $node = $objid;
587 $d = $self->{nodes}->{$node} ||
588 die "sim_hardware_cmd: no such node '$node'\n";
589 }
590
591 $self->log('info', "execute $cmdstr", $logid);
592
593 if ($cmd eq 'power') {
594 die "sim_hardware_cmd: unknown action '$action'\n"
595 if $action !~ m/^(on|off)$/;
596
597 if ($cstatus->{$node}->{power} ne $action) {
598 if ($action eq 'on') {
599
600 $d->{crm} = $self->crm_control('start', $d, $lock_fh) if !defined($d->{crm});
601 $d->{lrm} = $self->lrm_control('start', $d, $lock_fh) if !defined($d->{lrm});
602 $d->{lrm_restart} = undef;
ba2a45cd 603 $cstatus->{$node}->{cfs} = {};
e08a0717
TL
604
605 } else {
606
607 if ($d->{crm}) {
608 $d->{crm_env}->log('info', "killed by poweroff");
609 $self->crm_control('stop', $d, $lock_fh);
610 $d->{crm} = undef;
611 }
612 if ($d->{lrm}) {
613 $d->{lrm_env}->log('info', "killed by poweroff");
614 $self->lrm_control('stop', $d, $lock_fh);
615 $d->{lrm} = undef;
616 $d->{lrm_restart} = undef;
617 }
618
619 $self->watchdog_reset_nolock($node);
620 $self->write_service_status($node, {});
621 }
622 }
623
624 $cstatus->{$node}->{power} = $action;
625 $cstatus->{$node}->{network} = $action;
626 $cstatus->{$node}->{shutdown} = undef;
627
628 $self->write_hardware_status_nolock($cstatus);
629
630 } elsif ($cmd eq 'network') {
631 die "sim_hardware_cmd: unknown network action '$action'"
632 if $action !~ m/^(on|off)$/;
633 $cstatus->{$node}->{network} = $action;
634
635 $self->write_hardware_status_nolock($cstatus);
636
ba2a45cd
TL
637 } elsif ($cmd eq 'cfs') {
638 die "sim_hardware_cmd: unknown cfs action '$action' for node '$node'"
639 if $action !~ m/^(rw|update)$/;
b94b4785
FE
640 die "sim_hardware_cmd: unknown cfs command '$param' for '$action' on node '$node'"
641 if $param !~ m/^(work|fail)$/;
ba2a45cd 642
b94b4785 643 $cstatus->{$node}->{cfs}->{$action} = $param eq 'work';
ba2a45cd
TL
644 $self->write_hardware_status_nolock($cstatus);
645
e08a0717
TL
646 } elsif ($cmd eq 'reboot' || $cmd eq 'shutdown') {
647 $cstatus->{$node}->{shutdown} = $cmd;
648
649 $self->write_hardware_status_nolock($cstatus);
650
651 $self->lrm_control('shutdown', $d, $lock_fh) if defined($d->{lrm});
652 } elsif ($cmd eq 'restart-lrm') {
653 if ($d->{lrm}) {
654 $d->{lrm_restart} = 1;
655 $self->lrm_control('shutdown', $d, $lock_fh);
656 }
657 } elsif ($cmd eq 'crm') {
658
659 if ($action eq 'stop') {
660 if ($d->{crm}) {
661 $d->{crm_stop} = 1;
662 $self->crm_control('shutdown', $d, $lock_fh);
663 }
664 } elsif ($action eq 'start') {
665 $d->{crm} = $self->crm_control('start', $d, $lock_fh) if !defined($d->{crm});
666 } else {
667 die "sim_hardware_cmd: unknown action '$action'";
668 }
669
670 } elsif ($cmd eq 'service') {
667670b2
TL
671 if ($action eq 'started' || $action eq 'disabled' ||
672 $action eq 'stopped' || $action eq 'ignored') {
e08a0717
TL
673
674 $self->set_service_state($sid, $action);
675
676 } elsif ($action eq 'migrate' || $action eq 'relocate') {
677
678 die "sim_hardware_cmd: missing target node for '$action' command"
b94b4785 679 if !$param;
e08a0717 680
b94b4785 681 $self->queue_crm_commands_nolock("$action $sid $param");
e08a0717 682
21caf0db
FE
683 } elsif ($action eq 'stop') {
684
685 die "sim_hardware_cmd: missing timeout for '$action' command"
686 if !defined($param);
687
688 $self->queue_crm_commands_nolock("$action $sid $param");
689
e08a0717
TL
690 } elsif ($action eq 'add') {
691
0e13a6c1 692 $self->add_service($sid, {state => $params[1] || 'started', node => $param});
e08a0717
TL
693
694 } elsif ($action eq 'delete') {
695
696 $self->delete_service($sid);
697
698 } elsif ($action eq 'lock') {
699
b94b4785 700 $self->lock_service($sid, $param);
e08a0717
TL
701
702 } elsif ($action eq 'unlock') {
703
b94b4785 704 $self->unlock_service($sid, $param);
e08a0717
TL
705
706 } else {
707 die "sim_hardware_cmd: unknown service action '$action' " .
708 "- not implemented\n"
709 }
710 } else {
711 die "sim_hardware_cmd: unknown command '$cmdstr'\n";
712 }
713
714 return $cstatus;
715 };
716
717 return $self->global_lock($code);
718}
719
720# for controlling the resource manager services
721sub crm_control {
722 my ($self, $action, $data, $lock_fh) = @_;
723
724 die "implement in subclass";
725}
726
727sub lrm_control {
728 my ($self, $action, $data, $lock_fh) = @_;
729
bf93e2a2 730 die "implement in subclass";
0cfd8f5b
DM
731}
732
733sub run {
734 my ($self) = @_;
735
bf93e2a2 736 die "implement in subclass";
0cfd8f5b 737}
9329c1e2
DM
738
739my $modify_watchog = sub {
740 my ($self, $code) = @_;
741
742 my $update_cmd = sub {
743
744 my $filename = "$self->{statusdir}/watchdog_status";
289e4784 745
9329c1e2
DM
746 my ($res, $wdstatus);
747
748 if (-f $filename) {
749 my $raw = PVE::Tools::file_get_contents($filename);
750 $wdstatus = decode_json($raw);
751 } else {
752 $wdstatus = {};
753 }
289e4784 754
9329c1e2
DM
755 ($wdstatus, $res) = &$code($wdstatus);
756
757 PVE::Tools::file_set_contents($filename, encode_json($wdstatus));
758
759 return $res;
760 };
761
762 return $self->global_lock($update_cmd);
763};
764
0590c6a7
DM
765sub watchdog_reset_nolock {
766 my ($self, $node) = @_;
767
768 my $filename = "$self->{statusdir}/watchdog_status";
769
770 if (-f $filename) {
771 my $raw = PVE::Tools::file_get_contents($filename);
772 my $wdstatus = decode_json($raw);
773
774 foreach my $id (keys %$wdstatus) {
775 delete $wdstatus->{$id} if $wdstatus->{$id}->{node} eq $node;
776 }
289e4784 777
0590c6a7
DM
778 PVE::Tools::file_set_contents($filename, encode_json($wdstatus));
779 }
780}
781
9329c1e2
DM
782sub watchdog_check {
783 my ($self, $node) = @_;
784
785 my $code = sub {
786 my ($wdstatus) = @_;
787
788 my $res = 1;
789
790 foreach my $wfh (keys %$wdstatus) {
791 my $wd = $wdstatus->{$wfh};
792 next if $wd->{node} ne $node;
793
794 my $ctime = $self->get_time();
795 my $tdiff = $ctime - $wd->{update_time};
796
0bba8f60 797 if ($tdiff > $watchdog_timeout) { # expired
9329c1e2
DM
798 $res = 0;
799 delete $wdstatus->{$wfh};
800 }
801 }
289e4784 802
9329c1e2
DM
803 return ($wdstatus, $res);
804 };
805
806 return &$modify_watchog($self, $code);
807}
808
809my $wdcounter = 0;
810
811sub watchdog_open {
812 my ($self, $node) = @_;
813
814 my $code = sub {
815 my ($wdstatus) = @_;
816
817 ++$wdcounter;
818
819 my $id = "WD:$node:$$:$wdcounter";
820
821 die "internal error" if defined($wdstatus->{$id});
822
823 $wdstatus->{$id} = {
824 node => $node,
825 update_time => $self->get_time(),
826 };
827
828 return ($wdstatus, $id);
829 };
830
831 return &$modify_watchog($self, $code);
832}
833
834sub watchdog_close {
835 my ($self, $wfh) = @_;
836
837 my $code = sub {
838 my ($wdstatus) = @_;
839
840 my $wd = $wdstatus->{$wfh};
841 die "no such watchdog handle '$wfh'\n" if !defined($wd);
842
843 my $tdiff = $self->get_time() - $wd->{update_time};
0bba8f60 844 die "watchdog expired" if $tdiff > $watchdog_timeout;
9329c1e2
DM
845
846 delete $wdstatus->{$wfh};
847
848 return ($wdstatus);
849 };
850
851 return &$modify_watchog($self, $code);
852}
853
854sub watchdog_update {
855 my ($self, $wfh) = @_;
856
857 my $code = sub {
858 my ($wdstatus) = @_;
859
860 my $wd = $wdstatus->{$wfh};
861
862 die "no such watchdog handle '$wfh'\n" if !defined($wd);
863
864 my $ctime = $self->get_time();
865 my $tdiff = $ctime - $wd->{update_time};
866
0bba8f60 867 die "watchdog expired" if $tdiff > $watchdog_timeout;
289e4784 868
9329c1e2
DM
869 $wd->{update_time} = $ctime;
870
871 return ($wdstatus);
872 };
873
874 return &$modify_watchog($self, $code);
875}
876
5db695c3
FE
877sub get_static_node_stats {
878 my ($self) = @_;
879
880 my $cstatus = $self->read_hardware_status_nolock();
881
882 my $stats = {};
883 for my $node (keys $cstatus->%*) {
884 $stats->{$node} = { $cstatus->{$node}->%{qw(cpus memory)} };
885 }
886
887 return $stats;
888}
889
0cfd8f5b 8901;