]>
git.proxmox.com Git - pmg-api.git/blob - PMG/API2/Nodes.pm
7734977e9aadd3319dbcc72169be25778c096065
1 package PMG
::API2
::NodeInfo
;
5 use Time
::Local
qw(timegm_nocheck);
11 use PVE
::JSONSchema
qw(get_standard_option);
12 use PMG
::RESTEnvironment
;
19 use PMG
::API2
::Subscription
;
22 use PMG
::API2
::Services
;
23 use PMG
::API2
::Network
;
24 use PMG
::API2
::ClamAV
;
25 use PMG
::API2
::SpamAssassin
;
26 use PMG
::API2
::Postfix
;
27 use PMG
::API2
::MailTracker
;
28 use PMG
::API2
::Backup
;
30 use base
qw(PVE::RESTHandler);
32 __PACKAGE__-
>register_method ({
33 subclass
=> "PMG::API2::Postfix",
37 __PACKAGE__-
>register_method ({
38 subclass
=> "PMG::API2::ClamAV",
42 __PACKAGE__-
>register_method ({
43 subclass
=> "PMG::API2::SpamAssassin",
44 path
=> 'spamassassin',
47 __PACKAGE__-
>register_method ({
48 subclass
=> "PMG::API2::Network",
52 __PACKAGE__-
>register_method ({
53 subclass
=> "PMG::API2::Tasks",
57 __PACKAGE__-
>register_method ({
58 subclass
=> "PMG::API2::Services",
62 __PACKAGE__-
>register_method ({
63 subclass
=> "PMG::API2::Subscription",
64 path
=> 'subscription',
67 __PACKAGE__-
>register_method ({
68 subclass
=> "PMG::API2::APT",
72 __PACKAGE__-
>register_method ({
73 subclass
=> "PMG::API2::MailTracker",
77 __PACKAGE__-
>register_method ({
78 subclass
=> "PMG::API2::Backup",
82 __PACKAGE__-
>register_method ({
86 permissions
=> { user
=> 'all' },
87 description
=> "Node index.",
89 additionalProperties
=> 0,
91 node
=> get_standard_option
('pve-node'),
100 links
=> [ { rel
=> 'child', href
=> "{name}" } ],
107 { name
=> 'backup' },
108 { name
=> 'clamav' },
109 { name
=> 'spamassassin' },
110 { name
=> 'postfix' },
111 { name
=> 'services' },
112 { name
=> 'syslog' },
114 { name
=> 'tracker' },
116 { name
=> 'report' },
117 { name
=> 'status' },
118 { name
=> 'subscription' },
119 { name
=> 'termproxy' },
120 { name
=> 'rrddata' },
126 __PACKAGE__-
>register_method({
132 permissions
=> { check
=> [ 'admin', 'audit' ] },
133 description
=> "Gather various system information about a node",
135 additionalProperties
=> 0,
137 node
=> get_standard_option
('pve-node'),
144 return PMG
::Report
::generate
();
147 __PACKAGE__-
>register_method({
151 protected
=> 1, # fixme: can we avoid that?
153 permissions
=> { check
=> [ 'admin', 'audit' ] },
154 description
=> "Read node RRD statistics",
156 additionalProperties
=> 0,
158 node
=> get_standard_option
('pve-node'),
160 description
=> "Specify the time frame you are interested in.",
162 enum
=> [ 'hour', 'day', 'week', 'month', 'year' ],
165 description
=> "The RRD consolidation function",
167 enum
=> [ 'AVERAGE', 'MAX' ],
182 return PMG
::Utils
::create_rrd_data
(
183 "pmg-node-v1.rrd", $param->{timeframe
}, $param->{cf
});
187 __PACKAGE__-
>register_method({
191 description
=> "Read system log",
194 permissions
=> { check
=> [ 'admin', 'audit' ] },
196 additionalProperties
=> 0,
198 node
=> get_standard_option
('pve-node'),
211 pattern
=> '^\d{4}-\d{2}-\d{2}( \d{2}:\d{2}(:\d{2})?)?$',
212 description
=> "Display all log since this date-time string.",
217 pattern
=> '^\d{4}-\d{2}-\d{2}( \d{2}:\d{2}(:\d{2})?)?$',
218 description
=> "Display all log until this date-time string.",
222 description
=> "Service ID",
235 description
=> "Line number",
239 description
=> "Line text",
248 my $restenv = PMG
::RESTEnvironment-
>get();
250 my $service = $param->{service
};
251 $service = PMG
::Utils
::lookup_real_service_name
($service)
254 my ($count, $lines) = PVE
::Tools
::dump_journal
(
255 $param->{start
}, $param->{limit
},
256 $param->{since
}, $param->{'until'}, $service);
258 $restenv->set_result_attrib('total', $count);
264 __PACKAGE__-
>register_method ({
268 permissions
=> { check
=> [ 'admin' ] },
270 description
=> "Creates a Terminal proxy.",
272 additionalProperties
=> 0,
274 node
=> get_standard_option
('pve-node'),
277 description
=> "Run 'apt-get dist-upgrade' instead of normal shell.",
284 additionalProperties
=> 0,
286 user
=> { type
=> 'string' },
287 ticket
=> { type
=> 'string' },
288 port
=> { type
=> 'integer' },
289 upid
=> { type
=> 'string' },
295 my $node = $param->{node
};
297 if ($node ne PVE
::INotify
::nodename
()) {
298 die "termproxy to remote node not implemented";
301 my $authpath = "/nodes/$node";
303 my $restenv = PMG
::RESTEnvironment-
>get();
304 my $user = $restenv->get_user();
306 raise_perm_exc
('user != root@pam') if $param->{upgrade
} && $user ne 'root@pam';
308 my $ticket = PMG
::Ticket
::assemble_vnc_ticket
($user, $authpath);
310 my $family = PVE
::Tools
::get_host_address_family
($node);
311 my $port = PVE
::Tools
::next_vnc_port
($family);
315 if ($user eq 'root@pam') {
316 if ($param->{upgrade
}) {
317 my $upgradecmd = "pmgupgrade --shell";
318 # $upgradecmd = PVE::Tools::shellquote($upgradecmd) if $remip;
319 $shcmd = [ '/bin/bash', '-c', $upgradecmd ];
321 $shcmd = [ '/bin/login', '-f', 'root' ];
324 $shcmd = [ '/bin/login' ];
327 my $cmd = ['/usr/bin/termproxy', $port, '--path', $authpath,
333 syslog
('info', "starting termproxy $upid\n");
335 my $cmdstr = join (' ', @$cmd);
336 syslog
('info', "launch command: $cmdstr");
338 PVE
::Tools
::run_command
($cmd);
343 my $upid = $restenv->fork_worker('termproxy', "", $user, $realcmd);
345 PVE
::Tools
::wait_for_vnc_port
($port);
355 __PACKAGE__-
>register_method({
356 name
=> 'vncwebsocket',
357 path
=> 'vncwebsocket',
359 permissions
=> { check
=> [ 'admin' ] },
360 description
=> "Opens a weksocket for VNC traffic.",
362 additionalProperties
=> 0,
364 node
=> get_standard_option
('pve-node'),
366 description
=> "Ticket from previous call to vncproxy.",
371 description
=> "Port number returned by previous vncproxy call.",
381 port
=> { type
=> 'string' },
387 my $authpath = "/nodes/$param->{node}";
389 my $restenv = PMG
::RESTEnvironment-
>get();
390 my $user = $restenv->get_user();
392 PMG
::Ticket
::verify_vnc_ticket
($param->{vncticket
}, $user, $authpath);
394 my $port = $param->{port
};
396 return { port
=> $port };
399 __PACKAGE__-
>register_method({
403 description
=> "Read DNS settings.",
405 permissions
=> { check
=> [ 'admin', 'audit' ] },
407 additionalProperties
=> 0,
409 node
=> get_standard_option
('pve-node'),
414 additionalProperties
=> 0,
417 description
=> "Search domain for host-name lookup.",
422 description
=> 'First name server IP address.',
427 description
=> 'Second name server IP address.',
432 description
=> 'Third name server IP address.',
441 my $res = PVE
::INotify
::read_file
('resolvconf');
446 __PACKAGE__-
>register_method({
447 name
=> 'update_dns',
450 description
=> "Write DNS settings.",
454 additionalProperties
=> 0,
456 node
=> get_standard_option
('pve-node'),
458 description
=> "Search domain for host-name lookup.",
462 description
=> 'First name server IP address.',
463 type
=> 'string', format
=> 'ip',
467 description
=> 'Second name server IP address.',
468 type
=> 'string', format
=> 'ip',
472 description
=> 'Third name server IP address.',
473 type
=> 'string', format
=> 'ip',
478 returns
=> { type
=> "null" },
482 PVE
::INotify
::update_file
('resolvconf', $param);
488 __PACKAGE__-
>register_method({
492 description
=> "Read server time and time zone settings.",
494 permissions
=> { check
=> [ 'admin', 'audit' ] },
496 additionalProperties
=> 0,
498 node
=> get_standard_option
('pve-node'),
503 additionalProperties
=> 0,
506 description
=> "Time zone",
510 description
=> "Seconds since 1970-01-01 00:00:00 UTC.",
512 minimum
=> 1297163644,
515 description
=> "Seconds since 1970-01-01 00:00:00 (local time)",
517 minimum
=> 1297163644,
525 my $ltime = timegm_nocheck
(localtime($ctime));
527 timezone
=> PVE
::INotify
::read_file
('timezone'),
535 __PACKAGE__-
>register_method({
536 name
=> 'set_timezone',
539 description
=> "Set time zone.",
543 additionalProperties
=> 0,
545 node
=> get_standard_option
('pve-node'),
547 description
=> "Time zone. The file '/usr/share/zoneinfo/zone.tab' contains the list of valid names.",
552 returns
=> { type
=> "null" },
556 PVE
::INotify
::write_file
('timezone', $param->{timezone
});
561 __PACKAGE__-
>register_method({
565 description
=> "Read server status. This is used by the cluster manager to test the node health.",
567 permissions
=> { check
=> [ 'admin', 'qmanager', 'audit' ] },
570 additionalProperties
=> 0,
572 node
=> get_standard_option
('pve-node'),
577 additionalProperties
=> 1,
580 description
=> "Seconds since 1970-01-01 00:00:00 UTC.",
582 minimum
=> 1297163644,
585 description
=> "The uptime of the system in seconds.",
590 description
=> "Database is synced with other nodes.",
598 my $restenv = PMG
::RESTEnvironment-
>get();
599 my $cinfo = $restenv->{cinfo
};
603 my $res = { time => $ctime, insync
=> 1 };
605 my $si = PMG
::DBTools
::cluster_sync_status
($cinfo);
606 foreach my $cid (keys %$si) {
607 my $lastsync = $si->{$cid};
608 my $sdiff = $ctime - $lastsync;
609 $sdiff = 0 if $sdiff < 0;
610 $res->{insync
} = 0 if $sdiff > (60*3);
613 my ($uptime, $idle) = PVE
::ProcFSTools
::read_proc_uptime
();
614 $res->{uptime
} = $uptime;
616 my ($avg1, $avg5, $avg15) = PVE
::ProcFSTools
::read_loadavg
();
617 $res->{loadavg
} = [ $avg1, $avg5, $avg15];
619 my ($sysname, $nodename, $release, $version, $machine) = POSIX
::uname
();
621 $res->{kversion
} = "$sysname $release $version";
623 $res->{cpuinfo
} = PVE
::ProcFSTools
::read_cpuinfo
();
625 my $stat = PVE
::ProcFSTools
::read_proc_stat
();
626 $res->{cpu
} = $stat->{cpu
};
627 $res->{wait} = $stat->{wait};
629 my $meminfo = PVE
::ProcFSTools
::read_meminfo
();
631 free
=> $meminfo->{memfree
},
632 total
=> $meminfo->{memtotal
},
633 used
=> $meminfo->{memused
},
637 free
=> $meminfo->{swapfree
},
638 total
=> $meminfo->{swaptotal
},
639 used
=> $meminfo->{swapused
},
642 $res->{pmgversion
} = PMG
::pmgcfg
::package() . "/" .
643 PMG
::pmgcfg
::version_text
();
645 my $dinfo = df
('/', 1); # output is bytes
648 total
=> $dinfo->{blocks
},
649 avail
=> $dinfo->{bavail
},
650 used
=> $dinfo->{used
},
651 free
=> $dinfo->{blocks
} - $dinfo->{used
},
654 if (my $subinfo = PVE
::INotify
::read_file
('subscription')) {
655 if (my $level = $subinfo->{level
}) {
656 $res->{level
} = $level;
663 __PACKAGE__-
>register_method({
667 permissions
=> { check
=> [ 'admin' ] },
669 description
=> "Reboot or shutdown a node.",
672 additionalProperties
=> 0,
674 node
=> get_standard_option
('pve-node'),
676 description
=> "Specify the command.",
678 enum
=> [qw(reboot shutdown)],
682 returns
=> { type
=> "null" },
686 if ($param->{command
} eq 'reboot') {
687 system ("(sleep 2;/sbin/reboot)&");
688 } elsif ($param->{command
} eq 'shutdown') {
689 system ("(sleep 2;/sbin/poweroff)&");
695 package PMG
::API2
::Nodes
;
700 use PVE
::RESTHandler
;
701 use PVE
::JSONSchema
qw(get_standard_option);
703 use PMG
::RESTEnvironment
;
705 use base
qw(PVE::RESTHandler);
707 __PACKAGE__-
>register_method ({
708 subclass
=> "PMG::API2::NodeInfo",
712 __PACKAGE__-
>register_method ({
716 permissions
=> { user
=> 'all' },
717 description
=> "Cluster node index.",
719 additionalProperties
=> 0,
728 links
=> [ { rel
=> 'child', href
=> "{node}" } ],
733 my $nodename = PVE
::INotify
::nodename
();
735 my $res = [ { node
=> $nodename } ];
739 $done->{$nodename} = 1;
741 my $restenv = PMG
::RESTEnvironment-
>get();
742 my $cinfo = $restenv->{cinfo
};
744 foreach my $ni (values %{$cinfo->{ids
}}) {
745 push @$res, { node
=> $ni->{name
} } if !$done->{$ni->{name
}};
746 $done->{$ni->{name
}} = 1;