From 3b27fcae28165e8c2b7d16a21c5a379e5b20127e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nico=20Sch=C3=BCmann?= Date: Mon, 1 Dec 2014 20:46:39 +0100 Subject: [PATCH] Initial Zabbix --- .../batman-gateway-mode-enabled.sh | 2 + .../batman-maximum-gateway-metric.sh | 21 ++ .../batman-visible-gateway-count.sh | 2 + gateway_scripts/conntrack_count.sh | 2 + gateway_scripts/conntrack_max.sh | 3 + gateway_scripts/dhcp-pool-usage-percent.sh | 214 ++++++++++++++++++ gateway_scripts/dhcp_server_running.sh | 2 + gateway_scripts/route-table42-reachable.sh | 14 ++ gateway_scripts/route-table42-reachable6.sh | 14 ++ sudoers.d/20_zabbix | 5 + zabbix_agentd.d/userparameter_batman.conf | 4 + zabbix_agentd.d/userparameter_conntrack.conf | 2 + zabbix_agentd.d/userparameter_dhcp.conf | 3 + .../userparameter_route-table42.conf | 2 + 14 files changed, 290 insertions(+) create mode 100755 gateway_scripts/batman-gateway-mode-enabled.sh create mode 100755 gateway_scripts/batman-maximum-gateway-metric.sh create mode 100755 gateway_scripts/batman-visible-gateway-count.sh create mode 100755 gateway_scripts/conntrack_count.sh create mode 100755 gateway_scripts/conntrack_max.sh create mode 100755 gateway_scripts/dhcp-pool-usage-percent.sh create mode 100755 gateway_scripts/dhcp_server_running.sh create mode 100755 gateway_scripts/route-table42-reachable.sh create mode 100755 gateway_scripts/route-table42-reachable6.sh create mode 100644 sudoers.d/20_zabbix create mode 100644 zabbix_agentd.d/userparameter_batman.conf create mode 100644 zabbix_agentd.d/userparameter_conntrack.conf create mode 100644 zabbix_agentd.d/userparameter_dhcp.conf create mode 100644 zabbix_agentd.d/userparameter_route-table42.conf diff --git a/gateway_scripts/batman-gateway-mode-enabled.sh b/gateway_scripts/batman-gateway-mode-enabled.sh new file mode 100755 index 0000000..402d183 --- /dev/null +++ b/gateway_scripts/batman-gateway-mode-enabled.sh @@ -0,0 +1,2 @@ +#!/bin/sh +/usr/sbin/batctl gw | grep server | wc -l diff --git a/gateway_scripts/batman-maximum-gateway-metric.sh b/gateway_scripts/batman-maximum-gateway-metric.sh new file mode 100755 index 0000000..3239c8d --- /dev/null +++ b/gateway_scripts/batman-maximum-gateway-metric.sh @@ -0,0 +1,21 @@ +#!/bin/bash + +GATEWAYS=$(/usr/sbin/batctl gwl | awk -F' ' 'NR <= 1 {next} {print $1}') +METRIC=0 + +for i in $GATEWAYS +do + TRACEROUTE=$(/usr/sbin/batctl traceroute "$i" | awk 'NR>1') + if [[ "$TRACEROUTE" =~ "Unreachable" ]]; then + echo 99 + exit + fi + + THISMETRIC=$(wc -l <<< $TRACEROUTE) + + if [ "$THISMETRIC" -gt "$METRIC" ]; then + METRIC=$THISMETRIC + fi +done + +echo $METRIC diff --git a/gateway_scripts/batman-visible-gateway-count.sh b/gateway_scripts/batman-visible-gateway-count.sh new file mode 100755 index 0000000..070655a --- /dev/null +++ b/gateway_scripts/batman-visible-gateway-count.sh @@ -0,0 +1,2 @@ +#!/bin/sh +expr $(/usr/sbin/batctl gwl | wc -l) - 1 diff --git a/gateway_scripts/conntrack_count.sh b/gateway_scripts/conntrack_count.sh new file mode 100755 index 0000000..65757ba --- /dev/null +++ b/gateway_scripts/conntrack_count.sh @@ -0,0 +1,2 @@ +#!/bin/sh +cat /proc/sys/net/ipv4/netfilter/ip_conntrack_count diff --git a/gateway_scripts/conntrack_max.sh b/gateway_scripts/conntrack_max.sh new file mode 100755 index 0000000..c2d515a --- /dev/null +++ b/gateway_scripts/conntrack_max.sh @@ -0,0 +1,3 @@ +#!/bin/sh +cat /proc/sys/net/ipv4/netfilter/ip_conntrack_max + diff --git a/gateway_scripts/dhcp-pool-usage-percent.sh b/gateway_scripts/dhcp-pool-usage-percent.sh new file mode 100755 index 0000000..c60e1d5 --- /dev/null +++ b/gateway_scripts/dhcp-pool-usage-percent.sh @@ -0,0 +1,214 @@ +#!/usr/bin/perl -w +# +# Copyright (C) 2008 Rien Broekstra +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; version 2 dated June, +# 1991. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +# +# +# Munin plugin to measure saturation of DHCP pools. +# +# Configuration variables: +# +# conffile - path to dhcpd's configuration file (default "/etc/dhcp/dhcpd.conf") +# leasefile - path to dhcpd's leases file (default "/var/lib/dhcp/dhcpd.leases") +# +# Parameters: +# +# config (required) +# +# Version 1.0, 2-12-2008 + +use POSIX; +use Time::Local; +use strict; + +my $CONFFILE = exists $ENV{'conffile'} ? $ENV{'conffile'} : "/etc/dhcp/dhcpd.conf"; +my $LEASEFILE = exists $ENV{'leasefile'} ? $ENV{'leasefile'} : "/var/lib/dhcp/dhcpd.leases"; + +if ( defined $ARGV[0] and $ARGV[0] eq "autoconf" ) { + +} +elsif ( defined $ARGV[0] and $ARGV[0] eq "config" ) { + my (%pools, $start, $label); + + # Print general information + print "graph_title DHCP pool usage (in %)\n"; + print "graph_args --upper-limit 100 -l 0\n"; + print "graph_vlabel %\n"; + print "graph_category network\n"; + + # Determine the available IP pools + %pools = determine_pools(); + + # Print a label for each pool + foreach $start (keys %pools) { + $label = ip2string($start); + $label =~ s/\./\_/g; + print "$label.label Pool ".ip2string($start)."\n"; + print "$label.warning 75\n"; + print "$label.critical 100\n"; + } +} +else { + my (@activeleases, %pools, $start, $end, $size, $free, $label, $lease); + + # Determine all leased IP addresses + @activeleases = determine_active_leases(); + + # Determine the available IP pools + %pools = determine_pools(); + + my $noOutput = 0; + my $cumUsed = 0; + my $cumFree = 0; + if ( defined $ARGV[0] and ($ARGV[0] eq "free" or $ARGV[0] eq "used") ) { + $noOutput = 1; + } + # For each pool, count how many leases from that pool are currently active + foreach $start (keys %pools) { + $size = $pools{$start}; + $end = $start+$size; + $free = $size; + + foreach $lease (@activeleases) { + if ($lease >= $start && $lease <= $end) { + $cumUsed++; + $free--; + } + } + $label = ip2string($start); + $label =~ s/\./\_/g; + if ($noOutput == 0) { + print sprintf("%.1f", 100*($size-$free)/$size)."\n"; + } + $cumFree += $free; + if ($free == 0) { + $cumFree = 0; + } + } + if ( defined $ARGV[0] ) { + if ($ARGV[0] eq "free") { + print $cumFree."\n"; + } + if ($ARGV[0] eq "used") { + print $cumUsed."\n"; + } + } +} + +# Parse dhcpd.conf for range statements. +# +# Returns a hash with start IP -> size +sub determine_pools { + my (%pools, @conffile, $line, $start, $end, $size); + + open(CONFFILE, "<${CONFFILE}") || exit -1; + @conffile = ; + close (CONFFILE); + + foreach $line (@conffile) { + if ($line =~ /range[\s]+([\d]+\.[\d]+\.[\d]+\.[\d]+)[\s]+([\d]+\.[\d]+\.[\d]+\.[\d]+)/) { + $start = string2ip($1); + $end = string2ip($2); + $size = $end - $start; + defined($start) || next; + defined($end) || next; + + $pools{$start} = $size; + } + } + return %pools; +} + +# Very simple parser for dhcpd.leases. This will break very easily if dhcpd decides to +# format the file differently. Ideally a simple recursive-descent parser should be used. +# +# Returns an array with currently leased IP's +sub determine_active_leases { + my (@leasefile, $startdate, $enddate, $lease, @activeleases, $mytz, $line, %saw); + + open(LEASEFILE, "<${LEASEFILE}") || exit -1; + @leasefile = ; + close (LEASEFILE); + + @activeleases = (); + + # Portable way of converting a GMT date/time string to timestamp is setting TZ to UTC, and then calling mktime() + $mytz = $ENV{'TZ'}; + $ENV{'TZ'} = 'UTC 0'; + tzset(); + + foreach $line (@leasefile) { + if ($line =~ /lease ([\d]+\.[\d]+\.[\d]+\.[\d]+)/) { + $lease = string2ip($1); + defined($lease) || next; + + undef $startdate; + undef $enddate; + } + elsif ($line =~ /starts \d ([\d]{4})\/([\d]{2})\/([\d]{2}) ([\d]{2}):([\d]{2}):([\d]{2})/) { + $startdate = mktime($6, $5, $4, $3, $2-1, $1-1900, 0, 0); + } + elsif ($line =~ /ends \d ([\d]{4})\/([\d]{2})\/([\d]{2}) ([\d]{2}):([\d]{2}):([\d]{2})/) { + $enddate = mktime($6, $5, $4, $3, $2-1, $1-1900, 0, 0); + if (defined($enddate) && defined($startdate) && defined($lease)) { + if ($startdate < time() && $enddate > time()) { + push (@activeleases, $lease); + } + } + } + + } + + # Set TZ back to its original setting + if (defined($mytz)) { + $ENV{'TZ'} = $mytz; + } + else { + delete $ENV{'TZ'}; + } + tzset(); + + # Sort the array, strip doubles, and return + return grep(!$saw{$_}++, @activeleases); +} + +# +# Helper routine to convert an IP address a.b.c.d into an integer +# +# Returns an integer representation of an IP address +sub string2ip { + my $string = shift; + defined($string) || return undef; + if ($string =~ /([\d]+)\.([\d]+)\.([\d]+)\.([\d]+)/) { + if ($1 < 0 || $1 > 255 || $2 < 0 || $2 > 255 || $3 < 0 || $3 > 255 || $4 < 0 || $4 > 255) { + return undef; + } + else { + return $1 << 24 | $2 << 16 | $3 << 8 | $4; + } + } + return undef; +} + +# +# Returns a dotted quad notation of an +# +sub ip2string { + my $ip = shift; + defined ($ip) || return undef; + return sprintf ("%d.%d.%d.%d", ($ip >> 24) & 0xff, ($ip >> 16) & 0xff, ($ip >> 8) & 0xff, $ip & 0xff); +} + diff --git a/gateway_scripts/dhcp_server_running.sh b/gateway_scripts/dhcp_server_running.sh new file mode 100755 index 0000000..2aa25cb --- /dev/null +++ b/gateway_scripts/dhcp_server_running.sh @@ -0,0 +1,2 @@ +#!/bin/sh +ps ax | grep dhcpd | grep -v grep | wc -l diff --git a/gateway_scripts/route-table42-reachable.sh b/gateway_scripts/route-table42-reachable.sh new file mode 100755 index 0000000..1d12727 --- /dev/null +++ b/gateway_scripts/route-table42-reachable.sh @@ -0,0 +1,14 @@ +#!/bin/sh +TARGET=8.8.8.8 +PACKETMARK=1 +GATEWAY_IP=#CHANGEME +NUMPROBES=4 +THRESHOLD=2 + +RECEIVED=$(ping -m"$PACKETMARK" -I "$GATEWAY_IP" -c "$NUMPROBES" "$TARGET" | grep -oP '\d+(?= received)') + +if [ "$RECEIVED" -ge "$THRESHOLD" ]; then + echo 1 +else + echo 0 +fi diff --git a/gateway_scripts/route-table42-reachable6.sh b/gateway_scripts/route-table42-reachable6.sh new file mode 100755 index 0000000..f8f1237 --- /dev/null +++ b/gateway_scripts/route-table42-reachable6.sh @@ -0,0 +1,14 @@ +#!/bin/sh +TARGET=2001:4860:4860::8888 +PACKETMARK=1 +GATEWAY_IP6=#CHANGEME +NUMPROBES=4 +THRESHOLD=2 + +RECEIVED=$(ping6 -m"$PACKETMARK" -I "$GATEWAY_IP6" -c "$NUMPROBES" "$TARGET" | grep -oP '\d+(?= received)') + +if [ "$RECEIVED" -ge "$THRESHOLD" ]; then + echo 1 +else + echo 0 +fi diff --git a/sudoers.d/20_zabbix b/sudoers.d/20_zabbix new file mode 100644 index 0000000..b471ad9 --- /dev/null +++ b/sudoers.d/20_zabbix @@ -0,0 +1,5 @@ +zabbix ALL= NOPASSWD: /opt/bin/batman-gateway-mode-enabled.sh +zabbix ALL= NOPASSWD: /opt/bin/batman-maximum-gateway-metric.sh +zabbix ALL= NOPASSWD: /opt/bin/batman-visible-gateway-count.sh +zabbix ALL= NOPASSWD: /opt/bin/route-table42-reachable6.sh +zabbix ALL= NOPASSWD: /opt/bin/route-table42-reachable.sh diff --git a/zabbix_agentd.d/userparameter_batman.conf b/zabbix_agentd.d/userparameter_batman.conf new file mode 100644 index 0000000..cf30a90 --- /dev/null +++ b/zabbix_agentd.d/userparameter_batman.conf @@ -0,0 +1,4 @@ +UserParameter=batman-gateway-mode-enabled,sudo /opt/bin/batman-gateway-mode-enabled.sh +UserParameter=batman-maximum-gateway-metric,sudo /opt/bin/batman-maximum-gateway-metric.sh +UserParameter=batman-visible-gateway-count,sudo /opt/bin/batman-visible-gateway-count.sh + diff --git a/zabbix_agentd.d/userparameter_conntrack.conf b/zabbix_agentd.d/userparameter_conntrack.conf new file mode 100644 index 0000000..1749e14 --- /dev/null +++ b/zabbix_agentd.d/userparameter_conntrack.conf @@ -0,0 +1,2 @@ +UserParameter=ip_conntrack_count,/opt/bin/conntrack_count.sh +UserParameter=ip_conntrack_max,/opt/bin/conntrack_max.sh diff --git a/zabbix_agentd.d/userparameter_dhcp.conf b/zabbix_agentd.d/userparameter_dhcp.conf new file mode 100644 index 0000000..a0bb3e1 --- /dev/null +++ b/zabbix_agentd.d/userparameter_dhcp.conf @@ -0,0 +1,3 @@ +UserParameter=dhcp_lease_free,/opt/bin/dhcp-pool-usage-percent.sh free +UserParameter=dhcp_lease_used,/opt/bin/dhcp-pool-usage-percent.sh used +UserParameter=dhcp_server_running,/opt/bin/dhcp_server_running.sh diff --git a/zabbix_agentd.d/userparameter_route-table42.conf b/zabbix_agentd.d/userparameter_route-table42.conf new file mode 100644 index 0000000..06c4179 --- /dev/null +++ b/zabbix_agentd.d/userparameter_route-table42.conf @@ -0,0 +1,2 @@ +UserParameter=route-table42-reachable,sudo /opt/bin/route-table42-reachable.sh +UserParameter=route-table42-reachable6,sudo /opt/bin/route-table42-reachable6.sh