]> git.proxmox.com Git - pve-ha-manager.git/blob - src/PVE/API2/HA/Status.pm
add API to query ha status
[pve-ha-manager.git] / src / PVE / API2 / HA / Status.pm
1 package PVE::API2::HA::Status;
2
3 use strict;
4 use warnings;
5
6 use PVE::SafeSyslog;
7 use PVE::INotify;
8 use PVE::Cluster;
9 use PVE::HA::Config;
10 use PVE::JSONSchema qw(get_standard_option);
11 use PVE::RPCEnvironment;
12 use PVE::HA::Env::PVE2;
13
14 use PVE::RESTHandler;
15
16 use base qw(PVE::RESTHandler);
17
18 my $nodename = PVE::INotify::nodename();
19
20 my $timestamp_to_status = sub {
21 my ($ctime, $timestamp) = @_;
22
23 my $tdiff = $ctime - $timestamp;
24 if ($tdiff > 30) {
25 return "old timestamp - dead?";
26 } elsif ($tdiff < -2) {
27 return "detected time drift!";
28 } else {
29 return "active";
30 }
31 };
32
33 __PACKAGE__->register_method ({
34 name => 'status',
35 path => '',
36 method => 'GET',
37 description => "Get HA manger status.",
38 parameters => {
39 additionalProperties => 0,
40 properties => {},
41 },
42 returns => { type => 'array' },
43 code => sub {
44 my ($param) = @_;
45
46 my $res = [];
47
48 if (PVE::Cluster::check_cfs_quorum(1)) {
49 push @$res, { id => 'quorum', type => 'quorum',
50 node => $nodename, status => "OK", quorate => 1 };
51 } else {
52 push @$res, { id => 'quorum', type => 'quorum', node => $nodename,
53 status => "No quorum on node '$nodename'!", quorate => 0 };
54 }
55
56 my $haenv = PVE::HA::Env::PVE2->new($nodename);
57
58 my $status = $haenv->read_manager_status();
59
60 my $ctime = $haenv->get_time();
61
62 if (defined($status->{master_node}) && defined($status->{timestamp})) {
63 my $master = $status->{master_node};
64 my $status_str = &$timestamp_to_status($ctime, $status->{timestamp});
65 my $time_str = localtime($status->{timestamp});
66 my $status_text = "$master ($status_str, $time_str)";
67 push @$res, { id => 'master', type => 'master', node => $master,
68 status => $status_text, timestamp => $status->{timestamp} };
69 }
70
71 foreach my $node (sort keys %{$status->{node_status}}) {
72 my $lrm_status = $haenv->read_lrm_status($node);
73 my $id = "lrm:$node";
74 if (!$lrm_status->{timestamp}) {
75 push @$res, { id => $id, type => 'lrm', node => $node,
76 status => "$node (unable to read lrm status)"};
77 } else {
78 my $status_str = &$timestamp_to_status($ctime, $lrm_status->{timestamp});
79 my $time_str = localtime($lrm_status->{timestamp});
80 my $status_text = "$node ($status_str, $time_str)";
81 push @$res, { id => $id, type => 'lrm', node => $node,
82 status => $status_text, timestamp => $lrm_status->{timestamp} };
83 }
84 }
85
86 foreach my $sid (sort keys %{$status->{service_status}}) {
87 my $d = $status->{service_status}->{$sid};
88 push @$res, { id => "service:$sid", type => 'service', sid => $sid,
89 node => $d->{node}, status => "$sid ($d->{node}, $d->{state})" };
90 }
91
92 return $res;
93 }});
94
95
96
97 1;