-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathfechar_chamado_zabbix.pl
238 lines (182 loc) · 6.76 KB
/
fechar_chamado_zabbix.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#!/usr/bin/perl -w
use strict;
use warnings;
# Use ../ as lib location
use File::Basename;
use FindBin qw($RealBin);
use lib dirname($RealBin);
use SOAP::Lite;
use Data::Dumper;
use Getopt::Long;
use XML::Simple;
my $TicketSearchID;
my $Operation = 0;
my $body = '';
my $triggerid = 0;
my $subject = '';
my $UserLogin = "otrs.isaac";
my $Password = "pass.isaac";
my $system = 'zabbix';
GetOptions (
"subject=s" => \$subject,
"body=s" => \$body,
"triggerid=s" => \$triggerid
);
# #---
# Variables to be defined.
$Operation = "TicketUpdate";
# This is the URL for the web service
# The format is
# <HTTP_TYPE>:://<OTRS_FQDN>/nph-genericinterface.pl/Webservice/<WEB_SERVICE_NAME>
# Or
# <HTTP_TYPE>:://<OTRS_FQDN>/nph-genericinterface.pl/WebserviceID/<WEB_SERVICE_ID>
my $URL = "http://10.20.19.47/otrs/nph-genericinterface.pl/Webservice/ZabbixOTRS";
# This name space should match the specified name space in the SOAP transport for the web service.
my $NameSpace = 'http://www.otrs.org/TicketConnector/';
# This is operation to execute, it could be TicketCreate, TicketUpdate, TicketGet, TicketSearch
# Or SessionCreate. and they must to be defined in the web service.
my $OperationSearch = 'TicketSearch';
# This variable is used to store all the parameters to be included on a request in XML format, each
# Operation has a determined set of mandatory and non mandatory parameters to work correctly, please
# Check OTRS Admin Manual in order to get the complete list.
my $XMLDataSearch = "
<UserLogin>$UserLogin</UserLogin>
<Password>$Password</Password>
<States>aberto</States>
<DynamicField_triggerid>
<Equals>$triggerid</Equals>
</DynamicField_triggerid>
";
print $XMLDataSearch."\n";
# Create a SOAP::Lite data structure from the provided XML data structure.
my $SOAPData = SOAP::Data
->type( 'xml' => $XMLDataSearch );
my $SOAPObject = SOAP::Lite
->uri($NameSpace)
->proxy($URL)
->$OperationSearch($SOAPData);
# Check for a fault in the soap code.
if ( $SOAPObject->fault ) {
print $SOAPObject->faultcode, " ", $SOAPObject->faultstring, "\n";
}
# Otherwise print the results.
else {
# Get the XML response part from the SOAP message.
my $XMLResponse = $SOAPObject->context()->transport()->proxy()->http_response()->content();
# Deserialize response (convert it into a perl structure).
my $Deserialized = eval {
SOAP::Deserializer->deserialize($XMLResponse);
};
print "debug \n";
# Remove all the headers and other not needed parts of the SOAP message.
my $Body = $Deserialized->body();
# Just output relevant data and no the operation name key (like TicketCreateResponse).
for my $ResponseKey ( keys %{$Body} ) {
my @TicketNumberId;
my $result = Dumper( $Body->{$ResponseKey} );
print $result;
my @TicketNumberIdParser = split(/\n/, $result);
my $aux = 0;
my $NumberTickeLimit = $#TicketNumberIdParser - 1;
foreach(@TicketNumberIdParser){
if ($_ =~ /TicketID/ and $#TicketNumberIdParser == '2')
{
@TicketNumberId = split(/\'/, $_);
# Update ticket
# Create a SOAP::Lite data structure from the provided XML data structure.
print "ticketNumber: $TicketNumberId[3]\n";
my $XMLDataUpdate = "
<UserLogin>$UserLogin</UserLogin>
<Password>$Password</Password>
<TicketID>$TicketNumberId[3]</TicketID>
<Ticket>
<State>resolvido</State>
</Ticket>
<Article>
<Subject>$subject</Subject>
<Body>$body</Body>
<TimeUnit>100</TimeUnit>
<ContentType>text/plain; charset=utf8</ContentType>
</Article>
";
# For debug
print $XMLDataUpdate;
my $SOAPDataUpdate = SOAP::Data
->type( 'xml' => $XMLDataUpdate );
my $SOAPObjectUpdate = SOAP::Lite
->uri($NameSpace)
->proxy($URL)
->$Operation($SOAPDataUpdate);
# Check for a fault in the soap code.
if ( $SOAPObjectUpdate->fault ) {
print $SOAPObjectUpdate->faultcode, " ", $SOAPObjectUpdate->faultstring, "\n";
}
# Otherwise print the results.
else {
# Get the XML response part from the SOAP message.
my $XMLResponseUpdate = $SOAPObjectUpdate->context()->transport()->proxy()->http_response()->content();
# Deserialize response (convert it into a perl structure).
my $DeserializedUpdate = eval {
SOAP::Deserializer->deserialize($XMLResponseUpdate);
};
# Remove all the headers and other not needed parts of the SOAP message.
my $Body = $DeserializedUpdate->body();
# Just output relevant data and no the operation name key (like TicketCreateResponse).
for my $ResponseKey ( keys %{$Body} ) {
print Dumper( $Body->{$ResponseKey} );
}
}
}
# Closing several calls at once...
if ($#TicketNumberIdParser > '2' and ($aux > 1 and $NumberTickeLimit > $aux))
{
if($TicketNumberIdParser[$aux] =~ m/(\d+)/) {
# Update Ticket
# Create a SOAP::Lite data structure from the provided XML data structure.
my $XMLDataUpdate = "
<UserLogin>$UserLogin</UserLogin>
<Password>$Password</Password>
<TicketID>$1</TicketID>
<Ticket>
<State>resolvido</State>
</Ticket>
<Article>
<Subject>$subject</Subject>
<Body>$body</Body>
<TimeUnit>100</TimeUnit>
<ContentType>text/plain; charset=utf8</ContentType>
</Article>
";
# For debug
#print $XMLDataUpdate;
my $SOAPDataUpdate = SOAP::Data
->type( 'xml' => $XMLDataUpdate );
my $SOAPObjectUpdate = SOAP::Lite
->uri($NameSpace)
->proxy($URL)
->$Operation($SOAPDataUpdate);
# Check for a fault in the soap code.
if ( $SOAPObjectUpdate->fault ) {
print $SOAPObjectUpdate->faultcode, " ", $SOAPObjectUpdate->faultstring, "\n";
}
# Otherwise print the results.
else {
# Get the XML response part from the SOAP message.
my $XMLResponseUpdate = $SOAPObjectUpdate->context()->transport()->proxy()->http_response()->content();
# Deserialize response (convert it into a perl structure).
my $DeserializedUpdate = eval {
SOAP::Deserializer->deserialize($XMLResponseUpdate);
};
# Remove all the headers and other not needed parts of the SOAP message.
my $Body = $DeserializedUpdate->body();
# Just output relevant data and no the operation name key (like TicketCreateResponse).
for my $ResponseKey ( keys %{$Body} ) {
print Dumper( $Body->{$ResponseKey} );
}
}
}
}
$aux = $aux + 1;
}
}
}