ãããã£ãŠã æåã«ããããããšã¯ããã¹ãŠã®ãã°ãzabbixã«åéãããããã¯ãŒã¯ããŒãããšã«ã°ã«ãŒãåããŠãæ©åšã«ã¢ã¯ã»ã¹ããæéãç¡é§ã«ããã«åžžã«ç®ã§ã¡ãã»ãŒãžã確èªã§ããããã«ããããšã§ããã
2ã€ç®ã¯ãçããªãã€ãã³ãã«ã泚æãæãããšã§ãã
å€ãã®äººã¯ãZabbixãšãŒãžã§ã³ããã€ã³ã¹ããŒã«ãããŠãããµãŒããŒãŸãã¯ã³ã³ãã¥ãŒã¿ãŒã§ãããè¡ãæ¹æ³ãç¥ã£ãŠããŸããçµã¿èŸŒã¿ã®ããŒã¿èŠçŽ log []ãlogrt []ããããŸãã
ããããZabbixãšãŒãžã§ã³ããã€ã³ã¹ããŒã«ã§ããªããããã¯ãŒã¯æ©åšãããã°ãåéããå¿ èŠãããå Žåã¯ã©ãã§ããããïŒ å®éãzabbixãšãŒãžã§ã³ããããåãPCã§syslogãµãŒããŒãèšå®ãã ãã°[]ã䜿çšããŠãã®ããŒã¿ãzabbix ã«è»¢éã§ããŸã ã zabbixãšãŒãžã§ã³ãã䜿çšããŠãããã¯ãŒã¯ããŒãã«æ¥ç¶ãããããŒã¿èŠçŽ ãšãã®ããªã¬ãŒã®ã¿ã次ã«ç€ºããŸãããããã¯çŽæçã«ãããã«ãããã®ã§ãã ãã®ããŒã¿ããããã¯ãŒã¯ããã€ã¹ã«çŽæ¥æ·»ä»ããããšã¯ã§ããŸããïŒ ã§ããŸãã
ãã®ããã«ã¯ãzabbixãµãŒããŒãŸãã¯zabbixãããã·ãæèŒãããã·ã³ã§zabbix_sender ã Zabbix APIããã³rsyslogãå¿ èŠã§ã ã ããŒãã¹ãšããŠããããã¯ãŒã¯ãããããsyslogã¡ãã»ãŒãžãã°ãžã®è¿ éãªã³ã³ããã¹ãé·ç§»ãååŸããŸãã
çµæã¯ã©ã®ããã«ãªããŸããïŒ ããŠããã®ãããªãã®ïŒ
ã³ã³ããã¹ãåŒã³åºãïŒ
æ¹æ³
çŽ æŽãããã¹ãããŒã¯ã§ããœãªã¥ãŒã·ã§ã³ã¢ãŒããã¯ãã£ã¯æ¬¡ã®ããã«ãªããŸãã
1.ãããã¯ãŒã¯ããã€ã¹ããã®ãã°ã¯ãã¹ãŠãrsyslogãåæã«ååšããZabbixãµãŒããŒãŸãã¯ãããã·ãåãããµãŒããŒã«åé¡ãããŸãã
2. rsyslogã¯ãïŒ3ïŒZabbixã®ã©ã®ãã¹ãããã¡ãã»ãŒãžãæ¥ãããå€æããã¹ã¯ãªãããå®è¡ããŸã
4.ã¡ãã»ãŒãžã¯ãzabbix_senderãŠãŒãã£ãªãã£ãä»ããŠzabbixã«éãããŸã
ããŠããããã¯ãŒã¯ã®éçããzabbiksãžã®ã¡ãã»ãŒãžãã¹ããã«ããã¹ã«ãŒããå§ããŸããã
ãããã¯ãŒã¯æ©åšäž
ããã§ã¯ãã¹ãŠãç°¡åã§ãã syslogã¡ãã»ãŒãžã®å®å ãšããŠãZabbixãµãŒããŒãŸãã¯Zabbixãããã·ãåãããã·ã³ãæå®ããŸãã é倧床ããã³æœèšã«ã¡ãã»ãŒãžãéä¿¡ããããã«æ©åšãæ§æããŸãã
äžéšã®D-Linkã§ã¯ã次ã®ããã«ãªããŸãã
enable syslog create syslog host 1 ipaddress 10.2.0.21 severity debug state enable
ãããŠã次ã®ãããªCiscoã«ãŒã¿ãŒã§èšã£ãŠã¿ãŸãããã
cisco1# cisco1#config terminal Enter configuration commands, one per line. End with CNTL/Z. cisco1(config)#logging 10.2.0.21 cisco1(config)#service timestamps debug datetime localtime show-timezone msec cisco1(config)#service timestamps log datetime localtime show-timezone msec cisco1(config)#logging facility local3 cisco1(config)#logging trap informational cisco1(config)#end
ã«ã¹ã¿ãã€ãºãããŸãããïŒ ã©ãã
Zabbixã®Webã€ã³ã¿ãŒãã§ã€ã¹ã§
æãã·ã³ãã«ã§æãç解ãããããã®ããå§ããŸãããã Zabbixã§ã¯ãTemplate_Syslogãã³ãã¬ãŒããäœæãããã®äžã«1ã€ã®ããŒã¿èŠçŽ ãè¿œå ããŸãïŒ
次ã®ããã«ãã£ãŒã«ãã«å ¥åããŸãã
ãã£ãŒã«ã
| 䟡å€
| ã泚æ
|
---|---|---|
å | syslog | |
çš®é¡ | Zabbix Trapper | |
ã㌠| syslog | ãããæ£ç¢ºãªååã§ããããšãéèŠã§ãïŒZabbix APIãããã«æ£ããåäœãããããïŒ |
æ å ±ã®çš®é¡ | ãã°ïŒãã°ïŒ | |
ãã°å ã®æå»åœ¢åŒïŒãã°ïŒ | yyyyxMMxddxhhxmmxssxxxxxx | RFC5424ã®åœ¢åŒã§æ¥ä»ãæ£ãã決å®ããããã®ãã¹ã¯ |
次ã«ããã®ãã³ãã¬ãŒãããsyslogã¡ãã»ãŒãžãåéãããã¹ãŠã®ãããã¯ãŒã¯ããŒãã«æ·»ä»ããŸãã ãã°ãZabbixã«éãããIPã¢ãã¬ã¹ãã€ã³ã¿ãŒãã§ãŒã¹ã§æå®ããããšãéèŠã§ãã ããããªããšãã¡ãã»ãŒãžã®ãœãŒã¹ãç¹å®ã§ããŸããã
syslogãµãŒããŒ
ZabbixãµãŒããŒã䜿çšããŠããã¹ãã«syslogãµãŒããŒãèšå®ããŸãã ç§ãã¡ã®å Žåãããã¯å€ãã®Linuxãã£ã¹ããªãã¥ãŒã·ã§ã³ã«ä»å±ããŠããäžè¬çãªrsyslogã§ãã syslog-ngããæã¡ã®å Žåã¯ãã»ãŒåãæ¹æ³ã§ãã¹ãŠãå®è¡ã§ããŸãã
æãåçŽãªå ŽåãsyslogãµãŒããŒã¯ãã¡ãã»ãŒãžã®æ©èœãšé倧床ã«å¿ããŠãåä¿¡ããã¡ãã»ãŒãžããã¡ã€ã«ã«å解ããŸãã ãã ããä»ã®å¯èœæ§ããããŸãã ããšãã°ãrsyslogã§ã¯ãã¡ãã»ãŒãžããšã«ä»»æã®ã¹ã¯ãªãããå®è¡ã§ããŸãã ãã®é¢æ°ã䜿çšããŸãã
解決ããå¿ èŠããã2çªç®ã®åé¡ã¯ãZabbixãã°ã«ã¡ãã»ãŒãžãè¿œå ããããŒãã決å®ããããã®æ©åšã®èå¥ã§ãã è§æ¬åŒ§å ã®ãœãŒã¹IPã¢ãã¬ã¹ããã¡ãã»ãŒãžèªäœã®è¡ã«è¿œå ããããšã§è§£æ±ºããŸãã
ãã®ãã¹ãŠã®ããã«ãæ§æãã¡ã€ã«/etc/rsyslog.d/zabbix_rsyslog.confãäœæããŸã
#add template for network devices $template network-fmt,"%TIMESTAMP:::date-rfc3339% [%fromhost-ip%] %pri-text% %syslogtag%%msg%\n" #exclude unwanted messages: :msg, contains, "Child connection from ::ffff:10.2.0.21" ~ :msg, contains, "exit after auth (ubnt): Disconnect received" ~ :msg, contains, "password auth succeeded for 'ubnt' from ::ffff:10.2.0.21" ~ :msg, contains, "exit before auth: Exited normally" ~ #action for every message: if $fromhost-ip != '127.0.0.1' then ^/usr/local/bin/zabbix_syslog_lkp_host.pl;network-fmt & ~
rsyslogã®èšå®ãäœæããŸãããããã¯ãããŒã«ã«ãã¹ãããã§ã¯ãªãç¹å®ã®æ¹æ³ã§åä¿¡ãããã¹ãŠã®ã¡ãã»ãŒãžããã©ãŒãããããsyslogã¡ãã»ãŒãžãåŒæ°ãšããŠã¹ã¯ãªãã/usr/local/bin/zabbix_syslog_lkp_host.plãå®è¡ããŸãã
åæã«ãïŒexclude奜ãŸãããªãã¡ãã»ãŒãžã®ã»ã¯ã·ã§ã³ã§ã¯ããã°ã€ã³ãè©°ãŸã£ãŠããã¡ãã»ãŒãžãäºåã«ããã£ãŠããå Žåã¯ç Žæ£ã§ããŸãã äŸãšããŠãããã€ãã®æçš¿ãããã«æ®ããŸãã
rsyslogèšå®ã®æåŸã«ã/etc/rsyslog.confãã¡ã€ã«ã®æ¬¡ã®è¡ã®ã³ã¡ã³ããå€ããŠãUDPçµç±ã§ãããã¯ãŒã¯çµç±ã§Syslogã¡ãã»ãŒãžãåä¿¡ããããšãå¿ããªãã§ãã ããã
$ModLoad imudp $UDPServerRun 514
ããã§ãã/ usr / local / bin / zabbix_syslog_lkp_host.plã¹ã¯ãªããã¯rsyslogã«å®è¡ãæ瀺ããŸããïŒ èŠããã«ã圌ã¯ãã®ã¡ãã»ãŒãžãzabbix_senderçµç±ã§Zabbix_serverãŸãã¯Zabbix_proxyã«éä¿¡ããã ãã§ãã
/usr/bin/zabbix_sender -z ** -k syslog -o *SYSLOG-* -s **
ç·šéæžã¿ïŒããããå®éã«ã¯æšæºã®zabbix_senderãŠãŒãã£ãªãã£ãå®è¡ããå¿ èŠã¯ãŸã£ãããããŸããã / usr / bin / zabbix_senderãæ¯åãã«ããŠããã»ã¹ãæé©åããªãããã«ããã®æ©èœã¯ã¹ã¯ãªããèªäœã®å éšã«å®è£ ã§ããŸãã éèŠãªè¿œå mcleod095ãããããšã ïŒ
ããããã¡ãã»ãŒãžãéä¿¡ãããIPã¢ãã¬ã¹ã®ã¿ãç¥ãããŠãããããã¹ã¯ãªããã¯ã©ã®ããã«* NAME *ã«ãªããïŒã€ãŸããã¡ãã»ãŒãžãæ¥ç¶ãããããŒãïŒãã©ã®ããã«ç¥ãã®ã§ããããïŒ
ãããè¡ãã«ã¯ãZabbix APIã䜿çšããŸããããã«ãããIPã¢ãã¬ã¹ã§* NAME *ãèŠã€ããããšãã§ããŸãã
/usr/local/bin/zabbix_syslog_lkp_host.pl
#!/usr/bin/perl use 5.010; use strict; use warnings; use JSON::RPC::Legacy::Client; use Data::Dumper; use Config::General; use CHI; use List::MoreUtils qw (any); use English '-no_match_vars'; use Readonly; use MIME::Base64 qw(encode_base64); use IO::Socket::INET; our $VERSION = 2.0; Readonly my $CACHE_TIMEOUT => 600; Readonly my $CACHE_DIR => '/tmp/zabbix_syslog_cache'; my $conf = Config::General->new('/usr/local/etc/zabbix_syslog.cfg'); my %Config = $conf->getall; #Authenticate yourself my $client = JSON::RPC::Legacy::Client->new(); 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 $id = 0; my $message = shift @ARGV || die "Syslog message required as an argument\n"; #Grab syslog message from rsyslog #get ip from message my $ip; #IP regex patter part my $ipv4_octet = q/(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/; if ( $message =~ / \[ ((?:$ipv4_octet[.]){3}${ipv4_octet}) \]/msx ) { $ip = $1; } else { die "No IP in square brackets found in '$message', cannot continue\n"; } my $cache = CHI->new( driver => 'File', root_dir => $CACHE_DIR, ); my $hostname = $cache->get($ip); if ( !defined $hostname ) { $authID = login(); my @hosts_found; my $hostid; foreach my $host ( hostinterface_get() ) { $hostid = $host->{'hostid'}; if ( any { /$hostid/msx } @hosts_found ) { next; } #check if $hostid already is in array then skip(next) else { push @hosts_found, $hostid; } ###########now get hostname if ( get_zbx_trapper_syslogid_by_hostid($hostid) ) { my $result = host_get($hostid); #return hostname if possible if ( $result->{'host'} ) { if ( $result->{'proxy_hostid'} == 0 ) #check if host monitored directly or via proxy { #lease $server as is } else { #assume that rsyslogd and zabbix_proxy are on the same server $server = 'localhost'; } $hostname = $result->{'host'}; } } } logout(); $cache->set( $ip, $hostname, $CACHE_TIMEOUT ); } zabbix_send( $server, $hostname, 'syslog', $message ); #______SUBS sub login { $json = { jsonrpc => '2.0', method => 'user.login', params => { user => $user, password => $password }, id => $id++, }; $response = $client->call( $url, $json ); # Check if response was successful die "Authentication failed\n" unless $response->content->{'result'}; if ( $debug > 0 ) { print Dumper $response->content->{'result'}; } return $response->content->{'result'}; } sub logout { $json = { jsonrpc => '2.0', method => 'user.logout', params => {}, id => $id++, auth => $authID, }; $response = $client->call( $url, $json ); # Check if response was successful warn "Logout failed\n" unless $response->content->{'result'}; return; } sub hostinterface_get { $json = { jsonrpc => '2.0', method => 'hostinterface.get', params => { output => [ 'ip', 'hostid' ], filter => { ip => $ip, }, # limit => 1, }, id => $id++, auth => $authID, }; $response = $client->call( $url, $json ); if ( $debug > 0 ) { print Dumper $response; } # Check if response was successful (not empty array in result) if ( !@{ $response->content->{'result'} } ) { logout(); die "hostinterface.get failed\n"; } return @{ $response->content->{'result'} } } sub get_zbx_trapper_syslogid_by_hostid { my $hostids = shift; $json = { jsonrpc => '2.0', method => 'item.get', params => { output => ['itemid'], hostids => $hostids, search => { 'key_' => 'syslog', type => 2, #type => 2 is zabbix_trapper status => 0, }, limit => 1, }, id => $id++, auth => $authID, }; $response = $client->call( $url, $json ); if ( $debug > 0 ) { print Dumper $response; } # Check if response was successful if ( !@{ $response->content->{'result'} } ) { logout(); die "item.get failed\n"; } #return itemid of syslog key (trapper type) return ${ $response->content->{'result'} }[0]->{itemid}; } sub host_get { my $hostids = shift; $json = { jsonrpc => '2.0', method => 'host.get', params => { hostids => [$hostids], output => [ 'host', 'proxy_hostid', 'status' ], filter => { status => 0, }, # only use hosts enabled limit => 1, }, id => $id++, auth => $authID, }; $response = $client->call( $url, $json ); if ( $debug > 0 ) { print Dumper $response; } # Check if response was successful if ( !$response->content->{'result'} ) { logout(); die "host.get failed\n"; } return ${ $response->content->{'result'} }[0]; #return result } sub zabbix_send { my $zabbixserver = shift; my $hostname = shift; my $item = shift; my $data = shift; Readonly my $SOCK_TIMEOUT => 10; Readonly my $SOCK_RECV_LENGTH => 1024; my $result; my $request = sprintf "<req>\n<host>%s</host>\n<key>%s</key>\n<data>%s</data>\n</req>\n", encode_base64($hostname), encode_base64($item), encode_base64($data); my $sock = IO::Socket::INET->new( PeerAddr => $zabbixserver, PeerPort => '10051', Proto => 'tcp', Timeout => $SOCK_TIMEOUT ); die "Could not create socket: $ERRNO\n" unless $sock; $sock->send($request); my @handles = IO::Select->new($sock)->can_read($SOCK_TIMEOUT); if ( $debug > 0 ) { print "item - $item, data - $data\n"; } if ( scalar(@handles) > 0 ) { $sock->recv( $result, $SOCK_RECV_LENGTH ); if ( $debug > 0 ) { print "answer from zabbix server $zabbixserver: $result\n"; } } else { if ( $debug > 0 ) { print "no answer from zabbix server\n"; } } $sock->close(); return; }
ãã¹/usr/local/bin/zabbix_syslog_lkp_host.plã«æ²¿ã£ãŠã¹ã¯ãªããããµãŒããŒã«ã³ããŒããæ§æãã¡ã€ã«ãäœæããŸã
/usr/local/etc/zabbix_syslog.cfgãšAPIçµç±ã§Zabbixã«æ¥ç¶ããããã®ãã©ã¡ãŒã¿ã æ§æã¯æ¬¡ã®ããã«ãªããŸãã
url = http://zabbix.local/zabbix/api_jsonrpc.php user = api_user password = password server = zabbix.local debug=0
ã¹ã¯ãªããã¯CPANã®ããã€ãã®Perlã¢ãžã¥ãŒã«ã䜿çšããŠããããã€ã³ã¹ããŒã«ããã³ãã³ããå®è¡ããŸãã
PERL_MM_USE_DEFAULT=1 perl -MCPAN -e 'install Readonly' PERL_MM_USE_DEFAULT=1 perl -MCPAN -e 'install CHI' PERL_MM_USE_DEFAULT=1 perl -MCPAN -e 'install JSON::RPC::Legacy::Client' PERL_MM_USE_DEFAULT=1 perl -MCPAN -e 'install Config::General'
ãŸãããããã®æ°ãããã¡ã€ã«ã«å¯Ÿããæš©éãæ§æããŸãã
chmod +x /usr/local/bin/zabbix_syslog_lkp_host.pl chown zabbix:zabbix /usr/local/etc/zabbix_syslog.cfg chmod 700 /usr/local/etc/zabbix_syslog.cfg
Zabbixã«ã¡ãã»ãŒãžãéä¿¡ããæºåããã¹ãŠæŽããŸãããrsyslogãåèµ·åããã ãã§ãã
service rsyslog restart
ãã®ç¬éããããã¹ãããšã«zabbixã®ã¡ãã»ãŒãžãåå¥ã«ç¢ºèªã§ããŸãã æè¿ã®ããŒã¿->ç®çã®ãã¹ã-> SyslogãéããŸã
ããªã¬ãŒ
æ©åšã®ã€ã³ã¿ãŒãã§ã€ã¹ãçµç±ããã«ã·ã¹ãã ã®ãã°ãèªã¿åãæ©èœã¯åªããŠããŸãïŒååãšããŠãæ©åšã®ãã°ã¯ã¡ã¢ãªå ã«ãããåèµ·ååŸãåç¶ããªããšããäºå®ã¯èšããŸã§ããããŸããïŒã ä»ã®ãããã³ã«ã®å Žåã®ããã«ããããã¯ç§ãã¡ã®ãããã¯ãŒã¯äžã§éåœçãªã¡ãã»ãŒãžãå¯åããªãããã«ããã®ã«åœ¹ç«ã¡ãŸãã
åæ©åšãšåæ©åšã¡ãŒã«ãŒã«ã¯ç¬èªã®ã¡ãã»ãŒãžããããŸãããã©ã®ããã«èŠããããç¥ããã«éèŠãªã¡ãã»ãŒãžãæ¢ãæ¹æ³ã¯ïŒ ãããã次ã®ããã«ïŒ
ãã¹ãŠã®syslogã¡ãã»ãŒãžã¯é倧床å±æ§ã䜿çšããŠåé¡ãããRFC5424ã«åŸã£ãŠæ¬¡ã®å€ãåãããšãã§ããŸãã
0ç·æ¥ïŒã·ã¹ãã ã¯äœ¿çšã§ããŸãã
1ã¢ã©ãŒãïŒããã«å¯ŸåŠããå¿ èŠããããŸã 2ã¯ãªãã£ã«ã«ïŒã¯ãªãã£ã«ã«ãªç¶æ 3ãšã©ãŒïŒãšã©ãŒç¶æ 4èŠåïŒèŠåæ¡ä»¶ 5éç¥ïŒæ£åžžã ãé倧ãªç¶æ 6æ å ±ïŒæ å ±ã¡ãã»ãŒãž 7ãããã°ïŒãããã°ã¬ãã«ã®ã¡ãã»ãŒãž |
é倧床ã«ã¯æ°å€ã ãã§ãªããæçµã¡ãã»ãŒãžã«å«ãŸããããã¹ãã®çç¥åœ¢ããããzabbix_senderãä»ããŠZabbixã«éä¿¡ãããŸãã
ãããã£ãŠãéçïŒã€ãŸãã補é å ïŒãããªãé«ãéèŠåºŠãå²ãåœãŠãŠããã¡ãã»ãŒãžãæ€çŽ¢ãããããã«ã€ããŠéç¥ã§ããŸãã ãããè¡ãã«ã¯ãTemplate_Syslogãã³ãã¬ãŒãã«ããªã¬ãŒãè¿œå ããŠã é倧床= warning以äžã®ãã¹ãŠã®ã€ãã³ãã«ã€ããŠéç¥ããŸãã
æåŸã«è¡ãããšã¯ããããã®æ°ããsyslogã¡ãã»ãŒãžã«é¢ããã¢ã©ãŒãïŒã¢ã¯ã·ã§ã³ïŒãèšå®ããããšã§ãã æ¡ä»¶ã§ã¯ãããªã¬ãŒåã«[SYSLOG]ãå«ãŸããŠããããšãããã³ã¡ãã»ãŒãžãé»åã¡ãŒã«ã§éä¿¡ããå¿ èŠãããããšã瀺ããŠããŸãã
çµæãšããŠãéèŠåºŠã®é«ãã¡ãã»ãŒãžãsyslogã«èœã¡ããã³ã«ã次ã®åœ¢åŒã®ã¡ãã»ãŒãžãåãåããŸãã
ã¡ãªã¿ã«ãã¢ã¯ã·ãã³ãã®ã¯ãªãã£ã«ã«ããªã¬ãŒãæã€ãã³ãã¬ãŒãã¯æºåãã§ããŠããŸãã
Template_Syslog
<?xml version="1.0" encoding="UTF-8"?> <zabbix_export> <version>2.0</version> <date>2015-03-13T14:27:56Z</date> <groups> <group> <name>Templates</name> </group> </groups> <templates> <template> <template>Template_Syslog</template> <name>Template_Syslog</name> <description/> <groups> <group> <name>Templates</name> </group> </groups> <applications> <application> <name>Log</name> </application> </applications> <items> <item> <name>Syslog</name> <type>2</type> <snmp_community/> <multiplier>0</multiplier> <snmp_oid/> <key>syslog</key> <delay>0</delay> <history>3</history> <trends>365</trends> <status>0</status> <value_type>2</value_type> <allowed_hosts/> <units/> <delta>0</delta> <snmpv3_contextname/> <snmpv3_securityname/> <snmpv3_securitylevel>0</snmpv3_securitylevel> <snmpv3_authprotocol>0</snmpv3_authprotocol> <snmpv3_authpassphrase/> <snmpv3_privprotocol>0</snmpv3_privprotocol> <snmpv3_privpassphrase/> <formula>1</formula> <delay_flex/> <params/> <ipmi_sensor/> <data_type>0</data_type> <authtype>0</authtype> <username/> <password/> <publickey/> <privatekey/> <port/> <description/> <inventory_link>0</inventory_link> <applications> <application> <name>Log</name> </application> </applications> <valuemap/> <logtimefmt>yyyyxMMxddxhhxmmxssxxxxxx</logtimefmt> </item> </items> <discovery_rules/> <macros/> <templates/> <screens/> </template> </templates> <triggers> <trigger> <expression>({Template_Syslog:syslog.str(.alert)}=1)and({Template_Syslog:syslog.nodata(900)}=0)</expression> <name>[SYSLOG] Alert message received</name> <url/> <status>0</status> <priority>4</priority> <description/> <type>0</type> <dependencies/> </trigger> <trigger> <expression>({Template_Syslog:syslog.str(.crit)}=1)and({Template_Syslog:syslog.nodata(900)}=0)</expression> <name>[SYSLOG] Critical message received</name> <url/> <status>0</status> <priority>3</priority> <description/> <type>0</type> <dependencies/> </trigger> <trigger> <expression>({Template_Syslog:syslog.str(.emerg)}=1)and({Template_Syslog:syslog.nodata(900)}=0)</expression> <name>[SYSLOG] Emergency message received</name> <url/> <status>0</status> <priority>5</priority> <description/> <type>0</type> <dependencies/> </trigger> <trigger> <expression>({Template_Syslog:syslog.str(.err)}=1)and({Template_Syslog:syslog.nodata(900)}=0)</expression> <name>[SYSLOG] Error received</name> <url/> <status>0</status> <priority>2</priority> <description/> <type>0</type> <dependencies/> </trigger> <trigger> <expression>({Template_Syslog:syslog.str(.warning)}=1)and({Template_Syslog:syslog.nodata(900)}=0)</expression> <name>[SYSLOG] Warning received</name> <url/> <status>0</status> <priority>1</priority> <description/> <type>0</type> <dependencies/> </trigger> </triggers> </zabbix_export>
ãã¡ããããã¹ãŠã®èŠåããšã©ãŒãéèŠãªã¡ãã»ãŒãžãªã©ããã£ããããå¿ èŠã¯ãããŸããã ããã¯ãç°åžžãªãã®ãèŠéããªãããã®äžè¬çãªãªãã·ã§ã³ã§ãã ããªã¬ãŒé¢æ°iregxpïŒïŒãregxpïŒïŒãstrïŒïŒã䜿çšãããš ãããå ·äœçãªã€ãã³ããåžžã«ãã°ã«èšé²ã§ããŸãã
ã«ãŒããžã®èªåããŠã³ã
syslogã¡ãã»ãŒãžã®æäœãç°¡çŽ åãããã1ã€ã®éèŠãªãã€ã³ããã€ãŸããããã¯ãŒã¯ãããããã®ã³ã³ããã¹ãé·ç§»ã«è§ŠããŸãã
1æ¥ã2æ¥ãéããããšãã§ããsyslogããŒã¿èŠçŽ ã«åãã¹ãã®URLãªã³ã¯ãæã§è¿œå ããããšã§èŠåŽããããšãã§ããŸãã
ããããããªãã®æã也ããããŠã¹ãã¯ãªãã¯ããããããªãã®å¿ãåããå§ããã§ãããã ãã®ã«ãŒãã³ã®èªååãæ¯æŽããããã«ãZabbix APIãå床åç §ããããšããå§ãããŸãã
ãããè¡ãã«ã¯ãã¹ã¯ãªãããã¢ããããŒãããŸã
1ïŒãããã¯ãŒã¯ãããã®ãã¹ãŠã®èŠçŽ ãååŸãã
2ïŒãã¹ãã¿ã€ãã®ãã¹ãŠã®èŠçŽ ã«ã€ããŠãkey = syslogã®ããŒã¿èŠçŽ ããããã©ããã確èªããŸã
3ïŒååšããå Žåããã®ããŒã¿èŠçŽ ã衚瀺ããããã®ãªã³ã¯ãæ¢åã®URLã®ãªã¹ãã«è¿œå ããŸãïŒSyslogãžã®URLãæ¢ã«ããå Žåã¯ãäœãããŸããïŒ
ã¹ã¯ãªããã®æºåãã§ããããZabbixãµãŒããŒã«ã®ã¿ãããã€ããŸãã
/usr/local/bin/zabbix_syslog_create_urls.pl
#!/usr/bin/perl #fixed URL for ZBX 2.4 use 5.010; use strict; use warnings; use JSON::RPC::Legacy::Client; use Data::Dumper; use Config::General; our $VERSION = 1.1; my $conf = Config::General->new('/usr/local/etc/zabbix_syslog.cfg'); my %Config = $conf->getall; #Authenticate yourself my $client = JSON::RPC::Legacy::Client->new(); 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 $id = 0; $authID = login(); my $syslog_url_base = 'history.php?action=showvalues'; my @selements; foreach my $map ( @{ map_get_extended() } ) { my $mapid=$map->{sysmapid}; #next unless ($mapid == 120 or $mapid == 116); #debug #put all mapelements into array @selements (so you can update map later!) @selements = @{ $map->{selements} }; foreach my $selement (@selements) { my $syslog_button_exists = 0; if ( $debug > 0 ) { print 'Object ID: ' . $selement->{selementid} . ' Type: ' . $selement->{elementtype} . ' Elementid ' . $selement->{elementid} . " \n"; } # elementtype=0 hosts if ( $selement->{elementtype} == 0 ) { my $hostid = $selement->{elementid}; 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); } logout(); #______SUBS sub get_syslogid_by_hostid { my $hostids = shift; $json = { jsonrpc => '2.0', method => 'item.get', params => { output => ['itemid'], hostids => $hostids, search => { 'key_' => 'syslog' }, limit => 1, }, id => $id++, auth => $authID, }; $response = $client->call( $url, $json ); # Check if response was successful if ( !$response->content->{'result'} ) { logout(); die "item.get failed\n"; } #return itemid of syslog key (trapper type) return ${ $response->content->{'result'} }[0]->{itemid}; } sub login { $json = { jsonrpc => '2.0', method => 'user.login', params => { user => $user, password => $password }, id => $id++, }; $response = $client->call( $url, $json ); # Check if response was successful die "Authentication failed\n" unless $response->content->{'result'}; if ( $debug > 0 ) { print Dumper $response->content->{'result'}; } return $response->content->{'result'}; } sub map_get { #retrieve all maps $json = { jsonrpc => '2.0', method => 'map.get', params => { output => ['sysmapid'] }, id => $id++, auth => "$authID", }; $response = $client->call( $url, $json ); # Check if response was successful if ( !$response->content->{'result'} ) { logout(); die "map.get failed\n"; } if ( $debug > 1 ) { print Dumper $response->content->{result}; } return $response->content->{result}; } sub logout { $json = { jsonrpc => '2.0', method => 'user.logout', params => {}, id => $id++, auth => $authID, }; $response = $client->call( $url, $json ); # Check if response was successful warn "Logout failed\n" unless $response->content->{'result'}; return; } sub map_get_extended { $json = { jsonrpc => '2.0', method => 'map.get', params => { selectSelements => 'extend', #sysmapids => $map, }, id => $id++, auth => $authID, }; $response = $client->call( $url, $json ); # Check if response was successful if ( !$response->content->{'result'} ) { logout(); die "map.get failed\n"; } if ( $debug > 1 ) { print Dumper $response->content->{'result'}; } return $response->content->{'result'}; } sub map_update { my $mapid = shift; my $selements_ref = shift; $json = { jsonrpc => '2.0', method => 'map.update', params => { selements => [@{$selements_ref}], sysmapid => $mapid, }, id => $id++, auth => $authID, }; if ( $debug > 0 ) { print "About to map.update this\n:"; print Dumper $json; } $response = $client->call( $url, $json ); if ( $debug > 0 ) { print Dumper $response; } # Check if response was successful if ( !$response->content->{'result'} ) { logout(); die "map.update failed\n"; } return; }
ãããŠãZabbixãµãŒããŒãæèŒãããã·ã³ã§ãã¹ã¯ãªãããããã«cronïŒzabbixãŠãŒã¶ãŒã«æé©ïŒã«è¿œå ããŸãã1æ¥1åã§ååã§ãã
* 1 * * * /usr/local/bin/zabbix_syslog_create_urls.pl
ãŸãããã¡ã€ã«ãå®è¡å¯èœã«ããããšãå¿ããªãã§ãã ããïŒ
chmod +x /usr/local/bin/zabbix_syslog_create_urls.pl
ã§ããïŒ
åèš
Zabbixã¯ãå€ãã®æ©èœããã®ãŸãŸäœ¿çšã§ããŸãã ãã ããå¿ èŠãªãã®ããªãå Žåã¯ãæ©ãã«çµ¶æããŸãã Zabbix APIãzabbix_senderã ãã©ã°ã€ã³ ã UserParameter-ããããã¹ãŠã®ããŒã«ã¯ãã·ã¹ãã ã®æ©èœãæ¡åŒµããããã«ããªãã®ãµãŒãã¹ã«ãããŸãã