-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_zammad.pl
executable file
·184 lines (129 loc) · 3.64 KB
/
check_zammad.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
#!/usr/bin/perl -w
# Name : check_zammad.pl
# Date : 05 09 2019
# Author : Karel Willems - [email protected]
# Summary : This is a Icinga plugin that checks if there are any open tickets in zammad
# Licence : Apache 2.0 - full text at http://www.apache.org/licenses/LICENSE-2.0
use LWP;
use JSON;
use Monitoring::Plugin;
use Scalar::Util qw(reftype);
use Data::Dumper;
my $plugin = Monitoring::Plugin->new(
plugin => 'check_zammadqueue',
shortname => 'Zammadqueue',
version => 'v0.1',
url => 'https://github.com/inuits/monitoring-plugins',
blurb => 'Icinga Plugin for checking new zammad tickets',
usage => 'Usage: ',
license => 'http://www.apache.org/licenses/LICENSE-2.0',
extra => '
Tested with zammad 2.9.0, perl v5.30.0'
);
my @args = (
{
spec => 'hostname|H=s',
usage => '-H, --hostname=STRING',
desc => 'Hostname of the Zammad server to read from',
required => 1,
},
{
spec => 'token|T=s',
usage => '-T, --token=STRING',
desc => 'token to authenticate with',
required => 1,
},
{
spec => 'protocol|p=s',
usage => '-p, --protocol=STRING',
desc => 'use http or https',
default => 'http',
required => 0,
},
{
spec => 'port|P=i',
usage => '-P, --port=INTEGER',
desc => 'specify port',
default => 80,
required => 0,
},
);
foreach my $arg (@args) {
add_arg( $plugin, $arg );
}
$plugin->getopts;
sub add_arg {
my $plugin = shift;
my $arg = shift;
my $spec = $arg->{'spec'};
my $help = $arg->{'usage'};
my $default = $arg->{'default'};
my $required = $arg->{'required'};
if ( defined $arg->{'desc'} ) {
my @desc;
if ( ref( $arg->{'desc'} ) ) {
@desc = @{ $arg->{'desc'} };
} else {
@desc = ( $arg->{'desc'} );
}
foreach my $d (@desc) {
$help .= "\n $d";
}
}
$plugin->add_arg(
spec => $spec,
help => $help,
default => $default,
required => $required,
);
}
check_queue ( $plugin );
sub send_request {
my $plugin = shift;
my $params = shift;
my $lwp = LWP::UserAgent->new(
timeout => '20',
ssl_opts => {
verify_hostname => 0,
SSL_verify_mode => 0
},
);
my $protocol = $plugin->opts->protocol . '://';
my $port = $plugin->opts->port;
my $url = $protocol . $plugin->opts->hostname . ':' . $port . '/api/v1/tickets/search?query=state:new';
my $request = HTTP::Request->new( GET => $url );
$request->header( 'Authorization', 'Token token=' . $params->{'token'} );
my $response = $lwp->request($request);
if ( HTTP::Status::is_error( $response->code ) ) {
$plugin->nagios_die( $response->content );
} else {
$response = JSON->new->allow_blessed->convert_blessed->decode( $response->content );
}
return $response;
}
sub check_queue {
my $plugin = shift;
my $message;
my $tickets_count = 0;
my @tickets_list;
my $titles = "\n";
my %params;
$params{'token'} = $plugin->opts->token;
my $response = send_request( $plugin, \%params);
$tickets_count = $response->{'tickets_count'};
@tickets_list = $response->{'assets'}->{'Ticket'};
foreach my $tickets (@tickets_list) {
for my $ticket ( %{ $tickets} ) {
if ( defined( $ticket->{'title'} )) {
$titles = $titles . $ticket->{'title'} . " \n" ;
}
}
}
if ( $tickets_count > 0 ) {
$message = "There are $tickets_count tickets open with titles: $titles";
$plugin->nagios_exit( CRITICAL, $message);
} else {
$message = "There are no new tickets open";
$plugin->nagios_exit( OK, $message);
}
}