diff -Nur smeserver-zabbix-agent-0.1/root/etc/e-smith/templates/etc/sudoers/00zabbixAgentAlias smeserver-zabbix-agent-0.1_mod/root/etc/e-smith/templates/etc/sudoers/00zabbixAgentAlias --- smeserver-zabbix-agent-0.1/root/etc/e-smith/templates/etc/sudoers/00zabbixAgentAlias 2011-10-26 18:20:16.000000000 +0200 +++ smeserver-zabbix-agent-0.1_mod/root/etc/e-smith/templates/etc/sudoers/00zabbixAgentAlias 2011-10-26 18:29:27.000000000 +0200 @@ -6,14 +6,6 @@ $runasroot .= "\n"; $OUT .= 'Cmnd_Alias ZABBIX_AGENT_ROOT = '.$runasroot; -if ( -x '/usr/sbin/asterisk' ){ - $OUT.=<<"HERE" - -Cmnd_Alias ZABBIX_AGENT_ASTERISK = /var/lib/zabbix/bin/check_asterisk * - -HERE -} - } Cmnd_Alias ZABBIX_AGENT_MYSQL = /usr/bin/du -s /var/lib/mysql diff -Nur smeserver-zabbix-agent-0.1/root/etc/e-smith/templates/etc/sudoers/30zabbixAgent smeserver-zabbix-agent-0.1_mod/root/etc/e-smith/templates/etc/sudoers/30zabbixAgent --- smeserver-zabbix-agent-0.1/root/etc/e-smith/templates/etc/sudoers/30zabbixAgent 2011-10-26 18:20:16.000000000 +0200 +++ smeserver-zabbix-agent-0.1_mod/root/etc/e-smith/templates/etc/sudoers/30zabbixAgent 2011-10-26 18:29:41.000000000 +0200 @@ -1,9 +1,2 @@ zabbix ALL=(root) NOPASSWD: ZABBIX_AGENT_ROOT zabbix ALL=(mysql) NOPASSWD: ZABBIX_AGENT_MYSQL -{ -if ( -x '/usr/sbin/asterisk' ){ - $OUT.=<<"HERE" -zabbix ALL=(asterisk) NOPASSWD: ZABBIX_AGENT_ASTERISK -HERE -} -} diff -Nur smeserver-zabbix-agent-0.1/root/etc/e-smith/templates/etc/zabbix/zabbix_agentd.conf/90UserParameters_asterisk smeserver-zabbix-agent-0.1_mod/root/etc/e-smith/templates/etc/zabbix/zabbix_agentd.conf/90UserParameters_asterisk --- smeserver-zabbix-agent-0.1/root/etc/e-smith/templates/etc/zabbix/zabbix_agentd.conf/90UserParameters_asterisk 2011-10-26 18:20:16.000000000 +0200 +++ smeserver-zabbix-agent-0.1_mod/root/etc/e-smith/templates/etc/zabbix/zabbix_agentd.conf/90UserParameters_asterisk 2011-10-26 18:30:24.000000000 +0200 @@ -1,15 +1,25 @@ { if ( -x '/usr/sbin/asterisk' ){ + my $login = ""; + my $user = ${'zabbix-agent'}{'AsteriskManagerLogin'} || ''; + my $pass = ${'zabbix-agent'}{'AsteriskManagerPassword'} || ''; + if ($user ne ''){ + $login .= "--user=$user"; + } + if ($pass ne ''){ + $login .= " --secret=$pass"; + } $OUT.=<<"HERE" # Type: Agent or Agent (active) -# Key: asterisk[key] where key can be max_latency, sip_peers, iax2_peers, channels, calls, version -# Type of information: Numeric (integer 64bit) or characters (for version) +# Key: asterisk[key] where key can be max_latency, sip_peers, channels, internal_calls, +# outgoing_calls, incoming_calls, external_calls, duration_internal or duration_external +# Type of information: Numeric (integer 64bit) # Units: depends on the key # Custom multiplier: Do not use # Store Value: As is -UserParameter=asterisk[*],/usr/bin/sudo -u asterisk /var/lib/zabbix/bin/check_asterisk_monitoring \$1 +UserParameter=asterisk[*],/var/lib/zabbix/bin/check_asterisk $login --what=\$1 HERE } diff -Nur smeserver-zabbix-agent-0.1/root/var/lib/zabbix/bin/check_asterisk smeserver-zabbix-agent-0.1_mod/root/var/lib/zabbix/bin/check_asterisk --- smeserver-zabbix-agent-0.1/root/var/lib/zabbix/bin/check_asterisk 2011-10-26 18:20:16.000000000 +0200 +++ smeserver-zabbix-agent-0.1_mod/root/var/lib/zabbix/bin/check_asterisk 2011-10-26 18:22:45.000000000 +0200 @@ -1,63 +1,210 @@ -#!/bin/bash +#!/usr/bin/perl -w -# Asterisk check script -# by Steeve Avoungou Firewall Services +use Asterisk::AMI::Common; +use Getopt::Long; -PATH=$PATH:/usr/sbin:/usr/local/sbin +open STDERR, '>/dev/null'; -if [ $# -ne "1" ]; then - echo "Usage:" - echo "$0 " - echo "Where can be one of version, sip_peers, iax2_peers, max_latency, channels, calls" - exit 1 -fi - - -case "$@" in - - # Show version of asterisk: - version) - echo $(asterisk -rx 'core show version') - ;; - - # Count SIP peers - sip_peers) - echo $(asterisk -rx 'sip show peers'| grep -v 'Name/username' | egrep -v '[[:digit:]] sip peers' | wc -l) - ;; - - # Display the higher latency - max_latency) - max_latency=0 - for i in $(asterisk -rx 'sip show peers' | awk '{print $8}' | grep -o '[0-9][0-9]*'); do - if [ "$i" -gt "$max_latency" ]; then - max_latency=$i - fi - done - for i in $(asterisk -rx 'iax2 show peers' | awk '{print $8}' | grep -o '[0-9][0-9]*'); do - if [ "$i" -gt "$max_latency" ]; then - max_latency=$i - fi - done - echo $max_latency - ;; - - # Count IAX2 peers - iax2_peers) - echo $(asterisk -rx 'iax2 show peers' | grep -v 'Name/Username' | egrep -v '[[:digit:]] iax2 peers' | wc -l) - ;; - - # Count number of active channels - channels) - echo $(asterisk -rx "core show channels" | grep 'active channel' | awk '{print $1}') - ;; - - # Count number of active calls - calls) - echo $(asterisk -rx "core show channels" | grep 'active call' | awk '{print $1}') - ;; - - *) - echo 'ZBX_NOTSUPPORTED' - ;; -esac +# Set some default +my $host = '127.0.0.1'; +my $port = '5038'; +my $user = 'zabbixagent'; +my $secret = 'zabbixsecret'; +my $what = 'sip_peers'; + +GetOptions( + "host=s" => \$host, + "port=s" => \$port, + "user=s" => \$user, + "secret=s" => \$secret, + "what=s" => \$what +); + +our $ast = Asterisk::AMI::Common->new( + PeerAddr => $host, + PeerPort => $port, + Username => $user, + Secret => $secret + ); + +die "Unable to connect to asterisk manager" unless ($ast); + + +sub help{ + print<<"EOF"; + +usage: $0 --host=asterisk.domain.tld --port=5038 --user=manager --secret=azerty --what=sip_peers + +--what can take the following argument: + + * sip_peers: number of connected sip peers + * max_latency: highest latency of connected sip_peers + * channels: total number of active channels + * internal_calls: number of active internal calls + * outgoing_calls: number of active outgoing calls (external) + * incoming_calls: number of active incoming calls (external) + * external_calls: number of external calls (in + out) + * duration_internal: actual highest duration of internal calls + * duration_external: actual highest duration of external calls + +EOF + + +if ($what eq 'sip_peers'){ + print get_connected_peers_num('sip'); +} +elsif ($what eq 'max_latency'){ + print get_max_peer_latency(); +} +elsif($what eq 'channels'){ + print get_active_channels_num(); +} +elsif ($what eq 'internal_calls'){ + print get_internal_call_num(); +} +elsif ($what eq 'outgoing_calls'){ + print get_outgoing_call_num(); +} +elsif ($what eq 'incoming_calls'){ + print get_incoming_call_num(); +} +elsif ($what eq 'external_calls'){ + print get_outgoing_call_num() + get_incoming_call_num(); +} +elsif ($what eq 'duration_internal'){ + print get_max_duration_internal(); +} +elsif ($what eq 'duration_external'){ + print get_max_duration_external(); +} +else{ + help(); + $ast->disconnect(); + exit (1); +} + +$ast->disconnect(); +exit(0); + +# Return the number of connected peers for +# the specified protocol (only SIP supporteed for now) +sub get_connected_peers_num{ + my $proto = shift; + my $peers; + if ($proto eq 'sip'){ + $peers = get_sip_peers(); + } + else{ + return 'unsupported protocol'; + } + my $num = 0; + foreach my $peer (keys %{$peers}){ + my $status = $peers->{$peer}->{'Status'}; + $num++ if ($status =~ m/^OK/); + } + return $num; +} + +# Return the list of SIP peers (as a hashref) +sub get_sip_peers{ + return $ast->sip_peers(); +} + +# Return the highest latency of all the peers +sub get_max_peer_latency{ + my $peers = get_sip_peers(); + my $latency = 0; + foreach my $peer (keys %{$peers}){ + my $status = $peers->{$peer}->{'Status'}; + $status =~ /\((\d+)\sms\)/; + $latency = $1 if ($1 > $latency); + } + return $latency; +} + +# Return channels list as a hashref +sub get_channels(){ + return $ast->channels(); +} + +# Return the number of channels +sub get_active_channels_num{ + my $channels = get_channels(); + my $num = 0; + foreach my $chan (keys %{$channels}){ + $num++; + } + return $num; +} + +# Return the number of active channels +sub get_up_channels_num{ + my $channels = get_channels(); + my $num = 0; + foreach my $chan (keys %{$channels}){ + my $status = $channels->{$chan}->{'State'}; + $num++ if ($status eq 'Up'); + } + return $num; +} + +# Return the number of outgoing calls +sub get_outgoing_call_num{ + my $channels = get_channels(); + my $num = 0; + foreach my $chan (keys %{$channels}){ + my $context = $channels->{$chan}->{'Context'}; + my $status = $channels->{$chan}->{'State'}; + $num++ if ($context eq 'macro-dialout-trunk' and $status eq 'Up'); + } + return $num; +} + +# Return the number of incoming calls +sub get_incoming_call_num{ + my $channels = get_channels(); + my $num = 0; + foreach my $chan (keys %{$channels}){ + my $context = $channels->{$chan}->{'Context'}; + my $status = $channels->{$chan}->{'State'}; + $num++ if ($context =~ /^from\-(trunk|pstn|zaptel|dahdi)/ and $status eq 'Up'); + } + return $num; +} + +# Return the number of internal calls +sub get_internal_call_num{ + my $channels = get_channels(); + my $num = 0; + foreach my $chan (keys %{$channels}){ + my $context = $channels->{$chan}->{'Context'}; + my $status = $channels->{$chan}->{'State'}; + $num++ if (($context eq 'macro-dial' or $context eq 'from-internal') and $status eq 'Up'); + } + return $num +} + +# Return the max duration of current internal calls +sub get_max_duration_internal{ + my $channels = get_channels(); + my $max = 0; + foreach my $chan (keys %{$channels}){ + my $dur = $channels->{$chan}->{'Seconds'}; + my $context = $channels->{$chan}->{'Context'}; + $max = $dur if (($context eq 'macro-dial' or $context eq 'from-internal') and $dur > $max); + } + return $max +} + +# Return the max duration of current external calls (in or out) +sub get_max_duration_external{ + my $channels = get_channels(); + my $max = 0; + foreach my $chan (keys %{$channels}){ + my $dur = $channels->{$chan}->{'Seconds'}; + my $context = $channels->{$chan}->{'Context'}; + $max = $dur if (($context eq 'macro-dialout-trunk' or $context =~ /^from\-(trunk|pstn|zaptel|dahdi)/) and $dur > $max); + } + return $max +}