]>
git.proxmox.com Git - pmg-api.git/blob - src/PMG/CLI/pmgdb.pm
1 package PMG
::CLI
::pmgdb
;
9 use PVE
::Tools
qw(extract_param);
14 use PMG
::RESTEnvironment
;
18 use PMG
::ClusterConfig
;
21 use PMG
::API2
::RuleDB
;
23 use base
qw(PVE::CLIHandler);
25 sub setup_environment
{
26 PMG
::RESTEnvironment-
>setup_default_cli_env();
30 my ($ruledb, $og) = @_;
32 my $objects = $ruledb->load_group_objects ($og->{id
});
34 foreach my $obj (@$objects) {
35 my $desc = encode
('UTF-8', $obj->short_desc());
36 print " OBJECT $obj->{id}: $desc\n";
41 my ($ruledb, $rule) = @_;
43 $ruledb->load_rule_attributes($rule);
50 my $active = $rule->{active
} ?
'active' : 'inactive';
51 my $dir = $direction->{$rule->{direction
}};
52 my $rulename = encode
('UTF-8', $rule->{name
});
54 print "Found RULE $rule->{id} (prio: $rule->{priority}, $dir, $active): $rulename\n";
56 my $print_group = sub {
57 my ($type, $og, $print_mode) = @_;
58 my $oname = encode
('UTF-8', $og->{name
});
61 my $and = $og->{and} // 0;
62 my $invert = $og->{invert
} // 0;
63 $mode = " (and=$and, invert=$invert)";
65 print " FOUND $type GROUP $og->{id}${mode}: $oname\n";
66 print_objects
($ruledb, $og);
69 my $print_type_mode = sub {
71 my $and = $rule->{"$type-and"};
72 my $invert = $rule->{"$type-invert"};
73 if (defined($and) || defined($invert)) {
74 my $print_type = uc($type);
75 print " $print_type mode: and=" . ($and // 0) . " invert=". ($invert // 0) . "\n";
79 my ($from, $to, $when, $what, $action) =
80 $ruledb->load_groups($rule);
82 $print_type_mode->("from") if scalar(@$from);
83 foreach my $og (@$from) {
84 $ruledb->load_group_attributes($og);
85 $print_group->("FROM", $og, 1);
87 $print_type_mode->("to") if scalar(@$to);
88 foreach my $og (@$to) {
89 $ruledb->load_group_attributes($og);
90 $print_group->("TO", $og, 1);
92 $print_type_mode->("when") if scalar(@$when);
93 foreach my $og (@$when) {
94 $ruledb->load_group_attributes($og);
95 $print_group->("WHEN", $og, 1);
97 $print_type_mode->("what") if scalar(@$what);
98 foreach my $og (@$what) {
99 $ruledb->load_group_attributes($og);
100 $print_group->("WHAT", $og, 1);
102 foreach my $og (@$action) {
103 $print_group->("ACTION", $og);
107 __PACKAGE__-
>register_method ({
111 description
=> "Print the PMG rule database.",
113 additionalProperties
=> 0,
116 returns
=> { type
=> 'null'},
120 my $dbh = PMG
::DBTools
::open_ruledb
("Proxmox_ruledb");
121 my $ruledb = PMG
::RuleDB-
>new($dbh);
123 my $rules = $ruledb->load_rules();
125 foreach my $rule (@$rules) {
126 print_rule
($ruledb, $rule);
135 __PACKAGE__-
>register_method ({
139 description
=> "Delete PMG rule database.",
141 additionalProperties
=> 0,
144 returns
=> { type
=> 'null'},
148 my $list = PMG
::DBTools
::database_list
();
150 my $dbname = "Proxmox_ruledb";
152 die "Database '$dbname' does not exist\n" if !$list->{$dbname};
154 syslog
('info', "delete rule database");
156 PMG
::DBTools
::delete_ruledb
($dbname);
161 __PACKAGE__-
>register_method ({
165 description
=> "Initialize/Upgrade the PMG rule database.",
167 additionalProperties
=> 0,
171 description
=> "Delete existing database.",
177 description
=> "Reset and update statistic database.",
183 returns
=> { type
=> 'null'},
187 PMG
::Utils
::cond_add_default_locale
();
189 my $list = PMG
::DBTools
::database_list
();
191 my $dbname = "Proxmox_ruledb";
193 if (!$list->{$dbname} || $param->{force
}) {
195 if ($list->{$dbname}) {
196 print "Destroy existing rule database\n";
197 PMG
::DBTools
::delete_ruledb
($dbname);
200 print "Initialize rule database\n";
202 my $dbh = PMG
::DBTools
::create_ruledb
($dbname);
203 my $ruledb = PMG
::RuleDB-
>new($dbh);
204 PMG
::DBTools
::init_ruledb
($ruledb);
210 my $dbh = PMG
::DBTools
::open_ruledb
("Proxmox_ruledb");
211 my $ruledb = PMG
::RuleDB-
>new($dbh);
213 print "Analyzing/Upgrading existing Databases...";
214 PMG
::DBTools
::upgradedb
($ruledb);
217 # reset and update statistic databases
218 if ($param->{statistics
}) {
219 print "Generating Proxmox Statistic Databases... ";
220 PMG
::Statistic
::clear_stats
($dbh);
221 my $cinfo = PVE
::INotify
::read_file
("cluster.conf");
222 PMG
::Statistic
::update_stats
($dbh, $cinfo);
233 __PACKAGE__-
>register_method ({
237 description
=> "Update the PMG statistic database.",
239 additionalProperties
=> 0,
242 returns
=> { type
=> 'null'},
246 my $dbh = PMG
::DBTools
::open_ruledb
("Proxmox_ruledb");
247 print "Updating Proxmox Statistic Databases... ";
248 my $cinfo = PVE
::INotify
::read_file
("cluster.conf");
249 PMG
::Statistic
::update_stats
($dbh, $cinfo);
257 'dump' => [ __PACKAGE__
, 'dump', []],
258 delete => [ __PACKAGE__
, 'delete', []],
259 init
=> [ __PACKAGE__
, 'init', []],
260 reset => [ 'PMG::API2::RuleDB', 'reset_ruledb', []],
261 update
=> [ __PACKAGE__
, 'update', []],