Debian Codegraph @

src/notification/microsoft/office365/teams/custom/workflowapi.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 notification::microsoft::office365::teams::custom::workflowapi;
22
23 use strict;
24 use warnings;
25 use centreon::plugins::http;
26 use centreon::plugins::statefile;
27 use JSON::XS;
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 'teams-workflow:s' => { name => 'teams_workflow' },
46 'port:s' => { name => 'port' },
47 'proto:s' => { name => 'proto' },
48 'timeout:s' => { name => 'timeout' }
49 });
50 }
51 $options{options}->add_help(package => __PACKAGE__, sections => 'REST API OPTIONS', once => 1);
52
53 $self->{output} = $options{output};
54 $self->{http} = centreon::plugins::http->new(%options);
55
56 return $self;
57 }
58
59 sub set_defaults {}
60
61 sub set_options {
62 my ($self, %options) = @_;
63
64 $self->{option_results} = $options{option_results};
65 $self->{http}->add_header(key => 'Accept', value => 'application/json');
66 $self->{http}->add_header(key => 'Content-Type', value => 'application/json');
67 }
68
69 sub check_options {
70 my ($self, %options) = @_;
71
72 $self->{teams_workflow} = (defined($self->{option_results}->{teams_workflow})) ? $self->{option_results}->{teams_workflow} : '';
73 $self->{proto} = (defined($self->{option_results}->{proto})) ? $self->{option_results}->{proto} : 'https';
74 $self->{port} = (defined($self->{option_results}->{port})) ? $self->{option_results}->{port} : 443;
75 $self->{timeout} = (defined($self->{option_results}->{timeout})) ? $self->{option_results}->{timeout} : 30;
76
77 if ($self->{teams_workflow} eq '') {
78 $self->{output}->add_option_msg(short_msg => 'Need to specify the --teams-workflow option.');
79 $self->{output}->option_exit();
80 }
81
82 $self->{http}->set_options(%{$self->{option_results}}, hostname => 'dummy');
83
84 return 0;
85 }
86
87 sub json_decode {
88 my ($self, %options) = @_;
89
90 $options{content} =~ s/\r//mg;
91 my $decoded;
92 eval {
93 $decoded = JSON::XS->new->allow_nonref(1)->utf8->decode($options{content});
94 };
95 if ($@) {
96 $self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@");
97 $self->{output}->option_exit();
98 }
99
100 return $decoded;
101 }
102
103 sub teams_post_notification {
104 my ($self, %options) = @_;
105
106 my $encoded_data = JSON::XS->new->utf8->encode($options{json_request});
107
108 my $content = $self->{http}->request(
109 method => 'POST',
110 full_url => $self->{teams_workflow},
111 query_form_post => $encoded_data
112 );
113
114 return $content;
115 }
116
117 1;
118
119 __END__
120
121 =head1 NAME
122
123 O365 Teams Workflows API
124
125 =head1 SYNOPSIS
126
127 O365 Teams Workflows API
128
129 =head1 REST API OPTIONS
130
131 =over 8
132
133 =item B<--teams-workflow>
134
135 Define the Workflow full URI (required).
136
137 =item B<--port>
138
139 Define the API port (default: 443).
140
141 =item B<--proto>
142
143 Define the protocol if needed (default: 'https').
144
145 =item B<--timeout>
146
147 Define the HTTP timeout.
148
149 =back
150
151 =head1 DESCRIPTION
152
153 B<custom>.
154
155 =cut