-
Notifications
You must be signed in to change notification settings - Fork 1
/
otrs.ZabbixStat.pl
213 lines (178 loc) · 5.96 KB
/
otrs.ZabbixStat.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
#!/usr/bin/perl
## --
## bin/otrs.ZabbixStat.pl - generate stats for Zabbix
## 2018 houspi https://github.com/houspi
## --
=head1 DESCRIPTION
Script returns some stats for Zabbix
Ticket activity: number of created/closed tickets
User activity: number of active agents/customers
Count of open tickets in queues
Count of tickets in various states
Usage: /opt/otrs/bin/otrs.ZabbixStat.pl [params]
Without any params this script works in LLD mode
returns a list of queues and states
=cut
use strict;
use warnings;
use utf8;
use File::Basename;
use FindBin qw($RealBin);
use lib dirname($RealBin);
use lib dirname($RealBin) . '/Kernel/cpan-lib';
use lib dirname($RealBin) . '/Custom';
use Kernel::System::ObjectManager;
use JSON;
my $CacheTTL = 2678400; # seconds, this means 1 month
my %get_stats_rules = (
"users" => \&GetStatByUsers,
"statetype" => \&GetStatByStateType,
"queue" => \&GetStatByQueue,
);
my @ClosedStateTypeIDs = ( 3, 5, 6, 7 );
my @OpenedStateTypeIDs = ( 1, 2, 4);
my @UserTypes = qw(User Customer);
my $rv = "-1";
if (!scalar(@ARGV) ) {
$rv = Discover();
} else {
my $stat_type = $ARGV[0];
if( exists($get_stats_rules{$stat_type}) ) {
$rv = $get_stats_rules{$stat_type}->();
}
}
print "$rv\n";
=item Discover
LLD mode
returns a list of queues, states and users
=cut
sub Discover {
my %json_data;
$json_data{'data'} = ();
local $Kernel::OM = Kernel::System::ObjectManager->new();
my %Queues = $Kernel::OM->Get('Kernel::System::Queue')->QueueList( Valid => 1 );
map {push( @{$json_data{'data'}}, { "{#QUEUEID}" => int($_), "{#QUEUENAME}" => $Queues{$_} }) } sort keys %Queues;
my %ListType = $Kernel::OM->Get('Kernel::System::State')->StateTypeList( UserID => 1 );
map {push( @{$json_data{'data'}}, { "{#STATETYPEID}" => int($_), "{#STATETYPENAME}" => $ListType{$_} } )} sort keys %ListType;
foreach ( @UserTypes ) {
push( @{$json_data{'data'}}, { "{#USERTYPENAME}" => $_ } );
}
return to_json(\%json_data);
}
=item GetStatByUsers
returns the number of online users or customers
=cut
sub GetStatByUsers {
my $UserType = $ARGV[1] || '';
if ( $UserType ne 'User' && $UserType ne 'Customer' ) {
return "-1";
}
local $Kernel::OM = Kernel::System::ObjectManager->new();
my $AuthSession = $Kernel::OM->Get('Kernel::System::AuthSession');
my %Result = ();
if ( $AuthSession->can('GetActiveSessions') ) {
%Result = $AuthSession->GetActiveSessions( UserType => $UserType );
} else {
my %ExpiredSessions = ();
foreach ( @{($AuthSession->GetExpiredSessionIDs())[0]} ) {
$ExpiredSessions{$_} = 1;
}
my $Total = 0;
foreach ( $AuthSession->GetAllSessionIDs() ) {
if ( !exists($ExpiredSessions{$_}) ) {
my %Data = $AuthSession->GetSessionIDData( SessionID => $_ );
$Total++ if ( $Data{UserType} eq $UserType );
}
}
$Result{Total} = $Total;
}
return $Result{Total};
}
=item GetStatByStateType
returns the number of created and closed tickets for the last time
=cut
sub GetStatByStateType {
my $StateType = $ARGV[1] || '';
my $CacheKey = join(":", "STATETYPE", $StateType);
use POSIX qw(strftime);
my $CurGetStat = strftime "%Y-%m-%d %H:%M:%S", localtime;
local $Kernel::OM = Kernel::System::ObjectManager->new();
my $CacheObject = $Kernel::OM->Get('Kernel::System::Cache');
my $LastGetStat = $CacheObject->Get(
Type => 'GetStatByStateType',
Key => $CacheKey,
);
if (!$LastGetStat) {
$LastGetStat = $CurGetStat;
}
my $Count;
my %Params;
if ($StateType eq 'Created' ) {
%Params = (
Result => 'COUNT',
UserID => 1,
TicketCreateTimeNewerDate => $LastGetStat,
);
} elsif ($StateType eq 'Closed') {
%Params = (
Result => 'COUNT',
UserID => 1,
StateTypeIDs => \@ClosedStateTypeIDs,
TicketChangeTimeNewerDate => $LastGetStat,
);
} else {
return -1;
}
$Count = $Kernel::OM->Get('Kernel::System::Ticket')->TicketSearch( %Params );
$CacheObject->Set(
Type => 'GetStatByStateType',
Key => $CacheKey,
Value => $CurGetStat,
TTL => $CacheTTL,
CacheInMemory => 0,
CacheInBackend => 1,
);
return $Count;
}
=item GetStatByQueue
returns the number of tickets in the open state type in a specific queue
<queue id> <state type> <count with subqueues>
=cut
sub GetStatByQueue {
my $QueueID = $ARGV[1] || 3; # spam queue by default
$QueueID =~ s/\D//g;
$QueueID = 3 unless($QueueID);
my $StateType = $ARGV[2] || 'opened';
$StateType =~ tr/A-Z/a-z/; # for backward compatibility
my $UseSubQueues = $ARGV[3] || 0;
$UseSubQueues = 0 if ( $UseSubQueues =~ /\D/ or ($UseSubQueues != 0 and $UseSubQueues != 1));
local $Kernel::OM = Kernel::System::ObjectManager->new();
# Use SQL query to check queue
# because the QueueLookup method prints error message if the queue doesn't exists
my $Select = $Kernel::OM->Get('Kernel::System::DB')->SelectAll(
SQL => "SELECT COUNT(*) FROM queue WHERE id=?",
Bind => [ \$QueueID ]
);
return -1 if (!@{$Select}[0]->[0]);
my $Count;
my %Params;
if ($StateType eq 'opened' ) {
%Params = (
Result => 'COUNT',
UserID => 1,
QueueIDs => [ $QueueID ],
UseSubQueues => $UseSubQueues,
StateTypeIDs => \@OpenedStateTypeIDs,
);
} else {
%Params = (
Result => 'COUNT',
UserID => 1,
QueueIDs => [ $QueueID ],
UseSubQueues => $UseSubQueues,
StateType => [ $StateType ],
);
}
$Count = $Kernel::OM->Get('Kernel::System::Ticket')->TicketSearch( %Params );
return $Count;
}