Debian Codegraph @

src/apps/mq/ibmmq/mqi/custom/api.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::mq::ibmmq::mqi::custom::api;
22
23 use base qw(centreon::plugins::mode);
24
25 use strict;
26 use warnings;
27 use centreon::plugins::misc;
28
29 sub new {
30 my ($class, %options) = @_;
31 my $self = {};
32 bless $self, $class;
33
34 if (!defined($options{output})) {
35 print "Class Custom: Need to specify 'output' argument.\n";
36 exit 3;
37 }
38 if (!defined($options{options})) {
39 $options{output}->add_option_msg(short_msg => "Class Custom: Need to specify 'options' argument.");
40 $options{output}->option_exit();
41 }
42
43 if (!defined($options{noptions})) {
44 $options{options}->add_options(arguments => {
45 'hostname:s' => { name => 'hostname' },
46 'port:s' => { name => 'port' },
47 'channel:s' => { name => 'channel' },
48 'timeout:s' => { name => 'timeout' }
49 });
50 }
51 $options{options}->add_help(package => __PACKAGE__, sections => 'CUSTOM MQI OPTIONS', once => 1);
52
53 $self->{output} = $options{output};
54
55 centreon::plugins::misc::mymodule_load(
56 output => $self->{output},
57 module => 'MQSeries::QueueManager',
58 error_msg => "Cannot load module 'MQSeries::QueueManager'."
59 );
60 centreon::plugins::misc::mymodule_load(
61 output => $self->{output},
62 module => 'MQSeries::Command',
63 error_msg => "Cannot load module 'MQSeries::Command'."
64 );
65
66 $self->{connected} = 0;
67 return $self;
68 }
69
70 sub set_options {
71 my ($self, %options) = @_;
72
73 $self->{option_results} = $options{option_results};
74 }
75
76 sub set_defaults {}
77
78 sub check_options {
79 my ($self, %options) = @_;
80
81 $self->{hostname} = (defined($self->{option_results}->{hostname})) ? $self->{option_results}->{hostname} : '';
82 $self->{channel} = (defined($self->{option_results}->{channel})) ? $self->{option_results}->{channel} : '';
83 $self->{port} = (defined($self->{option_results}->{port})) && $self->{option_results}->{port} =~ /(\d+)/ ? $1 : 1414;
84 $self->{timeout} = (defined($self->{option_results}->{timeout})) && $self->{option_results}->{timeout} =~ /(\d+)/ ? $1 : 30;
85
86 if ($self->{hostname} eq '') {
87 $self->{output}->add_option_msg(short_msg => 'Need to specify --hostname option.');
88 $self->{output}->option_exit();
89 }
90 if ($self->{channel} eq '') {
91 $self->{output}->add_option_msg(short_msg => 'Need to specify --channel option.');
92 $self->{output}->option_exit();
93 }
94
95 return 0;
96 }
97
98 sub get_hostname {
99 my ($self, %options) = @_;
100
101 return $self->{hostname};
102 }
103
104 sub get_port {
105 my ($self, %options) = @_;
106
107 return $self->{port};
108 }
109
110 sub get_qmgr_name {
111 my ($self, %options) = @_;
112
113 return $self->{qmgr_name};
114 }
115
116 sub my_logger {}
117
118 sub connect {
119 my ($self, %options) = @_;
120
121 return if ($self->{connected} == 1);
122
123 my $reason;
124 $self->{qmgr} = MQSeries::QueueManager->new(
125 QueueManager => '',
126 ConnectTimeout => $self->{timeout},
127 Carp => \&my_logger,
128 Reason => \$reason,
129 ClientConn => {
130 'ChannelName' => $self->{channel},
131 'TransportType' => 'TCP',
132 'ConnectionName' => $self->{hostname} . '(' . $self->{port} . ')',
133 'MaxMsgLength' => 16 * 1024 * 1024
134 }
135 );
136 if (!$self->{qmgr}) {
137 $self->{output}->add_option_msg(short_msg => 'unable to connect to the queue manager: ' . MQSeries::MQReasonToText($reason));
138 $self->{output}->option_exit();
139 }
140
141 $self->{mq_command} = MQSeries::Command->new(QueueManager => $self->{qmgr}, CommandVersion => eval('MQSeries::MQCFH_VERSION_3'));
142 if (!$self->{mq_command}) {
143 $self->{output}->add_option_msg(short_msg => 'unable to instantiate command object');
144 $self->{output}->option_exit();
145 }
146
147 $self->{connected} = 1;
148
149 my $results = $self->execute_command(command => 'InquireQueueManager');
150 $self->{qmgr_name} = $results->[0]->{QMgrName};
151 }
152
153 sub execute_command {
154 my ($self, %options) = @_;
155
156 $self->connect();
157 my @results;
158 if ($options{command} eq 'InquireQueueManager') {
159 @results = $self->{mq_command}->InquireQueueManager(%{$options{attrs}});
160 } elsif ($options{command} eq 'InquireQueueManagerStatus') {
161 @results = $self->{mq_command}->InquireQueueManagerStatus(%{$options{attrs}});
162 } elsif ($options{command} eq 'InquireChannelStatus') {
163 @results = $self->{mq_command}->InquireChannelStatus(%{$options{attrs}});
164 } elsif ($options{command} eq 'InquireQueueStatus') {
165 @results = $self->{mq_command}->InquireQueueStatus(%{$options{attrs}});
166 } elsif ($options{command} eq 'InquireChannel') {
167 @results = $self->{mq_command}->InquireChannel(%{$options{attrs}});
168 }
169
170 if (!@results) {
171 $self->{output}->add_option_msg(short_msg => "method '$options{command}' issue: " . $self->{mq_command}->ReasonText());
172 $self->{output}->option_exit();
173 }
174
175 return \@results;
176 }
177
178 1;
179
180 __END__
181
182 =head1 NAME
183
184 IBM MQ MQI
185
186 =head1 CUSTOM MQI OPTIONS
187
188 IBM MQ MQI
189
190 =over 8
191
192 =item B<--hostname>
193
194 Hostname or IP address.
195
196 =item B<--port>
197
198 Port used (default: 1414)
199
200 =item B<--channel>
201
202 Channel name.
203
204 =item B<--timeout>
205
206 Set timeout in seconds (default: 30).
207
208 =back
209
210 =head1 DESCRIPTION
211
212 B<custom>.
213
214 =cut