]>
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
;
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'.",
321 my $cmd = ["/usr/bin/mini-journalreader"];
322 push @$cmd, '-n', $param->{lastentries
} if $param->{lastentries
};
323 push @$cmd, '-b', $param->{since
} if $param->{since
};
324 push @$cmd, '-e', $param->{until} if $param->{until};
325 push @$cmd, '-f', $param->{startcursor
} if $param->{startcursor
};
326 push @$cmd, '-t', $param->{endcursor
} if $param->{endcursor
};
328 PVE
::Tools
::run_command
($cmd, outfunc
=> $parser);
334 __PACKAGE__-
>register_method ({
338 permissions
=> { check
=> [ 'admin' ] },
340 description
=> "Creates a Terminal proxy.",
342 additionalProperties
=> 0,
344 node
=> get_standard_option
('pve-node'),
347 description
=> "Run 'apt-get dist-upgrade' instead of normal shell.",
354 additionalProperties
=> 0,
356 user
=> { type
=> 'string' },
357 ticket
=> { type
=> 'string' },
358 port
=> { type
=> 'integer' },
359 upid
=> { type
=> 'string' },
365 my $node = $param->{node
};
367 if ($node ne PVE
::INotify
::nodename
()) {
368 die "termproxy to remote node not implemented";
371 my $authpath = "/nodes/$node";
373 my $restenv = PMG
::RESTEnvironment-
>get();
374 my $user = $restenv->get_user();
376 raise_perm_exc
('user != root@pam') if $param->{upgrade
} && $user ne 'root@pam';
378 my $ticket = PMG
::Ticket
::assemble_vnc_ticket
($user, $authpath);
380 my $family = PVE
::Tools
::get_host_address_family
($node);
381 my $port = PVE
::Tools
::next_vnc_port
($family);
385 if ($user eq 'root@pam') {
386 if ($param->{upgrade
}) {
387 my $upgradecmd = "pmgupgrade --shell";
388 # $upgradecmd = PVE::Tools::shellquote($upgradecmd) if $remip;
389 $shcmd = [ '/bin/bash', '-c', $upgradecmd ];
391 $shcmd = [ '/bin/login', '-f', 'root' ];
394 $shcmd = [ '/bin/login' ];
397 my $cmd = ['/usr/bin/termproxy', $port, '--path', $authpath,
403 syslog
('info', "starting termproxy $upid\n");
405 my $cmdstr = join (' ', @$cmd);
406 syslog
('info', "launch command: $cmdstr");
408 PVE
::Tools
::run_command
($cmd);
413 my $upid = $restenv->fork_worker('termproxy', "", $user, $realcmd);
415 PVE
::Tools
::wait_for_vnc_port
($port);
425 __PACKAGE__-
>register_method({
426 name
=> 'vncwebsocket',
427 path
=> 'vncwebsocket',
429 permissions
=> { check
=> [ 'admin' ] },
430 description
=> "Opens a weksocket for VNC traffic.",
432 additionalProperties
=> 0,
434 node
=> get_standard_option
('pve-node'),
436 description
=> "Ticket from previous call to vncproxy.",
441 description
=> "Port number returned by previous vncproxy call.",
451 port
=> { type
=> 'string' },
457 my $authpath = "/nodes/$param->{node}";
459 my $restenv = PMG
::RESTEnvironment-
>get();
460 my $user = $restenv->get_user();
462 PMG
::Ticket
::verify_vnc_ticket
($param->{vncticket
}, $user, $authpath);
464 my $port = $param->{port
};
466 return { port
=> $port };
469 __PACKAGE__-
>register_method({
473 description
=> "Read DNS settings.",
475 permissions
=> { check
=> [ 'admin', 'audit' ] },
477 additionalProperties
=> 0,
479 node
=> get_standard_option
('pve-node'),
484 additionalProperties
=> 0,
487 description
=> "Search domain for host-name lookup.",
492 description
=> 'First name server IP address.',
497 description
=> 'Second name server IP address.',
502 description
=> 'Third name server IP address.',
511 my $res = PVE
::INotify
::read_file
('resolvconf');
516 __PACKAGE__-
>register_method({
517 name
=> 'update_dns',
520 description
=> "Write DNS settings.",
524 additionalProperties
=> 0,
526 node
=> get_standard_option
('pve-node'),
528 description
=> "Search domain for host-name lookup.",
532 description
=> 'First name server IP address.',
533 type
=> 'string', format
=> 'ip',
537 description
=> 'Second name server IP address.',
538 type
=> 'string', format
=> 'ip',
542 description
=> 'Third name server IP address.',
543 type
=> 'string', format
=> 'ip',
548 returns
=> { type
=> "null" },
552 PVE
::INotify
::update_file
('resolvconf', $param);
558 __PACKAGE__-
>register_method({
562 description
=> "Read server time and time zone settings.",
564 permissions
=> { check
=> [ 'admin', 'audit' ] },
566 additionalProperties
=> 0,
568 node
=> get_standard_option
('pve-node'),
573 additionalProperties
=> 0,
576 description
=> "Time zone",
580 description
=> "Seconds since 1970-01-01 00:00:00 UTC.",
582 minimum
=> 1297163644,
585 description
=> "Seconds since 1970-01-01 00:00:00 (local time)",
587 minimum
=> 1297163644,
595 my $ltime = timegm_nocheck
(localtime($ctime));
597 timezone
=> PVE
::INotify
::read_file
('timezone'),
605 __PACKAGE__-
>register_method({
606 name
=> 'set_timezone',
609 description
=> "Set time zone.",
613 additionalProperties
=> 0,
615 node
=> get_standard_option
('pve-node'),
617 description
=> "Time zone. The file '/usr/share/zoneinfo/zone.tab' contains the list of valid names.",
622 returns
=> { type
=> "null" },
626 PVE
::INotify
::write_file
('timezone', $param->{timezone
});
631 __PACKAGE__-
>register_method({
635 description
=> "Read server status. This is used by the cluster manager to test the node health.",
637 permissions
=> { check
=> [ 'admin', 'qmanager', 'audit' ] },
640 additionalProperties
=> 0,
642 node
=> get_standard_option
('pve-node'),
647 additionalProperties
=> 1,
650 description
=> "Seconds since 1970-01-01 00:00:00 UTC.",
652 minimum
=> 1297163644,
655 description
=> "The uptime of the system in seconds.",
660 description
=> "Database is synced with other nodes.",
668 my $restenv = PMG
::RESTEnvironment-
>get();
669 my $cinfo = $restenv->{cinfo
};
673 my $res = { time => $ctime, insync
=> 1 };
675 my $si = PMG
::DBTools
::cluster_sync_status
($cinfo);
676 foreach my $cid (keys %$si) {
677 my $lastsync = $si->{$cid};
678 my $sdiff = $ctime - $lastsync;
679 $sdiff = 0 if $sdiff < 0;
680 $res->{insync
} = 0 if $sdiff > (60*3);
683 my ($uptime, $idle) = PVE
::ProcFSTools
::read_proc_uptime
();
684 $res->{uptime
} = $uptime;
686 my ($avg1, $avg5, $avg15) = PVE
::ProcFSTools
::read_loadavg
();
687 $res->{loadavg
} = [ $avg1, $avg5, $avg15];
689 my ($sysname, $nodename, $release, $version, $machine) = POSIX
::uname
();
691 $res->{kversion
} = "$sysname $release $version";
693 $res->{cpuinfo
} = PVE
::ProcFSTools
::read_cpuinfo
();
695 my $stat = PVE
::ProcFSTools
::read_proc_stat
();
696 $res->{cpu
} = $stat->{cpu
};
697 $res->{wait} = $stat->{wait};
699 my $meminfo = PVE
::ProcFSTools
::read_meminfo
();
701 free
=> $meminfo->{memfree
},
702 total
=> $meminfo->{memtotal
},
703 used
=> $meminfo->{memused
},
707 free
=> $meminfo->{swapfree
},
708 total
=> $meminfo->{swaptotal
},
709 used
=> $meminfo->{swapused
},
712 $res->{pmgversion
} = PMG
::pmgcfg
::package() . "/" .
713 PMG
::pmgcfg
::version_text
();
715 my $dinfo = df
('/', 1); # output is bytes
718 total
=> $dinfo->{blocks
},
719 avail
=> $dinfo->{bavail
},
720 used
=> $dinfo->{used
},
721 free
=> $dinfo->{blocks
} - $dinfo->{used
},
724 if (my $subinfo = PVE
::INotify
::read_file
('subscription')) {
725 if (my $level = $subinfo->{level
}) {
726 $res->{level
} = $level;
733 __PACKAGE__-
>register_method({
737 permissions
=> { check
=> [ 'admin' ] },
739 description
=> "Reboot or shutdown a node.",
742 additionalProperties
=> 0,
744 node
=> get_standard_option
('pve-node'),
746 description
=> "Specify the command.",
748 enum
=> [qw(reboot shutdown)],
752 returns
=> { type
=> "null" },
756 if ($param->{command
} eq 'reboot') {
757 system ("(sleep 2;/sbin/reboot)&");
758 } elsif ($param->{command
} eq 'shutdown') {
759 system ("(sleep 2;/sbin/poweroff)&");
765 package PMG
::API2
::Nodes
;
770 use PVE
::RESTHandler
;
771 use PVE
::JSONSchema
qw(get_standard_option);
773 use PMG
::RESTEnvironment
;
775 use base
qw(PVE::RESTHandler);
777 __PACKAGE__-
>register_method ({
778 subclass
=> "PMG::API2::NodeInfo",
782 __PACKAGE__-
>register_method ({
786 permissions
=> { user
=> 'all' },
787 description
=> "Cluster node index.",
789 additionalProperties
=> 0,
798 links
=> [ { rel
=> 'child', href
=> "{node}" } ],
803 my $nodename = PVE
::INotify
::nodename
();
805 my $res = [ { node
=> $nodename } ];
809 $done->{$nodename} = 1;
811 my $restenv = PMG
::RESTEnvironment-
>get();
812 my $cinfo = $restenv->{cinfo
};
814 foreach my $ni (values %{$cinfo->{ids
}}) {
815 push @$res, { node
=> $ni->{name
} } if !$done->{$ni->{name
}};
816 $done->{$ni->{name
}} = 1;