]>
git.proxmox.com Git - pmg-api.git/blob - PMG/API2/Nodes.pm
1 package PMG
::API2
::NodeInfo
;
6 use Time
::Local
qw(timegm_nocheck);
10 use PVE
::JSONSchema
qw(get_standard_option);
11 use PMG
::RESTEnvironment
;
16 use PMG
::API2
::Services
;
17 use PMG
::API2
::Network
;
18 use PMG
::API2
::ClamAV
;
19 use PMG
::API2
::Postfix
;
21 use base
qw(PVE::RESTHandler);
23 __PACKAGE__-
>register_method ({
24 subclass
=> "PMG::API2::Postfix",
28 __PACKAGE__-
>register_method ({
29 subclass
=> "PMG::API2::ClamAV",
33 __PACKAGE__-
>register_method ({
34 subclass
=> "PMG::API2::Network",
38 __PACKAGE__-
>register_method ({
39 subclass
=> "PMG::API2::Tasks",
43 __PACKAGE__-
>register_method ({
44 subclass
=> "PMG::API2::Services",
48 __PACKAGE__-
>register_method ({
52 permissions
=> { user
=> 'all' },
53 description
=> "Node index.",
55 additionalProperties
=> 0,
57 node
=> get_standard_option
('pve-node'),
66 links
=> [ { rel
=> 'child', href
=> "{name}" } ],
73 { name
=> 'postfix' },
74 { name
=> 'services' },
78 { name
=> 'vncshell' },
79 { name
=> 'rrddata' },
85 __PACKAGE__-
>register_method({
89 protected
=> 1, # fixme: can we avoid that?
91 description
=> "Read node RRD statistics",
93 additionalProperties
=> 0,
95 node
=> get_standard_option
('pve-node'),
97 description
=> "Specify the time frame you are interested in.",
99 enum
=> [ 'hour', 'day', 'week', 'month', 'year' ],
102 description
=> "The RRD consolidation function",
104 enum
=> [ 'AVERAGE', 'MAX' ],
119 return PMG
::Utils
::create_rrd_data
(
120 "pmg-node-v1.rrd", $param->{timeframe
}, $param->{cf
});
124 __PACKAGE__-
>register_method({
128 description
=> "Read system log",
132 additionalProperties
=> 0,
134 node
=> get_standard_option
('pve-node'),
147 pattern
=> '^\d{4}-\d{2}-\d{2}( \d{2}:\d{2}(:\d{2})?)?$',
148 description
=> "Display all log since this date-time string.",
153 pattern
=> '^\d{4}-\d{2}-\d{2}( \d{2}:\d{2}(:\d{2})?)?$',
154 description
=> "Display all log until this date-time string.",
158 description
=> "Service ID",
171 description
=> "Line number",
175 description
=> "Line text",
184 my $restenv = PMG
::RESTEnvironment-
>get();
186 my $service = $param->{service
};
187 if ($service && $service eq 'postfix') {
188 $service = 'postfix@-';
191 my ($count, $lines) = PVE
::Tools
::dump_journal
(
192 $param->{start
}, $param->{limit
},
193 $param->{since
}, $param->{'until'}, $service);
195 $restenv->set_result_attrib('total', $count);
200 __PACKAGE__-
>register_method ({
205 description
=> "Creates a VNC Shell proxy.",
207 additionalProperties
=> 0,
209 node
=> get_standard_option
('pve-node'),
213 description
=> "use websocket instead of standard vnc.",
219 additionalProperties
=> 0,
221 user
=> { type
=> 'string' },
222 ticket
=> { type
=> 'string' },
223 port
=> { type
=> 'integer' },
224 upid
=> { type
=> 'string' },
230 my $node = $param->{node
};
232 # we only implement the websocket based VNC here
233 my $websocket = $param->{websocket
} // 1;
234 die "standard VNC not implemented" if !$websocket;
236 my $authpath = "/nodes/$node";
238 my $restenv = PMG
::RESTEnvironment-
>get();
239 my $user = $restenv->get_user();
241 my $ticket = PMG
::Ticket
::assemble_vnc_ticket
($user, $authpath);
243 my $family = PVE
::Tools
::get_host_address_family
($node);
244 my $port = PVE
::Tools
::next_vnc_port
($family);
248 if ($user eq 'root@pam') {
249 $shcmd = [ '/bin/login', '-f', 'root' ];
251 $shcmd = [ '/bin/login' ];
254 my $cmd = ['/usr/bin/vncterm', '-rfbport', $port,
255 '-timeout', 10, '-notls', '-listen', 'localhost',
261 syslog
('info', "starting vnc proxy $upid\n");
263 my $cmdstr = join (' ', @$cmd);
264 syslog
('info', "launch command: $cmdstr");
267 foreach my $k (keys %ENV) {
268 next if $k eq 'PATH' || $k eq 'TERM' || $k eq 'USER' || $k eq 'HOME';
273 $ENV{PVE_VNC_TICKET
} = $ticket; # pass ticket to vncterm
275 PVE
::Tools
::run_command
($cmd, errmsg
=> "vncterm failed");
284 my $upid = $restenv->fork_worker('vncshell', "", $user, $realcmd);
286 PVE
::Tools
::wait_for_vnc_port
($port);
296 __PACKAGE__-
>register_method({
297 name
=> 'vncwebsocket',
298 path
=> 'vncwebsocket',
300 description
=> "Opens a weksocket for VNC traffic.",
302 additionalProperties
=> 0,
304 node
=> get_standard_option
('pve-node'),
306 description
=> "Ticket from previous call to vncproxy.",
311 description
=> "Port number returned by previous vncproxy call.",
321 port
=> { type
=> 'string' },
327 my $authpath = "/nodes/$param->{node}";
329 my $restenv = PMG
::RESTEnvironment-
>get();
330 my $user = $restenv->get_user();
332 PMG
::Ticket
::verify_vnc_ticket
($param->{vncticket
}, $user, $authpath);
334 my $port = $param->{port
};
336 return { port
=> $port };
339 __PACKAGE__-
>register_method({
343 description
=> "Read DNS settings.",
346 additionalProperties
=> 0,
348 node
=> get_standard_option
('pve-node'),
353 additionalProperties
=> 0,
356 description
=> "Search domain for host-name lookup.",
361 description
=> 'First name server IP address.',
366 description
=> 'Second name server IP address.',
371 description
=> 'Third name server IP address.',
380 my $res = PVE
::INotify
::read_file
('resolvconf');
385 __PACKAGE__-
>register_method({
386 name
=> 'update_dns',
389 description
=> "Write DNS settings.",
393 additionalProperties
=> 0,
395 node
=> get_standard_option
('pve-node'),
397 description
=> "Search domain for host-name lookup.",
401 description
=> 'First name server IP address.',
402 type
=> 'string', format
=> 'ip',
406 description
=> 'Second name server IP address.',
407 type
=> 'string', format
=> 'ip',
411 description
=> 'Third name server IP address.',
412 type
=> 'string', format
=> 'ip',
417 returns
=> { type
=> "null" },
421 PVE
::INotify
::update_file
('resolvconf', $param);
427 __PACKAGE__-
>register_method({
431 description
=> "Read server time and time zone settings.",
434 additionalProperties
=> 0,
436 node
=> get_standard_option
('pve-node'),
441 additionalProperties
=> 0,
444 description
=> "Time zone",
448 description
=> "Seconds since 1970-01-01 00:00:00 UTC.",
450 minimum
=> 1297163644,
453 description
=> "Seconds since 1970-01-01 00:00:00 (local time)",
455 minimum
=> 1297163644,
463 my $ltime = timegm_nocheck
(localtime($ctime));
465 timezone
=> PVE
::INotify
::read_file
('timezone'),
473 __PACKAGE__-
>register_method({
474 name
=> 'set_timezone',
477 description
=> "Set time zone.",
481 additionalProperties
=> 0,
483 node
=> get_standard_option
('pve-node'),
485 description
=> "Time zone. The file '/usr/share/zoneinfo/zone.tab' contains the list of valid names.",
490 returns
=> { type
=> "null" },
494 PVE
::INotify
::write_file
('timezone', $param->{timezone
});
500 package PMG
::API2
::Nodes
;
505 use PVE
::RESTHandler
;
506 use PVE
::JSONSchema
qw(get_standard_option);
508 use PMG
::RESTEnvironment
;
510 use base
qw(PVE::RESTHandler);
512 __PACKAGE__-
>register_method ({
513 subclass
=> "PMG::API2::NodeInfo",
517 __PACKAGE__-
>register_method ({
521 permissions
=> { user
=> 'all' },
522 description
=> "Cluster node index.",
524 additionalProperties
=> 0,
533 links
=> [ { rel
=> 'child', href
=> "{node}" } ],
538 my $nodename = PVE
::INotify
::nodename
();
540 my $res = [ { node
=> $nodename } ];
544 $done->{$nodename} = 1;
546 my $restenv = PMG
::RESTEnvironment-
>get();
547 my $cinfo = $restenv->{cinfo
};
549 foreach my $ni (values %{$cinfo->{ids
}}) {
550 push @$res, { node
=> $ni->{name
} } if !$done->{$ni->{name
}};
551 $done->{$ni->{name
}} = 1;