-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathzabbix_syslog_create_urls.pl
207 lines (159 loc) · 5.35 KB
/
zabbix_syslog_create_urls.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
#!/usr/bin/perl
use 5.010;
use strict;
use warnings;
use FindBin qw($Bin);
use lib "$Bin/lib";
use Data::Dumper;
use Config::General;
use ZabbixAPI;
our $VERSION = 4.0;
my $conf;
$conf = eval {Config::General->new('/usr/local/etc/zabbix_syslog.cfg')};
if ($@) {
eval {$conf = Config::General->new('/etc/zabbix/zabbix_syslog.cfg')};
if ($@) {die "Please check that config file is available as /usr/local/etc/zabbix_syslog.cfg or /etc/zabbix/zabbix_syslog.cfg\n";}
}
my %Config = $conf->getall;
#Authenticate yourself
my $url = $Config{'url'} || die "URL is missing in zabbix_syslog.cfg\n";
my $user = $Config{'user'} || die "API user is missing in zabbix_syslog.cfg\n";
my $password = $Config{'password'} || die "API user password is missing in zabbix_syslog.cfg\n";
my $server = $Config{'server'} || die "server hostname is missing in zabbix_syslog.cfg\n";
my $debug = $Config{'debug'};
my ( $authID, $response, $json );
my $zbx = ZabbixAPI->new( { api_url => $url, username => $user, password => $password } );
$zbx->login();
my $syslog_url_base = 'history.php?action=showvalues';
my @selements;
foreach my $map ( @{ map_get_extended() } ) {
my $mapid=$map->{sysmapid};
#put all map elements into array @selements (so you can update map later!)
@selements = @{ $map->{selements} };
print "INFO: Checking map with mapid $map->{sysmapid}\n";
foreach my $selement (@selements) {
my $syslog_button_exists = 0;
if ( $debug > 0 ) {
print 'Object ID: '
. $selement->{selementid}
. ' Type: '
. $selement->{elementtype}."\n";
}
# elementtype=0 hosts
if ( $selement->{elementtype} == 0 ) {
my $hostid;
#Zabbix API 3.4+
if (exists($selement->{elements}->[0]->{hostid})) {
$hostid = $selement->{elements}->[0]->{hostid};
}
#Zabbix API before 3.4
elsif (exists($selement->{elementid})) {
$hostid = $selement->{elementid};
}
else {
die "Cannot get hostid of selement $selement->{selementid}\n";
}
my $itemid = get_syslogid_by_hostid($hostid);
if ($itemid) {
#and add urls:
my $syslog_exists = 0;
foreach my $syslog_url ( @{ $selement->{urls} } ) {
$syslog_exists = 0;
if ( $syslog_url->{name} =~ 'Syslog' ) {
$syslog_exists = 1;
$syslog_url->{'name'} = 'Syslog';
$syslog_url->{'url'} =
$syslog_url_base
. '&itemids['
. $itemid . ']='
. $itemid;
}
}
if ( $syslog_exists == 0 ) {
#syslog item doesn't exist... add it
push @{ $selement->{urls} },
{
'name' => 'Syslog',
'url' => $syslog_url_base
. '&itemids['
. $itemid . ']='
. $itemid
};
}
}
}
}
map_update($mapid,\@selements);
}
$zbx->logout();
#______SUBS
sub get_syslogid_by_hostid {
my $hostid = shift;
my $params = {
output => ['itemid'],
hostids => $hostid,
filter => {'key_' => 'syslog' },
limit => 1,
};
my $result = $zbx->do('item.get',$params);
# Check if response was successful
if ( !$result ) {
$zbx->logout();
die "item.get failed\n";
}
#return itemid of syslog key (trapper type)
return ${ $result }[0]->{itemid};
}
sub map_get {
#retrieve all maps
my $params = {
output => ['sysmapid']
};
my $result = $zbx->do('map.get',$params);
# Check if response was successful
if ( !$result ) {
$zbx->logout();
die "map.get failed\n";
}
if ( $debug > 1 ) { print Dumper $result; }
return $result;
}
sub map_get_extended {
my $params = {
selectSelements => 'extend',
#sysmapids => $map,
};
my $result = $zbx->do('map.get',$params);
# Check if response was successful
if ( !$result ) {
$zbx->logout();
die "map.get failed\n";
}
if ( $debug > 1 ) {
print Dumper $result;
}
return $result;
}
sub map_update {
my $mapid = shift;
my $selements_ref = shift;
my $params = {
selements => [@{$selements_ref}],
sysmapid => $mapid,
};
my $result;
eval {$result=$zbx->do('map.update',$params);};
if($@){
warn "Failed to update map with mapid $mapid, check for write permissions for this map\n";
}
else {
if ( $debug > 0 ) {
print "About to map.update this\n:";
print Dumper $params;
}
if ( $debug > 0 ) {
print Dumper $result;
}
}
return;
}