Debian Codegraph @

src/apps/proxmox/ve/restapi/mode/discovery.pm

raw
1 #
2 # Copyright 2024 Centreon (http://www.centreon.com/)
3 #
4 # Centreon is a full-fledged industry-strength solution that meets
5 # the needs in IT infrastructure and application monitoring for
6 # service performance.
7 #
8 # Licensed under the Apache License, Version 2.0 (the "License");
9 # you may not use this file except in compliance with the License.
10 # You may obtain a copy of the License at
11 #
12 # http://www.apache.org/licenses/LICENSE-2.0
13 #
14 # Unless required by applicable law or agreed to in writing, software
15 # distributed under the License is distributed on an "AS IS" BASIS,
16 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 # See the License for the specific language governing permissions and
18 # limitations under the License.
19 #
20
21 package apps::proxmox::ve::restapi::mode::discovery;
22
23 use base qw(centreon::plugins::mode);
24
25 use strict;
26 use warnings;
27 use JSON::XS;
28
29 sub new {
30 my ($class, %options) = @_;
31 my $self = $class->SUPER::new(package => __PACKAGE__, %options);
32 bless $self, $class;
33
34 $options{options}->add_options(arguments => {
35 'resource-type:s' => { name => 'resource_type' },
36 'prettify' => { name => 'prettify' }
37 });
38
39 return $self;
40 }
41
42 sub check_options {
43 my ($self, %options) = @_;
44 $self->SUPER::init(%options);
45
46 if (!defined($self->{option_results}->{resource_type}) || $self->{option_results}->{resource_type} eq '') {
47 $self->{option_results}->{resource_type} = 'nodes';
48 }
49 if ($self->{option_results}->{resource_type} !~ /^node|vm$/) {
50 $self->{output}->add_option_msg(short_msg => 'unknown resource type');
51 $self->{output}->option_exit();
52 }
53 }
54
55 sub discovery_vm {
56 my ($self, %options) = @_;
57
58 my $vms = $options{custom}->api_list_vms();
59
60 my $disco_data = [];
61 foreach my $vm_id (keys %$vms) {
62 my $vm = {};
63 $vm->{uuid} = $vm_id;
64 $vm->{name} = $vms->{$vm_id}->{Name};
65 $vm->{state} = $vms->{$vm_id}->{State};
66 $vm->{node_name} = $vms->{$vm_id}->{Node};
67
68 push @$disco_data, $vm;
69 }
70
71 return $disco_data;
72 }
73
74 sub discovery_node {
75 my ($self, %options) = @_;
76
77 my $nodes = $options{custom}->api_list_nodes();
78
79 my $disco_data = [];
80 foreach my $node_id (keys %$nodes) {
81 my $node = {};
82 $node->{uuid} = $node_id;
83 $node->{name} = $nodes->{$node_id}->{Name};
84 $node->{state} = $nodes->{$node_id}->{State};
85
86 push @$disco_data, $node;
87 }
88
89 return $disco_data;
90 }
91
92 sub run {
93 my ($self, %options) = @_;
94
95 my $disco_stats;
96 $disco_stats->{start_time} = time();
97
98 my $results = [];
99 if ($self->{option_results}->{resource_type} eq 'vm') {
100 $results = $self->discovery_vm(
101 custom => $options{custom}
102 );
103 } else {
104 $results = $self->discovery_node(
105 custom => $options{custom}
106 );
107 }
108
109 $disco_stats->{end_time} = time();
110 $disco_stats->{duration} = $disco_stats->{end_time} - $disco_stats->{start_time};
111 $disco_stats->{discovered_items} = scalar(@$results);
112 $disco_stats->{results} = $results;
113
114 my $encoded_data;
115 eval {
116 if (defined($self->{option_results}->{prettify})) {
117 $encoded_data = JSON::XS->new->utf8->pretty->encode($disco_stats);
118 } else {
119 $encoded_data = JSON::XS->new->utf8->encode($disco_stats);
120 }
121 };
122 if ($@) {
123 $encoded_data = '{"code":"encode_error","message":"Cannot encode discovered data into JSON format"}';
124 }
125
126 $self->{output}->output_add(short_msg => $encoded_data);
127 $self->{output}->display(nolabel => 1, force_ignore_perfdata => 1);
128 $self->{output}->exit();
129 }
130
131 1;
132
133 __END__
134
135 =head1 MODE
136
137 Resources discovery.
138
139 =over 8
140
141 =item B<--resource-type>
142
143 Choose the type of resources to discover (can be: 'vm', 'node').
144
145 =back
146
147 =cut