]>
git.proxmox.com Git - pmg-api.git/blob - PMG/API2/Nodes.pm
1 package PMG
::API2
::NodeInfo
;
5 use Time
::Local
qw(timegm_nocheck);
11 use PVE
::JSONSchema
qw(get_standard_option);
12 use PMG
::RESTEnvironment
;
18 use PMG
::API2
::Subscription
;
21 use PMG
::API2
::Services
;
22 use PMG
::API2
::Network
;
23 use PMG
::API2
::ClamAV
;
24 use PMG
::API2
::Postfix
;
25 use PMG
::API2
::MailTracker
;
27 use base
qw(PVE::RESTHandler);
29 __PACKAGE__-
>register_method ({
30 subclass
=> "PMG::API2::Postfix",
34 __PACKAGE__-
>register_method ({
35 subclass
=> "PMG::API2::ClamAV",
39 __PACKAGE__-
>register_method ({
40 subclass
=> "PMG::API2::Network",
44 __PACKAGE__-
>register_method ({
45 subclass
=> "PMG::API2::Tasks",
49 __PACKAGE__-
>register_method ({
50 subclass
=> "PMG::API2::Services",
54 __PACKAGE__-
>register_method ({
55 subclass
=> "PMG::API2::Subscription",
56 path
=> 'subscription',
59 __PACKAGE__-
>register_method ({
60 subclass
=> "PMG::API2::APT",
64 __PACKAGE__-
>register_method ({
65 subclass
=> "PMG::API2::MailTracker",
69 __PACKAGE__-
>register_method ({
73 permissions
=> { user
=> 'all' },
74 description
=> "Node index.",
76 additionalProperties
=> 0,
78 node
=> get_standard_option
('pve-node'),
87 links
=> [ { rel
=> 'child', href
=> "{name}" } ],
95 { name
=> 'postfix' },
96 { name
=> 'services' },
99 { name
=> 'tracker' },
101 { name
=> 'status' },
102 { name
=> 'subscription' },
103 { name
=> 'vncshell' },
104 { name
=> 'rrddata' },
110 __PACKAGE__-
>register_method({
114 protected
=> 1, # fixme: can we avoid that?
116 permissions
=> { check
=> [ 'admin', 'audit' ] },
117 description
=> "Read node RRD statistics",
119 additionalProperties
=> 0,
121 node
=> get_standard_option
('pve-node'),
123 description
=> "Specify the time frame you are interested in.",
125 enum
=> [ 'hour', 'day', 'week', 'month', 'year' ],
128 description
=> "The RRD consolidation function",
130 enum
=> [ 'AVERAGE', 'MAX' ],
145 return PMG
::Utils
::create_rrd_data
(
146 "pmg-node-v1.rrd", $param->{timeframe
}, $param->{cf
});
150 __PACKAGE__-
>register_method({
154 description
=> "Read system log",
158 additionalProperties
=> 0,
160 node
=> get_standard_option
('pve-node'),
173 pattern
=> '^\d{4}-\d{2}-\d{2}( \d{2}:\d{2}(:\d{2})?)?$',
174 description
=> "Display all log since this date-time string.",
179 pattern
=> '^\d{4}-\d{2}-\d{2}( \d{2}:\d{2}(:\d{2})?)?$',
180 description
=> "Display all log until this date-time string.",
184 description
=> "Service ID",
197 description
=> "Line number",
201 description
=> "Line text",
210 my $restenv = PMG
::RESTEnvironment-
>get();
212 my $service = $param->{service
};
213 $service = PMG
::Utils
::lookup_real_service_name
($service)
216 my ($count, $lines) = PVE
::Tools
::dump_journal
(
217 $param->{start
}, $param->{limit
},
218 $param->{since
}, $param->{'until'}, $service);
220 $restenv->set_result_attrib('total', $count);
225 __PACKAGE__-
>register_method ({
230 description
=> "Creates a VNC Shell proxy.",
232 additionalProperties
=> 0,
234 node
=> get_standard_option
('pve-node'),
237 description
=> "Run 'apt-get dist-upgrade' instead of normal shell.",
244 description
=> "use websocket instead of standard vnc.",
250 additionalProperties
=> 0,
252 user
=> { type
=> 'string' },
253 ticket
=> { type
=> 'string' },
254 port
=> { type
=> 'integer' },
255 upid
=> { type
=> 'string' },
261 my $node = $param->{node
};
263 if ($node ne PVE
::INotify
::nodename
()) {
264 die "vncproxy to remote node not implemented";
267 # we only implement the websocket based VNC here
268 my $websocket = $param->{websocket
} // 1;
269 die "standard VNC not implemented" if !$websocket;
271 my $authpath = "/nodes/$node";
273 my $restenv = PMG
::RESTEnvironment-
>get();
274 my $user = $restenv->get_user();
276 raise_perm_exc
('user != root@pam') if $param->{upgrade
} && $user ne 'root@pam';
278 my $ticket = PMG
::Ticket
::assemble_vnc_ticket
($user, $authpath);
280 my $family = PVE
::Tools
::get_host_address_family
($node);
281 my $port = PVE
::Tools
::next_vnc_port
($family);
285 if ($user eq 'root@pam') {
286 if ($param->{upgrade
}) {
287 my $upgradecmd = "pmgupgrade --shell";
288 # $upgradecmd = PVE::Tools::shellquote($upgradecmd) if $remip;
289 $shcmd = [ '/bin/bash', '-c', $upgradecmd ];
291 $shcmd = [ '/bin/login', '-f', 'root' ];
294 $shcmd = [ '/bin/login' ];
297 my $cmd = ['/usr/bin/vncterm', '-rfbport', $port,
298 '-timeout', 10, '-notls', '-listen', 'localhost',
304 syslog
('info', "starting vnc proxy $upid\n");
306 my $cmdstr = join (' ', @$cmd);
307 syslog
('info', "launch command: $cmdstr");
310 foreach my $k (keys %ENV) {
311 next if $k eq 'PATH' || $k eq 'TERM' || $k eq 'USER' || $k eq 'HOME';
316 $ENV{PVE_VNC_TICKET
} = $ticket; # pass ticket to vncterm
318 PVE
::Tools
::run_command
($cmd, errmsg
=> "vncterm failed");
327 my $upid = $restenv->fork_worker('vncshell', "", $user, $realcmd);
329 PVE
::Tools
::wait_for_vnc_port
($port);
339 __PACKAGE__-
>register_method({
340 name
=> 'vncwebsocket',
341 path
=> 'vncwebsocket',
343 description
=> "Opens a weksocket for VNC traffic.",
345 additionalProperties
=> 0,
347 node
=> get_standard_option
('pve-node'),
349 description
=> "Ticket from previous call to vncproxy.",
354 description
=> "Port number returned by previous vncproxy call.",
364 port
=> { type
=> 'string' },
370 my $authpath = "/nodes/$param->{node}";
372 my $restenv = PMG
::RESTEnvironment-
>get();
373 my $user = $restenv->get_user();
375 PMG
::Ticket
::verify_vnc_ticket
($param->{vncticket
}, $user, $authpath);
377 my $port = $param->{port
};
379 return { port
=> $port };
382 __PACKAGE__-
>register_method({
386 description
=> "Read DNS settings.",
389 additionalProperties
=> 0,
391 node
=> get_standard_option
('pve-node'),
396 additionalProperties
=> 0,
399 description
=> "Search domain for host-name lookup.",
404 description
=> 'First name server IP address.',
409 description
=> 'Second name server IP address.',
414 description
=> 'Third name server IP address.',
423 my $res = PVE
::INotify
::read_file
('resolvconf');
428 __PACKAGE__-
>register_method({
429 name
=> 'update_dns',
432 description
=> "Write DNS settings.",
436 additionalProperties
=> 0,
438 node
=> get_standard_option
('pve-node'),
440 description
=> "Search domain for host-name lookup.",
444 description
=> 'First name server IP address.',
445 type
=> 'string', format
=> 'ip',
449 description
=> 'Second name server IP address.',
450 type
=> 'string', format
=> 'ip',
454 description
=> 'Third name server IP address.',
455 type
=> 'string', format
=> 'ip',
460 returns
=> { type
=> "null" },
464 PVE
::INotify
::update_file
('resolvconf', $param);
470 __PACKAGE__-
>register_method({
474 description
=> "Read server time and time zone settings.",
477 additionalProperties
=> 0,
479 node
=> get_standard_option
('pve-node'),
484 additionalProperties
=> 0,
487 description
=> "Time zone",
491 description
=> "Seconds since 1970-01-01 00:00:00 UTC.",
493 minimum
=> 1297163644,
496 description
=> "Seconds since 1970-01-01 00:00:00 (local time)",
498 minimum
=> 1297163644,
506 my $ltime = timegm_nocheck
(localtime($ctime));
508 timezone
=> PVE
::INotify
::read_file
('timezone'),
516 __PACKAGE__-
>register_method({
517 name
=> 'set_timezone',
520 description
=> "Set time zone.",
524 additionalProperties
=> 0,
526 node
=> get_standard_option
('pve-node'),
528 description
=> "Time zone. The file '/usr/share/zoneinfo/zone.tab' contains the list of valid names.",
533 returns
=> { type
=> "null" },
537 PVE
::INotify
::write_file
('timezone', $param->{timezone
});
542 __PACKAGE__-
>register_method({
546 description
=> "Read server status. This is used by the cluster manager to test the node health.",
550 additionalProperties
=> 0,
552 node
=> get_standard_option
('pve-node'),
557 additionalProperties
=> 1,
560 description
=> "Seconds since 1970-01-01 00:00:00 UTC.",
562 minimum
=> 1297163644,
565 description
=> "The uptime of the system in seconds.",
570 description
=> "Database is synced with other nodes.",
578 my $restenv = PMG
::RESTEnvironment-
>get();
579 my $cinfo = $restenv->{cinfo
};
583 my $res = { time => $ctime, insync
=> 1 };
585 my $si = PMG
::DBTools
::cluster_sync_status
($cinfo);
586 foreach my $cid (keys %$si) {
587 my $lastsync = $si->{$cid};
588 my $sdiff = $ctime - $lastsync;
589 $sdiff = 0 if $sdiff < 0;
590 $res->{insync
} = 0 if $sdiff > (60*3);
593 my ($uptime, $idle) = PVE
::ProcFSTools
::read_proc_uptime
();
594 $res->{uptime
} = $uptime;
596 my ($avg1, $avg5, $avg15) = PVE
::ProcFSTools
::read_loadavg
();
597 $res->{loadavg
} = [ $avg1, $avg5, $avg15];
599 my ($sysname, $nodename, $release, $version, $machine) = POSIX
::uname
();
601 $res->{kversion
} = "$sysname $release $version";
603 $res->{cpuinfo
} = PVE
::ProcFSTools
::read_cpuinfo
();
605 my $stat = PVE
::ProcFSTools
::read_proc_stat
();
606 $res->{cpu
} = $stat->{cpu
};
607 $res->{wait} = $stat->{wait};
609 my $meminfo = PVE
::ProcFSTools
::read_meminfo
();
611 free
=> $meminfo->{memfree
},
612 total
=> $meminfo->{memtotal
},
613 used
=> $meminfo->{memused
},
617 free
=> $meminfo->{swapfree
},
618 total
=> $meminfo->{swaptotal
},
619 used
=> $meminfo->{swapused
},
622 $res->{pmgversion
} = PMG
::pmgcfg
::package() . "/" .
623 PMG
::pmgcfg
::version_text
();
625 my $dinfo = df
('/', 1); # output is bytes
628 total
=> $dinfo->{blocks
},
629 avail
=> $dinfo->{bavail
},
630 used
=> $dinfo->{used
},
631 free
=> $dinfo->{bavail
} - $dinfo->{used
},
634 if (my $subinfo = PVE
::INotify
::read_file
('subscription')) {
635 if (my $level = $subinfo->{level
}) {
636 $res->{level
} = $level;
644 package PMG
::API2
::Nodes
;
649 use PVE
::RESTHandler
;
650 use PVE
::JSONSchema
qw(get_standard_option);
652 use PMG
::RESTEnvironment
;
654 use base
qw(PVE::RESTHandler);
656 __PACKAGE__-
>register_method ({
657 subclass
=> "PMG::API2::NodeInfo",
661 __PACKAGE__-
>register_method ({
665 permissions
=> { user
=> 'all' },
666 description
=> "Cluster node index.",
668 additionalProperties
=> 0,
677 links
=> [ { rel
=> 'child', href
=> "{node}" } ],
682 my $nodename = PVE
::INotify
::nodename
();
684 my $res = [ { node
=> $nodename } ];
688 $done->{$nodename} = 1;
690 my $restenv = PMG
::RESTEnvironment-
>get();
691 my $cinfo = $restenv->{cinfo
};
693 foreach my $ni (values %{$cinfo->{ids
}}) {
694 push @$res, { node
=> $ni->{name
} } if !$done->{$ni->{name
}};
695 $done->{$ni->{name
}} = 1;