]>
git.proxmox.com Git - pmg-api.git/blob - src/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
;
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' },
113 { name
=> 'journal' },
115 { name
=> 'tracker' },
117 { name
=> 'report' },
118 { name
=> 'status' },
119 { name
=> 'subscription' },
120 { name
=> 'termproxy' },
121 { name
=> 'rrddata' },
127 __PACKAGE__-
>register_method({
133 permissions
=> { check
=> [ 'admin', 'audit' ] },
134 description
=> "Gather various system information about a node",
136 additionalProperties
=> 0,
138 node
=> get_standard_option
('pve-node'),
145 return PMG
::Report
::generate
();
148 __PACKAGE__-
>register_method({
152 protected
=> 1, # fixme: can we avoid that?
154 permissions
=> { check
=> [ 'admin', 'audit' ] },
155 description
=> "Read node RRD statistics",
157 additionalProperties
=> 0,
159 node
=> get_standard_option
('pve-node'),
161 description
=> "Specify the time frame you are interested in.",
163 enum
=> [ 'hour', 'day', 'week', 'month', 'year' ],
166 description
=> "The RRD consolidation function",
168 enum
=> [ 'AVERAGE', 'MAX' ],
183 return PMG
::Utils
::create_rrd_data
(
184 "pmg-node-v1.rrd", $param->{timeframe
}, $param->{cf
});
188 __PACKAGE__-
>register_method({
192 description
=> "Read system log",
195 permissions
=> { check
=> [ 'admin', 'audit' ] },
197 additionalProperties
=> 0,
199 node
=> get_standard_option
('pve-node'),
212 pattern
=> '^\d{4}-\d{2}-\d{2}( \d{2}:\d{2}(:\d{2})?)?$',
213 description
=> "Display all log since this date-time string.",
218 pattern
=> '^\d{4}-\d{2}-\d{2}( \d{2}:\d{2}(:\d{2})?)?$',
219 description
=> "Display all log until this date-time string.",
223 description
=> "Service ID",
236 description
=> "Line number",
240 description
=> "Line text",
249 my $restenv = PMG
::RESTEnvironment-
>get();
251 my $service = $param->{service
};
252 $service = PMG
::Utils
::lookup_real_service_name
($service)
255 my ($count, $lines) = PVE
::Tools
::dump_journal
(
256 $param->{start
}, $param->{limit
},
257 $param->{since
}, $param->{'until'}, $service);
259 $restenv->set_result_attrib('total', $count);
264 __PACKAGE__-
>register_method({
268 description
=> "Read Journal",
270 permissions
=> { check
=> [ 'admin', 'audit' ] },
273 additionalProperties
=> 0,
275 node
=> get_standard_option
('pve-node'),
277 description
=> "Display all log since this UNIX epoch. Conflicts with 'startcursor'.",
283 description
=> "Display all log until this UNIX epoch. Conflicts with 'endcursor'.",
289 description
=> "Limit to the last X lines. Conflicts with a range.",
295 description
=> "Start after the given Cursor. Conflicts with 'since'.",
300 description
=> "End before the given Cursor. Conflicts with 'until'.",
315 my $cmd = ["/usr/bin/mini-journalreader"];
316 push @$cmd, '-n', $param->{lastentries
} if $param->{lastentries
};
317 push @$cmd, '-b', $param->{since
} if $param->{since
};
318 push @$cmd, '-e', $param->{until} if $param->{until};
319 push @$cmd, '-f', $param->{startcursor
} if $param->{startcursor
};
320 push @$cmd, '-t', $param->{endcursor
} if $param->{endcursor
};
323 my $parser = sub { push @$lines, shift };
325 PVE
::Tools
::run_command
($cmd, outfunc
=> $parser);
331 __PACKAGE__-
>register_method ({
335 permissions
=> { check
=> [ 'admin' ] },
337 description
=> "Creates a Terminal proxy.",
339 additionalProperties
=> 0,
341 node
=> get_standard_option
('pve-node'),
344 description
=> "Run 'apt-get dist-upgrade' instead of normal shell.",
351 additionalProperties
=> 0,
353 user
=> { type
=> 'string' },
354 ticket
=> { type
=> 'string' },
355 port
=> { type
=> 'integer' },
356 upid
=> { type
=> 'string' },
362 my $node = $param->{node
};
364 if ($node ne PVE
::INotify
::nodename
()) {
365 die "termproxy to remote node not implemented";
368 my $authpath = "/nodes/$node";
370 my $restenv = PMG
::RESTEnvironment-
>get();
371 my $user = $restenv->get_user();
373 raise_perm_exc
('user != root@pam') if $param->{upgrade
} && $user ne 'root@pam';
375 my $ticket = PMG
::Ticket
::assemble_vnc_ticket
($user, $authpath);
377 my $family = PVE
::Tools
::get_host_address_family
($node);
378 my $port = PVE
::Tools
::next_vnc_port
($family);
382 if ($user eq 'root@pam') {
383 if ($param->{upgrade
}) {
384 my $upgradecmd = "pmgupgrade --shell";
385 # $upgradecmd = PVE::Tools::shellquote($upgradecmd) if $remip;
386 $shcmd = [ '/bin/bash', '-c', $upgradecmd ];
388 $shcmd = [ '/bin/login', '-f', 'root' ];
391 $shcmd = [ '/bin/login' ];
394 my $cmd = ['/usr/bin/termproxy', $port, '--path', $authpath,
400 syslog
('info', "starting termproxy $upid\n");
402 my $cmdstr = join (' ', @$cmd);
403 syslog
('info', "launch command: $cmdstr");
405 PVE
::Tools
::run_command
($cmd);
410 my $upid = $restenv->fork_worker('termproxy', "", $user, $realcmd);
412 PVE
::Tools
::wait_for_vnc_port
($port);
422 __PACKAGE__-
>register_method({
423 name
=> 'vncwebsocket',
424 path
=> 'vncwebsocket',
426 permissions
=> { check
=> [ 'admin' ] },
427 description
=> "Opens a weksocket for VNC traffic.",
429 additionalProperties
=> 0,
431 node
=> get_standard_option
('pve-node'),
433 description
=> "Ticket from previous call to vncproxy.",
438 description
=> "Port number returned by previous vncproxy call.",
448 port
=> { type
=> 'string' },
454 my $authpath = "/nodes/$param->{node}";
456 my $restenv = PMG
::RESTEnvironment-
>get();
457 my $user = $restenv->get_user();
459 PMG
::Ticket
::verify_vnc_ticket
($param->{vncticket
}, $user, $authpath);
461 my $port = $param->{port
};
463 return { port
=> $port };
466 __PACKAGE__-
>register_method({
470 description
=> "Read DNS settings.",
472 permissions
=> { check
=> [ 'admin', 'audit' ] },
474 additionalProperties
=> 0,
476 node
=> get_standard_option
('pve-node'),
481 additionalProperties
=> 0,
484 description
=> "Search domain for host-name lookup.",
489 description
=> 'First name server IP address.',
494 description
=> 'Second name server IP address.',
499 description
=> 'Third name server IP address.',
508 my $res = PVE
::INotify
::read_file
('resolvconf');
513 __PACKAGE__-
>register_method({
514 name
=> 'update_dns',
517 description
=> "Write DNS settings.",
521 additionalProperties
=> 0,
523 node
=> get_standard_option
('pve-node'),
525 description
=> "Search domain for host-name lookup.",
529 description
=> 'First name server IP address.',
530 type
=> 'string', format
=> 'ip',
534 description
=> 'Second name server IP address.',
535 type
=> 'string', format
=> 'ip',
539 description
=> 'Third name server IP address.',
540 type
=> 'string', format
=> 'ip',
545 returns
=> { type
=> "null" },
549 PVE
::INotify
::update_file
('resolvconf', $param);
555 __PACKAGE__-
>register_method({
559 description
=> "Read server time and time zone settings.",
561 permissions
=> { check
=> [ 'admin', 'audit' ] },
563 additionalProperties
=> 0,
565 node
=> get_standard_option
('pve-node'),
570 additionalProperties
=> 0,
573 description
=> "Time zone",
577 description
=> "Seconds since 1970-01-01 00:00:00 UTC.",
579 minimum
=> 1297163644,
582 description
=> "Seconds since 1970-01-01 00:00:00 (local time)",
584 minimum
=> 1297163644,
592 my $ltime = timegm_nocheck
(localtime($ctime));
594 timezone
=> PVE
::INotify
::read_file
('timezone'),
602 __PACKAGE__-
>register_method({
603 name
=> 'set_timezone',
606 description
=> "Set time zone.",
610 additionalProperties
=> 0,
612 node
=> get_standard_option
('pve-node'),
614 description
=> "Time zone. The file '/usr/share/zoneinfo/zone.tab' contains the list of valid names.",
619 returns
=> { type
=> "null" },
623 PVE
::INotify
::write_file
('timezone', $param->{timezone
});
628 __PACKAGE__-
>register_method({
632 description
=> "Read server status. This is used by the cluster manager to test the node health.",
634 permissions
=> { check
=> [ 'admin', 'qmanager', 'audit' ] },
637 additionalProperties
=> 0,
639 node
=> get_standard_option
('pve-node'),
644 additionalProperties
=> 1,
647 description
=> "Seconds since 1970-01-01 00:00:00 UTC.",
649 minimum
=> 1297163644,
652 description
=> "The uptime of the system in seconds.",
657 description
=> "Database is synced with other nodes.",
665 my $restenv = PMG
::RESTEnvironment-
>get();
666 my $cinfo = $restenv->{cinfo
};
670 my $res = { time => $ctime, insync
=> 1 };
672 my $si = PMG
::DBTools
::cluster_sync_status
($cinfo);
673 foreach my $cid (keys %$si) {
674 my $lastsync = $si->{$cid};
675 my $sdiff = $ctime - $lastsync;
676 $sdiff = 0 if $sdiff < 0;
677 $res->{insync
} = 0 if $sdiff > (60*3);
680 my ($uptime, $idle) = PVE
::ProcFSTools
::read_proc_uptime
();
681 $res->{uptime
} = $uptime;
683 my ($avg1, $avg5, $avg15) = PVE
::ProcFSTools
::read_loadavg
();
684 $res->{loadavg
} = [ $avg1, $avg5, $avg15];
686 my ($sysname, $nodename, $release, $version, $machine) = POSIX
::uname
();
688 $res->{kversion
} = "$sysname $release $version";
690 $res->{cpuinfo
} = PVE
::ProcFSTools
::read_cpuinfo
();
692 my $stat = PVE
::ProcFSTools
::read_proc_stat
();
693 $res->{cpu
} = $stat->{cpu
};
694 $res->{wait} = $stat->{wait};
696 my $meminfo = PVE
::ProcFSTools
::read_meminfo
();
698 free
=> $meminfo->{memfree
},
699 total
=> $meminfo->{memtotal
},
700 used
=> $meminfo->{memused
},
704 free
=> $meminfo->{swapfree
},
705 total
=> $meminfo->{swaptotal
},
706 used
=> $meminfo->{swapused
},
709 $res->{pmgversion
} = PMG
::pmgcfg
::package() . "/" .
710 PMG
::pmgcfg
::version_text
();
712 my $dinfo = df
('/', 1); # output is bytes
715 total
=> $dinfo->{blocks
},
716 avail
=> $dinfo->{bavail
},
717 used
=> $dinfo->{used
},
718 free
=> $dinfo->{blocks
} - $dinfo->{used
},
721 if (my $subinfo = PVE
::INotify
::read_file
('subscription')) {
722 if (my $level = $subinfo->{level
}) {
723 $res->{level
} = $level;
730 __PACKAGE__-
>register_method({
734 permissions
=> { check
=> [ 'admin' ] },
736 description
=> "Reboot or shutdown a node.",
739 additionalProperties
=> 0,
741 node
=> get_standard_option
('pve-node'),
743 description
=> "Specify the command.",
745 enum
=> [qw(reboot shutdown)],
749 returns
=> { type
=> "null" },
753 if ($param->{command
} eq 'reboot') {
754 system ("(sleep 2;/sbin/reboot)&");
755 } elsif ($param->{command
} eq 'shutdown') {
756 system ("(sleep 2;/sbin/poweroff)&");
762 package PMG
::API2
::Nodes
;
767 use PVE
::RESTHandler
;
768 use PVE
::JSONSchema
qw(get_standard_option);
770 use PMG
::RESTEnvironment
;
772 use base
qw(PVE::RESTHandler);
774 __PACKAGE__-
>register_method ({
775 subclass
=> "PMG::API2::NodeInfo",
779 __PACKAGE__-
>register_method ({
783 permissions
=> { user
=> 'all' },
784 description
=> "Cluster node index.",
786 additionalProperties
=> 0,
795 links
=> [ { rel
=> 'child', href
=> "{node}" } ],
800 my $nodename = PVE
::INotify
::nodename
();
802 my $res = [ { node
=> $nodename } ];
806 $done->{$nodename} = 1;
808 my $restenv = PMG
::RESTEnvironment-
>get();
809 my $cinfo = $restenv->{cinfo
};
811 foreach my $ni (values %{$cinfo->{ids
}}) {
812 push @$res, { node
=> $ni->{name
} } if !$done->{$ni->{name
}};
813 $done->{$ni->{name
}} = 1;