]>
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 $type_text = $obj->otype_text();
36 my $desc = encode
('UTF-8', $obj->short_desc());
37 print " OBJECT $type_text $obj->{id}: $desc\n";
42 my ($ruledb, $rule, $rule_status) = @_;
44 $ruledb->load_rule_attributes($rule);
46 return if !$rule->{active
} && $rule_status eq 'active';
47 return if $rule->{active
} && $rule_status eq 'inactive';
54 my $active = $rule->{active
} ?
'ACTIVE' : 'inactive';
55 my $dir = $direction->{$rule->{direction
}};
56 my $rulename = encode
('UTF-8', $rule->{name
});
58 print "RULE $rule->{id} (prio: $rule->{priority}, $dir, $active): $rulename\n";
60 my $print_group = sub {
61 my ($type, $og, $print_mode) = @_;
62 my $oname = encode
('UTF-8', $og->{name
});
65 my $and = $og->{and} // 0;
66 my $invert = $og->{invert
} // 0;
67 $mode = " (and=$and, invert=$invert)";
69 print " $type group $og->{id}${mode}: $oname\n";
70 print_objects
($ruledb, $og);
73 my $print_type_mode = sub {
75 my $and = $rule->{"$type-and"};
76 my $invert = $rule->{"$type-invert"};
77 if (defined($and) || defined($invert)) {
78 my $print_type = uc($type);
79 print " $print_type mode: and=" . ($and // 0) . " invert=". ($invert // 0) . "\n";
83 my ($from, $to, $when, $what, $action) =
84 $ruledb->load_groups($rule);
86 $print_type_mode->("from") if scalar(@$from);
87 foreach my $og (@$from) {
88 $ruledb->load_group_attributes($og);
89 $print_group->("FROM", $og, 1);
91 $print_type_mode->("to") if scalar(@$to);
92 foreach my $og (@$to) {
93 $ruledb->load_group_attributes($og);
94 $print_group->("TO", $og, 1);
96 $print_type_mode->("when") if scalar(@$when);
97 foreach my $og (@$when) {
98 $ruledb->load_group_attributes($og);
99 $print_group->("WHEN", $og, 1);
101 $print_type_mode->("what") if scalar(@$what);
102 foreach my $og (@$what) {
103 $ruledb->load_group_attributes($og);
104 $print_group->("WHAT", $og, 1);
106 foreach my $og (@$action) {
107 $print_group->("ACTION", $og);
111 __PACKAGE__-
>register_method ({
115 description
=> "Print the PMG rule database.",
117 additionalProperties
=> 0,
120 description
=> "Which rules should be printed",
122 enum
=> [qw(all active inactive)],
128 returns
=> { type
=> 'null'},
132 my $rule_status = $param->{rules
} // '';
133 my $dbh = PMG
::DBTools
::open_ruledb
("Proxmox_ruledb");
134 my $ruledb = PMG
::RuleDB-
>new($dbh);
136 my $rules = $ruledb->load_rules();
138 foreach my $rule (@$rules) {
139 print_rule
($ruledb, $rule, $rule_status);
148 __PACKAGE__-
>register_method ({
152 description
=> "Delete PMG rule database.",
154 additionalProperties
=> 0,
157 returns
=> { type
=> 'null'},
161 my $list = PMG
::DBTools
::database_list
();
163 my $dbname = "Proxmox_ruledb";
165 die "Database '$dbname' does not exist\n" if !$list->{$dbname};
167 syslog
('info', "delete rule database");
169 PMG
::DBTools
::delete_ruledb
($dbname);
174 __PACKAGE__-
>register_method ({
178 description
=> "Initialize/Upgrade the PMG rule database.",
180 additionalProperties
=> 0,
184 description
=> "Delete existing database.",
190 description
=> "Reset and update statistic database.",
196 returns
=> { type
=> 'null'},
200 PMG
::Utils
::cond_add_default_locale
();
202 my $list = PMG
::DBTools
::database_list
();
204 my $dbname = "Proxmox_ruledb";
206 if (!$list->{$dbname} || $param->{force
}) {
208 if ($list->{$dbname}) {
209 print "Destroy existing rule database\n";
210 PMG
::DBTools
::delete_ruledb
($dbname);
213 print "Initialize rule database\n";
215 my $dbh = PMG
::DBTools
::create_ruledb
($dbname);
216 my $ruledb = PMG
::RuleDB-
>new($dbh);
217 PMG
::DBTools
::init_ruledb
($ruledb);
223 my $dbh = PMG
::DBTools
::open_ruledb
("Proxmox_ruledb");
224 my $ruledb = PMG
::RuleDB-
>new($dbh);
226 print "Analyzing/Upgrading existing Databases...";
227 PMG
::DBTools
::upgradedb
($ruledb);
230 # reset and update statistic databases
231 if ($param->{statistics
}) {
232 print "Generating Proxmox Statistic Databases... ";
233 PMG
::Statistic
::clear_stats
($dbh);
234 my $cinfo = PVE
::INotify
::read_file
("cluster.conf");
235 PMG
::Statistic
::update_stats
($dbh, $cinfo);
246 __PACKAGE__-
>register_method ({
250 description
=> "Update the PMG statistic database.",
252 additionalProperties
=> 0,
255 returns
=> { type
=> 'null'},
259 my $dbh = PMG
::DBTools
::open_ruledb
("Proxmox_ruledb");
260 print "Updating Proxmox Statistic Databases... ";
261 my $cinfo = PVE
::INotify
::read_file
("cluster.conf");
262 PMG
::Statistic
::update_stats
($dbh, $cinfo);
270 'dump' => [ __PACKAGE__
, 'dump', []],
271 delete => [ __PACKAGE__
, 'delete', []],
272 init
=> [ __PACKAGE__
, 'init', []],
273 reset => [ 'PMG::API2::RuleDB', 'reset_ruledb', []],
274 update
=> [ __PACKAGE__
, 'update', []],