]> git.proxmox.com Git - pve-ha-manager.git/blob - src/PVE/API2/HA/Status.pm
improve verbosity of API status call
[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 => 'index',
35 path => '',
36 method => 'GET',
37 permissions => { user => 'all' },
38 description => "Directory index.",
39 parameters => {
40 additionalProperties => 0,
41 properties => {},
42 },
43 returns => {
44 type => 'array',
45 items => {
46 type => "object",
47 properties => {},
48 },
49 links => [ { rel => 'child', href => "{name}" } ],
50 },
51 code => sub {
52 my ($param) = @_;
53
54 my $result = [
55 { name => 'current' },
56 { name => 'manager_status' },
57 ];
58
59 return $result;
60 }});
61
62 __PACKAGE__->register_method ({
63 name => 'status',
64 path => 'current',
65 method => 'GET',
66 description => "Get HA manger status.",
67 permissions => {
68 check => ['perm', '/', [ 'Sys.Audit' ]],
69 },
70 parameters => {
71 additionalProperties => 0,
72 properties => {},
73 },
74 returns => { type => 'array' },
75 code => sub {
76 my ($param) = @_;
77
78 my $res = [];
79
80 if (PVE::Cluster::check_cfs_quorum(1)) {
81 push @$res, { id => 'quorum', type => 'quorum',
82 node => $nodename, status => "OK", quorate => 1 };
83 } else {
84 push @$res, { id => 'quorum', type => 'quorum', node => $nodename,
85 status => "No quorum on node '$nodename'!", quorate => 0 };
86 }
87
88 my $haenv = PVE::HA::Env::PVE2->new($nodename);
89
90 my $status = $haenv->read_manager_status();
91
92 my $ctime = $haenv->get_time();
93
94 if (defined($status->{master_node}) && defined($status->{timestamp})) {
95 my $master = $status->{master_node};
96 my $status_str = &$timestamp_to_status($ctime, $status->{timestamp});
97 my $time_str = localtime($status->{timestamp});
98 my $status_text = "$master ($status_str, $time_str)";
99 push @$res, { id => 'master', type => 'master', node => $master,
100 status => $status_text, timestamp => $status->{timestamp} };
101 }
102
103 # compute active services for all nodes
104 my $active_count = {};
105 foreach my $sid (sort keys %{$status->{service_status}}) {
106 my $sd = $status->{service_status}->{$sid};
107 next if !$sd->{node};
108 $active_count->{$sd->{node}} = 0 if !defined($active_count->{$sd->{node}});
109 my $req_state = $sd->{state};
110 next if !defined($req_state);
111 next if $req_state eq 'stopped';
112 next if $req_state eq 'freeze';
113 $active_count->{$sd->{node}}++;
114 }
115
116 foreach my $node (sort keys %{$status->{node_status}}) {
117 my $lrm_status = $haenv->read_lrm_status($node);
118 my $id = "lrm:$node";
119 if (!$lrm_status->{timestamp}) {
120 push @$res, { id => $id, type => 'lrm', node => $node,
121 status => "$node (unable to read lrm status)"};
122 } else {
123 my $status_str = &$timestamp_to_status($ctime, $lrm_status->{timestamp});
124 if ($status_str eq 'active') {
125 my $lrm_mode = $lrm_status->{mode} || 'active';
126 my $lrm_state = $lrm_status->{state} || 'unknown';
127 if ($lrm_mode ne 'active') {
128 $status_str = "$lrm_mode mode";
129 } else {
130 if ($lrm_state eq 'wait_for_agent_lock' && !$active_count->{$node}) {
131 $status_str = 'idle';
132 } else {
133 $status_str = $lrm_state;
134 }
135 }
136 }
137
138 my $time_str = localtime($lrm_status->{timestamp});
139 my $status_text = "$node ($status_str, $time_str)";
140 push @$res, { id => $id, type => 'lrm', node => $node,
141 status => $status_text, timestamp => $lrm_status->{timestamp} };
142 }
143 }
144
145 foreach my $sid (sort keys %{$status->{service_status}}) {
146 my $d = $status->{service_status}->{$sid};
147 push @$res, { id => "service:$sid", type => 'service', sid => $sid,
148 node => $d->{node}, status => "$sid ($d->{node}, $d->{state})" };
149 }
150
151 return $res;
152 }});
153
154 __PACKAGE__->register_method ({
155 name => 'manager_status',
156 path => 'manager_status',
157 method => 'GET',
158 description => "Get full HA manger status, including LRM status.",
159 permissions => {
160 check => ['perm', '/', [ 'Sys.Audit' ]],
161 },
162 parameters => {
163 additionalProperties => 0,
164 properties => {},
165 },
166 returns => { type => 'object' },
167 code => sub {
168 my ($param) = @_;
169
170 my $haenv = PVE::HA::Env::PVE2->new($nodename);
171
172 my $status = $haenv->read_manager_status();
173
174 my $data = { manager_status => $status };
175
176 $data->{quorum} = {
177 node => $nodename,
178 quorate => PVE::Cluster::check_cfs_quorum(1),
179 };
180
181 foreach my $node (sort keys %{$status->{node_status}}) {
182 my $lrm_status = $haenv->read_lrm_status($node);
183 $data->{lrm_status}->{$node} = $lrm_status;
184 }
185
186 return $data;
187 }});
188
189 1;